gl01/internal/game/render.go

490 lines
19 KiB
Go
Raw Permalink Normal View History

2022-02-24 13:18:05 +08:00
package game
2022-01-20 21:58:50 +08:00
import (
2022-02-05 19:25:44 +08:00
"errors"
"math/rand"
"time"
"unsafe"
"edgaru089.ink/go/gl01/internal/asset"
"edgaru089.ink/go/gl01/internal/igwrap"
"edgaru089.ink/go/gl01/internal/igwrap/backend"
"edgaru089.ink/go/gl01/internal/io"
"edgaru089.ink/go/gl01/internal/render"
"edgaru089.ink/go/gl01/internal/util"
"edgaru089.ink/go/gl01/internal/util/itype"
2022-01-20 21:58:50 +08:00
"github.com/go-gl/gl/all-core/gl"
2022-02-24 13:18:05 +08:00
"github.com/go-gl/glfw/v3.3/glfw"
2022-01-20 21:58:50 +08:00
"github.com/go-gl/mathgl/mgl32"
"github.com/inkyblackness/imgui-go/v4"
)
2022-02-05 19:25:44 +08:00
const (
SSAOSampleCount = 32 // Number of samples in the SSAO pass. Must stay the same with ssao.frag
)
2022-01-20 21:58:50 +08:00
var (
2022-01-22 23:06:41 +08:00
ShadowmapSize = itype.Vec2i{6144, 6144} // Size of the shadow mapping
2022-02-05 19:25:44 +08:00
RandomSize = itype.Vec2i{32, 32} // Size of the random mapping
2022-01-20 21:58:50 +08:00
)
2022-02-24 13:18:05 +08:00
// renderData holds OpenGL state used by the game renderer.
type renderData struct {
2022-01-22 23:06:41 +08:00
lastDisplaySize itype.Vec2i
2022-02-05 19:25:44 +08:00
startTime time.Time
texRand uint32 // random texture mapping, RGBA8 (0~1)
2022-01-22 23:06:41 +08:00
2022-01-29 22:29:27 +08:00
// Depth mapping pass
2022-01-22 23:06:41 +08:00
depthmap struct {
2022-02-24 13:18:05 +08:00
fbo, tex uint32 // Framebuffer Object and Texture.
shader *render.Shader // Shader.
2022-01-22 23:06:41 +08:00
}
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-02-05 19:25:44 +08:00
// Textures. Position/Depth(View Space); Normal/Lightspace Depth; Diffuse Color/Specular Intensity.
2022-01-22 23:06:41 +08:00
pos, norm, color uint32
2022-02-24 13:18:05 +08:00
depth uint32 // Depth renderbuffer.
shader *render.Shader // Geometry pass shaders.
2022-01-22 23:06:41 +08:00
}
2022-02-05 19:25:44 +08:00
// Screen-Space Ambient Occlusion (SSAO) pass
ssao struct {
2022-02-24 13:18:05 +08:00
fbo uint32 // Framebuffer
ambient uint32 // Ambient strength output texture [0,1] (Red channel)
uboSamples uint32 // Uniform Buffer Object pointing to the array of samples
shader *render.Shader // SSAO pass shader
2022-02-05 19:25:44 +08:00
// SSAO blur pass
blur struct {
fbo uint32
output uint32
2022-02-24 13:18:05 +08:00
shader *render.Shader
2022-02-05 19:25:44 +08:00
}
}
2022-01-29 22:29:27 +08:00
// Deferred lighting pass
lighting struct {
2022-02-24 13:18:05 +08:00
shader *render.Shader // Deferred lighting pass shaders
2022-01-29 22:29:27 +08:00
}
// Semi-transparent pass
water struct {
2022-02-24 13:18:05 +08:00
shader *render.Shader
}
2022-01-29 22:29:27 +08:00
// Output pass
output struct {
2022-02-05 19:25:44 +08:00
fbo uint32 // Output framebuffer object, rendering to the output texture.
2022-01-29 22:29:27 +08:00
tex uint32 // Output texture, rendered to the back buffer at the end.
//depth uint32 // Output depth renderbuffer, use gbuffer.depth
2022-02-24 13:18:05 +08:00
shader *render.Shader // Shader used to copy output.tex to back buffer.
2022-01-29 22:29:27 +08:00
}
2022-02-24 13:18:05 +08:00
texture *render.Texture // World texture atlas
2022-01-20 21:58:50 +08:00
}
2022-02-24 13:18:05 +08:00
func (g *Game) initRender() (err error) {
r := &g.render
2022-01-20 21:58:50 +08:00
2022-02-24 13:18:05 +08:00
r.depthmap.shader, err = render.NewShader(asset.WorldShaderShadowmapVert, asset.WorldShaderShadowmapFrag)
2022-01-20 21:58:50 +08:00
if err != nil {
2022-02-05 19:25:44 +08:00
return errors.New("depthmap: " + err.Error())
2022-01-20 21:58:50 +08:00
}
2022-02-24 13:18:05 +08:00
r.gbuffer.shader, err = render.NewShader(asset.WorldShaderGeometryVert, asset.WorldShaderGeometryFrag)
2022-01-22 23:06:41 +08:00
if err != nil {
2022-02-05 19:25:44 +08:00
return errors.New("gbuffer: " + err.Error())
}
2022-02-24 13:18:05 +08:00
r.ssao.shader, err = render.NewShader(asset.WorldShaderSSAOVert, asset.WorldShaderSSAOFrag)
2022-02-05 19:25:44 +08:00
if err != nil {
return errors.New("ssao: " + err.Error())
}
2022-02-24 13:18:05 +08:00
r.ssao.blur.shader, err = render.NewShader(asset.WorldShaderSSAOBlurVert, asset.WorldShaderSSAOBlurFrag)
2022-02-05 19:25:44 +08:00
if err != nil {
return errors.New("ssao_blur: " + err.Error())
2022-01-22 23:06:41 +08:00
}
2022-02-24 13:18:05 +08:00
r.lighting.shader, err = render.NewShader(asset.WorldShaderLightingVert, asset.WorldShaderLightingFrag)
2022-01-20 21:58:50 +08:00
if err != nil {
2022-02-05 19:25:44 +08:00
return errors.New("lighting: " + err.Error())
2022-01-20 21:58:50 +08:00
}
2022-02-24 13:18:05 +08:00
r.water.shader, err = render.NewShader(asset.WorldShaderWaterVert, asset.WorldShaderWaterFrag)
if err != nil {
2022-02-05 19:25:44 +08:00
return errors.New("water: " + err.Error())
}
2022-02-24 13:18:05 +08:00
r.output.shader, err = render.NewShader(asset.WorldShaderOutputVert, asset.WorldShaderOutputFrag)
2022-01-29 22:29:27 +08:00
if err != nil {
2022-02-05 19:25:44 +08:00
return errors.New("output: " + err.Error())
2022-01-29 22:29:27 +08:00
}
2022-01-20 21:58:50 +08:00
2022-02-15 23:37:42 +08:00
// get the maximum anisotropic filtering level
var maxaf float32
gl.GetFloatv(gl.MAX_TEXTURE_MAX_ANISOTROPY, &maxaf)
2022-01-20 21:58:50 +08:00
asset.InitWorldTextureAtlas()
2022-02-24 13:18:05 +08:00
r.texture = render.NewTexture()
2022-02-21 14:13:24 +08:00
r.texture.UpdatesRGB(asset.WorldTextureAtlas.Image)
2022-01-21 21:08:12 +08:00
r.texture.GenerateMipMap()
2022-02-15 23:37:42 +08:00
gl.BindTexture(gl.TEXTURE_2D, r.texture.Handle())
gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAX_ANISOTROPY, maxaf)
2022-02-25 17:58:42 +08:00
r.depthmap.shader.SetUniformTexture("tex", r.texture)
2022-01-22 23:06:41 +08:00
r.gbuffer.shader.SetUniformTexture("tex", r.texture)
r.water.shader.SetUniformTexture("tex", r.texture)
2022-02-24 13:18:05 +08:00
igwrap.SetTextureFlag(r.texture.Handle(), igwrap.TextureFlag_Linear, igwrap.TextureFlag_FlipY)
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
2022-02-05 19:25:44 +08:00
// generate random mapping
{
data := make([]uint32, RandomSize[0]*RandomSize[1])
for i := range data {
data[i] = rand.Uint32()
}
gl.GenTextures(1, &r.texRand)
gl.BindTexture(gl.TEXTURE_2D, r.texRand)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(RandomSize[0]), int32(RandomSize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, util.Ptr(data))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
}
r.ssao.shader.SetUniformTextureHandle("rand", r.texRand)
r.ssao.shader.SetUniformVec2f("randSize", RandomSize.ToFloat32())
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)
2022-02-05 19:25:44 +08:00
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])
2022-01-22 23:06:41 +08:00
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)
2022-02-05 19:25:44 +08:00
r.ssao.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos)
r.ssao.shader.SetUniformTextureHandle("gNorm", r.gbuffer.norm)
r.water.shader.SetUniformTextureHandle("gPos", r.gbuffer.pos)
2022-02-05 19:25:44 +08:00
// generate SSAO friends
gl.GenFramebuffers(1, &r.ssao.fbo)
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.fbo)
// ambient strength texture
gl.GenTextures(1, &r.ssao.ambient)
gl.BindTexture(gl.TEXTURE_2D, r.ssao.ambient)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, 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.ssao.ambient, 0)
r.ssao.blur.shader.SetUniformTextureHandle("tex", r.ssao.ambient)
2022-02-05 19:25:44 +08:00
// uniform buffer object for samples
ssaoSamples := make([]itype.Vec4f, SSAOSampleCount)
for i := range ssaoSamples {
sample := itype.Vec3d{rand.Float64()*2 - 1, rand.Float64()*2 - 1, rand.Float64() + 0.05}
sample = sample.Normalize().Multiply(rand.Float64()).Multiply(rand.Float64())
ssaoSamples[i] = itype.Vec4f{
float32(sample[0]),
float32(sample[1]),
float32(sample[2]),
0,
}
}
gl.GenBuffers(1, &r.ssao.uboSamples)
gl.BindBuffer(gl.UNIFORM_BUFFER, r.ssao.uboSamples)
gl.BufferData(gl.UNIFORM_BUFFER, int(unsafe.Sizeof(float32(0))*4*SSAOSampleCount), util.Ptr(ssaoSamples), gl.STATIC_DRAW)
// bind UBO (onto binding 0 for now)
gl.BindBufferBase(gl.UNIFORM_BUFFER, 0, r.ssao.uboSamples)
gl.UniformBlockBinding(r.ssao.shader.Handle(), gl.GetUniformBlockIndex(r.ssao.shader.Handle(), gl.Str("uboSamples\x00")), 0)
// SSAO blur pass
gl.GenFramebuffers(1, &r.ssao.blur.fbo)
gl.BindFramebuffer(gl.FRAMEBUFFER, r.ssao.blur.fbo)
gl.GenTextures(1, &r.ssao.blur.output)
gl.BindTexture(gl.TEXTURE_2D, r.ssao.blur.output)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, 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.ssao.blur.output, 0)
r.lighting.shader.SetUniformTextureHandle("ssaoAmbient", r.ssao.blur.output)
2022-01-29 22:29:27 +08:00
// 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)
2022-02-15 23:37:42 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
2022-01-29 22:29:27 +08:00
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)
// set the texture flags for ImGUI
igwrap.SetTextureFlag(r.gbuffer.color, igwrap.TextureFlag_Linear)
igwrap.SetTextureFlag(r.ssao.ambient, igwrap.TextureFlag_Red)
2022-01-22 23:06:41 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
r.lastDisplaySize = io.DisplaySize
2022-02-05 19:25:44 +08:00
r.startTime = time.Now()
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.
2022-02-24 13:18:05 +08:00
func (g *Game) ResizeDisplay(newSize itype.Vec2i) {
gl.BindTexture(gl.TEXTURE_2D, g.render.gbuffer.pos) // G-Buffer, Position
2022-02-21 14:09:38 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
2022-02-24 13:18:05 +08:00
gl.BindTexture(gl.TEXTURE_2D, g.render.gbuffer.norm) // G-Buffer, Normal
2022-02-21 14:09:38 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.FLOAT, nil)
2022-02-24 13:18:05 +08:00
gl.BindTexture(gl.TEXTURE_2D, g.render.gbuffer.color) // G-Buffer, Albedo Color
2022-02-21 14:09:38 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
2022-02-24 13:18:05 +08:00
gl.BindRenderbuffer(gl.RENDERBUFFER, g.render.gbuffer.depth) // G-Buffer, Depth (Renderbuffer)
2022-02-21 14:09:38 +08:00
gl.RenderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
2022-02-24 13:18:05 +08:00
gl.BindTexture(gl.TEXTURE_2D, g.render.ssao.ambient) // SSAO Output Ambient Strength
2022-02-21 14:09:38 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, gl.UNSIGNED_BYTE, nil)
2022-02-24 13:18:05 +08:00
gl.BindTexture(gl.TEXTURE_2D, g.render.ssao.blur.output) // SSAO Blurred Output
2022-02-21 14:09:38 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RED, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RED, gl.UNSIGNED_BYTE, nil)
2022-02-24 13:18:05 +08:00
gl.BindTexture(gl.TEXTURE_2D, g.render.output.tex) // Output Texture
2022-02-21 14:09:38 +08:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
2022-02-24 13:18:05 +08:00
g.render.lastDisplaySize = newSize
2022-01-22 23:06:41 +08:00
}
2022-02-15 23:37:42 +08:00
var (
sun = [3]float32{0.2, 0.4, 0.3}
alpha = float32(0.55)
gamma = float32(2.2)
exposure = float32(1)
atlasScale = float32(1)
2022-02-15 23:37:42 +08:00
)
2022-01-20 21:58:50 +08:00
2022-02-24 13:18:05 +08:00
// Render, called with a OpenGL context, renders the game.
func (g *Game) Render(win *glfw.Window) {
gl.Viewport(0, 0, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
matv, matp := g.view.View(), g.view.Perspective()
matvp := matp.Mul4(matv)
allclock := util.NewClock()
lastclock := util.NewClock()
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
2022-02-24 13:18:05 +08:00
if g.render.lastDisplaySize != io.DisplaySize {
g.ResizeDisplay(io.DisplaySize)
2022-01-22 23:06:41 +08:00
}
2022-02-26 13:55:07 +08:00
if g.paused {
imgui.SliderFloat3("Sun", &sun, -1, 1)
imgui.SliderFloat("Water Alpha", &alpha, 0, 1)
imgui.SliderFloat("Gamma", &gamma, 1.6, 2.8)
imgui.SliderFloat("Exposure", &exposure, 0, 2)
}
2022-01-20 21:58:50 +08:00
normalSun := itype.Vec3f(sun).Normalize()
2022-01-22 23:06:41 +08:00
gl.Enable(gl.CULL_FACE)
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
lastclock.Restart()
2022-01-20 21:58:50 +08:00
// 1. Render to depth map
gl.Viewport(0, 0, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]))
2022-02-24 13:18:05 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.depthmap.fbo)
2022-01-20 21:58:50 +08:00
gl.Clear(gl.DEPTH_BUFFER_BIT)
2022-02-25 21:10:38 +08:00
gl.Disable(gl.CULL_FACE)
2022-01-20 21:58:50 +08:00
2022-02-24 13:18:05 +08:00
lightPos := g.view.EyePos.Add(normalSun.Multiply(50))
lightView := mgl32.LookAt(lightPos[0], lightPos[1], lightPos[2], g.view.EyePos[0], g.view.EyePos[1], g.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()
2022-02-24 13:18:05 +08:00
io.RenderDir = g.view.EyePos.Add(lightPos.Negative()).ToFloat64()
2022-02-24 13:18:05 +08:00
g.render.depthmap.shader.UseProgram()
2022-02-25 21:10:38 +08:00
g.render.depthmap.shader.BindTextures()
2022-02-24 13:18:05 +08:00
g.render.depthmap.shader.SetUniformMat4("lightspace", lightspace)
2022-01-22 23:06:41 +08:00
2022-02-24 13:18:05 +08:00
g.world.Render()
g.world.RenderWater()
2022-01-22 23:06:41 +08:00
2022-02-24 13:18:05 +08:00
gl.Flush()
io.Diagnostics.Times.RenderPasses.Depthmap = lastclock.Restart()
2022-01-22 23:06:41 +08:00
// 2. Geometry pass, render to G-buffer
io.RenderPos = io.ViewPos
io.RenderDir = io.ViewDir
2022-02-24 13:18:05 +08:00
gl.Viewport(0, 0, int32(g.render.lastDisplaySize[0]), int32(g.render.lastDisplaySize[1]))
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.gbuffer.fbo)
2022-01-22 23:06:41 +08:00
gl.ClearColor(0, 0, 0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
2022-02-25 21:10:38 +08:00
gl.Enable(gl.CULL_FACE)
2022-01-22 23:06:41 +08:00
gl.Disable(gl.BLEND)
2022-02-24 13:18:05 +08:00
g.render.gbuffer.shader.UseProgram()
g.render.gbuffer.shader.BindTextures()
g.render.gbuffer.shader.SetUniformMat4("lightspace", lightspace)
g.render.gbuffer.shader.SetUniformMat4("view", matv)
g.render.gbuffer.shader.SetUniformMat4("projection", matp)
g.render.gbuffer.shader.SetUniformMat4("mvp", matvp)
g.render.gbuffer.shader.SetUniformVec3f("viewPos", g.view.EyePos)
2022-01-20 21:58:50 +08:00
2022-02-24 13:18:05 +08:00
g.world.Render()
2022-01-20 21:58:50 +08:00
2022-02-24 13:18:05 +08:00
gl.Flush()
io.Diagnostics.Times.RenderPasses.Geometry = lastclock.Restart()
2022-02-05 19:25:44 +08:00
// 3/1. SSAO pass
2022-02-24 13:18:05 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.ssao.fbo)
2022-02-05 19:25:44 +08:00
gl.ClearColor(1, 1, 1, 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
2022-02-24 13:18:05 +08:00
g.render.ssao.shader.UseProgram()
g.render.ssao.shader.BindTextures()
g.render.ssao.shader.SetUniformMat4("view", matv)
g.render.ssao.shader.SetUniformMat4("projection", matp)
g.render.ssao.shader.SetUniformVec3f("viewPos", g.view.EyePos)
2022-02-05 19:25:44 +08:00
2022-02-24 13:18:05 +08:00
render.DrawScreenQuad()
2022-02-05 19:25:44 +08:00
2022-02-24 13:18:05 +08:00
// 3/2. SSAO blur pass
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.ssao.blur.fbo)
g.render.ssao.blur.shader.UseProgram()
g.render.ssao.blur.shader.BindTextures()
g.render.ssao.blur.shader.SetUniformVec2f("screenSize", g.render.lastDisplaySize.ToFloat32())
2022-02-05 19:25:44 +08:00
2022-02-24 13:18:05 +08:00
render.DrawScreenQuad()
2022-02-05 19:25:44 +08:00
2022-02-24 13:18:05 +08:00
gl.Flush()
io.Diagnostics.Times.RenderPasses.SSAO = lastclock.Restart()
2022-02-05 19:25:44 +08:00
// 4. Render the actual output with deferred lighting
2022-02-24 13:18:05 +08:00
gl.BindFramebuffer(gl.FRAMEBUFFER, g.render.output.fbo)
2022-02-15 23:37:42 +08:00
gl.ClearColor(0, 0, 0, 0)
2022-01-29 22:29:27 +08:00
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Disable(gl.DEPTH_TEST)
2022-02-24 13:18:05 +08:00
g.render.lighting.shader.UseProgram()
g.render.lighting.shader.BindTextures()
g.render.lighting.shader.SetUniformMat4("lightspace", lightspace)
g.render.lighting.shader.SetUniformVec3f("viewPos", g.view.EyePos)
g.render.lighting.shader.SetUniformVec4f("fogColor", io.FogColor)
g.render.lighting.shader.SetUniformVec3f("sun", normalSun)
2022-01-20 21:58:50 +08:00
2022-02-24 13:18:05 +08:00
render.DrawScreenQuad()
2022-02-24 13:18:05 +08:00
gl.Flush()
io.Diagnostics.Times.RenderPasses.Lighting = lastclock.Restart()
2022-02-05 19:25:44 +08:00
// 5. Render water
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
gl.Enable(gl.CULL_FACE)
gl.Enable(gl.BLEND)
2022-02-26 14:05:55 +08:00
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
gl.BlendEquation(gl.FUNC_ADD)
2022-02-24 13:18:05 +08:00
g.render.water.shader.UseProgram()
g.render.water.shader.BindTextures()
2022-02-24 13:18:05 +08:00
g.render.water.shader.SetUniformMat4("lightspace", lightspace)
g.render.water.shader.SetUniformMat4("view", matv)
g.render.water.shader.SetUniformMat4("projection", matp)
g.render.water.shader.SetUniformVec3f("viewPos", g.view.EyePos)
g.render.water.shader.SetUniformVec4f("fogColor", io.FogColor)
g.render.water.shader.SetUniformVec3f("sun", normalSun)
g.render.water.shader.SetUniformFloat("alpha", alpha)
g.render.water.shader.SetUniformVec2f("screenSize", g.render.lastDisplaySize.ToFloat32())
2022-02-24 13:18:05 +08:00
g.world.RenderWater()
// And render framewires
render.Framewire.Render(g.view)
2022-01-29 22:29:27 +08:00
// Finally. Copy the output texture to the back buffer
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)
2022-02-15 23:37:42 +08:00
gl.ClearColor(io.ClearColor[0], io.ClearColor[1], io.ClearColor[2], io.ClearColor[3])
gl.Clear(gl.COLOR_BUFFER_BIT)
2022-01-29 22:29:27 +08:00
gl.Disable(gl.DEPTH_TEST)
gl.Disable(gl.BLEND)
2022-02-24 13:18:05 +08:00
g.render.output.shader.UseProgram()
g.render.output.shader.BindTextures()
g.render.output.shader.SetUniformFloat("gamma", gamma)
g.render.output.shader.SetUniformFloat("exposure", exposure)
2022-01-29 22:29:27 +08:00
2022-02-24 13:18:05 +08:00
render.DrawScreenQuad()
2022-01-29 22:29:27 +08:00
2022-02-24 13:18:05 +08:00
gl.Flush()
io.Diagnostics.Times.RenderPasses.Postfx = lastclock.Restart()
io.Diagnostics.Times.Render = allclock.Elapsed()
// Show Information?
if io.ShowDebugInfo {
2022-02-24 13:18:05 +08:00
g.renderDebugInfo()
}
2022-02-24 13:18:05 +08:00
backend.Render(win)
2022-02-24 20:20:33 +08:00
2022-01-20 21:58:50 +08:00
}