CSCI 461: Computer Graphics

Middlebury College, Fall 2023

Lecture 02: Ray Tracing

By the end of today's lecture, you will be able to:

  • generate camera rays for a view directly aligned with the z-axis,
  • review some of the math used in computer graphics,
  • intersect rays with simple geometric primitives, such as planes, spheres and triangles.

Search Google Images for "ray tracing."

Look for images that show ray tracing on/off. What kinds of differences do you observe?

How do we see things?


(source)

The main idea of ray tracing.

Send "rays" from an observation point (e.g. a camera) through each pixel in an image and into the (virtual) 3d world. Whatever is hit by the ray defines the color of the pixel.


Click to open the ray tracing demo.

Ingredients for a ray tracer.

  1. Set up your image and place your observer (camera/eye).
  2. For each pixel in your image:
    1. Create a ray originating at your camera position that passes through this pixel.
    2. Determine the closest object in your scene intersected by the ray.
    3. Determine the color of the pixel, based on the intersection.

Step 1: setting up a camera and image plane.

What are the dimensions of the image plane in 3d space?

Which coordinate system should we use?

For a perspective view, rays will start at eye and pass throagh each pixel.

orthographic

perspective

Step 2a: Calculating the 3d coordinates of a pixel.

$$ x = -\frac{w}{2} + w \frac{(i + 0.5)}{n_x} \quad y = -\frac{h}{2} + h \frac{(n_y - 0.5 - j)}{n_y} $$

What is the z-coordinate of each pixel in our coordinate system?

Step 2b: Calculating the intersection of a ray with a sphere.

Why? because lots of things can be approximated as spheres!

Calculating the intersection of a ray with a sphere.

More practice with glMatrix!

Step 2b: Calculating the intersection of a ray with a triangle.

Why? because most things are not spheres :(

$$ \vec{p}(u, v) = \vec{a} u + \vec{b} v + (1 - u - v) \vec{c}$$

Calculating the intersection of a ray with a triangle.

$$ \vec{p}(u, v) = \vec{x}(t) \quad \rightarrow \quad \vec{a} u + \vec{b} v + (1 - u - v) \vec{c} = \vec{e} + \vec{r}\ t. $$

$$ \left[ \begin{array}{ccc} (a_x - c_x) & (b_x - c_x) & - r_x \\ (a_y - c_y) & (b_y - c_y) & - r_y \\ (a_z - c_z) & (b_z - c_z) & - r_z \end{array}\right] \left[ \begin{array}{c} u \\ v \\ t \end{array}\right] = \left[ \begin{array}{c} e_x - c_x \\ e_y - c_y \\ e_z - c_z \end{array}\right] $$

check $0 \le u, v, w \le 1$
where $w = 1 - u - v$

Recap

  • Calculate dimensions of image plane ($w$ and $h$) from field-of-view ($\alpha$) and image aspect ratio.
  • Calculate 3d coordinates of each pixel and direction of ray from eye to pixel.
  • Intesect ray with objects in scene (spheres, triangles, etc.).
  • Use closest intersection point: smallest $t$ value.
  • What's missing? looks 2d :( shading next week!