Advertisement Jump to content
Sign in to follow this  

Getting trouble with obtaining depth info

This topic is 2482 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

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

Share this post

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

Share this post

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

Share this post

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


Share this post

Link to post
Share on other sites
What did you expect the viewport values to be? Sounds like the correct values if your viewport covers the whole window.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!