Title: Ray Tracing
1Ray Tracing
2An Ideal Ray Tracer
- Cast light rays from all light sources.
- Follow them to the eye point.
- Reflect off of solid objects.
- Pass through transparent objects (refraction).
- Obtain color by light absorption.
- Diffuse reflection/refraction multiplies rays.
- For rays that hit the eye point, intersect with
window on view plane, which corresponds to a
screen pixel. - Add the light rays color intensities to the
pixel. - Problem It could take forever to draw an image!
3Ray Tracing That Works
- Observation We are only interested in rays that
cross the eye point. - Idea Start at eye point and trace backwards.
Light Source
Shadow Ray
Eye
Object
Eye Ray
Shadow Ray
Object
Light Source
4Visible Surface Determination
- Ray tracing can be used as a visible surface
determination algorithm - Often called ray casting.
- (A ray is a uni-directional line segment).
- The algorithm
- For each pixel
- Determine the world coordinates of the pixels
center. - Cast a ray from the eye through the pixels
center. - Check for intersection of this ray with any
object. - For closest intersection, determine color of
object at that point.
5Color Determination
- To determine the color at the intersection of an
eye ray with an object, we can - Simply assign the color of the object (no
shading). - Determine the shading of the object.
- Calculate the contribution of a texture at that
point. - Some combination of the above
- Shading
- Cast ray from intersection point to each light
source. - The shadow ray.
- Light contributes to shading only if the shadow
ray does not intersect an object in the scene.
6Intersections
- Ray tracing algorithms are dominated by
intersection testing and determination - Need efficient methods to test for intersection
- Does the eye ray or shadow ray intersect an
object? - Need efficient method to calculate intersection
points and normal vectors (for lighting
calculations). - Ray/Sphere
- Ray/Plane
- Ray/Polygon
7Ray/Sphere Intersection (Algebraic
Solution)
- Ray is defined by R(t) Ro Rdt where t gt 0.
- Ro Origin of ray at (xo, yo, zo)
- Rd Direction of ray xd, yd, zd (unit vector)
- Sphere's surface is defined by the set of points
(xs, ys, zs) satisfying the equation - (xs - xc)2 (ys - yc)2 (zs - zc)2 - rs2
0 - Center of sphere (xc, yc, zc)
- Radius of sphere rs
8Possible Cases of Ray/Sphere Intersection
1. Ray intersects sphere twice with tgt0 2. Ray
tangent to sphere 3. Ray intersects sphere with
tlt0 4. Ray originates inside sphere 5. Ray does
not intersect sphere
1
2
3
4
5
9Solving For t
- Substitute the basic ray equation
- x xo xdt
- y yo ydt
- z zo zdt
- into the equation of the sphere
- (x0 xdt - xc)2 (y0 ydt - yc)2 (z0 zdt -
zc)2 - rs2 0 - This is a quadratic equation in t At2 Bt C
0, where - A xd2 yd2 zd2
- B 2xd(x0 - xc) yd(y0 - yc) zd(z0 - zc)
- C (x0 - xc)2 (y0 - yc)2 (z0 - zc)2 - rs2
- Note A1
10Relation of t to Intersection
- We want the smallest positive t - call it ti
t0
Discriminant 0
t1
t0
t0
t1
t1
Discriminant lt 0
11Actual Intersection
- Intersection point, (xi, yi, zi) (xoxdti,
yoydti, zozdti) - Unit vector normal to the surface at this point
is - N (xi - xc) / rs, (yi - yc) / rs, (zi - zc) /
rs - If the ray originates inside the sphere, N should
be negated so that it points back toward the
center.
N
N
12Summary (Algebraic Solution)
- Calculate A, B and C of the quadratic
- Calculate discriminant (If lt 0, then no
intersection) - Calculate t0
- If t0 lt 0, then calculate t1 (If t1 lt 0, no
intersection point on ray) - Calculate intersection point
- Calculate normal vector at point
- Helpful pointers
- Precompute rs2
- Precompute 1/rs
- If computed t is very small then, due to rounding
error, you may not have a valid intersection
13Ray/Sphere Intersection (Geometric Solution)
- In the case of the sphere there are a number of
tests that we can make to determine that there is
not an intersection. This approach allows us to
avoid calculations and result in a faster
algorithm. - 1. Find if the ray's origin is outside the sphere.
Origin to center vector, OC C - Ro Length
squared of OC OC2 If OC2 lt rs2 then
the ray origin is inside the sphere (go to step
4) Else if OC2 gt rs2 then the ray origin is
outside the sphere
14R/S Geometric Solution (cont.)
- 2. Find the length of the ray to the point along
the ray closest to the sphere's center.
L OC cos ß OC Rd cos ß OCRd
If the ray is outside and points away from the
sphere (L lt 0), the ray must miss the sphere.
15R/S Geometric Solution (cont.)
- 3. Else find the squared distance from the
closest approach along the ray to the sphere's
center to the sphere's surface (HC2 half chord
distance squared).
D2 OC2 - L2 HC2 rs2 - D2 rs2 -
OC2 L2
If HC2 lt 0, the ray misses the sphere.
16R/S Geometric Solution (cont.)
- 4. Else find t for the intersection
- ti L - HC for rays originating outside the
sphere - ti L HC for rays originating inside or on
the sphere - 5. Calculate intersection point (xi, yi, zi) by
plugging ti into Ro tRd - 6. Calculate normal vector
- (xi - xc)/rs (yi - yc)/rs, (zi - zc)/rs
- (negate if ray originates inside sphere)
17Summary (Geometric Solution)
- Find distance squared between ray origin and
center of sphere, test for ray outside sphere. - Calculate ray distance which is closest to the
center of the sphere. - Test if the ray points away from the sphere.
- Find square of half chord intersection distance.
- Test if square is negative.
- Calculate intersection distance.
- Find intersection point.
- Calculate normal vector at point.
- These techniques are no slower than the
algebraic solution and often allow you to
determine that there is no intersection using
fewer calculations.
18Ray/Plane Intersection
- Ray is defined by R(t) Ro Rdt where t gt 0
- Ro Origin of ray at (xo, yo, zo)
- Rd Direction of ray xd, yd, zd (unit
vector) - Plane is defined by A, B, C, D
- Ax By Cz D 0 for a point in the plane
- Normal Vector, N A, B, C (unit vector)
- A2 B2 C2 1
19Ray/Plane (cont.)
- Substitute the ray equation into the 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)
- Note The normal vector of the plane should
usually (except for backface removal) be for the
surface facing the ray. If it isn't then it
should be reversed before illumination is
calculated. - If N Rd gt 0 then use N -A, B, C
20What Can Happen?
21Ray/Plane Summary
- Intersection point
- (xi, yi, zi) (xo xdti, yo ydti, zo zdti)
- Calculate N Rd and compare it to zero.
- Calculate ti and compare it to zero.
- Compute intersection point.
- Compare N Rd to zero and reverse normal if
appropriate
22Ray/Polygon Intersection
- A polygon is defined by a set of p points Gn
(xn, yn, zn) n 0, 1, ..., (p-1) - The polygon is in a plane, Ax By Cz D 0,
Normal N A, B, C - Find the intersection of the ray with the plane
of the polygon. - Throw away the coordinate of each point for which
the corresponding normal component is of the
greatest magnitude (dominant coordinate). - You now have a 2-D polygon defined by the set of
points - (un, vn) n 0, 1, .., p-1
- Translate the polygon so that the intersection
point of the ray with the plane of the polygon
goes to the origin of the u,v system. Call these
translated points - (un, vn) n 0, 1, .., p-1
- Determine whether the origin is within the 2D
polygon.
23Ray/Polygon (cont.)
24Ray/Polygon Algorithm
- To determine whether the origin is within a 2D
polygon, we only need to count the number of
times the polygons edges cross the positive
u-axis as we walk around the polygon. Odd number
crossings origin is within. - num_crossings 0
- sign_holder sign(v0)
- for(a0 a lt p a)
- b (a1) p
- next_sign_holder sign(vb)
- if (sign_holder ! next_sign_holder)
- if (ua gt 0 and ub gt 0)
- num_crossings num_crossings 1
- else if (ua gt 0 or ub gt 0)
- int ua - va(ub - ua) / (vb - va)
- if(int gt 0)
- num_crossings num_crossings 1
-
- sign_holder next_sign_holder
-
-
- if(num_crossings 2 1) ray intersects polygon