Sign in to follow this  

Write into multi depth buffer

This topic is 2036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I need to create several depth buffer (for each vertex emitted by a geometry shader) on my FBO.
Here the source code:
[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
//....
[/CODE]

In fragment shader, I have:
[CODE]
layout (location = 0) out float fragDepth;
void main()
{
fragDepth = gl_FragCoord.z / gl_FragCoord.w;
}
[/CODE]

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. Edited by zenux

Share this post


Link to post
Share on other sites
[quote name='zenux' timestamp='1336314246' post='4937774']
it seems that the value has not been affected by the depth test function: glDepthFunc(GL_LESS)
[/quote]
Have you enabled depth testing and writing ?

Share this post


Link to post
Share on other sites
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. Edited by zenux

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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.

Share this post


Link to post
Share on other sites
gl_TexCoord and some other stuff are deprecated since GLSL v1.5...

I mean this:
[code]
[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
}
[/code]
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. Edited by FXACE

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites
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. Edited by FXACE

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[quote name='zenux' timestamp='1336894976' post='4939728']
* I have already several depth buffers (see my first post).
[/quote]
Anything you had attached to GL_COLOR_ATTACHMENTi are used for your purposes (but for OpenGL they are notified as color buffer) and they are not reacting with depth buffer...
[quote name='zenux' timestamp='1336894976' post='4939728']
* What OpenGL will write in GL_DEPTH_ATTACHMENT ? A common depth buffer will be wrote for all vertex emitted by the geometry shader ?
[/quote]
If your FBO doesn't have something attached into GL_DEPTH_ATTACHMENT OpenGL can't do any operations with depth buffer because it doesn't exist. A common (screen) depth buffer will not be touched till you are working with your FBO.

So, you need to attach something into GL_DEPTH_ATTACHMENT (texture or render buffer) and do it (what you want) in multi-pass or...
you can attach large depth texture (internal format: GL_DEPTH_COMPONENT) which would contain a couple of textures (which you planned to make them with separated "depth" textures):
+---+
|012|
|345|
+---+
each number - area of depth buffer which is used for something, for your purposes (in fragment shader you need to do custom clipping).

Best wishes, FXACE. Edited by FXACE

Share this post


Link to post
Share on other sites

This topic is 2036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this