Professional Documents
Culture Documents
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
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
βv βv
-w αu -w αu
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 ;
}
P0
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)
C C
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
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]
Virtual Viewpoint
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
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)
7
Basic idea Recursive Shading Model
n