You are on page 1of 8

Outline

Foundations of Computer Graphics §  Camera Ray Casting (choose ray directions)
Online Lecture 10: Ray Tracing 2 – Nuts and Bolts §  Ray-object intersections
Camera Ray Casting §  Ray-tracing transformed objects
§  Lighting calculations
Ravi Ramamoorthi §  Recursive ray tracing

Outline in Code Ray Casting


Image Raytrace (Camera cam, Scene scene, int width, int height)
{
Image image = new Image (width, height) ;
for (int i = 0 ; i < height ; i++)
for (int j = 0 ; j < width ; j++) {
Ray ray = RayThruPixel (cam, i, j) ; Virtual Viewpoint
Intersection hit = Intersect (ray, scene) ;
image[i][j] = FindColor (hit) ;
}
Virtual Screen Objects
return image ;
} Multiple intersections:
Ray intersects
misses all object: Useusing
objects:shade
Pixel closest
colored one
color,
black (as does
lights, OpenGL)
materials

Finding Ray Direction Similar to gluLookAt derivation


§  Goal is to find ray direction for given pixel i and j §  gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx,
upy, upz)
§  Many ways to approach problem
§  Objects in world coord, find dirn of each ray (we do this) §  Camera at eye, looking at center, with up direction being up
§  Camera in canonical frame, transform objects (OpenGL) Up vector

§  Basic idea


§  Ray has origin (camera center) and direction Eye
§  Find direction given camera params and i and j

§  Camera params as in gluLookAt Center


From earlier lecture on deriving gluLookAt
§  Lookfrom[3], LookAt[3], up[3], fov

1
Constructing a coordinate frame? Constructing a coordinate frame
a b×w
We want to associate w with a, and v with b w= u= v =w ×u
a b×w
§  But a and b are neither orthogonal nor unit norm
§  And we also need to find u §  We want to position camera at origin, looking down –Z dirn
a §  Hence, vector a is given by eye – center
w=
a
§  The vector b is simply the up vector Up vector
b×w
u=
b×w Eye

v =w ×u
From basic math lecture - Vectors: Orthonormal Basis Frames Center

Canonical viewing geometry Canonical viewing geometry

βv βv

-w αu -w αu

⎛ fovx ⎞ ⎛ j − (width / 2) ⎞ ⎛ fovy ⎞ ⎛ (height / 2) − i ⎞


α = tan ⎜ × β = tan ⎜ ×
⎝ 2 ⎟⎠ ⎜⎝ width / 2 ⎟⎠ ⎝ 2 ⎟⎠ ⎜⎝ height / 2 ⎟⎠

Canonical viewing geometry


Foundations of Computer Graphics
Online Lecture 10: Ray Tracing 2 – Nuts and Bolts
βv
α u + βv − w
-w αu ray = eye +
α u + βv − w
Ray-Object Intersections

Ravi Ramamoorthi
⎛ fovx ⎞ ⎛ j − (width / 2) ⎞ ⎛ fovy ⎞ ⎛ (height / 2) − i ⎞
α = tan ⎜ × β = tan ⎜ ×
⎝ 2 ⎟⎠ ⎜⎝ width / 2 ⎟⎠ ⎝ 2 ⎟⎠ ⎜⎝ height / 2 ⎟⎠

2
Outline Outline in Code
Image Raytrace (Camera cam, Scene scene, int width, int height)
§  Camera Ray Casting (choosing ray directions) {
Image image = new Image (width, height) ;
§  Ray-object intersections
for (int i = 0 ; i < height ; i++)
§  Ray-tracing transformed objects for (int j = 0 ; j < width ; j++) {
Ray ray = RayThruPixel (cam, i, j) ;
§  Lighting calculations Intersection hit = Intersect (ray, scene) ;
image[i][j] = FindColor (hit) ;
§  Recursive ray tracing
}
return image ;
}

Ray-Sphere Intersection Ray-Sphere Intersection


     
ray ≡ P = P0 + P1t ray ≡ P = P0 + P1t
       
sphere ≡ (P − C) i (P − C) − r 2 = 0 sphere ≡ (P − C) i (P − C) − r 2 = 0
Substitute

P0

Ray-Sphere Intersection Ray-Sphere Intersection


     
ray ≡ P = P0 + P1t ≡ P = P0 + P1t
ray
       
sphere ≡ (P − C) i (P − C) − r 2 = 0 sphere ≡ (P − C) i (P − C) − r 2 = 0
Substitute    Substitute   
ray ≡ P = P0 + P1t ray ≡ P = P0 + P1t
           
sphere ≡ (P0 + P1t − C) i (P0 + P1t − C) − r 2 = 0 sphere ≡ (P0 + P1t − C) i (P0 + P1t − C) − r 2 = 0
Simplify Simplify
        
t 2 (P1 i P1) + 2t P1 i (P0 − C) + (P0 − C) i (P0 − C) − r 2 = 0

3
 
Ray-Sphere Intersection
      
Ray-Sphere Intersection
t (P1 i P1) + 2t P1 i (P0 − C) + (P0 − C) i (P0 − C) − r 2 = 0
2   
§  Intersection point: ray ≡ P = P0 + P1t
Solve quadratic equations for t
§  2 real positive roots: pick smaller root §  Normal (for sphere, this is same as coordinates
in sphere frame of reference, useful other tasks)
§  Both roots same: tangent to sphere  
P −C
normal =  
§  One positive, one negative root: ray P −C
origin inside sphere (pick + root)
§  Complex roots: no intersection (check
discriminant of equation first)

Ray-Triangle Intersection Ray-Triangle Intersection


§  One approach: Ray-Plane intersection, then §  One approach: Ray-Plane intersection, then
check if inside triangle check if inside triangle n=
(C − A) × (B − A)
(C − A) × (B − A)
§  Plane equation: A B §  Plane equation: A
B

C C

Ray-Triangle Intersection Ray-Triangle Intersection


§  One approach: Ray-Plane intersection, then §  One approach: Ray-Plane intersection, then
check if inside triangle n=
(C − A) × (B − A) check if inside triangle n=
(C − A) × (B − A)
(C − A) × (B − A) (C − A) × (B − A)
§  Plane equation: A
B §  Plane equation: A
B
       
plane ≡ P i n − A i n = 0 plane ≡ P i n − A i n = 0
   
A i n − P0 i n
§  Combine with ray equation t=  
   P1 i n
C ray ≡ P = P0 + P1t C
    
(P0 + P1t) i n = A i n

4
Ray inside Triangle Ray inside Triangle
B
§  Once intersect with plane, need to find if in triangle A
β P = α A + βB + γ C
α α ≥ 0, β ≥ 0, γ ≥ 0
§  Many possibilities for triangles, general polygons P
α + β +γ =1
γ
§  We find parametrically [barycentric coordinates]. Also
useful for other applications (texture mapping) C
A B
β P − A = β (B − A) + γ (C − A)
α P = α A + βB + γ C
P 0 ≤ β ≤1 , 0 ≤γ ≤1
α ≥ 0, β ≥ 0, γ ≥ 0
β +γ ≤1
γ α + β +γ =1

Other primitives Ray Scene Intersection


Intersection (ray, scene) {
§  Much early work in ray tracing focused on ray-primitive
mindist = infinity; hitobject = NULL ;
intersection tests
For each object in scene { // Find closest intersection; test all objects
§  Cones, cylinders, ellipsoids t = Intersect (ray, object) ;
if (t > 0 && t < mindist) // closer than previous closest object
§  Boxes (especially useful for bounding boxes)
mindist = t ; hitobject = object ;
§  General planar polygons }
§  Many more return IntersectionInfo(mindist, hitobject) ; // may already be in Intersect()
}

Outline Ray-Tracing Transformed Objects


§  Camera Ray Casting (choosing ray directions) We have an optimized ray-sphere test
§  But we want to ray trace an ellipsoid…
§  Ray-object intersections
Solution: Ellipsoid transforms sphere
§  Ray-tracing transformed objects §  Apply inverse transform to ray, use ray-sphere
§  Allows for instancing (traffic jam of cars)
§  Lighting calculations
§  Same idea for other primitives
§  Recursive ray tracing

5
Transformed Objects
§  Consider a general 4x4 transform M (matrix stacks) Foundations of Computer Graphics
§  Apply inverse transform M-1 to ray
§  Locations stored and transform in homogeneous coordinates Online Lecture 10: Ray Tracing 2 – Nuts and Bolts
§  Vectors (ray directions) have homogeneous coordinate set Lighting Calculations
to 0 [so there is no action because of translations]

§  Do standard ray-surface intersection as modified


Ravi Ramamoorthi
§  Transform intersection back to actual coordinates
§  Intersection point p transforms as Mp
§  Normals n transform as M-tn. Do all this before lighting

Outline Outline in Code


Image Raytrace (Camera cam, Scene scene, int width, int height)
§  Camera Ray Casting (choosing ray directions) {
Image image = new Image (width, height) ;
§  Ray-object intersections
for (int i = 0 ; i < height ; i++)
§  Ray-tracing transformed objects for (int j = 0 ; j < width ; j++) {
Ray ray = RayThruPixel (cam, i, j) ;
§  Lighting calculations Intersection hit = Intersect (ray, scene) ;
image[i][j] = FindColor (hit) ;
§  Recursive ray tracing
}
return image ;
}

Shadows Light Source Shadows: Numerical Issues


%  Numerical inaccuracy may cause intersection to be
below surface (effect exaggerated in figure)
%  Causing surface to incorrectly shadow itself
J  Move a little towards light before shooting shadow ray

Virtual Viewpoint

Virtual Screen Objects


Shadow raytotolight
Shadow ray lightisisunblocked:
blocked: object
objectvisible
in shadow

6
Lighting Model Material Model
§  Similar to OpenGL §  Diffuse reflectance (r g b)
§  Lighting model parameters (global) §  Specular reflectance (r g b)
§  Ambient r g b L0
§  Attenuation const linear quadratic L = const + lin * d + quad * d 2 §  Shininess s

§  Per light model parameters §  Emission (r g b)


§  Directional light (direction, RGB parameters)
§  All as in OpenGL
§  Point light (location, RGB parameters)
§  Some differences from HW 2 syntax

Shading Model
n Foundations of Computer Graphics
I = K a + K e + ∑Vi Li (K d max (li i n,0) + K s (max(hi i n,0))s )
i=1 Online Lecture 10: Ray Tracing 2 – Nuts and Bolts
§  Global ambient term, emission from material Recursive Ray Tracing
§  For each light, diffuse specular terms
§  Note visibility/shadowing for each light (not in OpenGL) Ravi Ramamoorthi
§  Evaluated per pixel per light (not per vertex)

Outline Mirror Reflections/Refractions


§  Camera Ray Casting (choosing ray directions)
§  Ray-object intersections
§  Ray-tracing transformed objects
Virtual Viewpoint
§  Lighting calculations
§  Recursive ray tracing
Virtual Screen Objects
Generate reflected ray in mirror direction,
Get reflections and refractions of objects

7
Basic idea Recursive Shading Model
n

For each pixel I = Ka + Ke + ∑V L (K max (l i n,0) + K (max(h i n,0)) ) + K I


i i d i s i
s
s R
+ KT IT
i=1
§  Trace Primary Eye Ray, find intersection §  Highlighted terms are recursive specularities [mirror
§  Trace Secondary Shadow Ray(s) to all light(s)
reflections] and transmission (latter is extra)
§  Color = Visible ? Illumination Model : 0 ;
§  Trace secondary rays for mirror reflections and
§  Trace Reflected Ray refractions, include contribution in lighting model
§  Color += reflectivity * Color of reflected ray
§  GetColor calls RayTrace recursively (the I values in
equation above of secondary rays are obtained by
recursive calls)

Problems with Recursion Some basic add ons


§  Area light sources and soft shadows: break into
§  Reflection rays may be traced forever
grid of n x n point lights
§  Use jittering: Randomize direction of shadow ray
within small box for given light source direction
§  Generally, set maximum recursion depth §  Jittering also useful for antialiasing shadows when
shooting primary rays

§  More complex reflectance models


§  Same for transmitted rays (take refraction into account) §  Simply update shading model
§  But at present, we can handle only mirror global
illumination calculations

You might also like