From 0b531b20f7539453ff9e2f7835c5028dc7fae3e7 Mon Sep 17 00:00:00 2001 From: Vincent Lejeune Date: Mon, 27 Jan 2014 19:20:20 +0100 Subject: [PATCH] SSAO: Use depth buffer. --- data/shaders/ssao.frag | 16 +++++++--------- src/graphics/post_processing.cpp | 7 ++++--- src/graphics/shaders.cpp | 11 +++++++---- src/graphics/shaders.hpp | 4 ++-- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/data/shaders/ssao.frag b/data/shaders/ssao.frag index 66cd5d46f..c692d9c3a 100644 --- a/data/shaders/ssao.frag +++ b/data/shaders/ssao.frag @@ -1,12 +1,13 @@ #version 130 -uniform sampler2D normals_and_depth; +uniform sampler2D ntex; +uniform sampler2D dtex; uniform sampler2D noise_texture; uniform mat4 invprojm; uniform mat4 projm; uniform vec4 samplePoints[16]; in vec2 uv; -out vec4 FragColor; +out float ao; const float strengh = 4.; const float radius = .4f; @@ -22,8 +23,8 @@ vec3 rand(vec2 co) void main(void) { - vec4 cur = texture(normals_and_depth, uv); - float curdepth = texture(normals_and_depth, uv).a; + vec4 cur = texture(ntex, uv); + float curdepth = texture(dtex, uv).x; vec4 FragPos = invprojm * (2.0f * vec4(uv, curdepth, 1.0f) - 1.0f); FragPos /= FragPos.w; @@ -48,7 +49,7 @@ void main(void) bool isInsideTexture = (sampleProj.x > -1.) && (sampleProj.x < 1.) && (sampleProj.y > -1.) && (sampleProj.y < 1.); // get the depth of the occluder fragment - float occluderFragmentDepth = texture(normals_and_depth, (sampleProj.xy * 0.5) + 0.5).a; + float occluderFragmentDepth = texture(dtex, (sampleProj.xy * 0.5) + 0.5).x; // Position of the occluder fragment in worldSpace vec4 occluderPos = invprojm * vec4(sampleProj.xy, 2.0 * occluderFragmentDepth - 1.0, 1.0f); occluderPos /= occluderPos.w; @@ -57,8 +58,5 @@ void main(void) bl += isOccluded ? smoothstep(radius, 0, distance(samplePos, FragPos)) : 0.; } - // output the result - float ao = 1.0 - bl * invSamples; - - FragColor = vec4(ao); + ao = 1.0 - bl * invSamples; } diff --git a/src/graphics/post_processing.cpp b/src/graphics/post_processing.cpp index 15020a9e4..70760dc5b 100644 --- a/src/graphics/post_processing.cpp +++ b/src/graphics/post_processing.cpp @@ -529,10 +529,11 @@ void PostProcessing::renderSSAO(const core::matrix4 &invprojm, const core::matri glUseProgram(FullScreenShader::SSAOShader::Program); glBindVertexArray(FullScreenShader::SSAOShader::vao); - setTexture(0, static_cast(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH))->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR); - setTexture(1, static_cast(noise_tex)->getOpenGLTextureName(), GL_NEAREST, GL_NEAREST); + setTexture(0, static_cast(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH))->getOpenGLTextureName(), GL_NEAREST, GL_NEAREST); + setTexture(1, static_cast(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH))->DepthBufferTexture, GL_LINEAR, GL_LINEAR); + setTexture(2, static_cast(noise_tex)->getOpenGLTextureName(), GL_NEAREST, GL_NEAREST); - FullScreenShader::SSAOShader::setUniforms(projm, invprojm, 0, 1); + FullScreenShader::SSAOShader::setUniforms(projm, invprojm, 0, 1, 2); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 736c31b85..7f4497f4a 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -1256,7 +1256,8 @@ namespace FullScreenShader } GLuint SSAOShader::Program; - GLuint SSAOShader::uniform_normals_and_depth; + GLuint SSAOShader::uniform_ntex; + GLuint SSAOShader::uniform_dtex; GLuint SSAOShader::uniform_noise_texture; GLuint SSAOShader::uniform_invprojm; GLuint SSAOShader::uniform_projm; @@ -1267,7 +1268,8 @@ namespace FullScreenShader void SSAOShader::init() { Program = LoadProgram(file_manager->getAsset("shaders/screenquad.vert").c_str(), file_manager->getAsset("shaders/ssao.frag").c_str()); - uniform_normals_and_depth = glGetUniformLocation(Program, "normals_and_depth"); + uniform_ntex = glGetUniformLocation(Program, "ntex"); + uniform_dtex = glGetUniformLocation(Program, "dtex"); uniform_noise_texture = glGetUniformLocation(Program, "noise_texture"); uniform_invprojm = glGetUniformLocation(Program, "invprojm"); uniform_projm = glGetUniformLocation(Program, "projm"); @@ -1388,13 +1390,14 @@ namespace FullScreenShader }*/ } - void SSAOShader::setUniforms(const core::matrix4& projm, const core::matrix4 &invprojm, unsigned TU_ntex, unsigned TU_noise) + void SSAOShader::setUniforms(const core::matrix4& projm, const core::matrix4 &invprojm, unsigned TU_ntex, unsigned TU_dtex, unsigned TU_noise) { glUniformMatrix4fv(FullScreenShader::SSAOShader::uniform_invprojm, 1, GL_FALSE, invprojm.pointer()); glUniformMatrix4fv(FullScreenShader::SSAOShader::uniform_projm, 1, GL_FALSE, projm.pointer()); glUniform4fv(FullScreenShader::SSAOShader::uniform_samplePoints, 16, FullScreenShader::SSAOShader::SSAOSamples); - glUniform1i(FullScreenShader::SSAOShader::uniform_normals_and_depth, TU_ntex); + glUniform1i(FullScreenShader::SSAOShader::uniform_ntex, TU_ntex); + glUniform1i(FullScreenShader::SSAOShader::uniform_dtex, TU_dtex); glUniform1i(FullScreenShader::SSAOShader::uniform_noise_texture, TU_noise); } diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index d96247417..f0866e309 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -415,12 +415,12 @@ class SSAOShader { public: static GLuint Program; - static GLuint uniform_normals_and_depth, uniform_noise_texture, uniform_invprojm, uniform_projm, uniform_samplePoints; + static GLuint uniform_ntex, uniform_dtex, uniform_noise_texture, uniform_invprojm, uniform_projm, uniform_samplePoints; static GLuint vao; static float SSAOSamples[64]; static void init(); - static void setUniforms(const core::matrix4& projm, const core::matrix4 &invprojm, unsigned TU_ntex, unsigned TU_noise); + static void setUniforms(const core::matrix4& projm, const core::matrix4 &invprojm, unsigned TU_ntex, unsigned TU_dtex, unsigned TU_noise); }; class FogShader