CS 371
Computer Graphics
Spring 2005


Lecture 2: A Simple Ray Tracer

A Note on the Readings

There is some basic, easy-to-digest information on graphics hardware that you should know. It is in Chapter 1 of your text. Please read it. These notes are on a completely different topic! Also, read Sections 4.1-4.3 on basic properties of vectors.

Goal

Given a scene composed of geometric objects, produce an image (2D array of pixel values) of (part of) the scene.

Idea

Project scene onto a rectangular region of a viewing plane Alternately: Sample the scene by casting rays from the eye, through the view plane, into the scene.

What We Need

Components

Algorithm

Given a camera, some ambient light, and a scene (list of spheres), we compute sampling rays from eye and determine closest (first) point on list of spheres hit by ray. Ambient lighting co-efficients are used for color of pixel

Pseudo-code

Questions

Representing a Ray

Given a point p=(px,py,pz) and a vector d=(dx,dy,dz), the ray R through p in direction d is given by the function R(t)=p + t d, where t>0 and t (x,y,z)=(t x,t y,t z).

Constructing Sampling Rays

Given a point s=(sx,sy,sz) on the view plane, and the position of the eye e=(ex,ey,ez), the sampling ray uses e as its point and s-e as its direction.

Note: R(0) = e and R(1) = s.

If we want a rows by cols grid of samples using the VP with corners (-1,1,0), (1,1,0),(1,-1,0) and (-1,-1,0), we can divide the VP into rows ·cols rectangles, and use the center of rectangle (i,j) as s.

Some Remarks on Dot Products

See Section 4.3 of your text.

The dot product of two vectors v = (x,y,z) and v' = (x',y',z') is defined by v ·v' = x x' + y y' + z z'.

Note: Dot products have many nice properties, including

Read all about dot product properties in your text!

Intersecting a Ray with a Sphere

A sphere S with center q = (a,b,c) and radius r>0 has it's surface given by all points (x,y,z) having
(x-a)2 + (y-b)2 + (z-c)2 = r2,
or, equivalently, by all points (x,y,z) having F(x,y,z) = 0 where
F(x,y,z) = (x-a)2 + (y-b)2 + (z-c)2 - r2.

For ray R to intersect sphere S, we need that, for some t>0

F(R(t)) = (px+t dx - a)2 + (py+t dy - b)2 + (pz+t dz - c)2 - r2 = 0.
Note: We usually write F(t) instead of F(R(t)).

This can be simplified using dot product notation: We can write

F(t) = (p + t d - q) ·(p + t d - q) - r2 = 0.

Or:

F(t) = (p - q) ·(p - q) + 2 t d ·(p - q) + t2 d ·d - r2 = 0.

We can re-write this as F(t) = A t2 + 2 B t + C = 0, where

Note: By the quadratic formula

t= (-2 B ±sqrt((2 B)2-4AC))/(2A).
Thus R intersects S iff B2-AC >= 0.

(The 4 was factored out and cancelled with the 2 A in the denominator.). This quantity B2-AC is called the discriminant.

If R does intersect S, we then choose the smaller positive of the (usually) 2 values of t corresponding to intersections of R & S.

Determining the Color of the Sample

Each object in our scene will store a description of how it interacts with light. This interaction can get a bit complex, so for now we assume that the object interacts as a diffuse reflector of ambient light. In theory, there should be an ambient light coefficient Ia,lambda for each wavelength lambda, but, in practice a small number of wavelengths are used (such as RGB or YCM).

We define, for each of the three primary colors red, green and blue, an ambient light intensity for the scene. It is a vector Ia = (Ia,r, Ia,g, Ia,b) of values in the interval [0,1]. Each object has ambient lighting co-efficients rhoa = (rhoa,r, rhoa,g, rhoa,b) in the same interval. If there were no light source, the pixel color of an object with ambient lighting co-efficients rhoa would get color

(Ia,r rhoa,r, Ia,g rhoa,g, Ia,b rhoa,b).

lenhart@cs.williams.edu