# CS488 - Introduction to Computer Graphics - Lecture 13

1. Due date for assignment 4
2. List of project objectives

# Ray Tracing

The idea is simple:

for each pixel
calculate the eye/pixel ray
project the ray into the scene
colour the pixel with the result

This calculation is linear in the number of pixels.

Now let's look at the parts of this algorithm

E + t*(P - E)

### Find the First Intersection

closest = infinity
for each object in scene {
if ( eye ray intersects ) calculate distance (t)
if ( distance < closest ) closest = distance; set intersection
}

This calculation is linear in the number of objects

• Therefore, ray tracing is linear in the product of the number of pixels and the number of objects

#### Object in scene

An object is a set of points {Q} satisfying

• f(Q) = 0
• This is the surface of an object. (How do you know?)
• Why don't we care about inside points?

Solve the equation

• f(E + t*(P-E)) = 0 for t.
• For example, all points, Q, on the surface of a sphere, centred at C, of radius r, satisfies |Q-C| = r
• Thus, we solve | E - C + t*(P-E) | = r
• which amounts to (E - C + t*(P-E)).(E - C + t*(P-E)) = r^2
• This is the quadratic equation
• t^2 | P-E |^2 + 2t (E-C).(P-E) + | E-C |^2 - r^2 = 0

which has zero, one or two solutions.

• Most of the time there is no solution
• Calculate the discriminant. Why?
• It is: (r^2 - |E-C|^2 (sun(\theta))^2) / |P-E|^2

The surface might be defined by an algorithm, or you may not be able to find an analytic solution. Then use a root-finding method

• For example, float f(Q) returns distance of point (Q) from surface
• Submit f(E + t*(P-E)) to your favourite root-finder

#### Object is planar, e.g. a triangle

Equation of the plane is \sum_i ai*xi = b. Where do the parameters (ai) come from?

• Normal vector is n = (a0, a1, a2, 0)
• Take the cross product of any two edges n = (Vn - Vn+1) x (Vm - Vm+1). Now we know the ai.
• Substitute and vertex Vn = (v0, v1, v2)
• b = \sum ai*vi

Substitute eye ray into the plane equation and solve for t. This point is on the plane: is it inside the polygon?

1. Triangle: calculate barycentric coordinates; if < 0 outside.
2. Convex polygon: do n half-plane tests
3. Clip the point against the polygon edges

#### Another idea: intersect in object coordinates

• In object coordinates each point is given by the object definition.
• In world coordinates each point in object coordinates is Q = MQ', where Q' is the point in object coordinates
• M is an affine transformation, therefore invertible

f'(Q') is the object definition in object coordinates

• Use invM to transform E,P to E',P', then solve the equation for t
• Find intersection point in world coordinates as E + t*(P-E)

For a game application you can just use a bit mask

When we have found the intersection point we need to calculate a colour.

We know

1. The eye ray, V
2. Surface properties
3. The normal vector
• from a planar primitive
• by interpolation from vertex normals
• by calculation from equation of surface

For each light

1. Calculate illumination: RGB, l

Accumulate outgoing light

Use a line from the intersection point to the light source as an eye ray and do it all again.

Calculate the deviation of the eye ray and continue following it.

### Recursive Ray Tracing

Just what the name says, but why would you do it?

1. Unusual light paths for illumination, such as colour bleeding, reflection

### Surface Normals

Implicit surfaces, i.e., given by f(Q) = 0.

• Normal is perpendicular to the tangent space n . (Q' - Q) as Q' -> Q for all Q' on the surface.
• Normal is grad f(Q) = i df/dx + j df/dy + k df/dz

Transforming the normal vector

1. Under an affine transformation, M, Q goes to MQ, and Q' goes to M(Q').
2. The new tangent, n1, is defined by n1.M(Q'-Q) = 0.
3. Notice the structure: contravariant versus covariant.

### Constructive Solid Geometry (CSG)

Geometric primitives can make other geometric primitives by

1. union
2. intersection
3. subtraction

The result is like sculpting; the problem is that describing the result is very difficult; the solution is

• Keep the combined primitives uncombined in the model
• Do the set operations on the ray

### Texture Mapping

1. Basic
2. Map 2D image onto primitive using a 2D affine transformation
• Simple if the surface of the primitive is flat
• otherwise, ...
• Texture pixels normally do not match display pixels, so some image processing may be needed.
3. Backwards map intersection point with ray into the image to get the surface properties
2. Normal Mapping (Bump mapping)
1. Start with a difference surface, defined with respect to the surface
2. Calculate the normals to the difference surface and map them onto the surface of the primitive
3. Use the mapped surface models for lighting
4. No occlusion, shadows are wrong, silhouettes are wrong, nobody notices!
3. Solid Textures
1. Solution to mapping texture onto curved surfaces
2. Usually procedural

### Speeding Up Intersection Tests

1. Bounding Volumes (usually Boxes)
1. Find the smallest - or close to smallest - regular volume that encloses the object
2. Use ray-intersect-volume as a pretest for fast negative results.
• benefit of quicker `miss'detection
• cost of getting a good bounding surface
• Both increase as complexity of primitive increases

Scene subdision

1. Divide the scene volume into sub-volumes: many ways to do it, such as