samedi 30 août 2014

Create a vector from a point to the x,y of the mouse, OpenGL, C++


Vote count:

0




I am trying to write a OpenGL application where an object is fired from a point( In this case 0,0,0 ) and flies to the x,y position of the mouse. This is the system I am currently using: Create an object at the point 0,0,0 Get the x,y position of the mouse. Convert the mouse to 3d co-ordinates. Get a line between the start point and (mouse-X,m mouse-Y, far plane ) where the far plane is set to 4,294,967,295. Use the parametric line equation to move the object along this line.


The problem is that the x,y on the far plane does not seem to correspond to the x,y mouse position so the object flies on the wrong line. I am pretty sure that the line/para,etric equation part is working ok, but the conversion between 2d and 3-d space may not. Here is what I have tried:


First convert to window co-ordinates:



POINT *mouse = new POINT();
mouse->x = mousePosition3D.x;
mouse->y = mousePosition3D.y;
ScreenToClient( windowHandle, mouse );


Then to 3-d co-ordinates



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

//All matrices in use need to be retrived
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
//Set the co-ords to lookup, based on the mouse position passed to this
winX = (float)x;
winY = (float)viewport[3] - (float)y;
//Set the z to 0.99, for some reason the object will fly totally incorrectly otherwise
winZ = 0.999; //Get a point on the bettween FAR and NEAR-Clipping planes
//Convert the co-ords
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
//Return there values
returnMe.x = posX;
returnMe.y = posY;
returnMe.z = posZ;
return returnMe;


I am wondering has any body do anything similar to this or what maths i may need to do to get the objects to fly along the correct line.



asked 1 min ago

Alan

29






Create a vector from a point to the x,y of the mouse, OpenGL, C++

Aucun commentaire:

Enregistrer un commentaire