Ray tracing idea
Ray Tracing
CS 465 Lecture 3
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 1 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 2
Ray tracing algorithm Plane projection in drawing
for each pixel {
compute viewing ray
intersect ray with scene
compute illumination at visible point
[CS 417 Spring 2002]
put result into image
}
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 3 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 4
Plane projection in photography Generating eye rays
This is another model for what we are doing Use window analogy directly
applies more directly in realistic rendering
[CS 417 Spring 2002]
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 5 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 6
Vector math review Ray: a half line
Vectors and points Standard representation: point p and direction d
Vector operations
addition this is a parametric equation for the line
scalar product lets us directly generate the points on the line
if we restrict to t > 0 then we have a ray
More products
note replacing d with ad doesnt change ray (a > 0)
dot product
cross product
Bases and orthogonality
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 7 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 8
Ray-sphere intersection: algebraic Ray-sphere intersection: algebraic
Condition 1: point is on ray Solution for t by quadratic formula:
Condition 2: point is on sphere
assume unit sphere; see Shirley or notes for general
simpler form holds when d is a unit vector
but we wont assume this in practice (reason later)
Substitute: Ill use the unit-vector form to make the geometric interpretation
this is a quadratic equation in t
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 9 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 10
Ray-sphere intersection: geometric Ray-box intersection
Could intersect with 6 faces individually
Better way: box is the intersection of 3 slabs
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 11 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 12
Ray-slab intersection Intersecting intersections
2D example Each intersection
is an interval
3D is the same!
Want last
entry point and
first exit point
Shirley fig. 10.16
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 13 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 14
Image so far Intersection against many shapes
With eye ray generation and sphere intersection The basic idea is:
hit (ray, tMin, tMax) {
tBest = +inf; hitSurface = null;
Surface s = new Sphere((0.0, 0.0, 0.0), 1.0);
for surface in surfaceList {
for 0 <= iy < ny
t = surface.intersect(ray, tMin, tMax);
for 0 <= ix < nx {
if t < tBest {
ray = camera.getRay(ix, iy);
tBest = t;
if (s.intersect(ray, 0, +inf) < +inf)
hitSurface = surface;
image.set(ix, iy, white);
}
}
}
return hitSurface, t;
}
this is linear in the number of shapes
but there are sublinear methods (acceleration structures)
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 19 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 20
Image so far Shading
With eye ray generation and scene intersection Compute light reflected toward camera
Inputs:
eye direction
for 0 <= iy < ny
for 0 <= ix < nx { light direction
ray = camera.getRay(ix, iy); (for each of many lights)
c = scene.trace(ray, 0, +inf); surface normal
image.set(ix, iy, c); surface parameters
} (color, shininess, )
More on this in the
next lecture
trace(ray, tMin, tMax) {
surface, t = hit(ray, tMin, tMax);
if (surface != null) return surface.color();
else return black;
}
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 21 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 22
Image so far Shadows
trace(Ray ray, tMin, tMax) { Surface is only illuminated if nothing blocks its view of the light.
surface, t = hit(ray, tMin, tMax);
if (surface != null) { With ray tracing its easy to check
point = ray.evaluate(t); just intersect a ray with the scene!
normal = surface.getNormal(point);
return surface.shade(ray, point,
normal, light);
}
else return black;
}
shade(ray, point, normal, light) {
v_E = normalize(ray.direction);
v_L = normalize(light.pos - point);
// compute shading
}
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 23 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 24
Image so far Shadow rounding errors
Dont fall victim to one of the classic blunders:
shade(ray, point, normal, light) {
shadRay = (point, light.pos - point);
if (shadRay not blocked) {
v_E = normalize(ray.direction);
v_L = normalize(light.pos - point);
// compute shading
}
return black;
}
Whats going on?
hint: at what t does the shadow ray intersect the surface youre shading?
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 25 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 26
Multiple lights
Solution: shadow rays start a tiny distance from the surface Important to fill in black shadows
Just loop over lights, add contributions
Ambient shading
black shadows are not really right
one solution: dim light at camera
alternative: all surface receive a bit more light
just add a constant ambient color to the shading
Do this by moving the start point, or by limiting the t range
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 27 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 28
Image so far Ray tracer architecture 101
You want a class called Ray
point and direction; evaluate(t)
shade(ray, point, normal, lights) { possible: tMin, tMax
result = ambient;
for light in lights { Some things can be intersected with rays
if (shadow ray not blocked) {
individual surfaces
result += shading contribution;
} the whole scene
} often need to be able to limit the range (e.g. shadow rays)
return result;
} Once you have the visible intersection, compute the color
this is an object thats associated with the object you hit
its job is to compute the color
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 29 Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 30
Architectural practicalities
Return values
surface intersection tends to want to return multiple values
t, surface or shader, normal vector, maybe surface point
in many programming languages (e.g. Java) this is a pain
typical solution: an intersection record
a class with fields for all these things
keep track of the intersection record for the closest intersection
be careful of accidental aliasing (which is very easy if youre new to Java)
Efficiency
in Java the (or, a) key to being fast is to minimize creation of objects
what objects are created for every ray? try to find a place for them
where you can reuse them.
Shadow rays can be cheaper (any intersection will do, dont need closest)
but: Get it Right, Then Make it Fast
Cornell CS465 Fall 2006 !Lecture 3 2006 Steve Marschner 31