Sign in to follow this  

Screen-to-World coordinates and Ray Tracing

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: [url=""][/url]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this