# Ray Tracing

## Intersection Tests

E + t*(P - E)

#### Object in scene

Set of points {Q} satisfying

• f(Q) = 0
• Usually f(Q) is a distance function from the surface.
• f(Q) > 0 means outside,
• f(Q) < 0 means inside
• 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.

#### 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 the affine modelling transformation, therefore invertible
• f'(Q') is the object definition in object coordinates, which is usually known
• 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)

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

We know

1. The eye ray, e = Q-E
2. Surface properties
3. The normal vector, n
• by interpolation from vertex normals
• by calculation from the equation of the surface

For each light

1. Calculate illumination: RGB - the colour, l - the light ray
2. Accumulate outgoing light
for ambient
use surface RGB and illuminant RGB to calculate colour
for each source of illumination
calculate the illumination of the intersection point by the source: RGB
calculate the direction of the illumination, l
use e, n, l to calculate intensity of light along eye ray
-- probably using Phong lighting model
use surface RGB and illuminant RGB to calculate colour
add to colour

Illumination may be blocked by occluding objects

In principle,

1. Do an intersection test from the light
• in the direction of the eye ray intersection
2. If it hits the eye ray intersection, then
• intersection illuminated by that light

else

• intersection in shadow from that light

   cast a ray from the illuminant in the direction of the intersection
if first point intersected is the intersection then

In practice this means

1. Translate the illuminant to the origin
2. Run the usual intersection code using l

Just extent the eye ray into the virtual world behind the mirror

In practice this means

1. Translate the intersection point on the mirror to the origin
2. Run the usual intersection code using e = e - 2 n \dot e

## 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

## Transforming Surface Normals

#### The tangent plane (space)

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

• Normal is perpendicular to the tangent space: n \dot (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

Calculating the tangent plane in practice

Either

Or

1. Choose two points, Q1 & Q2, slighty away from Q in different directions
2. The points must lie on the surface: f(Q1) = f(Q2) = 0
3. The plane spanned by Q-Q1 and Q-Q2 is an approximation to the tangent plane

#### The normal vector

The normal vector is the outward facing unit vector perpendicular to the tangent plane.

It is defined by

1. |n| = 1
2. n \dot t1 = 0
3. n \dot t2 = 0

t1 & t2 can be the vectors Q-Q1 & Q-Q2.

In practice

1. 2 & 3 are linear equations
2. Solve them up to a constant
• That is, solve for nx/nz and ny/nz
3. Use 1 to set the overall normalization

#### Transformations

Transforming the normal vector

1. Under an affine transformation, M, Q goes to MQ, and Q' goes to MQ'.
2. The new tangent, n1, is defined by n1.M(Q'-Q) = 0.
3. u \dot Mv = 0 if and only if inv(transposeM)u \dot v = 0.
4. Therefore n' = inv(transposeM)n is the transformed normal vector.

Most of the time this doesn't matter, but ...

# Modelling

#### 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

1. Basic