How can I move the object along with the mouse?

Hi all,

I’m designing an office planner program. I want to move a selected object along with the mouse until the user finds a place to put the item and put it by left-clicking. However I can’t move object with the mouse, ie. I can’t project mouse coordinates to the office world! Y coordinate is always zero because the floor lies on xz plane. I’m waiting for your help, comment, etc…

Thanks in advance…

You can use gluUnproject to get object coordinates from mouse coordinates see here.

Two options:
(1) Call gluUnproject twice - once with winZ=0 and once with winZ=1 to get two 3d-points lying at the near and far plane. From these you can construct a ray and intersect it with y=0 if that is where your plane lies.

(2) Read a value from the depthbuffer at the mouse location using glReadPixels and use it as winZ.

http://nehe.gamedev.net/data/articles/article.asp?article=13

I use this code, I get some values but I don’t know what they are?

GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, 0.0, modelview, projection, viewport, &posX1, &posY1, &posZ1);
gluUnProject( winX, winY, 1.0, modelview, projection, viewport, &posX2, &posY2, &posZ2);


I did sth. like that. Then I found the point where line of p1 and p2 intersects xz plane: it is always (0,0,0) !!! Plz help.

Without having your code its hard to tell where the problem is. What values are returned in posX1, posY1, posZ1 etc? Are they reasonable? To check i suggest you save p1 and p2 when you click the mouse and every time you paint your scene draw a line from p1 to p2. By rotating/moving the camera you should see if the line hits the target you clicked before.

Also you code to compute the intersection might be wrong.

http://avp.boun.edu.tr/kats/myViewer.rar

I zip my project here. Could you download and tell me why sometimes the tables can’t be picked even I click on them (especially when you click the upper part of the table, it is not selected!) Selected table are red and other one is blue… My projection fnc is Engine::processPassiveMouseMotion. It can’t calculate where the mouse projects in the 3D space.

I have not the time to look at your code in detail. It seems your ray/xz-plane intersection is correct. However you seem to have confused some other things regarding the viewing transforms. Looking at your scene::draw i see that you are using gluLookAt wrong - it has to be applied to the modelview matrix, not projection. (read here ).

Also you seem to setup a gluPickMatrix, which is not inteded to be used in conjunction with gluUnproject-picking. Depending on your code it might lead to incorrect viewport / model transformations interfering with gluUnproject where you provide the viewing transforms as parameters. You must make sure the provided parameters are consistent with the state currently shown on screen.

I suggest you try to read some tutorials about the setup of the projection/modelview/viewport first before trying picking.

Hope it helped.