Ray Tracing
Matthew W. Campbell
Senior, Computer Science Dept.
Ray tracing overview
Technique for rendering 3D graphics.
Point sampling technique.
#rays = #pixels (assuming normal sampling)
Models light interaction among surfaces and
the environment.
Uses illumination models to determine color value
at the intersection of a ray and an object.
Supports transparency, reflections, refractions,
etc.
Ray tracing concept and
samples
Light
Shadow
Ray Object
Reflection
Ray
Image
Plane
Eye
Ray tracing algorithm
Begin tracing.
Construct a ray through each pixel in the scene.
Pixel color = result of tracing the ray
Tracing the ray
Find intersection of ray with closest object in the scene.
Compute intersection point and normal at the intersection
point.
Shade the point.
Shade the point
For each light…
Determine if point is in shadow relative to light source.
Using illumination model, determine color at the intersection
point.
Constructing primary rays
Ray is defined by R(t) = Ro + Rd*t where
t>0.
Ro = Origin of ray at (xo, yo, zo)
Rd = Direction of ray [xd, yd, zd] (unit vector).
Determine world location of pixel (i,j).
Origin = (x,y,z) camera position.
Direction (x,y,z) =
j – (WIDTH/2)
i – (HEIGHT/2)
255.0 **Fairly abitrary, just interpret as FOV.
Normalize direction vector.
Ray – Plane intersection
Plane equation:
Ax + By + Cz + D = 0 (point on plane)
(A,B,C) is normal of the plane (unit vector)
Ray/Plane cont.
Substitute ray equation into plane
equation.
A(xo + xdt) + B(yo + ydt) + C(zo +zdt) + D = 0
Solve for t.
t = -(Axo + Byo + Czo + D) / (Axd + Byd + Czd)
t = -(N • Ro + D) / (N • Rd)
Ray/Plane Summary
Compute N • Rd
If equals zero, ray and plane are parallel,
therefore, no intersection.
Compute t
t = -(N • Ro + D) / (N • Rd)
If t rs2 then ray origin is
outside of the sphere.
Ray-Sphere Cont.
Step 2:
Calculate ray distance which is closest to the center of
the sphere.
L = ||OC|| cos ß = ||OC|| ||Rd|| cos ß = OC•Rd
If the ray is outside of the sphere and points away from
the sphere, L must be < 0, and the ray misses the
sphere.
Ray-Sphere Cont.
Step 3:
Calculate the squared distance from the closest
approach along the ray to the sphere’s center – to the
sphere’s surface. (HC2 = half coord distance squared).
D2 = ||OC||2 – L2
HC2 = rs2 – D2
If HC2 < 0, ray misses sphere.
Ray-Sphere Cont.
Step 4:
Find t.
t = L – HC, if ray originates outside the sphere.
t = L + HC, if ray originates inside the sphere.
Step 5:
Calculate the intersection point.
(x,y,z) = Ro + (tRd) = I
Step 6:
Calculate the normal vector.
N = (I-Sc) / Sr, negate if ray originates inside of the
sphere.
Ray-Polygon Intersection
We define a polygon as a list of n points:
{(x0, y0, z0), (x1, y1, z1),…(xn-1, yn-1, zn-1)}.
The polygon is in a plane, i.e. we use planar
polygons.
Algorithm overview
Intersect the ray with the plane of the polygon.
Project the polygon into 2D space.
Translate the polygon so the intersection point of
the ray and the plane sits at the origin.
Determine whether the origin is within the 2D
polygon.
Ray-Polygon Cont.
Project the polygon into 2D space.
Planar projection.
Take the polygon’s normal and find the
dominate component.
Throw away the corresponding coordinate of
each point in the polygon.
Ex: Polygon normal = (0,0,1) = Lies on the XY
plane.
Throw away the z component of each point in
the polygon resulting in a 2D polygon.
Ray-Polygon Cont.
Translate the polygon and check to see if
origin is inside.
Translate the polygon by the intersection point of
the ray and the plane (= I).
For all points in the polygon, Pnew = Pold – I
To determine whether the origin is within a 2D
polygon, we only need to count the number of
times the polygon’s edges cross the positive u´-
axis as we walk around the polygon. Odd number
crossings: origin is within.
Ray-Polygon Cont.
Ray tracing optimizations
Adaptive grid sampling
Instead of using one ray per pixel, use one ray per
block of pixels if the detail in that part of the
image is small enough.
Spatial Partitioning!!
Uniform grids.
Octrees, KD-trees, BSP-trees.
Any BVH or bounding volume hierarchy.
Reduce recursion depth when calculating
reflections and refractions.