Archived

This topic is now archived and is closed to further replies.

ophir

How to use gluUnProject ?

Recommended Posts

ophir    122
I have a problem. I have spent two days on it. Maybe I can''t work it out without going crazy. I want to get realy world coordinate. First, get window coordinate (wndX1,wndY1,wndZ1).Then use gluUnProject to translate it into world coordinate (realX,realY,realZ).But it doesn''t work. What''s going wrong?? and Is there any better way to do this? Here is the code: GLdouble modelMatrix[16]; GLdouble projMatrix[16]; GLint viewport[4]; glGetDoublev(GL_MODELVIEW_MATRIX ,modelMatrix); glGetDoublev(GL_PROJECTION_MATRIX ,projMatrix); glGetIntegerv(GL_VIEWPORT,viewport); GLdouble wndX1,wndY1; wndX1= mousePoint.x; wndY1= viewport[3] - MousePoint.y; GLdouble wndZ1 ; glReadPixels(wndX1,wndY1,1,1,GL_DEPTH_COMPONENT ,GL_FLOAT, &wndZ1); GLdouble realX,realY,realZ; int value =gluUnProject(wndX1,wndY1,wndZ1,modelMatrix,projMatrix,viewport, &realX,&realY,&realZ); //sometimes value is GL_FALSE why?

Share this post


Link to post
Share on other sites
Quasimodem    151
I had big problems with gluUnProject for a while too. Try this-
find where you are calling gluPerspective and make sure your near z-value is 0.1

For example, something like this:

gluPerspective(45.0, width/height, 0.1, 100.0);

I''m sure it''ll work with other values than 0.1, but for a long time I had it set to 0.0 and gluUnProject WOULD NOT work. Hope that solves your problem too

Share this post


Link to post
Share on other sites
anubisX    122
Quasimodem : it works with anything greater than 0.0 :D
if you use 0.0 you practically have a divide by zero at hand (if you are familiar with projection work it out on a paper and you will see what i mean) which in the end means unpredicatble results for you

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
change the wndZs type to glfloat. Otherwise it returns junk.

Share this post


Link to post
Share on other sites