Getting "wrong" Mouse Coordinates

Hi,
i want to be able to “Drag and Drop” a Sphere im my App.
So, I get the Mouse-Coordinates with :

void GetMouseCoord(int PosX, int PosY) {

GLdouble PosZ ;
GLdouble objx, objy, objz;
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projMatrix[16];

glReadPixels(PosX, PosY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &PosZ);

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);

gluUnProject((GLdouble)PosX, (GLdouble)PosY, PosZ, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);

Move_Obj_X =  (objx) ;
Move_Obj_Y = -(objy) ;
Move_Obj_Z =  (objz) ;  

}

This work’s fine, but the Cordiantes are somekind of “wrong”.

When i move the Sphere to the Move_Obj_X,
Move_Obj_Y and Move_Obj_Z _ Coordiantes,
this point is far away from the mouse.

When i zoomed out the Scene (changing the Z-Value of the Camera), evrything is messed up, too :frowning:
I want to have my Object exactly, where the Mouse-Cursor is…

Can Anyone help me ???

PS : Please, no “Read The Red Book” - Answers :wink:

Hi !

First of all, you have switched the mouse Y coordinate first I guess ?, OpenGL has it’s origin in it’s lower left corner by default.

Now, if you are in perspecitive mode it’s not easy for OpenGL to transform a 2D point into a 3D point, OpenGL does not now the depth value you are after, so you might get a Z value that is far from what you expected, I don’t now a good way to solve it though.

Mikael