• 12
• 12
• 9
• 10
• 13

# Screen-to-World coordinates and Ray Tracing

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

## 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. glrObj->storeMVMatrix(); ... 

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.