Processing math: 100%

Ray Generation

Given a camera's orthonormal basis (pos,u,v,w), the width and height of the desired image (nx,ny), and the field of view (fov), we can generate a ray for each pixel in the image.

Ray

We begin by noting that a ray is a parametric line that begins at a point and continues on forever. A ray defined as: r=p+td where p is the starting position of the ray, d is the direction of the ray, and positive t is the parameter that generates the set r of postions along the ray.

Perspective Ray Generation

To create a ray for a given camera, note that each ray must pass through a pixel in the image plane. So, imagine an image plane in front of the camera. Each pixel has a position in 3D space and the ray for each pixel must pass through the 3D point. We will call this point s. The position of s can be computed as:

s=e+uu+vvdw

Where the scalars u and v are related to the image resolution as follows:

u=+(r)(i+0.5)nx v=b+(tb)(j+0.5)ny

i and j are the index of the pixel the ray is being computed for. nx and ny are the width and height of the image plane in pixels. , r, b, t are the distance from the camera center to the left, right, bottom, and top of the image frame. The origin for this image frame is in the left bottom corner. If the camera is centered in the frame (as is typical), then these values are related to nx2 and ny2. For example, would be nx2 and r would be nx2.

The scalar d controls how far the image plane is from the camera origin. This is related to the field-of-view. A small FOV results in a larger d. Given a horizontal FOV, d can be computed as:

d=cot(fov2)nx2

Finally, once s is known, the ray direction d can be computed as se. The ray position is just the same as the camera position.