Jump to content
  • Advertisement
Sign in to follow this  
Phynix

gluUnproject

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

How to use gluUnproject? And does it work well even if you translate/rotate the view via glTranslate/glRotate?

Share this post


Link to post
Share on other sites
Advertisement
But I've encountered a problem in my code: It always says the depth (z value) is 1.0, even when the point is over a shape. what's wrong? I copied the exact code from nehe:

Vertex GetOGLPos(int x, int y){
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelMatrix );
glGetDoublev( GL_PROJECTION_MATRIX, projMatrix );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelMatrix, projMatrix, viewport, &posX, &posY, &posZ);

return Vertex(posX, posY, posZ);
}

Share this post


Link to post
Share on other sites
Could this be due to the fact that I am rotating/translating the view via glTranslate/glRotate, and not using gluLookAt?

Share this post


Link to post
Share on other sites
Hang on, let me try out that code and see why it doesn't work. It looks good to me. The only thing is you forgot to subtract 1 from winY, but that's a minor issue.

It definitely has nothing to do with whether you use glTranslate/glRotate or gluLookAt to modify your modelview matrix.

Edit: Yeah, it works fine for me. Are you saying that the winZ (depth or z-buffer value) is always 1.0?

Are you sure your OpenGL window has a depth buffer enabled? It really shouldn't be doing that (when your x,y are over an object).

Share this post


Link to post
Share on other sites
ok, the posZ variable is always is -837.8708, unless i keep the mouse on the top portion of the screen, where it is 1.0. The winZ variable is always 1.0, unless the mouse is in the top portion of the screen (i'd say like 40 pixels or so), where it is a really huge number (like 8 digits). The depth buffer is enabled (I enabled GL_DEPTH_TEST). Can I see your code, just to see what changes there are?

Share this post


Link to post
Share on other sites
Quote:
The winZ variable is always 1.0, unless the mouse is in the top portion of the screen (i'd say like 40 pixels or so), where it is a really huge number (like 8 digits).

You have a problem in step one, getting the correct value of winZ (the depth buffer value at the x,y pixel). There's no point in worrying about the rest of thet algorithm, because it will never work until you get step one working.

It should never be a really huge number, because it's supposed to always be clamped to a value between 0.0 and 1.0. If you're getting a number outside of that range, something's wrong.

Post some more exact code where you're getting the value of winZ (i.e. the glReadPixel line and a printf statement). Perhaps you're doing something like printf("z = %d\n", winZ) instead of printf("z = %f\n", winZ)? Remember, winZ is a float.

Share this post


Link to post
Share on other sites
WOW. I found the answer. And it was really obvious. I was getting input from the depth buffer *before* I drew anything. Sorry for wasting your time.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!