Hello world,
I attached a depth buffer to my FBO and want to render the corresponding depth texture for debugging and need it for advanced rendering techniques like ssao.
Therefore I am doing the following
FBO init
glGenFramebuffers(1, &framebufferHandle);
glBindFramebuffer(GL_FRAMEBUFFER, framebufferHandle);
// pos, normal, color
// ...
// depth
glGenTextures(1, &depthTextureHandle);
glBindTexture(GL_TEXTURE_2D, depthTextureHandle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, windowWidth, windowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
// ...
//set attachments
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, positionTextureHandle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normalTextureHandle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, colorTextureHandle, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTextureHandle, 0);
/set the list of draw buffers.
GLenum drawBufferHandles[3] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
glDrawBuffers(3, drawBufferHandles);
Rendering the geometry:
glBindFramebuffer(GL_FRAMEBUFFER, framebufferHandle);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
gBufferShader->bind();
render();
// Compositing
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
finalCompositingShader->bind();
drawScreenFillingTri();
Inside drawScreenFillingTri()
glDisable(GL_DEPTH_TEST);
// array init stuff ...
// Activate textures
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, positionTextureHandle);
glUniform1i(positionMapHandle, 0);
// ... color and normal ...
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, depthTextureHandle);
glUniform1i(depthTextureHandle, 3);
glDrawArrays(GL_TRIANGLES, 0, 3);
// ...
glEnable(GL_DEPTH_TEST);
Inside my GBuffer fragment shader
layout(location = 0) out vec4 positionOutput;
layout(location = 1) out vec3 normalOutput;
layout(location = 2) out vec4 colorOutput;
void main(){
positionOutput = pass_Position;
normalOutput = normalize(pass_Normal);
colorOutput = texture(tex,pass_TexCoord);
}
and during my compositing
in vec2 passUV;
uniform sampler2D positionMap;
uniform sampler2D normalMap;
uniform sampler2D colorMap;
uniform sampler2D depthMap;
out vec4 fragmentColor;
void main() {
vec4 position = texture(positionMap, passUV);
vec4 normal = texture(normalMap, passUV);
vec4 color = texture(colorMap, passUV);
float depth = texture(depthMap, passUV).x;
fragmentColor = vec4(depth,depth,depth,1);
}
I cannot find my mistake here. The rendered image looks like this:
https://dl.dropboxusercontent.com/u/30405938/SSAO%202013-08-05%2014-42-09-29.jpg
I would be thankful if some nice person could help me out with this issue.
Greetings from Germany
Sc4v0r