diff --git a/include/fbo.h b/include/fbo.h index c17c90d..2e51617 100644 --- a/include/fbo.h +++ b/include/fbo.h @@ -9,16 +9,18 @@ class FBO { ~FBO(); GLuint fbo_id_; - GLuint color_texture_id_; - GLuint position_texture_id_; + GLuint albedo_gbuffer_id_; + GLuint position_gbuffer_id_; + GLuint normal_gbuffer_id_; GLuint depth_rbo_id_; void Init(int width, int height); void Bind(); void Unbind(); void Cleanup(); - GLuint GetColorTexture() const; - GLuint GetPositionTexture() const; + GLuint GetAlbedoGbuffer() const; + GLuint GetPositionGbuffer() const; + GLuint GetNormalGbuffer() const; }; #endif // FBO_H_ \ No newline at end of file diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 1085b9f..af1bfd2 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -14,8 +14,9 @@ layout(std140, binding = 0) uniform SceneUniforms vec4 eye_w; //world-space eye position }; -layout(location = 0) out vec4 FragColor; -layout(location = 1) out vec4 FragPosition; +layout(location = 0) out vec4 albedoGbuffer; +layout(location = 1) out vec4 positionGbuffer; +layout(location = 2) out vec4 normalGbuffer; float getHeight(vec2 uv) { return texture(heightTexture, uv).r * 0.025f; @@ -52,32 +53,7 @@ vec3 calculateNormalsFromHeightTexture() { } void main() { - //FragColor = vec4(fragNormal, 1.0f); - float shininess = 1.0f; - vec3 lightColor = vec3(0.5f); - vec3 objectColor = vec3(texture(heightTexture, fs_in.texCoord).r); - - // Ambient - vec3 ambient = vec3(0.15f) * vec3(0.15f); - - // Diffuse - //vec3 norm = normalize(normal); - vec3 norm = calculateNormalsFromHeightTexture(); - vec3 lightDir = normalize(vec3(1.0f, 1.0f, -1.0f) - fs_in.position); - float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = diff * vec3(texture(heightTexture, fs_in.texCoord).r); - - // Specular (Phong) - vec3 viewDir = normalize(eye_w.xyz - fs_in.position); - vec3 reflectDir = reflect(-lightDir, norm); - float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); - vec3 specular = spec * lightColor; - - // Combine results - vec3 result = ambient + diffuse; - FragColor = vec4(fs_in.normal, 1.0); - FragPosition = vec4(fs_in.position, 1.0); - - //FragColor = vec4(vec3(texture(heightTexture, fs_in.texCoord).r), 1.0); - //FragColor = vec4((fs_in.position * 0.5 + 0.5), 1.0); + albedoGbuffer = vec4(vec3(texture(heightTexture, fs_in.texCoord).r), 1.0); + positionGbuffer = vec4(fs_in.position, 1.0); + normalGbuffer = vec4(fs_in.normal, 1.0); } \ No newline at end of file diff --git a/shaders/quad_fragment.glsl b/shaders/quad_fragment.glsl index efa02cc..edc751c 100644 --- a/shaders/quad_fragment.glsl +++ b/shaders/quad_fragment.glsl @@ -5,16 +5,18 @@ out vec4 frag_color; in vec2 tex_coords; -uniform sampler2D colorTexture; -uniform sampler2D positionTexture; +uniform sampler2D albedoGbuffer; +uniform sampler2D positionGbuffer; +uniform sampler2D normalGbuffer; void main() { - vec3 hdr_color = texture(colorTexture, tex_coords).rgb; + vec3 hdr_color = texture(albedoGbuffer, 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)); - vec3 position = texture(positionTexture, tex_coords).rgb; + vec3 position = texture(positionGbuffer, tex_coords).rgb; + vec3 normal = texture(normalGbuffer, tex_coords).rgb; - frag_color = vec4(tone_mapped_color, 1.0); + frag_color = vec4(normal, 1.0); } diff --git a/source/fbo.cpp b/source/fbo.cpp index 1e56558..96887f0 100644 --- a/source/fbo.cpp +++ b/source/fbo.cpp @@ -4,7 +4,7 @@ #include "fbo.h" -FBO::FBO() : fbo_id_(-1), color_texture_id_(-1), position_texture_id_(-1), depth_rbo_id_(-1) {} +FBO::FBO() : fbo_id_(-1), albedo_gbuffer_id_(-1), position_gbuffer_id_(-1), normal_gbuffer_id_(-1), depth_rbo_id_(-1) {} FBO::~FBO() { Cleanup(); @@ -15,28 +15,38 @@ void FBO::Init(int width, int height) { glGenFramebuffers(1, &fbo_id_); glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_); - // Generate the color texture - glGenTextures(1, &color_texture_id_); - glBindTexture(GL_TEXTURE_2D, color_texture_id_); + // Generate the albedo gbuffer + glGenTextures(1, &albedo_gbuffer_id_); + glBindTexture(GL_TEXTURE_2D, albedo_gbuffer_id_); 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_texture_id_, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, albedo_gbuffer_id_, 0); - // Generate the position texture - glGenTextures(1, &position_texture_id_); - glBindTexture(GL_TEXTURE_2D, position_texture_id_); + // Generate the position gbuffer + glGenTextures(1, &position_gbuffer_id_); + glBindTexture(GL_TEXTURE_2D, position_gbuffer_id_); 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, position_texture_id_, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, position_gbuffer_id_, 0); - GLenum drawBuffers[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; - glDrawBuffers(2, drawBuffers); + // Generate the normal gbuffer + glGenTextures(1, &normal_gbuffer_id_); + glBindTexture(GL_TEXTURE_2D, normal_gbuffer_id_); + 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); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, normal_gbuffer_id_, 0); + + GLenum drawBuffers[3] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2}; + glDrawBuffers(3, drawBuffers); // RBO was causing issues. Not using for now // Generate the Depth renderbuffer @@ -61,11 +71,14 @@ void FBO::Unbind() { } void FBO::Cleanup() { - if (color_texture_id_ != -1) { - glDeleteTextures(1, &position_texture_id_); + if (albedo_gbuffer_id_ != -1) { + glDeleteTextures(1, &albedo_gbuffer_id_); } - if (position_texture_id_ != -1) { - glDeleteTextures(1, &color_texture_id_); + if (position_gbuffer_id_ != -1) { + glDeleteTextures(1, &position_gbuffer_id_); + } + if (normal_gbuffer_id_ != -1) { + glDeleteTextures(1, &normal_gbuffer_id_); } if (depth_rbo_id_ != -1) { glDeleteTextures(1, &depth_rbo_id_); @@ -75,10 +88,14 @@ void FBO::Cleanup() { } } -GLuint FBO::GetColorTexture() const { - return color_texture_id_; +GLuint FBO::GetAlbedoGbuffer() const { + return albedo_gbuffer_id_; } -GLuint FBO::GetPositionTexture() const { - return position_texture_id_; +GLuint FBO::GetPositionGbuffer() const { + return position_gbuffer_id_; +} + +GLuint FBO::GetNormalGbuffer() const { + return normal_gbuffer_id_; } \ No newline at end of file diff --git a/source/scene.cpp b/source/scene.cpp index 2326227..bd8d9d2 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -39,7 +39,7 @@ const std::string kHGTPath = "hgt/N02E016.hgt"; GLuint tex_id = -1; -float tessellationFactor = 1.0f; +float tessellationFactor = 32.0f; std::vector vertices; std::vector indices; @@ -240,14 +240,18 @@ void Scene::Display(GLFWwindow* window) { glUseProgram(quad_shader_program_); glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, fbo_.GetColorTexture()); + glBindTexture(GL_TEXTURE_2D, fbo_.GetAlbedoGbuffer()); glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, fbo_.GetPositionTexture()); + glBindTexture(GL_TEXTURE_2D, fbo_.GetPositionGbuffer()); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, fbo_.GetNormalGbuffer()); - GLint colorTextureLoc = glGetUniformLocation(quad_shader_program_, "colorTexture"); - glUniform1i(colorTextureLoc, 0); // Texture unit 0 - GLint positionTextureLoc = glGetUniformLocation(quad_shader_program_, "positionTexture"); + GLint colorTextureLoc = glGetUniformLocation(quad_shader_program_, "albedoGbuffer"); + glUniform1i(colorTextureLoc, 0); + GLint positionTextureLoc = glGetUniformLocation(quad_shader_program_, "positionGbuffer"); glUniform1i(positionTextureLoc, 1); + GLint normalTextureLoc = glGetUniformLocation(quad_shader_program_, "normalGbuffer"); + glUniform1i(normalTextureLoc, 2); // Render the full-screen quad glBindVertexArray(quad_vao_); @@ -322,7 +326,7 @@ void Scene::DrawGui(GLFWwindow* window) { // Draw FBO to ImGui window ImGui::Begin("Scene Window"); ImVec2 windowSize = ImGui::GetContentRegionAvail(); - ImGui::Image((void*)(intptr_t)post_fbo_.GetColorTexture(), windowSize, ImVec2(0, 1), ImVec2(1, 0)); + ImGui::Image((void*)(intptr_t)post_fbo_.GetAlbedoGbuffer(), windowSize, ImVec2(0, 1), ImVec2(1, 0)); ImGui::End(); ImGui::Render();