Jump to content
  • Advertisement
Sign in to follow this  
Prune

glGetPixels with GL_DEPTH_COMPONENT always returns 1

This topic is 4069 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
Advertisement
Hello? Can someone please help? I still haven't been able to solve this problem and I'm tearing my hair out here

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
hmm okay, have you enabled GL_DEPTH_TEST? That can cause the error I think.

Share this post


Link to post
Share on other sites
glEnable( GL_DEPTH_TEST );
glDepthMask(1);
glDepthFunc( GL_LEQUAL );

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!