SSAO: Use depth buffer.

This commit is contained in:
Vincent Lejeune 2014-01-27 19:20:20 +01:00
parent 5515f51f65
commit 0b531b20f7
4 changed files with 20 additions and 18 deletions

View File

@ -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;
}

View File

@ -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::video::COpenGLTexture*>(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH))->getOpenGLTextureName(), GL_LINEAR, GL_LINEAR);
setTexture(1, static_cast<irr::video::COpenGLTexture*>(noise_tex)->getOpenGLTextureName(), GL_NEAREST, GL_NEAREST);
setTexture(0, static_cast<irr::video::COpenGLTexture*>(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH))->getOpenGLTextureName(), GL_NEAREST, GL_NEAREST);
setTexture(1, static_cast<irr::video::COpenGLFBOTexture*>(irr_driver->getRTT(RTT_NORMAL_AND_DEPTH))->DepthBufferTexture, GL_LINEAR, GL_LINEAR);
setTexture(2, static_cast<irr::video::COpenGLTexture*>(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);

View File

@ -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);
}

View File

@ -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