Jump to content
  • Advertisement
Sign in to follow this  
neo187

OpenGL trouble using gluUnproject for 3D Mouse Coords

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

If you intended to correct an error in the post then please contact us.

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:

Untitled.jpg


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?

Thank you in advance!

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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:

Untitled2.jpg

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 this post


Link to post
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);


instead of plain:

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!