Write into multi depth buffer

Started by
10 comments, last by zenux 11 years, 11 months ago
Hello,

I need to create several depth buffer (for each vertex emitted by a geometry shader) on my FBO.
Here the source code:

glBindFramebuffer(GL_FRAMEBUFFER, fboID);
//depth buffer 1
glTexImage2D( GL_TEXTURE_2D, 0, GL_R32F, shadowMapResolution, shadowMapResolution, 0, GL_RED, GL_FLOAT, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);

//depth buffer2
//....


In fragment shader, I have:

layout (location = 0) out float fragDepth;
void main()
{
fragDepth = gl_FragCoord.z / gl_FragCoord.w;
}


Problem: the depth value is well written in my texture but the value is not right. Indeed, it seems that the value has not been affected by the depth test function: glDepthFunc(GL_LESS).

What can I do to solve this problem ?
Thank you in advance.
Advertisement

it seems that the value has not been affected by the depth test function: glDepthFunc(GL_LESS)

Have you enabled depth testing and writing ?
Thanks for your reply.
I have tried to execute "glDepthMask(GL_TRUE);" and "glEnable(GL_DEPTH_TEST);" just before draw objects to be sure but I still have the problem.
When I worked with geometry shaders (with classic GLSL) I had a problem with gl_FragCoord (which is carrying nothing on fragment shader, always returns vec4(0.0)). I had used built-in varying variable (gl_TexCoord[]) to store the same value which gl_FragCoord usually has ("gl_Position * 0.5 + 0.5",ignoring x and y screen coordinates in pixels), and this solved my problem. Maybe this is point on modern GLSL too.

Best wishes, FXACE.
You don't need to write gl_FragDepth if you are going to write it exactly how you have it. GL will write it for you before the shader so it can perform early depth test rejection and not run the pixel shader.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

@FXACE: sorry but I don't understand your solution. First, I think the built-in variable "gl_TexCoord" doesn't exist anymore since OpenGL 3.0, no ? Then, the variable gl_Position doesn't contain the right value because OpenGL has not done the depth test yet in vertex/geometry shader, no ?

@dpadam450: I don't write gl_FragDepth. I read the value.
gl_TexCoord and some other stuff are deprecated since GLSL v1.5...

I mean this:

[geometry shader]
uniform mat4 proj; // your projection matrix
uniform mat4 mv; // your modelview matrix
out vec4 fragCoord;
void main()
{
....
// for each incoming vertex (from vertex shader)
vec4 glposition = proj * (mv * vs_vertex); // this can be done in vertex shader
// vs_vertex - vec4
glposition /= glposition.w;
fragCoord = glposition * 0.5 + 0.5; // move from [-1..1] to [0..1]
gl_Position = glposition;
EmitVertex();
...
EndPrimitive();
}
[fragment shader]
in vec4 fragCoord;
....
void main()
{
// TODO with fragCoord
}

Well, as I said, I working with classic GLSL because of my old gpu, so, this code is need to be checked by someone who is working with modern gpus.
My point was "do not use gl_FragCoord with geometry shaders"...
Can you show us the screen-shot?
Best wishes, FXACE.
Thanks FXACE for your help.
I have try your source code but it's not working as I expected.

I get exactly the same value that gl_FragCoord.z: no depth test (GL_LESS) are applied on this value :(
Upload the screen of your shadow map (maybe that would be easier to determine the kind of bug).

Another thing:
What object did you attached to your FBO as GL_DEPTH_ATTACHMENT? If nothing, so your FBO doesn't have depth buffer.

Best wishes, FXACE.
Sorry, I haven't good screenshot of my shadow map. I have seen after debug session that the last object drawn is always visible on my shadow maps no matter his depth value.
If I sort my objects by depth value (from the further to the nearest): I haven't problem anymore.

No, I haven't GL_DEPTH_ATTACHMENT on my FBO for two reasons:
* I have already several depth buffers (see my first post).
* What OpenGL will write in GL_DEPTH_ATTACHMENT ? A common depth buffer will be wrote for all vertex emitted by the geometry shader ? I need different depth buffer: http://http.developer.nvidia.com/GPUGems3/elementLinks/10fig11.jpg

This topic is closed to new replies.

Advertisement