Little problem with gluUnProject

I use gluUnProject to get 2D position of mouse in 3D position in OpenGL scene.

I use this source :

here is camera rotation and stuff


here is all drawing and stuff

and OnMouseClick :

GLdouble D2Z;
GLdouble D3X;
GLdouble D3Y;
GLdouble D3Z;

glReadPixels(Screen.Mouse.Position.X,768 - Screen.Mouse.Position.Y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &D2Z);

gluUnProject(Screen.Mouse.Position.X,768 - Screen.Mouse.Position.Y,D2Z,ModelViewMatrix,ProjectionMatrix,ViewPort,&D3X,&D3Y,&D3Z);

BUT, the position is always litle bit next to camera. in 3D world. Its always almost the same as position of camera. So thats not what i need. Can somebody help me with this problem ?

[This message has been edited by jirkamelich (edited 12-30-2002).]

Note that, in general, a mouse click can’t give you ONE position in 3d space; a mouse click corresponds to a RAY in 3d space.

When you call gluUnProject(), you pass it some parameters which allows it to assume where on this ray to get the position. One parameter you might want to pass could be a Z buffer value that you read back and adjust for your current depth range/projection.

If you just use a fixed constant for the depth value, then clearly, moving the camera is going to move this point.

In general, I find it better to just do my own back transformation from window coordinates to world ray, and run my own intersection. Especially since I use quaternions, which are rather cheaper to invert than full matrices :slight_smile: