If you wish to reproduce the behavior of a FPS game camera, the simplest method is to combine a projection with a view transform. The projection will be responsible for the generation of the rays on a predefined reference frame, and the view matrix will transform them to the required location.
As you probably know, the convention in computer graphics for a perspective projection will transform a frustrum (defined with the screen dimensions, the field of view, and the near and far planes) to the unit cube. You can construct such projection as a matrix using the PerspectiveMatrix3 structure. Then the orientation and location of your camera in the world may be modeled by an Isometry3 initialized by
Now, what you actually want to do is to perform a inverse projection in order to retrieve the rays locations and their orientation. Thus, you need to use the inverse matrix that combines an inverse camera transformation and an inverse perspective projection. To do that you have to retrieve the 4x4 matrix representation of the isometry (using
na::to_homogeneous) and of the perspective (using
.to_matrix())), multiply them, and inverse the result. You may want to see that for an example on my toy ray-tracer.
This inverse transformation maps into world space any point on a face of the unit cube with constant
z value. In other words, sampling the rectangle with the four vertices given by:
(-1, +1, -1) (+1, +1, -1)
(-1, -1, -1) (+1, -1, -1)
and multiplying those samples by the inverse transformation previously computed, will give you a sampling of the visible near-plane of the frustrum in world coordinates (which corresponds exactly
to the blue rectangle on your diagram, modulo a change of sign along the view direction). The ray direction is then simply the normalized difference of this point with the camera location. For example, you may see that.
By the way, all that is quite similar to what you are already doing (including the
up vector that is required for the isometry construction). Though this will need less coding on your side and you will then be able to append more easily other transformations to your camera, or switch to another kind of projection (e.g. orthographic) very easily.