gl01/internal/render/render_world.go

302 lines
12 KiB
Go
Raw Normal View History

2022-01-20 21:58:50 +08:00
package render
import (
"edgaru089.ml/go/gl01/internal/asset"
"edgaru089.ml/go/gl01/internal/io"
"edgaru089.ml/go/gl01/internal/util/itype"
"edgaru089.ml/go/gl01/internal/world"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/mathgl/mgl32"
"github.com/inkyblackness/imgui-go/v4"
)
var (
2022-01-22 23:06:41 +08:00
ShadowmapSize = itype.Vec2i{6144, 6144} // Size of the shadow mapping
2022-01-20 21:58:50 +08:00
)
// WorldRenderer holds texture/shader resource and viewport
// information for world rendering.
type WorldRenderer struct {
2022-01-22 23:06:41 +08:00
lastDisplaySize itype.Vec2i
2022-01-29 22:29:27 +08:00
// Depth mapping pass
2022-01-22 23:06:41 +08:00
depthmap struct {
fbo, tex uint32 // Framebuffer Object and Texture.
shader *Shader // Shader.
}
2022-01-20 21:58:50 +08:00
2022-01-29 22:29:27 +08:00
// Geometry pass
2022-01-22 23:06:41 +08:00
gbuffer struct {
fbo uint32 // The Framebuffer object.
2022-01-29 22:29:27 +08:00
// Textures. Position/Lightspace Depth; Normal/Depth; Diffuse Color/Specular Intensity.
2022-01-22 23:06:41 +08:00
pos, norm, color uint32
depth uint32
shader *Shader // Geometry pass shaders.
}
2022-01-29 22:29:27 +08:00
// Deferred lighting pass
lighting struct {
shader *Shader // Deferred lighting pass shaders
}
// Semi-transparent pass
water struct {
shader *Shader
}
2022-01-29 22:29:27 +08:00
// Output pass
output struct {
fbo uint32 // Output framebuffer object.
tex uint32 // Output texture, rendered to the back buffer at the end.
//depth uint32 // Output depth renderbuffer, use gbuffer.depth
shader *Shader // Shader used to copy output.tex to back buffer.
}
2022-01-22 23:06:41 +08:00
texture *Texture // World texture atlas
2022-01-20 21:58:50 +08:00
}
// The default WorldRenderer.
var DefaultWorldRenderer WorldRenderer
// Init initializes the WorldRenderer.
func (r *WorldRenderer) Init(w *world.World) (err error) {
2022-01-29 22:29:27 +08:00
r.depthmap.shader, err = NewShader(asset.WorldShaderShadowmapVert, asset.WorldShaderShadowmapFrag)
2022-01-20 21:58:50 +08:00
if err != nil {
return err
}
2022-01-29 22:29:27 +08:00
r.gbuffer.shader, err = NewShader(asset.WorldShaderGeometryVert, asset.WorldShaderGeometryFrag)
2022-01-22 23:06:41 +08:00
if err != nil {
return err
}
2022-01-29 22:29:27 +08:00
r.lighting.shader, err = NewShader(asset.WorldShaderLightingVert, asset.WorldShaderLightingFrag)
2022-01-20 21:58:50 +08:00
if err != nil {
return err
}
r.water.shader, err = NewShader(asset.WorldShaderWaterVert, asset.WorldShaderWaterFrag)
if err != nil {
return err
}
2022-01-29 22:29:27 +08:00
r.output.shader, err = NewShader(asset.WorldShaderOutputVert, asset.WorldShaderOutputFrag)
if err != nil {
return err
}
2022-01-20 21:58:50 +08:00
asset.InitWorldTextureAtlas()
r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image)
2022-01-21 21:08:12 +08:00
r.texture.GenerateMipMap()
2022-01-22 23:06:41 +08:00
r.gbuffer.shader.SetUniformTexture("tex", r.texture)
r.water.shader.SetUniformTexture("tex", r.texture)
2022-01-20 21:58:50 +08:00
2022-01-22 23:06:41 +08:00
r.depthmap.shader.SetUniformMat4("model", mgl32.Ident4())
r.gbuffer.shader.SetUniformMat4("model", mgl32.Ident4())
r.water.shader.SetUniformMat4("model", mgl32.Ident4())
2022-01-20 21:58:50 +08:00
// and view and projection uniforms not yet set
2022-01-29 22:29:27 +08:00
gl.BindFragDataLocation(r.lighting.shader.Handle(), 0, gl.Str("outputColor\x00"))
gl.BindFragDataLocation(r.water.shader.Handle(), 0, gl.Str("outputColor\x00"))
2022-01-29 22:29:27 +08:00
gl.BindFragDataLocation(r.output.shader.Handle(), 0, gl.Str("outputColor\x00"))
2022-01-20 21:58:50 +08:00
// generate the depthmap and depthmap FBO
2022-01-22 23:06:41 +08:00
gl.GenFramebuffers(1, &r.depthmap.fbo)
gl.GenTextures(1, &r.depthmap.tex)
gl.BindTexture(gl.TEXTURE_2D, r.depthmap.tex)
2022-01-20 21:58:50 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]), 0, gl.DEPTH_COMPONENT, gl.FLOAT, nil)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
borderColor := []float32{1, 1, 1, 1}
gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
// attach depth texture as FBO's depth buffer
2022-01-22 23:06:41 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, r.depthmap.fbo)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, r.depthmap.tex, 0)
2022-01-20 21:58:50 +08:00
gl.DrawBuffer(gl.NONE)
gl.ReadBuffer(gl.NONE)
2022-01-22 23:06:41 +08:00
// attach the shadowmap to the shader
2022-01-29 22:29:27 +08:00
r.lighting.shader.SetUniformTextureHandle("shadowmap", r.depthmap.tex)
r.water.shader.SetUniformTextureHandle("shadowmap", r.depthmap.tex)
2022-01-22 23:06:41 +08:00
// generate G-buffer and friends
gl.GenFramebuffers(1, &r.gbuffer.fbo)
gl.BindFramebuffer(gl.FRAMEBUFFER, r.gbuffer.fbo)
// position
gl.GenTextures(1, &r.gbuffer.pos)
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.pos)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, r.gbuffer.pos, 0)
// normal
gl.GenTextures(1, &r.gbuffer.norm)
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.norm)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT1, gl.TEXTURE_2D, r.gbuffer.norm, 0)
// diffuse color
gl.GenTextures(1, &r.gbuffer.color)
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.color)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT2, gl.TEXTURE_2D, r.gbuffer.color, 0)
// depth
gl.GenRenderbuffers(1, &r.gbuffer.depth)
gl.BindRenderbuffer(gl.RENDERBUFFER, r.gbuffer.depth)
gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, r.gbuffer.depth)
// tell OpenGL which color attachments we'll use (of this framebuffer)
attachments := [...]uint32{gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1, gl.COLOR_ATTACHMENT2}
gl.DrawBuffers(int32(len(attachments)), &attachments[0])
// attach the textures
2022-01-29 22:29:27 +08:00
r.lighting.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos)
r.lighting.shader.SetUniformTextureHandle("gNorm", r.gbuffer.norm)
r.lighting.shader.SetUniformTextureHandle("gColor", r.gbuffer.color)
// generate the output texture and friends
gl.GenFramebuffers(1, &r.output.fbo)
gl.BindFramebuffer(gl.FRAMEBUFFER, r.output.fbo)
// output
gl.GenTextures(1, &r.output.tex)
gl.BindTexture(gl.TEXTURE_2D, r.output.tex)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, r.output.tex, 0)
// depth
gl.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, r.gbuffer.depth)
2022-01-29 22:29:27 +08:00
// attach textures
r.output.shader.SetUniformTextureHandle("tex", r.output.tex)
2022-01-22 23:06:41 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
r.lastDisplaySize = io.DisplaySize
2022-01-20 21:58:50 +08:00
2022-01-22 23:06:41 +08:00
initScreenQuad()
2022-01-20 21:58:50 +08:00
return nil
}
2022-01-22 23:06:41 +08:00
// ResizeDisplay resizes the size of the internal buffers dependent on the window size.
// It is called automatically most of the time.
func (r *WorldRenderer) ResizeDisplay(newSize itype.Vec2i) {
}
2022-01-20 21:58:50 +08:00
var sun = [3]float32{0.2, 0.4, 0.3}
var alpha float32 = 0.55
2022-01-20 21:58:50 +08:00
func (r *WorldRenderer) Render(world *world.World, view *View) {
io.RenderPos = io.ViewPos
io.RenderDir = io.ViewDir
2022-01-22 23:06:41 +08:00
// re-generate the G-buffers if the display size changed
if r.lastDisplaySize != io.DisplaySize {
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.pos)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.norm)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
gl.BindTexture(gl.TEXTURE_2D, r.gbuffer.color)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
gl.BindRenderbuffer(gl.RENDERBUFFER, r.gbuffer.depth)
2022-01-22 23:06:41 +08:00
gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
2022-01-29 22:29:27 +08:00
gl.BindTexture(gl.TEXTURE_2D, r.output.tex)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
2022-01-22 23:06:41 +08:00
r.lastDisplaySize = io.DisplaySize
}
2022-01-20 21:58:50 +08:00
imgui.SliderFloat3("Sun", &sun, -1, 1)
normalSun := itype.Vec3f(sun).Normalize()
imgui.SliderFloat("Water Alpha", &alpha, 0, 1)
2022-01-20 21:58:50 +08:00
2022-01-22 23:06:41 +08:00
gl.Enable(gl.CULL_FACE)
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
2022-01-20 21:58:50 +08:00
// 1. Render to depth map
gl.Viewport(0, 0, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]))
2022-01-22 23:06:41 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, r.depthmap.fbo)
2022-01-20 21:58:50 +08:00
gl.Clear(gl.DEPTH_BUFFER_BIT)
2022-01-22 23:06:41 +08:00
lightPos := view.EyePos.Add(normalSun.Multiply(50))
2022-01-20 21:58:50 +08:00
lightView := mgl32.LookAt(lightPos[0], lightPos[1], lightPos[2], view.EyePos[0], view.EyePos[1], view.EyePos[2], 0, 1, 0)
2022-01-22 23:06:41 +08:00
lightProjection := mgl32.Ortho(-50, 50, -50, 50, 1, 100)
2022-01-20 21:58:50 +08:00
lightspace := lightProjection.Mul4(lightView)
io.RenderPos = lightPos.ToFloat64()
io.RenderDir = view.EyePos.Add(lightPos.Negative()).ToFloat64()
2022-01-22 23:06:41 +08:00
r.depthmap.shader.UseProgram()
r.depthmap.shader.SetUniformMat4("lightspace", lightspace)
world.Render()
world.RenderWater()
2022-01-22 23:06:41 +08:00
// 2. Geometry pass, render to G-buffer
io.RenderPos = io.ViewPos
io.RenderDir = io.ViewDir
2022-01-22 23:06:41 +08:00
gl.Viewport(0, 0, int32(r.lastDisplaySize[0]), int32(r.lastDisplaySize[1]))
gl.BindFramebuffer(gl.FRAMEBUFFER, r.gbuffer.fbo)
gl.ClearColor(0, 0, 0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.Disable(gl.BLEND)
r.gbuffer.shader.UseProgram()
r.gbuffer.shader.BindTextures()
r.gbuffer.shader.SetUniformMat4("lightspace", lightspace)
r.gbuffer.shader.SetUniformMat4("view", view.View())
r.gbuffer.shader.SetUniformMat4("projection", view.Perspective())
r.gbuffer.shader.SetUniformVec3f("viewPos", view.EyePos)
2022-01-20 21:58:50 +08:00
world.Render()
2022-01-22 23:06:41 +08:00
// 3. Render the actual output with deferred lighting
2022-01-29 22:29:27 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, r.output.fbo)
gl.ClearColor(io.ClearColor[0], io.ClearColor[1], io.ClearColor[2], io.ClearColor[3])
2022-01-29 22:29:27 +08:00
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Disable(gl.DEPTH_TEST)
2022-01-29 22:29:27 +08:00
r.lighting.shader.UseProgram()
r.lighting.shader.BindTextures()
r.lighting.shader.SetUniformMat4("lightspace", lightspace)
r.lighting.shader.SetUniformVec3f("viewPos", view.EyePos)
r.lighting.shader.SetUniformVec4f("fogColor", io.FogColor)
2022-01-29 22:29:27 +08:00
r.lighting.shader.SetUniformVec3f("sun", normalSun)
2022-01-20 21:58:50 +08:00
DrawScreenQuad()
// 4. Render water
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
gl.Enable(gl.CULL_FACE)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
gl.BlendEquation(gl.FUNC_ADD)
r.water.shader.UseProgram()
r.water.shader.BindTextures()
r.water.shader.SetUniformMat4("lightspace", lightspace)
r.water.shader.SetUniformMat4("view", view.View())
r.water.shader.SetUniformMat4("projection", view.Perspective())
r.water.shader.SetUniformVec3f("viewPos", view.EyePos)
r.water.shader.SetUniformVec4f("fogColor", io.FogColor)
r.water.shader.SetUniformVec3f("sun", normalSun)
r.water.shader.SetUniformFloat("alpha", alpha)
world.RenderWater()
2022-01-29 22:29:27 +08:00
// Finally. Copy the output texture to the back buffer
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
gl.Disable(gl.DEPTH_TEST)
gl.Disable(gl.BLEND)
r.output.shader.UseProgram()
r.output.shader.BindTextures()
DrawScreenQuad()
// Show G-buffers?
/*if io.ShowDebugInfo {
DrawTexture(r.gbuffer.pos, itype.Rectf{0.5, 0.5, 0.5, 0.5}, DrawTextureChannels_R|DrawTextureChannels_G|DrawTextureChannels_B, 0, 32)
DrawTexture(r.gbuffer.depth, itype.Rectf{0.5, 0, 0.5, 0.5}, DrawTextureChannels_R, 0, 1)
}*/
2022-01-20 21:58:50 +08:00
}