Sign in to follow this  
Colin Mollenhour

problems w/sampling depth buffer values via texture

Recommended Posts

I've given up on directly accessing the depth buffer in a fragment shader as I no longer think it is possible (can't see how it could be done in <2 passes, but that didn't stop me from trying..). So, I'm trying to do it through a texture. I must avoid transferring across the AGP bus at all costs (I have an off-GPU way of doing what I want but 0-1 fps won't cut it, lol.) Basically I need to access the 24 values in the depth buffer surrounding the current fragment (5x5 w/ current in the middle). In my display, I use this to create the depth texture:
draw_object( &(models[0]) );	//draw the object, (need the depth buffer)
//GET THE DEPTH BUFFER INTO A TEXTURE
glBindTexture(GL_TEXTURE_2D, ZBufferTexture);
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, iwinWidth, iwinHeight, 0 );
glBindTexture(GL_TEXTURE_2D, ZBufferTexture);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, iwinWidth, iwinHeight);
			
glClear(GL_COLOR_BUFFER_BIT );	// clear color buffer
//SETUP SHADER
glUseProgramObjectARB(p1);
map = glGetUniformLocationARB(p1, "map");
glUniform2fARB(map, getPixelWidth(), getPixelHeight());
draw_object( &(models[0]) );		//draw with shaders
glUseProgramObjectARB(NULL);        //set default shader  
The map variable is 1/window width/height. I hope to use it later to convert depth image coordinates to exact texture coordinates My vertex shader is very simple (no lighting needed at all):
void main()
{
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
For now, if I could just render the depth buffer via the texture created, I'd be happy. Next I need to be able to index the surrounding values. So the fragment shader that (tries to) display the depth buffer:
uniform sampler2D depth; 
void main() {
   gl_FragColor = texture2D(depth, gl_FragCoord.xy);
}
It just shows the object in black.. What do I put for the coordinates of the texture? Will rgba all be the depth value? This is where I am really confused. I can render the depth buffer by changing the fragment shader to:
void main() {
    float dep = gl_FragCoord.z;
    gl_FragColor = vec4( dep, dep, dep, 1.0);
}
However, both methods are VERY slow (1fps on a 9800pro, 1280*512 window, vs. 600fps using default pipeline). Also, the second method doesn't help me since I need to access neighboring depth values. Any suggestions are greatly appreciated. I've been working on this almost 12hrs straight, and searching google and forums like a mad man. (to bad the search is down currently here) Thanks, Colin

Share this post


Link to post
Share on other sites
Quote:


draw_object( &(models[0]) ); //draw the object, (need the depth buffer)
//GET THE DEPTH BUFFER INTO A TEXTURE
glBindTexture(GL_TEXTURE_2D, ZBufferTexture);
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, iwinWidth, iwinHeight, 0 );
glBindTexture(GL_TEXTURE_2D, ZBufferTexture);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, iwinWidth, iwinHeight);


First, the last two OpenGL calls are redundant, you should remove then. This should also give you a substantial speedup.

Quote:

It just shows the object in black.. What do I put for the coordinates of the texture? Will rgba all be the depth value? This is where I am really confused.

I guess one of your problems is that you're re-writing the depth texture for every drawn object. Just read it from the z-buffer after you've rendered all your models.
To read the 5x5 pixel areas from the texture, just apply the depth texture to a quad which occupies exactly the whole screen (this is easy to do with an ortho projection), then, in your pixel shader, the u coordinate of the center will be gl_FragCoord.x/(iwinWidth-1) and the v coordinate of the center will be gl_FragCoord.y/(iwinHeight-1). To find the u and v coordinate of adjacent pixels, increment or decrement u or v by 1/(iwinWidth-1) and 1/(iwinHeight-1), respectively.
Remember to turn off texture filtering during the process, or the fetched texture samples may be unexpectly averaged.

Hope this is what you're looking for, good luck!

Share this post


Link to post
Share on other sites

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