# OpenGL trouble using gluUnproject for 3D Mouse Coords

This topic is 2635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all

I am trying to create a simple ray trace function whereby I can draw a 3D line that goes from the character to the point in space that the mouse is over. I am using the following code taken off this Nehe's tutorial :

Vector3 World::projectedMouse(float mx, float my){ GLdouble model_view[16]; GLint viewport[4]; GLdouble projection[16]; GLfloat winX, winY, winZ; GLdouble dx, dy, dz; glGetDoublev(GL_MODELVIEW_MATRIX, model_view); glGetDoublev(GL_PROJECTION_MATRIX, projection); glGetIntegerv(GL_VIEWPORT, viewport); winX = (float)mx; winY = (float)viewport[3] - (float)my; glReadPixels ((int)mx, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); gluUnProject(winX, winY, winZ, model_view, projection, viewport, &dx, &dy, &dz); Vector3 pr = Vector3(dy, dy, dz); glColor3f(1,0,0); glBegin (GL_LINE_LOOP); glVertex3f(player->getPosition().x, player->getPosition().y + 100, player->getPosition().z); // 0 glVertex3f(pr.x,pr.y,pr.z); // 1 glVertex3f(player->getPosition().x, player->getPosition().y, player->getPosition().z); // 0 glEnd(); return pr; } 

And I draw a line from the player position to the unprojected point but the results I get are really not accurate, the point in space rarely corresponds to where the mouse lies, most of the time it is some random spot in space. Here's a screen with the grey line that goes from the character to the sky when the mouse was simply over the character's head:

Am I doing something wrong in that function? Am I not doing something? Or is there a specific location in the Opengl code where that should be placed?

##### Share on other sites
The Problem is that the Function projects to the first Point of the World where it hits something.
When you click on the sceen and there is the tree, the point will lay on the tree, if you click at a side of the tree, the point
goes until it hits your skybox.

##### Share on other sites

The Problem is that the Function projects to the first Point of the World where it hits something.
When you click on the sceen and there is the tree, the point will lay on the tree, if you click at a side of the tree, the point
goes until it hits your skybox.

Hi. Thanks for your response, but i still think that there's something else.... This is another screen.... As you can see the cursor position and where the line hits are completely apart, and as I move it it kinda feels like the line moves randomly, even thought there is nothing in the scene apart from those three models and the skybox:

Could this have to do with the fact that the camera is mobile and follows the character position as it moves and therefore somehow messes up the projection of the function?

I just need to find a way to make this more accurate as I'd like to use it to shoot enemies...

Thank you!

##### Share on other sites
Right seems like it's fixed now! I get a properly looking line when using:

gluUnProject(winX, winY, 0, model_view, projection, viewport, &bx, &by, &bz); Vector3 pr2 = Vector3(bx, by, bz); 

gluUnProject(winX, winY, winZ, model_view, projection, viewport, &dx, &dy, &dz);Vector3 pr = Vector3(dx, dy, dz);

with unspecified Z... Plus i realised that there was a dy where there should have been a dx.... Maybe it was simply that...

am I right in thinking that using 0 as third parameter casts a point on the far plane whereas a 1 does on the near one? Or was it the other way round?

Anyhow, would anyone know where I can find some tutorial material on how to calculate intersection between a ray and a 3d object/its bounding box?

Thanks all

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634085
• Total Posts
3015409
×