From 250c108e97f8c72ca2d35a0ba03fd3f7b0359a40 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Wed, 14 Aug 2024 16:40:51 -0400 Subject: [PATCH] Implement HDR and gamma correction in post shader pass --- imgui.ini | 8 ++++---- shaders/fragment.glsl | 6 ++++-- shaders/quad_fragment.glsl | 17 +++++++++++------ shaders/quad_vertex.glsl | 6 +++--- source/fbo.cpp | 2 +- source/scene.cpp | 1 - 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/imgui.ini b/imgui.ini index 29d1fe7..87f08b5 100644 --- a/imgui.ini +++ b/imgui.ini @@ -5,7 +5,7 @@ Collapsed=0 [Window][Terrain Controls] Pos=0,19 -Size=1498,94 +Size=1280,94 Collapsed=0 DockId=0x00000001,0 @@ -19,17 +19,17 @@ Collapsed=0 [Window][TerraVisor] Pos=0,0 -Size=1498,720 +Size=1280,720 Collapsed=0 [Window][Scene Window] Pos=0,115 -Size=1498,605 +Size=1280,605 Collapsed=0 DockId=0x00000002,0 [Docking][Data] -DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1498,701 Split=Y Selected=0x9F2D9299 +DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,701 Split=Y Selected=0x9F2D9299 DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=1280,94 Selected=0xF69494A7 DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=1280,605 CentralNode=1 Selected=0x9F2D9299 diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index e6ae42a..fdf2154 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,4 +1,6 @@ #version 430 +precision highp float; + layout(binding = 0) uniform sampler2D diffuse_tex; layout(location = 1) uniform float time; @@ -31,7 +33,7 @@ in VertexData vec3 nw; //world-space normal vector } inData; //block is named 'inData' -out vec4 fragcolor; //the output color for this fragment +out vec4 frag_color; //the output color for this fragment void main(void) { @@ -54,6 +56,6 @@ void main(void) // vec4 specular_term = atten*ks*Ls*pow(max(0.0, dot(rw, vw)), shininess); // fragcolor = ambient_term + diffuse_term + specular_term; - fragcolor = vec4(0.0, 0.0, 1.0, 1.0); + frag_color = vec4(0.45f, 0.82f, 0.52f, 1.0f); } diff --git a/shaders/quad_fragment.glsl b/shaders/quad_fragment.glsl index 7a19693..58d6f30 100644 --- a/shaders/quad_fragment.glsl +++ b/shaders/quad_fragment.glsl @@ -1,12 +1,17 @@ -#version 330 core -out vec4 FragColor; +#version 430 +precision highp float; -in vec2 TexCoord; +out vec4 frag_color; -uniform sampler2D screenTexture; +in vec2 tex_coords; + +uniform sampler2D screen_texture; void main() { - FragColor = texture(screenTexture, TexCoord); - //FragColor = vec4(vec3(1.0 - texture(screenTexture, TexCoord)), 1.0); + vec3 hdr_color = texture(screen_texture, tex_coords).rgb; + vec3 tone_mapped_color = hdr_color / (hdr_color + vec3(1.0)); + tone_mapped_color = pow(tone_mapped_color, vec3(1.0/2.2)); + + frag_color = vec4(tone_mapped_color, 1.0); } diff --git a/shaders/quad_vertex.glsl b/shaders/quad_vertex.glsl index 7a4f575..2310e72 100644 --- a/shaders/quad_vertex.glsl +++ b/shaders/quad_vertex.glsl @@ -1,12 +1,12 @@ -#version 330 core +#version 430 layout(location = 0) in vec3 pos_attrib; //this variable holds the position of mesh vertices layout(location = 1) in vec2 tex_coord_attrib; layout(location = 2) in vec3 normal_attrib; -out vec2 TexCoord; +out vec2 tex_coords; void main() { gl_Position = vec4(pos_attrib.xy, 0.0, 1.0); - TexCoord = tex_coord_attrib; + tex_coords = tex_coord_attrib; } diff --git a/source/fbo.cpp b/source/fbo.cpp index 7d90cfc..6062b67 100644 --- a/source/fbo.cpp +++ b/source/fbo.cpp @@ -18,7 +18,7 @@ void FBO::Init(int width, int height) { // Generate the color texture glGenTextures(1, &color_texture_id_); glBindTexture(GL_TEXTURE_2D, color_texture_id_); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, nullptr); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); diff --git a/source/scene.cpp b/source/scene.cpp index b480f8d..0298408 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -153,7 +153,6 @@ void Scene::Display(GLFWwindow* window) { fbo_.Unbind(); - // post_fbo_ currently renders with a nasty artifact. Need to fix post_fbo_.Bind(); glDisable(GL_DEPTH_TEST);