From: hollasch@kpc.com (Steve Hollasch)
Newsgroups: comp.graphics
Subject: Re: Ray Transformation
Summary: Generating scene rays (e.g. for raytracing)
Date: 13 Oct 92 18:39:11 GMT
Article-I.D.: kpc.1992Oct13.183911.3790
Organization: Kubota Pacific Computer, Inc.

belme@waterloo.hp.com (Frank Belme) writes:
> I was wondering what a good way is to calculate the direction of each ray 
> for ray tracing a scene given an eyepoint, eye direction, and field of view
> angle.  Any help would be appreciated.  Thanks.

paulf@halcyon.com (Paul Forgey) writes:
| A method that I used, which is probably pretty clumsy, but it worked,
| was to take point (0, 1, 0), treat it as a 3d stick, and rotate it for
| the angles needed.
|
| Basically, I called that point zero rotation, then rotated around Y for
| my right/left heading.  Then, I rotated along X for my up/down heading.
|
| I took how many screen pixels of resolution I had for each direction
| (this ignores aspect ratios!), divided my field of view angle by that,
| and used this constant as my increments.
|
| I would highly recommend using vectors, as I effectively did here, to
| move your ray.  Also, I'm working on a method of drawing an imaginary
| line to check for intersections, rather than scoot-and-look.

    This topic comes up about once a year, so it looks like we're pretty
much on schedule.  The short answer is that using rotation angles to
generate scene rays is a bad idea, because you don't get a uniform
distribution of sampled points.  The problem is that this method yields a
higher sampling density near the origin than it does in the periphery.  You
also get strange artifacts like straight lines rendered as curves, and so
on.

    I don't have much time right now, but the basic idea is that you use the
line-of-sight vector and the up-vector to construct a virtual grid in front
of you that represents the pixel field (cross these two vectors to find the
third orthogonal vector to form your basis).  You should end up with a
corner point of your virtual grid, and two vectors (call them U and V) that
are in the same proportion to each other as the width and height of your
viewing screen.  Then use these two vectors and the origin point to index to
the proper virtual pixel you're interested in.  The scene ray is the ray
from the viewpoint to the center of this virtual pixel.

              __    . U                        This diagram is my crude
        __--~|  |  /|\                     attempt to show the virtual grid
  __--~|  |  | .|   |                      in front of the viewpoint P.
 |  | .| .| .|  |   |                      In this example, the screen is
 | .|  |  |  |__|   |                      4 pixels high and 5 pixels wide.
 |__|_-|--|##|  |   |                      
 |  |  | .|##| .|   |                          To hit the shaded pixel (I
 | .| .|  |##|  |   |                      would call this pixel <1,2> (note
 |__|__|__|##|__|   |               .P     that we're looking at the _back_
 |  |  |  |  |  |   |                      of the grid in this image).  So
 | .| .| .| .| .|   |                      the sample point is
 |__|_ |  |  |  |   |
 |  | ~|~~|--|__|   |                      C + 1(1/4)V + 2(1/3)U
 | .| .| .|  |  |   |
 |__|  |  | .| .|   |                          This will sample the lower left
/   ~~--__|  |  |   |                      corner of the pixel.  To sample the
~--__     ~~-|__|   |                      center of the pixels, offset the
`    ~~--__      C                         corner point by one half a pixel
V          ~~--__                          width and height:
                                                
                                           C = C + (1/2)(1/4)V (1/2)(1/3)U

    Oh well, so much for giving a quick explanation.  I'll leave it up to you
to figure out how to find U and V, but be sure to account for the viewing
angle and the pixel aspect ratio.

______________________________________________________________________________
Steve Hollasch                                   Kubota Pacific Computer, Inc.
hollasch@kpc.com                                 Santa Clara, California
Newsgroups: comp.graphics