ray tracing
1
image formation
2
image formation
3
rendering
computational simulation of image formation
4
rendering
given viewer, geometry, materials, lights
determine visibility and compute colors
5
raytracing
a specific rendering algorithm
6
raytracing algorithm
for each pixel {
determine viewing direction
intersect ray with scene
compute illumination
store result in pixel
}
7
vector math review
point: location in 3D space
vector: direction and magnitude
P = ( , , )Px Py Pz
v = ( , , )vx vy vz
8
vector math review
dot product
cross product
is orthogonal to and
a ⋅ b = |a||b| cos θ
|a × b| = |a||b| sin θ
a × b a b
9
vector math review
segment: set of points (line) between two points
with P(t) = A + t(B − A) t ∈ [0, 1]
10
vector math review
ray: infinite line from point in a given direction
with P(t) = E + td t ∈ [0, ∞]
11
vector math review
coordinate system aka frame
frame : position and orthonormal
axes
default (or world) frame: origin and three major axes
f = { , , , }fO fx fy fz
12
vector math review
point coords are defined wrt a frame
wrt (world if not
specified)
P = ( , , )Px Py Pz { , , , }fO fx fy fz
P = ((P − ) ⋅ , (P − ) ⋅ , (P − ) ⋅ )fO fx fO fy fO fz
13
vector math review
change of coordinate system
is w.r.t
f → f ′
= ( , , )P′ P ′x P ′
y P ′z P { , , , }f ′
O f ′x f ′
y f ′z
= ((P − ) ⋅ , (P − ) ⋅ , (P − ) ⋅ )P′ f ′O f ′
x f ′O f ′
y f ′O f ′
z
14
vector math review
change of coordinate system
is w.r.t
→ ff ′
= ( , , )P′ P ′x P ′
y P ′z P { , , , }f ′
O f ′x f ′
y f ′z
P = + + +f ′O P ′
xf ′x P ′
y f ′y P ′
z f ′z
15
vector math review
vector coords are defined wrt a frame
to change coord system, ignore origin
v = + +v′xf ′
x v′y f ′
y v′zf ′
z
= (v ⋅ , v ⋅ , v ⋅ )v′ f ′x f ′
y f ′z
16
vector math review
construct a frame from two non-orthonormal vectors ,
assume that is not parallel to
construct a frame from a vector
pick arbitrary and continue as above
z′ y′
z′ y′
z = /| |z′ z′
x = × z/| × z|y′ y′
y = z × xz′
y′
17
vector math review
infinite plane
with
normal:
P ∈ plane ; (P − C) ⋅ n = 0 ; P ⋅ n = d
P(u, v) = C + u ⋅ u + v ⋅ v (u, v) ∈ (−∞, ∞)2
n = u × v
18
vector math review
triangle baricentric coordinates
with
, ...
P(α, β, γ) = αA + βB + γC α + β + γ = 1P(α, β) = α(A − C) + β(B − C) + Cα = area(BCP)/area(ABC)
19
vector math review
sphere
P ∈ sphere ; |P − C| = R
P(u, v) = C + R ⋅ (cos ϕ sin θ, sin ϕ sin θ, cos θ)
20
viewing
for each pixel {
-> determine viewing direction
intersect ray with scene
compute illumination
store result in pixel
}
21
viewer model
a painter tracing objects on a canvas in front
[Marschner 2004 – original unknown]22
viewer model
equivalent to pinhole photography
[Marschner 2004 – original unknown] 23
viewer model -- parameters
camera frame: position and orientation , ,
image plane: distance and size ,
O x y zd w h
24
view frustum
all visible points within a truncated pyramid
25
generating view rays
for each pixel, ray from camera center to the pixel center
26
generating view rays
ray:
point on image plane
r = O + t(Q − O)/|Q − O|Q
27
generating view rays
image plane params: , origin at bottom
Q(u, v) = (u − 0.5)wx + (v − 0.5)hy − dz(u, v) ∈ [0, 1]2
28
geometry model
simple shapes
spheres, quads, traingles
complex shapes
handled as collections of simple shapes later in the course
29
ray-shape intersection
determine visible surface by finding closest intersection
along a ray
ray with
keep explicit bounds on
e.g. used in shadows and to improve numerical precision
if not specified otherwise: ,
mitigate numerical precision issues ("shadow acne")
value is scene depedent: start with
r : E + td t ∈ ( , )tmin tmax
t
= ϵtmin = ∞tmax
ϵ
10−5
30
ray-sphere intersection
point on a ray:
point on a sphere:
by substitution:
P(t) = E + td|P(t) − C| = R
|E + td − C| = R
31
ray-sphere intersection
algebraic equation:
with: , ,
determinant:
no solution for
a + bt + c = 0t2
a = |d|2 b = 2d ⋅ (E − C) c = |E − C −|2 R2
d = − 4acb2
d < 0
32
ray-sphere intersection
two solutions:
pick smallest such that
= (−b ± )/(2a)t± d√
t t ∈ ( , )tmin tmax
33
ray-sphere intersection
shading frame at with normal with
and
and
, where ,
P = fO n = fz
P = E + td = (P − C)/RPl
θ = arccos P lz ϕ = arctan( , )P l
y P lx
f = {P, x, y, }Pl x = (sin ϕ, cos ϕ, 0)y = (cos θcos ϕ, cos θsin ϕ, sin θ)
34
ray-plane intersection
point on a ray:
point on a plane:
by substitution:
P(t) = E + td(P(t) − C) ⋅ n = 0
(E + td − C) ⋅ n = 0
35
ray-plane intersection
one solution for , no/infinite solutions otherwise
check that
d ⋅ n ≠ 0
t =(C − E) ⋅ n
d ⋅ n
t ∈ ( , )tmin tmax
36
ray-plane intersection
shading frame: f = {e + td, u, v, n}
37
ray-triangle intersection
point on ray:
point on triangle:
by substitution:
P(t) = E + tdP(α, β) = α(A − C) + β(B − C) + C
E + td = α(A − C) + β(B − C) + C
38
ray-triangle intersection
E + td = α(A − C) + β(B − C) + C →
α(A − C) + β(B − C) − td = E − C →
αa + βb − td = e →
[ ] = e−d a b⎡⎣
t
α
β
⎤⎦
39
ray-triangle intersection
use Cramer's rule
test for
t = =| |e a b
| |−d a b(e × a) ⋅ b(d × b) ⋅ a
α = =| |−d e b| |−d a b
(d × b) ⋅ e(d × b) ⋅ a
β = =| |−d a e| |−d a b
(e × a) ⋅ d(d × b) ⋅ a
t ∈ ( , ), α ≥ 0, β ≥ 0, α + β ≤ 1tmin tmax 40
ray-triangle intersection
shading frame:
create frame by orthonomalization with
,
f = {e + td, u, v, n}
= (B − A) × (C − A)z′ = (B − A)x′
41
intersection and coord systems
transform the object
simple for triangles, since they transforms to triangles
but objects may require more complex intersection tests
transform the ray
much more elegant
works on any surface
allow for much simpler intersection tests
42
intersection and coord systems
ray wrt (e.g. world)
object defined wrt (in turn defined wrt )
transform rays
transform origin/direction as point/vector
intersect object with transformed ray
use standard intersection tests
transform intersection frame back to
transform origin/axes as point/vectors
r = {E, d} fo′ f ′ f
= { , }r′ E′ d′
o′ r′
f
43
image so far
44
intersecting many shapes
intersect each primitive
pick closest intersection
essentially a line search
45
intersecting many shapes --pseudocode
minDistance = infinity
hit = false
foreach surface s {
if(s.intersect(ray,intersection)) {
if(intersection.distance < minDistance) {
hit = true;
minDistance = intersection.distance;
}
}
}
46
image so far
47
shading
for each pixel {
determine viewing direction
intersect ray with scene
-> compute illumination
store result in pixel
}
48
shading
variation in observed color across a surface
49
shading
compute reflected light
depends on:
view position
incoming light, i.e. lighting
surface geometry
surface material
50
real-world materials
Metals Dielectric
[Marschner 2004] [Marschner 2004]
51
real-world materials
Metals Dielectric
[Marschner 2004] [Marschner 2004] 52
shading models
empirical models
produce believable images
simple and efficient
only for simple materials
physically-based shading models
can reproduce accurate effects
more complex and expensive
will concentrate on empirical models first
53
shading model
shading model: diffuse + specular reflection
diffuse reflection
light is reflected in every direction equally
colored by surface color
specular reflection
light is reflected only around the mirror direction
white for plastic-like surfaces (glossy paints)
colored for metals (brass, copper, gold)
54
incident light
beam of light is more spread on oblique surfaces
incident light depends on angle
light fraction: f = |n ⋅ l|
55
surface reflectance
surface reflectance is described by the BRDF, bidirectional
surface distribution functions
BRDF is simple for simple shading models
in general, the BRDF is a function of incoming and outgoing
angles
is the direction from the point to the light
is the direction from the point to the viewer
is the local shading frame that describes surface
orientation (normal and tangent)
ρ(l, v; f)lvf
56
lambert diffuse model
simple and efficient diffuse model
light is scattered uniformly in all directions
brdf:
surface color:
(l, v; f) =ρd kd
= (l, v; f) ⋅ |n ⋅ l| = |n ⋅ l|Cd ρd kd
57
lambert diffuse model
produce matte appearance
left-to-right: increasing kd
58
image so far
59
phong specular model
empirical, used to look good enough
cosine of mirror and view direction
reflected direction:
brdf:
r vr = −l + 2(n ⋅ l)n
(l, v; f) = max(0, v ⋅ rρs ks )n
= (l, v; f) ⋅ |n ⋅ l| = max(0, v ⋅ r ⋅ |n ⋅ l|Cs ρs ks )n
60
phong specular model
produces highlight, shiny appearance
left-to-right: increasing , top-to-bottom: increasing n ks
61
blinn specular model
slightly better than Phong
cosine of bisector and normal
bisector:
brdf:
h nh = (l + v)/|l + v|
(l, v; f) = max(0, n ⋅ hρs ks )n
= (l, v; f) ⋅ |n ⋅ l| = max(0, n ⋅ h ⋅ |n ⋅ l|Cs ρs ks )n
62
image so far
63
lighting
patterns of illumination in the environment
64
lighting
determines how much light reaches a point
depends on:
light geometry
light emission
scene geometry
65
light source models
describe how light is emitted from light sources
empirical light source models
point, directional, spot
physically-based light source models
area light, sky model
66
point lights
light is emitted equally from a point in all directions
simulate local lighting, different at each surface point
light direction:
light color:
SP
l = (S − P)/|S − P|L = /|S − Pkl |2
67
directional lights
light is emitted from infinity in one direction
simulate distant lighting, e.g. sun, same at all surface points
light direction:
light color:
d
Pl = d
L = kl
68
spot lights
same as points lights, but only emits in a cone around
simulate theatrical lights
cone falloff model arbitrary
light direction:
light color:
d
l = (S − P)/|S − P|L = ⋅ attenutation/|S − Pkl |2
69
shading model with multiple lights
add contribution of all lights for diffuse and specular
for Lambert and Phong
for Lambert and Blinn
i
C = ⋅ ( ( , v; f) + ( , v; f)) ⋅ |n ⋅ |∑iLi ρd li ρs li li
C = ⋅ ( + max(0, v ⋅ ) ⋅ |n ⋅ |∑iLi kd ks ri)
n li
C = ⋅ ( + max(0, n ⋅ ) ⋅ |n ⋅ |∑iLi kd ks hi)
n li
70
image so far
71
illumination models
describe how light spreads in the environment
direct illumination
incoming light comes directly from light sources
shadows
indirect illumination
incoming light comes from other objects
specular reflections (mirrors)
diffuse inter-reflections
72
illumination models
[PCG]
73
ray tracing lighting model
point/directional/spot light sources
sharp shadows
sharp reflection/refractions
hacked diffuse inter-reflection: ambient term
74
ray traced shadows
light contributes only if visible at surface point
no shadow shadow
75
ray traced shadows
send a shadow ray to check if light is visible
visible if no hits or if more than light distancet
76
ray traced shadows
shadow ray with
spot/point lights at :
directional lights:
scale lighting by visibility term which is 0 or 1
implementation detail: numerical precision
shadow acne: ray hits the visible point
solution: only intersect if , i.e.
r = P + tl t ∈ ( , )tmin tmax
S = length(S − P)tmax
= ∞tmax
(P)Vi
C = ⋅ (P)( + )|n ⋅ |∑iLi Vi ρd ρs li
t > ϵ = ϵtmin
77
image so far
78
ambient term hack
light bounces even in diffuse environment
ceiling are not black
shadows are not perfectly black
very expensive to compute
approximate (poorly) with a constant term
C = + ⋅ (P)( + )|n ⋅ |kd La ∑iLi Vi ρd ρs li
79
ray traced reflections and refractions
perfectly shiny surfaces reflects objects
recursively trace a ray if material is reflective or refractive
80
ray traced reflections and refractions
reflections: along mirror direction ,
scaled by
refractions: along refraction direction scaled by
implementation detail: recursion
avoid hitting visible point:
make sure you do not recurse indefinitely
r = −l + 2(l ⋅ n)nkr
kt
C = + ⋅ (P)( + )|n ⋅ | +kd La ∑iLi Vi ρd ρs li
+ raytrace(P, r) + raytrace(P, t)kr kt
> ϵtmin
81
image so far
82
antialiasing
83
antialiasing: removing jaggies
1 sample/pixel
84
antialiasing: removing jaggies
1 sample/pixel
85
antialiasing: removing jaggies
1 sample/pixel
86
antialiasing: removing jaggies
1 sample/pixel
87
antialiasing: removing jaggies
1 sample/pixel 9 sample/pixel
88
antialiasing: removing jaggies
1 sample/pixel 9 sample/pixel
89
antialiasing: removing jaggies
1 sample/pixel 9 sample/pixel
90
antialiasing: removing jaggies
1 sample/pixel 9 sample/pixel
91
antialiasing: removing jaggies
poor-man antialiasing:
for each pixel
take multiple samples
compute average
92
ray tracing pseudocode
for(i = 0; i < imageWidth; i ++) {
for(j = 0; j < imageHeight; j ++) {
u = (i + 0.5)/imageWidth;
v = (j + 0.5)/imageHeight;
ray = camera.generateRay(u,v);
c = computeColor(ray);
image[i][j] = c;
}
}
93
anti-aliased ray tracing pseudocode
for(i = 0; i < imageWidth; i ++) {
for(j = 0; j < imageHeight; j ++) {
color c = 0;
for(ii = 0; ii < numberOfSamples; ii ++) {
for(jj = 0; jj < numberofSamples; jj ++) {
u = (i+(ii+0.5)/numberOfSamples)/imageWidth;
v = (j+(jj+0.5)/numberofSamples)/imageHeight;
ray = camera.generateRay(u,v);
c += computeColor(ray);
}
}
image[i][j] = c / (numberOfSamples^2);
}
}94
image so far
95