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:
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.
-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