Getting trouble with obtaining depth info

Started by
4 comments, last by donguow 12 years ago
Hi guys,

I am trying to implement an app using Image Based Rendering (IBR) technique. At the very beginning stage, I try to get depth info from frame buffer. Here is a piece of code that I used to obtain depth info:

GLfloat* depth= new GLfloat[nWidth * nHeight];
glReadPixels(0, 0, nWidth, nHeight, GL_DEPTH_COMPONENT, GL_FLOAT, depth);

The result ends up looking pretty weird when I print it out.

for (int i = 0; i < nHeight * nWidth; i++)
cout << depth << " ";

depth (i = 0, 1, 2,...) have the same value 1. Can someone give me an explaination?

Thanks in advance
Advertisement
Because when you call glClear(GL_DEPTH_BUFFER_BIT) each pixel of depth buffer set to 1 (very far distance, 0 - too close).
You can change a value for clearing depth by calling glClearDepth
If you expecting different values (which can be computed from drawing scene) make sure:
- common things (like viewport, projection, ...) are set correctly (means "as you expect");
- glDepthMask is on;
- you are drawing some 3d objects (which are visible in projection) and you are calling glReadPixels nearly after them.

Little example "Getting depth value by mouse lookup":

// draw scene objects here......

{
// MouseX - cursor x position on window
// MouseY - cursor y position on window
// Height - height of your window
float depth;
glReadPixels(MouseX, Height - MouseY - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
printf("depth : %f \r", depth);
// if you want to retrieve 3d point at given cursor position use gluUnProject for that
}



Best wishes, FXACE.
Thanks FXACE. Now I can get depth info. Now I'm trying to save it into an image. If you have any idea, plz share :)
I ran into another problem when trying to get viewport info.


GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport)


viewport[0] and viewport[1] are always 0 while viewport[2] and viewport[3] correctly reflect the value of width and height of the window.

Any explaination?

-D
What did you expect the viewport values to be? Sounds like the correct values if your viewport covers the whole window.
Yep. That's correct. Thanks Bob. I am mistaken.

This topic is closed to new replies.

Advertisement