CS488 - Introduction to Computer Graphics - Lecture 13
Comments and Questions
- Due date for assignment 4
- List of project objectives
Ray Tracing
The idea is simple:
for each pixel
calculate the eye/pixel ray
project the ray into the scene
apply illumination and shading calculations
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
Eye-pixel ray
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
- 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?
- Triangle: calculate barycentric coordinates; if < 0 outside.
- Convex polygon: do n half-plane tests
- 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
- 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
Shading for Ray Tracing
When we have found the intersection point we need to calculate a
colour.
We know
- The eye ray, V
- Surface properties
- The normal vector
- from a planar primitive
- by interpolation from vertex normals
- by calculation from equation of surface
For each light
- Calculate illumination: RGB, l
Accumulate outgoing light
How about shadows?
Use a line from the intersection point to the light source as an eye ray
and do it all again.
How about reflection?
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?
- 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
- Under an affine transformation, M, Q goes to MQ, and Q' goes to
M(Q').
- The new tangent, n1, is defined by n1.M(Q'-Q) = 0.
- Notice the structure: contravariant versus covariant.
Constructive Solid Geometry (CSG)
Geometric primitives can make other geometric primitives by
- union
- intersection
- 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
- Basic
- Start with a 2D image: pixellated or procedural
- 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.
- Backwards map intersection point with ray into the image to get the
surface properties
- Normal Mapping (Bump mapping)
- Start with a difference surface, defined with respect to the
surface
- Calculate the normals to the difference surface and map them onto
the surface of the primitive
- Use the mapped surface models for lighting
- No occlusion, shadows are wrong, silhouettes are wrong, nobody
notices!
- Solid Textures
- Solution to mapping texture onto curved surfaces
- Usually procedural
Speeding Up Intersection Tests
- Bounding Volumes (usually Boxes)
- Find the smallest - or close to smallest - regular volume that
encloses the object
- Use ray-intersect-volume as a pretest for fast negative
results.
- Trade-off between
- benefit of quicker `miss'detection
- cost of getting a good bounding surface
- Both increase as complexity of primitive increases
Scene subdision
- Divide the scene volume into sub-volumes: many ways to do it, such
as
- quad-trees
- oct-trees
- BSP trees
- ...
- List all primitives by sub-volume.
- Only test primitives in the sub-volumes through which a ray
passes.
Return to: