View Full Version : How can I move the object along with the mouse?
01-07-2010, 02:12 AM
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...
01-07-2010, 02:38 AM
You can use gluUnproject to get object coordinates from mouse coordinates see here (http://www.opengl.org/sdk/docs/man/xhtml/gluUnProject.xml).
(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.
01-07-2010, 04:36 AM
I use this code, I get some values but I don't know what they are?
01-07-2010, 05:45 AM
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 - (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.
01-07-2010, 06:27 AM
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.
01-09-2010, 06:10 AM
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.
01-11-2010, 02:15 AM
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 (http://www.opengl.org/resources/faq/technical/viewing.htm) ).
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.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.