Sign in to follow this  

glGetPixels with GL_DEPTH_COMPONENT always returns 1

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

I wanted to use glReadPixels with GL_DEPTH_COMPONENT to get the depth value at a given mouse location, so I gan gluUnproject the mouse to world coordinates. However, the depth value returned is always 1, and I don't know what the problem is. I have standard setup: glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); ... glutEnterGameMode(); ... glCullFace(GL_BACK); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f, 2.0f / VH, 0.1f, 8.0f); glTranslatef(0.0f, 0.0f, -3.3f); glMatrixMode(GL_MODELVIEW); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); ... // Mouse function mx = static_cast<float>(sx); my = static_cast<float>(viewport[3]) - static_cast<float>(sy); float mz; glReadPixels(sx, viewport[3] - my, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mz); ... gluUnProject... Above, mz is always returned as 1. I've no idea what's wrong here. The scene is constructed of cubes that are 0.5 to -0.5 in the z dimension and are not translated in the z direction anywhere. I get also the 1 when I click on the background. [Edited by - Prune on May 18, 2007 6:02:22 PM]

Share this post


Link to post
Share on other sites
It looks to me that you invert the my coord twice?

my = static_cast<float>(viewport[3]) - static_cast<float>(sy);

... and then again in the glReadPixels call...

glReadPixels(sx, viewport[3] - my, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &mz);

Is this intensional?

I also think it looks a bit odd that you do a translation in your projection matrix.

Also, how do you obtain your mouse coords? if it is with GetCursorPos() you will have your mouse coords in screen-space. You will need to translate the coords to client-space. Use ScreenToClient() to convert them.

Share this post


Link to post
Share on other sites
ScreenToClient?

It's a full screen glut app. The coordinates returned to the glutMouseFunc are correct in screen space. The translation is because I don't want the camera to be at the origin; I want the cube array to be at origin. The inverted my is the first time inverted for the gluUnProject function. In any case the scene is X/Y symmetric, so that can't make a difference.

Share this post


Link to post
Share on other sites

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