Jump to content
  • Advertisement
Sign in to follow this  

Screen-to-World coordinates and Ray Tracing

This topic is 2705 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. :)

A recent project for my university course has me developing a 3D matching pairs game. As with all my other projects, I've taken this one too a few steps further. The player is able to navigate the world by free roaming, as they would as an observer in an FPS game for example.

Everything was going fine until my professor introduced ray tracing for the purpose of picking objects. I started trying to implement a method of converting mouse coordinates from the 2D viewport to the 3D world, and this gave me much more problems than I thought it would. In fact, my professor was a bit lost on this one too.

Here's a screenshot of my algorithms in action: http://i.imgur.com/1ezdN.png

Those lines (rays) are a result of me clicking in different areas on the island in front of me. If the code was working as it should, those rays should have been cast from the "camera"'s position, towards the island. Instead, they originate from *below* the island and shoot diagonally upwards, away from the "camera".

For the sake of trying to be as thorough as possible, I will include relevant bits of code.

The following code is used to convert screen coordinates at mouse point X and Y (mpX, mpY) into world space coordinates, as well as creating a Ray between the near plane and the far plane. It should be noted that the modelview matrix has already been stored prior to calling this method in the GLRenderer class. I will explain that later on.

Ray GLRenderer::getRayFromMouseCoords(int mpX, int mpY)
Ray r;
double pjMatrix[16];
double x, y, z;
int viewPort[4];

glGetIntegerv(GL_VIEWPORT, viewPort);
glGetDoublev(GL_PROJECTION_MATRIX, pjMatrix);

gluUnProject(mpX, viewPort[3] - mpY, 0, modelviewMatrix, pjMatrix, viewPort, &x, &y, &z);

r.origin = CVector((float)x, (float)y, (float)z);

gluUnProject(mpX, viewPort[3] - mpY, 1, modelviewMatrix, pjMatrix, viewPort, &x, &y, &z);

r.direction.x = (float)(x - r.origin.x);
r.direction.y = (float)(y - r.origin.y);
r.direction.z = (float)(z - r.origin.z);

return r;

The code below is from the main loop. The camera position and orientiation are the *only* transformations that occur before the modelview matrix is stored.


// Rotate according to the camera orientation.
glRotatef(camera.angle.x, 0.0f, 1.0f, 0.0f);
glRotatef(camera.angle.y, MathLib::cosL(camera.angle.x), 0.0f, MathLib::sinL(camera.angle.x));

// Translate according to the camera position.
glTranslatef(camera.pos.x, camera.pos.y, camera.pos.z);

// Store the modelview matrix.


I would be grateful if someone could help me out with this problem, or at least push me in the right direction, as it's been driving me insane in the last couple of days. I'm also sorry if I posted this in the wrong area.

Share this post

Link to post
Share on other sites
After some more fiddling, I actually managed to make this work by also storing the projection matrix after transforming the camera position and orientation. I'm pretty surprised that my professor also missed this little detail. Personally, I'm not quite there yet when it comes to graphic theory behind the different matrices, apart from basic understanding. I guess I will inquire him about it next week.

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!