View Full Version : Getiing "wrong" Mouse-Coordiantes ?

11-10-2002, 07:11 AM
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 :-(
I want to have my Object exactly, where the Mouse-Cursor is....

Can Anyone help me ???

11-10-2002, 11:09 AM
The obvious first question is whether you remembered to flip the mouse Y coordinate first.

OpenGL viewport coordinates have the origin in the bottom-left corner, but windowing systems normally have the origin in the top-left corner.

Also, I wouldn't bother trying to compute the Z coordinate; just ignore the Z value returned by glUnProject and use the object's existing Z value.

11-11-2002, 11:11 AM
but it still doesn't work.... :-(

My Problem is very simmilar the Topic "drag object with mouse?" from "detzel" a few posts above.....

Maybe he get's Drag n' Drop to work ;-)