# Converting Mouse Coords to 3d coords with Camera position [closed]

So I've looked at all the answers I can possibly find, and nothing seems to fit what I need exactly (because im bad at math).

I'll explain what I have thus far:

• A camera with x,y,z, Pitch and Yaw float variables.

Scene is rendered like so:

glTranslatef(0.0, 0.0, -(player->cam->getCamY())); // Distance from the ground
glRotatef(player->cam->getPitch(), 1.0, 0.0, 0.0);
glRotatef(-(player->cam->getYaw()), 0.0, 1.0, 0.0);
glTranslatef(-(player->cam->getCamX()), 0.0, -(player->cam->getCamZ()));


(Pitch looks up and down, Yaw looks left and right - the getCam's just return value of x, y or z of the camera)

• A ray function already implemented - It shoots a ray straight out of the camera into whatever direction the camera is facing, and intersects with whatever tile is in the center of the screen.

• and this function that I copied from the internet:

vector3d GetMousePos3D(int x, int y)
{
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, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

return vector3d(posX, posY, posZ);


which, as you can probably tell, does not return the desired results because it doesn't interact with the camera. I have no idea how to do this.

Basically, if I can work out the mouse coordinates relative to the camera coordinates, then everything should work fine.

If you need any other information, just ask. This problem is causing me a lot of distress, and the math feels beyond me.

## closed as too localized by MichaelHouse♦, bummzack, Sean Middleditch, Anko, Laurent CouvidouMay 10 '13 at 23:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

• I dont see anything wrong as far as your loading the correct modelview and projection matrix and using gluUnproject the returned coordinate should be fine. anyway if you want to cast a ray you may want to use 0.0 and 1.0 for gluUnproject with winZ parameter instead of reading it from the depth buffer. I dont understand the part with "Basically, if I can work out the mouse coordinates relative to the camera coordinates, then everything should work fine." – concept3d Apr 3 '13 at 6:08
• Oh yes, my mistake. I feel stupid, the solution was actually quite simple. I spent like 8 hours today on this. Sigh. – Jossos Apr 3 '13 at 8:21
• possible duplicate of converting a mouse click to a ray – Sean Middleditch Apr 7 '13 at 20:05