I’ve looked for this on the forum and so far I’ve seen a few things that seem to do what I want, but don’t actually seem to work. I’m relatively new to 3d geometry so obviously I’ve got stuck. I know it must be something simple but I’m not sure what. I know I have to raycast the cursor into the scene and get the intersection point of the plane. I just don’t know exactly how and need someone to point me in the right direction.
I’ll explain it in this image if I’ve lost anybody:
The floor in that picture is along the XZ plane. When I click where the mouse cursor is, the program needs to calculate the X and Z positions of that green dot on the plane.
This is what I’ve tried so far, based on things I’ve seen around the forum:
void GetCursorPlane(int x, int y, vertex *to)
GLdouble modelview, projection;
GLfloat mouseX, mouseY;
//Setup the camera (referencing angles from the openGL class)
glRotatef(openGL.cameraRotate.x, 1.0f, 0.0f, 0.0f);
glRotatef(openGL.cameraRotate.y -180.0f, 0.0f,-1.0f, 0.0f);
glRotatef(openGL.cameraRotate.z, 0.0f, 0.0f, 1.0f);
glTranslatef( 0.0f - openGL.cameraPos.x,
0.0f - openGL.cameraPos.y,
0.0f - openGL.cameraPos.z);
//Get the matrices and viewport details
glGetDoublev (GL_MODELVIEW_MATRIX, modelview);
glGetDoublev (GL_PROJECTION_MATRIX, projection);
//Mouse cursor pos correction (opengl’s upside down)
mouseX = (float)x;
mouseY = (float)viewport - (float)y;
gluUnProject( mouseX, mouseY, 1.0f, modelview, projection, viewport, &to->x, &to->y, &to->z);
This gives me coordinates that are way off what I need, and doesn’t seem to give remotely right values (eg, I click repeatadly along the top edge of the wall, left to right, and all the XYZ positions change instead of Z staying near constant).
[This message has been edited by dummstah (edited 11-25-2003).]