2
votes

Simply put - I want to draw a ray/line from the near clipping plane out to the far clipping plane using a perspective projection. I have what I believe are correctly normalized world coordinates generated from a mouse click using methods describe in various OpenGL/graphics programming guides.

The problem I am having is that it seems my ray is being drawn from outside the near clipping plane.

Background: This is for a simple model viewer I am building in Qt that requires a picking capability. I need to draw the ray in order to calculate intersections with objects in the scene. However, my basic problem is that I can seem to draw the ray correctly.

My perspective projection is defined:

gluPerspective(_fov, aspect, 0.1, 100.0);

where _fov is 45.0 degress, and aspect is the ratio of the window width/height.

Using my picking code, I've generated what I believe to be correctly normalized world coordinates based off of mouse clicks. An example of these coordinates:

-0.385753,-0.019608,-0.100000

However, when I try to draw a ray starting at that point, it looks like it is being drawn from outside of the clipping plane:

enter image description here

Maybe I am expecting something different, but in the example above I clicked on the nose of the airplane, generated the world coordinates above, and I am drawing the ray incorrectly (or so I believe). I was hoping to see the line being drawn from the location of the mouse click into the airplane model.

When I draw the ray I first load the identity matrix, and then draw a line from the near clipping plane coordinates to the far plane. Then I draw a sphere at the end of the ray (in this screenshot it is behind the plane).

            glPushMatrix();
            glLoadIdentity();

            glColor3f(0,0,1);
            glBegin(GL_LINES);
            glVertex3f(_near_ray.x(), _near_ray.y(), _near_ray.z());
            glVertex3f(_far_ray.x(), _far_ray.y(), _far_ray.z());
            glEnd();

            glTranslatef(_far_ray.x(), _far_ray.y(), _far_ray.z());

            glColor3f(1,0,0);
            glutWireSphere(1, 10, 10);

            glPopMatrix();

Any hints as to what I am doing wrong? The _far_ray coordinates are the same as the _near_ray except for the Z field. I want the ray to be drawn straight into the scene.

In The End... I'd just like to know how the draw the ray itself. I understand that there might be errors in my code that generates the coordinates, but what if I just wanted to draw an arbitrary ray from the near clipping plane straight into the scene. That is that I'd like answered.

1
Stupid check: did you call glMatrixMode(GL_MODELVIEW) last time before calling glLoadIdentity()?stgatilov
No, not at that point in the code. It was called earlier before the glPushMatrix() call in the snippet above. However, I went back and tested in at this point in the code with no effect.Mr. Shickadance
Could you please post exact coordinates of both points that you used to get the picture, and check which of these vertices is in the center of the picture, and which goes from the side?stgatilov
The coordinates were the same except for Z: -0.385753,-0.019608,-0.100000 and -0.385753,-0.019608,-100. But I think I found the problem: my mouse coordinate normalization seems off. I plotted a line across the screen on the X axis, and noticed from edge to edge differed by only around 0.1 units. My normalization expects +/1 unit across.Mr. Shickadance

1 Answers

3
votes

With perspective projection, a line looks like a point on the screen if and only if it passes through the eye position.

Since you revert modelview matrix to identity, the eye is located at the origin (according to this question). Pass (0, 0, 0) as one of the vertices, and hopefully you'll see that line degenerates into a point. Generally, the two 3d vectors used as vertices must be collinear.

If you do not revert modelview matrix, then you can draw a line from (eye) to (eye + dir), where eye is the first vector passed to gluLookAt, and dir is the any sufficiently large vector looking into proper direction.