PDA

View Full Version : Yupp, more help on glUnProject needed.



mums
06-10-2004, 02:57 PM
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.

mums
06-11-2004, 01:32 AM
Here's the code I've been using, in python/pseudo:

def init():
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

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

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():
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0.0, 0.0, -50.0)
glBegin(GL_QUADS)
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)
glEnd()

def main():
setup_screen_and_stuff
init()
resize()
while True:
draw()
flip
if left mouse button is pressed:
doMouseStuff(pos)

Sorry about the indent :p

mums
06-11-2004, 11:32 AM
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?