Yupp, more help on glUnProject needed.

Good night.

I’m been spending all evening searching for the solution to my problem, and I’ve looked through three dozen threads on these forums but I haven’t been able to find any solution. This makes me believe my problem might be much more trivial than I would suspect.

Here’s the deal. I’m using a simple quad translated to -50 units down the z axis, 10 units high and 10 across. Using the normal glReadPixels/glUnProject method I get pretty weird results. Clicking in say the lower left corner of the quad gives these world coordinates:

(-0.0097028091709283129, -0.0095295440940463359, 49.899608630543526)

the z-value seems to match the 50 I translated when drawing the quad, the offset is probably my depth range, 0.1 to 100. The other values would be correct, if the quad would have been translated to z = 0. It seems I’m getting some kind of mapping with glUnProject, but it is not using any type of depth algorithm for the screen position and z-value. glReadPixels gives a depth value of 0.00390233960934, which looks alright to the untrained eye.

Since I am an opengl newbie I’m not really sure what could be wrong. Am I missing some matrix operations I should be aware of? This example is as basic as it gets, and I’ve tried adapting to several other solutions presented in these forums but I just can’t get it to work. Any help would be great!

Oh, and any basic full-program source examples are most welcome.

Here’s the code I’ve been using, in python/pseudo:

def init():
glClearColor(0.0, 0.0, 0.0, 0.0)

def resize((w, h)):
glViewport(0, 0, w, h)
gluPerspective(45, 1.0 * w/h, 0.1, 100.0)

def doMouseStuff((xpos, ypos)):
mvmatrix = glGetDoublev(GL_MODELVIEW_MATRIX)
projmatrix = glGetDoublev(GL_PROJECTION_MATRIX)
viewport = glGetIntegerv(GL_VIEWPORT)
realy = viewport[3] - ypos
print “Coordinates at cursor are (%d,%d)” % (xpos, realy)
zpos = glReadPixelsf(xpos, realy, 1, 1, GL_DEPTH_COMPONENT)[0][0]
print zpos # This value seems correct.
print gluUnProject(xpos, realy, zpos, mvmatrix, projmatrix, viewport) # These values would be correct, but they’re scaled to small. Seems like no depth is calculated.

def draw():
glTranslatef(0.0, 0.0, -50.0)
glVertexf(-5.0, 5.0, 0.0)
glVertexf(5.0, 5.0, 0.0)
glVertexf(5.0, -5.0, 0.0)
glVertexf(-5.0, -5.0, 0.0)

def main():
while True:
if left mouse button is pressed:

Sorry about the indent :stuck_out_tongue:

Well, it seems I was wrong. The problem is not with gluUnProject. Rather the z-value I recieve from the GL_DEPTH_COMPONENT query is messed up. Testing with a depth value close to 1.0 gives pretty good results, it seems the depth buffer isn’t linear. So the new question would be: How do I get the correct value for the depth component?