ray tracing
computer graphics • ray tracing
© 2006 fabio pellacini • 1
image formation
computer graphics • ray tracing
© 2006 fabio pellacini • 2
image formation
computer graphics • ray tracing
© 2006 fabio pellacini • 3
rendering computational simulation of image formation
computer graphics • ray tracing
© 2006 fabio pellacini • 4
rendering • given viewer
computer graphics • ray tracing
© 2006 fabio pellacini • 5
rendering • given viewer, geometry
computer graphics • ray tracing
© 2006 fabio pellacini • 6
rendering • given viewer, geometry, materials
computer graphics • ray tracing
© 2006 fabio pellacini • 7
rendering • given viewer, geometry, materials and lights
computer graphics • ray tracing
© 2006 fabio pellacini • 8
rendering • given viewer, geometry, materials and lights • determine visibility
computer graphics • ray tracing
© 2006 fabio pellacini • 9
rendering • given viewer, geometry, materials and lights • determine visibility and simulate lighting
computer graphics • ray tracing
© 2006 fabio pellacini • 10
ray tracing a particular rendering algorithm
computer graphics • ray tracing
© 2006 fabio pellacini • 11
ray tracing algorithm for each pixel { determine viewing direction intersect ray with scene compute illumination store result in pixel }
computer graphics • ray tracing
© 2006 fabio pellacini • 12
vector math review – points and vectors • point – location in 3D space – P = (x,y,z)
• vector – direction and magnitude – V = (u,v,w)
computer graphics • ray tracing
© 2006 fabio pellacini • 13
vector math review – vector operations • dot product – V1•V2=|V1||V2|cos(θ)
• cross product – |V1×V2|=|V1||V2|sin(θ) – V1×V2 is perpendicular to V1 and V2
computer graphics • ray tracing
© 2006 fabio pellacini • 14
vector math review – segments • segment – set of points (line) between two points – P(t) = P0 + t (P1 – P0) with t ∈ [0,1]
computer graphics • ray tracing
© 2006 fabio pellacini • 15
vector math review – rays • ray – infinite line from point in a given direction – P(t) = P0 + t V with t ∈ [0,∞]
computer graphics • ray tracing
© 2006 fabio pellacini • 16
vector math review – planes • plane
– P ∈ plane ↔ (P − P0 ) ⋅ N = 0 ↔ P ⋅ N + d = 0 2 – P = P0 + uxˆ + vyˆ with (u , v) ∈ [−∞, ∞] – N = xˆ × yˆ
computer graphics • ray tracing
© 2006 fabio pellacini • 17
vector math review - triangle • triangle – points in three subspaces defined by triangle edges
• baricentric coordinates P =α A + β B+γ C P ∈ triangle ↔ α + β + γ = 1
( ) ( )
P = C + α CA + β CB with α ≥ 0, β ≥ 0, α + β ≤ 1 – geometric interpretation: relative area γ = AC / Atriangle computer graphics • ray tracing
© 2006 fabio pellacini • 18
ray tracing algorithm for each pixel { determine viewing direction intersect ray with scene compute illumination store result in pixel }
computer graphics • ray tracing
© 2006 fabio pellacini • 19
[Marschner 2004 – original unkwon]
viewer model – window analogy
computer graphics • ray tracing
© 2006 fabio pellacini • 20
viewer model - photography
[Marschner 2004 – original unkwon]
• equiv. to pinhole photography
computer graphics • ray tracing
© 2006 fabio pellacini • 21
viewer model - representation
• position – view point
• orientation – toward, up
• focal length – field of view computer graphics • ray tracing
© 2006 fabio pellacini • 22
viewer model – view frustum
computer graphics • ray tracing
© 2006 fabio pellacini • 23
generate viewing rays
computer graphics • ray tracing
© 2006 fabio pellacini • 24
generate viewing rays
computer graphics • ray tracing
© 2006 fabio pellacini • 25
generate viewing rays
computer graphics • ray tracing
© 2006 fabio pellacini • 26
ray tracing algorithm for each pixel { determine viewing direction intersect ray with scene compute illumination store result in pixel }
computer graphics • ray tracing
© 2006 fabio pellacini • 27
geometry model • simple shapes – spheres – triangles – etc.
• complex shapes – later in the course
computer graphics • ray tracing
© 2006 fabio pellacini • 28
ray-sphere intersection – algebraic ⎧P (t ) = E + tI 2 ⎨ 2 P O ( t ) − = R ⎩
point on ray
E + tI − O = R 2
by substitution
2
computer graphics • ray tracing
point on sphere
© 2006 fabio pellacini • 29
ray-sphere intersection – algebraic at 2 + bt + c = 0 algebraic equation a= I
2
b = 2I ⋅ (E − O ) c = E−O − R 2
2
computer graphics • ray tracing
© 2006 fabio pellacini • 30
ray-sphere intersection – algebraic d = b − 4ac determinant if d < 0 no solutions otherwise 2
P (t ) ⇔ t − = (− b − d ) / 2a
computer graphics • ray tracing
© 2006 fabio pellacini • 31
ray-sphere intersection – geometric
W = O−E t p = W ⋅ Iˆ if t p < 0 no solution
computer graphics • ray tracing
© 2006 fabio pellacini • 32
ray-sphere intersection – geometric
d = W⋅W −t 2
2 p
if d > R no solution 2
2
computer graphics • ray tracing
© 2006 fabio pellacini • 33
ray-sphere intersection – geometric
tr = R − d 2
2
P (t ) ⇔ t − = t p − t r
computer graphics • ray tracing
© 2006 fabio pellacini • 34
ray-sphere intersection – normal • surface normal need for lighting computation
N = (P − O ) / P − O
computer graphics • ray tracing
© 2006 fabio pellacini • 35
ray-triangle intersection 1 • intersect with a plane • check if the intersection point is in triangle
computer graphics • ray tracing
© 2006 fabio pellacini • 36
ray-plane intersection ⎧P (t ) = E + tI point on ray ⎨ ⎩P ⋅ N + d = 0 point on plane (E + tI ) ⋅ N + d = 0 by substitution E⋅N + d P (t ) ⇔ t = − I⋅N
computer graphics • ray tracing
© 2006 fabio pellacini • 37
ray-triangle intersection 1 • triangle is intersection of 3 half-spaces • check if P is on right side by comparing “normals”
computer graphics • ray tracing
© 2006 fabio pellacini • 38
ray-triangle intersection 1 intersects if
(B − A ) × (P − A ) ⋅ N > 0 (C − B ) × (P − B ) ⋅ N > 0 ( A − C ) × (P − C ) ⋅ N > 0
computer graphics • ray tracing
© 2006 fabio pellacini • 39
ray-triangle intersection 2 • use baricentric coordinates (useful later) • algebraic formulation point on ray ⎧P (t ) = E + tI ⎨ ⎩P (α , β ) = αA + β B + (1 − α − β )C point in the triangle E + tI = αA + β B + (1 − α − β )C
computer graphics • ray tracing
by substitution
© 2006 fabio pellacini • 40
ray-triangle intersection 2 E + tI = α (A − C ) + β (B − C ) + C → α (A − C) + β (B − C) − tI = E − C → α a + β b − tI = e → ⎡t ⎤ [− I a b]⎢⎢α ⎥⎥ = e ⎢⎣ β ⎥⎦
computer graphics • ray tracing
© 2006 fabio pellacini • 41
ray-triangle intersection 2 use Cramer' s rule | e a b | (e × a ) ⋅ b = t= | − I a b | (I × b ) ⋅ a | − I e b | (I × b ) ⋅ e = α= | − I a b | (I × b ) ⋅ a | − I a e | (e × a ) ⋅ I = β= | − I a b | (I × b ) ⋅ a test for α ≥ 0, β ≥ 0,α + β ≤ 1 computer graphics • ray tracing
© 2006 fabio pellacini • 42
images so far
computer graphics • ray tracing
© 2006 fabio pellacini • 43
intersecting many shapes • intersect each primitive • pick closest intersection
computer graphics • ray tracing
© 2006 fabio pellacini • 44
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; } } }
computer graphics • ray tracing
© 2006 fabio pellacini • 45
images so far
computer graphics • ray tracing
© 2006 fabio pellacini • 46
ray tracing algorithm for each pixel { determine viewing direction intersect ray with scene compute illumination store result in pixel }
computer graphics • ray tracing
© 2006 fabio pellacini • 47
shading variation in observed color across a surface
computer graphics • ray tracing
© 2006 fabio pellacini • 48
lighting patterns of illumination in the environment
computer graphics • ray tracing
© 2006 fabio pellacini • 49
shading • compute reflected light • depends on – – – –
viewer position incoming light, i.e. lighting surface geometry surface material
• more on this later
computer graphics • ray tracing
© 2006 fabio pellacini • 50
materials dielectric
[Marschner 2004]
metals
computer graphics • ray tracing
© 2006 fabio pellacini • 51
materials dielectric
[Marschner 2004]
metals
computer graphics • ray tracing
© 2006 fabio pellacini • 52
shading models • empirical shading models – produce believable images – simple and efficient – only for simple materials
• physically-based shading models – can reproduce accurate effects – more complex
• will concentrate empirical plastic-like model – more on this later in the course computer graphics • ray tracing
© 2006 fabio pellacini • 53
Phong 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)
computer graphics • ray tracing
© 2006 fabio pellacini • 54
diffuse reflection • light reflects equally in all directions – i.e. surface looks the same from all view points – view-independent
• but incident light depends on angle – beam of light is more spread on an oblique surface
computer graphics • ray tracing
© 2006 fabio pellacini • 55
lambertian shading model diffuse coefficient
Cd = k d Cl max(0, N ⋅ L) reflected diffuse illumination illumination from light
computer graphics • ray tracing
© 2006 fabio pellacini • 56
lambertian shading model • produces matte appearance
kd
computer graphics • ray tracing
© 2006 fabio pellacini • 57
images so far
computer graphics • ray tracing
© 2006 fabio pellacini • 58
specular shading • light reflects mostly around mirror direction – i.e. surface looks different from view points – view-dependent
• Phong specular model – empirical, but looks good enough – use cosine of mirror and view direction
• Blinn-Phong specular model – slightly better than Phong – use cosine of bisector and normal direction computer graphics • ray tracing
© 2006 fabio pellacini • 59
Phong specular model mirror direction
R = −L + 2(N ⋅ L )N Cs = k s Cl max(0, V ⋅ R )
n
reflected specular illumination specular coefficient
computer graphics • ray tracing
specular exponent
© 2006 fabio pellacini • 60
Phong specular model • produces highlights, shiny appearance n
ks
computer graphics • ray tracing
© 2006 fabio pellacini • 61
Blinn-Phong variation bisector direction
H = (L + V ) / L + V Cs = k s Cl max(0, N ⋅ H )
n
reflected specular illumination specular coefficient
computer graphics • ray tracing
specular exponent
© 2006 fabio pellacini • 62
shading model • putting the pieces together
C = Cd + C s =
= Cl [k d max(0, N ⋅ L) + k s max(0, V ⋅ R ) n ]
computer graphics • ray tracing
© 2006 fabio pellacini • 63
images so far
computer graphics • ray tracing
© 2006 fabio pellacini • 64
lighting • determines how much light reaches a point • depends on – light geometry – light emission – scene geometry
computer graphics • ray tracing
© 2006 fabio pellacini • 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 lights, sky model – will cover later in the course
computer graphics • ray tracing
© 2006 fabio pellacini • 66
point lights • light emitted equally from point in all directions – simulates local lights – sometimes r^2 falloff for a bit more realism
L =|| S − P || Cl = C / r 2
computer graphics • ray tracing
© 2006 fabio pellacini • 67
directional lights • light emitted from infinity in one direction – simulates distant lights, e.g. sun
L=D Cl = C
computer graphics • ray tracing
© 2006 fabio pellacini • 68
spot lights • same as point light, but only emit in a cone – simulate theatrical lights – cone falloff model arbitrary
L =|| S − P || Cl = C ⋅ att / r 2
computer graphics • ray tracing
© 2006 fabio pellacini • 69
multiple lights • add contribution for each light
C = ∑ (Cd )i + (Cs ) i = i
= ∑ (Cl ) i [k d max(0, N ⋅ (L)i ) + k s max(0, V ⋅ (R ) i ) n ] i
computer graphics • ray tracing
© 2006 fabio pellacini • 70
image so far
computer graphics • ray tracing
© 2006 fabio pellacini • 71
illumination models • describe how light spreads in the environments • direct illumination – incoming lights comes directly from sources • shadows
• indirect illumination – incoming lights comes from other objects • specular reflections (mirrors), diffuse inter-reflections
computer graphics • ray tracing
© 2006 fabio pellacini • 72
[PCG]
illumination models
computer graphics • ray tracing
© 2006 fabio pellacini • 73
ray tracing lighting model • captures – – – –
point/directional/spot light source models sharp shadows sharp reflections/refractions hacked diffuse inter-reflections: ambient term
• more accurate lighting later in the course
computer graphics • ray tracing
© 2006 fabio pellacini • 74
ray traced shadows • light contributes only if visible at surface point no shadow
computer graphics • ray tracing
shadow
© 2006 fabio pellacini • 75
ray traced shadows • cast a “shadow-ray” to check if light is visible • visible if no-hits or if distance > light distance
computer graphics • ray tracing
© 2006 fabio pellacini • 76
ray traced shadows • scale light by a visibility term
C = ∑ (Cd ) i + (C s ) i = i
= ∑ (Cl ) i Vi (P )[k d max(0, N ⋅ (L) i ) + k s max(0, V ⋅ (R ) i ) n ] i
light source visibility {0,1}
computer graphics • ray tracing
© 2006 fabio pellacini • 77
images so far
computer graphics • ray tracing
© 2006 fabio pellacini • 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 = Ca + ∑ [(Cd )i + (Cs )i ] = i
= k a Camb + ∑ [(Cd ) i + (Cs ) i ] i
ambient coefficient computer graphics • ray tracing
ambient illumination © 2006 fabio pellacini • 79
ray traced reflections • perfectly shiny surfaces reflects objects – recursively trace a ray if material is reflective • along mirror direction, scaled by reflection coeff. • mirror direction calculated for Phong
computer graphics • ray tracing
© 2006 fabio pellacini • 80
ray traced reflections • recursively ray trace, scale by reflection coeff. • refraction is the same along the transmission dir.
C = Ca + ∑ [(Cd ) i + (Cs ) i ] + Cr + Ct = i
= Ca + ∑ [(Cd ) i + (Cs ) i ] + i
+ k r raytrace(P, R ) + kt raytrace(P, T ) reflection coefficient
computer graphics • ray tracing
refraction coefficient
© 2006 fabio pellacini • 81
images so far
computer graphics • ray tracing
© 2006 fabio pellacini • 82
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
© 2006 fabio pellacini • 83
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
© 2006 fabio pellacini • 84
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
© 2006 fabio pellacini • 85
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
© 2006 fabio pellacini • 86
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
9 samples/pixel
© 2006 fabio pellacini • 87
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
9 samples/pixel
© 2006 fabio pellacini • 88
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
9 samples/pixel
© 2006 fabio pellacini • 89
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
9 samples/pixel
© 2006 fabio pellacini • 90
antialiasing - removing jaggies 1 sample/pixel
computer graphics • ray tracing
9 samples/pixel
© 2006 fabio pellacini • 91
antialiasing - removing jaggies • for each pixel – take multiple samples – compute average
• poor-man antialiasing – more on this later in the course
computer graphics • ray tracing
© 2006 fabio pellacini • 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; } }
computer graphics • ray tracing
© 2006 fabio pellacini • 93
antialiased 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 ++) { x = (i+(ii+0.5)/numberOfSamples)/imageWidth; y = (j+(jj+0.5)/numberofSamples)/imageHeight; ray = camera.generateRay(x,y); c += computeColor(ray); } } image[i][j] = c / (numberOfSamples^2); } }
computer graphics • ray tracing
© 2006 fabio pellacini • 94
images so far
computer graphics • ray tracing
© 2006 fabio pellacini • 95
ray tracing details • numerical precision issues come up – shadow acne: ray hits the visible point – solution: only intersect if distance > epsilon
• make sure you do not recurse infinitely when computing reflections or refractions – solution: simply stop after a given number
computer graphics • ray tracing
© 2006 fabio pellacini • 96
ray tracing wrap-up • simple and general algorithm – – – –
ray cast to evaluate visibility simplified shading model ray cast to evaluate shadows recursive execution for reflections/refractions
• inefficient – linear with number of primitives – sub-linear ray cast later in the course
• not photorealistic – too clean – missing soft shadows, realistic materials, realistic camera model, diffuse inter-reflection
• base of most general algorithms computer graphics • ray tracing
© 2006 fabio pellacini • 97