SLIDE 1 Introduction to Computer Graphics
Toshiya Hachisuka
SLIDE 2 Lecturer
- Toshiya Hachisuka(蜂須賀 恵也)
- Leading the computer graphics group
- Office: I-REF 503
- http://www.ci.i.u-tokyo.ac.jp/~hachisuka
first last
SLIDE 3
- Introduction to ray tracing
- Basic ray-object intersection
Today
SLIDE 6
How can we generate realistic images?
SLIDE 7 Rendering
Light sources Shapes Materials Camera data Computation
Input data Image
SLIDE 8 Interdisciplinary Nature
- Computer Science
- Algorithms
- Computational geometry
- Software engineering
- Physics
- Radiometry
- Optics
- Mathematics
- Algebra
- Calculus
- Statistics
- Perception
- Art
SLIDE 9 Ray Tracing - Concept
https://en.wikipedia.org/wiki/Ray_tracing_(graphics)#/media/File:Ray_trace_diagram.svg
SLIDE 10
Ray Tracing [Appel 1968]
Generate images with shadows using ray tracing
SLIDE 11
Ray Tracing [Whitted 1979]
Recursive ray tracing for reflections/refractions
SLIDE 12 Whitted Ray Tracing Today
http://alexrodgers.co.uk
SLIDE 13 Whitted Ray Tracing Today
http://alexrodgers.co.uk
You are going to implement something like this!
SLIDE 14 Ray Tracing - Pseudocode
for all pixels { ray = generate_camera_ray( pixel ) for all objects { hit = intersect( ray, object ) if “hit” is closer than “first_hit” {first_hit = hit} } pixel = shade( first_hit ) }
SLIDE 15 Ray Tracing - Data Structures
class object { bool intersect( ray ) } class ray { vector origin vector direction }
SLIDE 16
Pinhole Camera
Film = Image Pinhole (the image is flipped)
SLIDE 17
Pinhole Camera
Film Pinhole
SLIDE 18
Camera Coordinate System
up from to
SLIDE 19
Camera Coordinate System
~ u · ~ v = ~ v · ~ w = ~ w · ~ u = 0 ||~ u|| = ||~ v|| = ||~ w|| = 1
Orthonormal basis
~ e ~ v ~ u ~ w
SLIDE 20 Camera Coordinate System
~ v = ~ w × ~ u
Axes Origin
~ u = ~ Cup × ~ w || ~ Cup × ~ w|| ~ e = ~ Cfrom ~ Cup ~ Cfrom ~ Cto
Cto || Cfrom − Cto||
SLIDE 21 Up vector?
- Imagine a stick on top your head
- The stick = up vector
- Up vector is not always equal to ~
v ~ v = up ~ v
up
SLIDE 22
Generating a Camera Ray
~ v ~ u ~ w y = film hpixel j + 0.5 res y z = distance to film (x, y, z)
Pixel location in the camera coordinates
x = film wpixel i + 0.5 res x
SLIDE 23 Generating a Camera Ray
- Film size is not equal to image resolution!
Film with 82 resolution Same film with 162 resolution
SLIDE 24 Generating a Camera Ray
- Pixel location in the world coordinates:
- Camera ray in the world coordinates:
- rigin = ~
e direction =
||origin − pixel|| pixel = x u + y v + z w + e
SLIDE 25
Generating a Camera Ray
~ e ~ v ~ u ~ w
SLIDE 26 More Realistic Cameras
- “A realistic camera model for computer graphics”
- Ray tracing with actual lens geometry
- Distortion
Full Simulation Thin Lens Approximation
[Kolb et al.]
SLIDE 27 More Realistic Cameras
- “Efficient Monte Carlo Rendering with Realistic Lenses”
- Polynomial approximation of a lens system
[Hanika et al.]
SLIDE 28 Ray Tracing - Pseudocode
for all pixels { ray = generate_camera_ray( pixel ) for all objects { hit = intersect( ray, object ) if “hit” is closer than “first_hit” {first_hit = hit} } pixel = shade( first_hit ) }
SLIDE 29 Goal
~
d ~ p ~ p = ~
d
What we want to know
~ n
SLIDE 30 Ray-Sphere Intersection
- Sphere with center and radius
~ c = (cx, cy, cz) r ||(~ p − ~ c)||2 = r2
SLIDE 31 Ray-Sphere Intersection
- Sphere with center and radius
~ c = (cx, cy, cz) r ~ p = ~
d ||(~ p − ~ c)||2 = r2
Substitute
SLIDE 32 Ray-Sphere Intersection
- Sphere with center and radius
~ c = (cx, cy, cz) r ~ p = ~
d ||(~ p − ~ c)||2 = r2
Substitute
(~
d − ~ c) · (~
d − ~ c) = r2 ||~ v||2 = ~ v · ~ v
SLIDE 33 Ray-Sphere Intersection
- Sphere with center and radius
~ c = (cx, cy, cz) r ~ p = ~
d ||(~ p − ~ c)||2 = r2
Substitute
(~
d − ~ c) · (~
d − ~ c) = r2 ~ d · ~ dt2 + 2~ d · (~
c)t + (~
c) · (~
c) − r2 = 0
Quadratic equation of t Solve for t
||~ v||2 = ~ v · ~ v
SLIDE 34 Ray-Sphere Intersection
- can have (considering only real numbers)
- 0 solution : no hit point
- 1 solution : hit at the edge
- 2 solutions
- two negatives : hit points are behind
- two positives : hit points are front
- positive and negative : origin is in the sphere
t
SLIDE 35 Ray-Sphere Intersection
- Two hit points - take the closest
~
d ~ p0 ~ p1
SLIDE 36 Normal Vector
~
d ~ p ~ n = ~ p − ~ c r ~ r ·
p ~ c) · (~ p ~ c) r2 = 2(~ p ~ c)
SLIDE 37 Ray-Implicit Surface Intersection
- Generalized to any implicit surface
f(~ p(t)) = 0
Intersection point:
||(~ p(t) − ~ c)||2 − r2 = 0
Solve e.g., Normal vector:
~ n = ~ r · f(~ p(t)) ||~ r · f(~ p(t))||
SLIDE 38 Ray-Implicit Surface Intersection
- can be
- Linear: Plane
- Quadratic: Sphere
- Cubic: Bézier (cubic)
- Quartic: Phong tessellation
- ...and anything
f(~ p(t)) = 0
SLIDE 39 Ray-Implicit Surface Intersection
www.iquilezles.org
SLIDE 40 Ray-Implicit Surface Intersection
“Direct Ray Tracing of Full-Featured Subdivision Surfaces with Bezier Clipping”
SLIDE 41 Triangle Mesh
- Approximate shapes with triangles
http://www.graphics.rwth-aachen.de/publication/149/
SLIDE 42 Barycentric Coordinates
- Ratios of areas of the sub-triangles
~ c ~ b ~ a α = β = γ = Ac Aa Ab+ + Ac Aa Ab+ + Ac Aa Ab+ + Ac Ab Aa
SLIDE 43 Barycentric Coordinates
- Parametric description of a point in a triangle
~ p = ↵~ a + ~ b + ~ c 0 < α < 1 0 < β < 1 0 < γ < 1 α + β + γ = 1 ~ a ~ b ~ c ~ p
SLIDE 44 Barycentric Coordinates
- Interpolate values at the vertices
~ na ~ nb ~ nc ~ np ~ np = ↵~ na + ~ nb + ~ nc
Normalize it before use
SLIDE 45
Interpolation
SLIDE 46
Interpolation
SLIDE 47 Ray-Triangle Intersection
- Calculate as fast as possible
- Modification of ray-plane intersection
- Direct methods
- Cramer’s rule
- Signed volumes
(t, α, β, γ)
SLIDE 48
Ray-Triangle Intersection Cramer’s Rule
~ p = ↵~ a + ~ b + ~ c
SLIDE 49 Ray-Triangle Intersection Cramer’s Rule
~ p = ↵~ a + ~ b + ~ c ~
d
SLIDE 50 Ray-Triangle Intersection Cramer’s Rule
~ p = ↵~ a + ~ b + ~ c ~
d (1 − − )~ a + ~ b + ~ c
SLIDE 51 Ray-Triangle Intersection Cramer’s Rule
~ p = ↵~ a + ~ b + ~ c ~
d (1 − − )~ a + ~ b + ~ c
- x + tdx = (1 − β − γ)ax + βbx + γcx
- y + tdy = (1 − β − γ)ay + βby + γcy
- z + tdz = (1 − β − γ)az + βbz + γcz
3 equations for 3 unknowns
SLIDE 52 Ray-Triangle Intersection Cramer’s Rule
~ p = ↵~ a + ~ b + ~ c ~
d (1 − − )~ a + ~ b + ~ c ax − bx ax − cx dx ay − by ay − cy dy az − bz az − cz dz β γ t = ax − ox ay − oy aZ − oz
Solve the equation with Cramer’s Rule
det ⇣ ~ a,~ b,~ c ⌘ = ⇣ ~ a ×~ b ⌘ · ~ c
SLIDE 53 Ray-Triangle Intersection Cramer’s Rule
- Accept the solution only if
tclosest > t > 0 1 > β > 0 1 > γ > 0 1 > 1 − β − γ > 0 tclosest : the smallest positive t values so far
SLIDE 54 Ray-Triangle Intersection
- There are many different approaches!
- Numerical precision
- Performance
- Storage cost
- SIMD friendliness
- Genetic programming for performance
http://www.cs.utah.edu/~aek/research/triangle.pdf
SLIDE 55 GLSL Sandbox
- Interactive coding environment for WebGL
- You write a program for each pixel in GLSL
- Automatically loop over all the pixels
- Uses programmable shader units on GPUs
http://glslsandbox.com
SLIDE 56 GLSL implementation
for all pixels { ray = generate_camera_ray( pixel ) for all objects { hit = intersect( ray, object ) if “hit” is closer than “first_hit” {first_hit = hit} } pixel = shade( first_hit ) }
SLIDE 57 GLSL implementation
- Truth is…
- You can find ray tracing on GLSL sandbox
- “Copy & paste” is a good start, but make
sure you understand what’s going on and describe what you did in your submission
SLIDE 58 Next Time
for all pixels { ray = generate_camera_ray( pixel ) for all objects { hit = intersect( ray, object ) if “hit” is closer than “first_hit” {first_hit = hit} } pixel = shade( first_hit ) }