View Full Version : Finding clicked point in openGL
04-20-2011, 02:44 PM
I have working code which finds a clicked point with the depth buffer enabled. However, how can I do the same thing with the depth buffer disabled?
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
winX = (float)x;
winY = (float)viewport - (float)y;
glReadPixels(x, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
At this point, posX, posY, and posZ are valid.
The above code DOES NOT work if I do glDisable(GL_DEPTH_TEST);
04-20-2011, 02:52 PM
from glDisable (http://www.opengl.org/sdk/docs/man/xhtml/glDisable.xml):
If enabled, do depth comparisons and update the depth buffer. Note that even if the depth buffer exists and the depth mask is non-zero, the depth buffer is not updated if the depth test is disabled. See glDepthFunc and glDepthRange.
04-20-2011, 02:53 PM
Don't know as I have never tried turning off depth test. Is there some reason why it can't be left turned on even if just for the duration of the function call?
04-21-2011, 08:14 AM
I turned it on then off again for just this function call, with no luck. The reason I have it turned off is because I'm trying to do some simple 2D plane drawing for some debugging code which will eventually be 3D. Right now, I just need to test basics.
04-21-2011, 08:23 AM
So what I'm doing at this point is just using the GL_ALWAYS depth function to prevent the tearing I had before, and leaving the depth buffer enabled. Sorry it took me a while to figure this one out...openGL is still a black box to me.
04-21-2011, 09:10 PM
If you are using a perspective projection matrix, the depth is required to do the unprojection. If you cleared the depth buffer, and did all your rendering with GL_DEPTH_TEST disabled, then the read-back depth will be at the far plane. This will goof up the unproject calculation, since your objects are presumably not actually located at the far plane.
If you are using an orthographic projection, then the depth value doesn't matter at all -- you would get the same unprojected screen value regardless of the depth value, because things don't get smaller in the distance in orthographic view.
But with a perspective view, things in the distance look smaller, even though they don't change size in "world" coordinates. So a small delta in screen pixels is a big "world" distance at a large depth, but that same pixel distance is a small "world" distance if depth is small (the object is close, and looks big, but it isn't really). The unproject uses the depth value to tell the difference. If you were sampling depth at the far plane, the calcualted unprojected "world points" would be further apart than they should be.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.