gluUnproject

Started by
7 comments, last by Phynix 14 years, 11 months ago
How to use gluUnproject? And does it work well even if you translate/rotate the view via glTranslate/glRotate?
Advertisement
It's fairly well described in the man pages.

You can also read more about its usage in the Red Book.

Feel free to post again if something's unclear.
k, thanks for the link
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);
}
Could this be due to the fact that I am rotating/translating the view via glTranslate/glRotate, and not using gluLookAt?
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).
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?
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.
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.

This topic is closed to new replies.

Advertisement