From 326253a73b355d7ac993e0e6d71d8b6c36811306 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 15 Feb 2022 23:37:42 +0800 Subject: [PATCH] HDR output buffer (WIP) --- internal/asset/shader/world/geometry.vert | 3 ++- internal/asset/shader/world/lighting.frag | 10 ++++----- internal/asset/shader/world/output.frag | 7 ++++++- internal/asset/shader/world/water.frag | 2 +- internal/render/render_world.go | 25 +++++++++++++++++++---- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/internal/asset/shader/world/geometry.vert b/internal/asset/shader/world/geometry.vert index 877d7e5..31e1a21 100644 --- a/internal/asset/shader/world/geometry.vert +++ b/internal/asset/shader/world/geometry.vert @@ -3,6 +3,7 @@ uniform mat4 projection; uniform mat4 view; uniform mat4 model; +uniform mat4 mvp; uniform mat4 lightspace; uniform vec3 viewPos; @@ -33,7 +34,7 @@ void main() { vec4 posView = view * pos4; fragDepthView = posView.z; - gl_Position = projection * posView; + gl_Position = mvp * vec4(vert, 1); fragPosWorld -= viewPos; } diff --git a/internal/asset/shader/world/lighting.frag b/internal/asset/shader/world/lighting.frag index a80a95f..3f0d4b4 100644 --- a/internal/asset/shader/world/lighting.frag +++ b/internal/asset/shader/world/lighting.frag @@ -24,8 +24,6 @@ in vec2 fragPosScreen; out vec4 outputColor; -const float gamma = 2.2; - const float ambient = 0.3, specularStrength = 0.08, specularShininess = 8; const float fogDensity = .00003; @@ -64,12 +62,12 @@ void main() { color += vec4(fragColor.rgb * light, 0.0f); color.a = fragColor.a; - color.rgb = pow(color.rgb, vec3(1.0/gamma)); - float z = gl_FragCoord.z / gl_FragCoord.w; - float fog = clamp(exp(-fogDensity * z * z), 0.2, 1); + //float z = gl_FragCoord.z / gl_FragCoord.w; + //float fog = clamp(exp(-fogDensity * z * z), 0.2, 1); - outputColor = mix(fogColor, color, fog); + //outputColor = mix(fogColor, color, fog); + outputColor = color; } void lightSun() { diff --git a/internal/asset/shader/world/output.frag b/internal/asset/shader/world/output.frag index 2ce940e..69f156a 100644 --- a/internal/asset/shader/world/output.frag +++ b/internal/asset/shader/world/output.frag @@ -1,10 +1,15 @@ #version 330 uniform sampler2D tex; +uniform float gamma, exposure; in vec2 fragPosScreen; out vec4 outputColor; void main() { - outputColor = texture(tex, fragPosScreen); + vec4 texColor = texture(tex, fragPosScreen); + if (texColor.a < 1e-4) + discard; + + outputColor = vec4(pow(texColor.rgb / exposure, vec3(1.0/gamma)), 1.0f); } diff --git a/internal/asset/shader/world/water.frag b/internal/asset/shader/world/water.frag index 47783d9..fb08aea 100644 --- a/internal/asset/shader/world/water.frag +++ b/internal/asset/shader/world/water.frag @@ -43,7 +43,7 @@ void main() { color += vec4(fragColor.rgb * light, 0.0f); color.a = fragColor.a; - color.rgb = pow(color.rgb, vec3(1.0/gamma)); + color.rgb = color.rgb; float z = gl_FragCoord.z / gl_FragCoord.w; float fog = clamp(exp(-fogDensity * z * z), 0.2, 1); diff --git a/internal/render/render_world.go b/internal/render/render_world.go index 331e489..f2e6517 100644 --- a/internal/render/render_world.go +++ b/internal/render/render_world.go @@ -121,9 +121,15 @@ func (r *WorldRenderer) Init(w *world.World) (err error) { return errors.New("output: " + err.Error()) } + // get the maximum anisotropic filtering level + var maxaf float32 + gl.GetFloatv(gl.MAX_TEXTURE_MAX_ANISOTROPY, &maxaf) + asset.InitWorldTextureAtlas() r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image) r.texture.GenerateMipMap() + gl.BindTexture(gl.TEXTURE_2D, r.texture.Handle()) + gl.TexParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAX_ANISOTROPY, maxaf) r.gbuffer.shader.SetUniformTexture("tex", r.texture) r.water.shader.SetUniformTexture("tex", r.texture) @@ -261,7 +267,7 @@ func (r *WorldRenderer) Init(w *world.World) (err error) { // 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.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_ATTACHMENT0, gl.TEXTURE_2D, r.output.tex, 0) @@ -288,8 +294,12 @@ func (r *WorldRenderer) Init(w *world.World) (err error) { func (r *WorldRenderer) ResizeDisplay(newSize itype.Vec2i) { } -var sun = [3]float32{0.2, 0.4, 0.3} -var alpha float32 = 0.55 +var ( + sun = [3]float32{0.2, 0.4, 0.3} + alpha = float32(0.55) + gamma = float32(2.2) + exposure = float32(1) +) func (r *WorldRenderer) Render(world *world.World, view *View) { io.RenderPos = io.ViewPos @@ -317,6 +327,8 @@ func (r *WorldRenderer) Render(world *world.World, view *View) { imgui.SliderFloat3("Sun", &sun, -1, 1) normalSun := itype.Vec3f(sun).Normalize() imgui.SliderFloat("Water Alpha", &alpha, 0, 1) + imgui.SliderFloat("Gamma", &gamma, 1.6, 2.8) + imgui.SliderFloat("Exposure", &exposure, 0, 2) gl.Enable(gl.CULL_FACE) gl.Enable(gl.DEPTH_TEST) @@ -356,6 +368,7 @@ func (r *WorldRenderer) Render(world *world.World, view *View) { r.gbuffer.shader.SetUniformMat4("lightspace", lightspace) r.gbuffer.shader.SetUniformMat4("view", view.View()) r.gbuffer.shader.SetUniformMat4("projection", view.Perspective()) + r.gbuffer.shader.SetUniformMat4("mvp", view.Perspective().Mul4(view.View())) r.gbuffer.shader.SetUniformVec3f("viewPos", view.EyePos) world.Render() @@ -384,7 +397,7 @@ func (r *WorldRenderer) Render(world *world.World, view *View) { // 4. Render the actual output with deferred lighting gl.BindFramebuffer(gl.FRAMEBUFFER, r.output.fbo) - gl.ClearColor(io.ClearColor[0], io.ClearColor[1], io.ClearColor[2], io.ClearColor[3]) + gl.ClearColor(0, 0, 0, 0) gl.Clear(gl.COLOR_BUFFER_BIT) gl.Disable(gl.DEPTH_TEST) r.lighting.shader.UseProgram() @@ -418,10 +431,14 @@ func (r *WorldRenderer) Render(world *world.World, view *View) { // Finally. Copy the output texture to the back buffer gl.BindFramebuffer(gl.FRAMEBUFFER, 0) + gl.ClearColor(io.ClearColor[0], io.ClearColor[1], io.ClearColor[2], io.ClearColor[3]) + gl.Clear(gl.COLOR_BUFFER_BIT) gl.Disable(gl.DEPTH_TEST) gl.Disable(gl.BLEND) r.output.shader.UseProgram() r.output.shader.BindTextures() + r.output.shader.SetUniformFloat("gamma", gamma) + r.output.shader.SetUniformFloat("exposure", exposure) DrawScreenQuad()