Introduction to Computer Graphics Toshiya Hachisuka
Lecturer • Toshiya Hachisuka (蜂須賀 恵也) first last • Leading the computer graphics group • Office: I-REF 503 • http://www.ci.i.u-tokyo.ac.jp/~hachisuka
Today • Introduction to ray tracing • Basic ray-object intersection
LuxRender
LuxRender
How can we generate realistic images?
Rendering Input data Image Light sources Shapes Computation Materials Camera data
Interdisciplinary Nature • Computer Science • Algorithms • Computational geometry • Software engineering • Physics • Radiometry • Optics • Mathematics • Algebra • Calculus • Statistics • Perception • Art
Ray Tracing - Concept https://en.wikipedia.org/wiki/Ray_tracing_(graphics)#/media/File:Ray_trace_diagram.svg
Ray Tracing [Appel 1968] Generate images with shadows using ray tracing
Ray Tracing [Whitted 1979] Recursive ray tracing for reflections/refractions
Whitted Ray Tracing Today • Runs realtime on a GPU! http://alexrodgers.co.uk
Whitted Ray Tracing Today • Runs realtime on a GPU! You are going to implement something like this! http://alexrodgers.co.uk
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 ) }
Ray Tracing - Data Structures class object { bool intersect( ray ) } class ray { vector origin vector direction }
Pinhole Camera Pinhole Film = Image (the image is flipped)
Pinhole Camera Pinhole Film
Camera Coordinate System up from to
Camera Coordinate System ~ v ~ u ~ w ~ e Orthonormal basis ~ u · ~ v = ~ v · ~ w = ~ w · ~ u = 0 || ~ u || = || ~ v || = || ~ w || = 1
Camera Coordinate System • Given , , and C up ~ ~ ~ C from C to C from − � � C to � w = || � C from − � C to || ~ Axes C up × ~ w u = ~ || ~ w || C up × ~ ~ v = ~ w × ~ u e = ~ Origin ~ C from
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
Generating a Camera Ray ~ v x = film wpixel i + 0.5 res x ( x, y, z ) y = film hpixel j + 0.5 ~ u ~ w res y z = distance to film Pixel location in the camera coordinates
Generating a Camera Ray • Film size is not equal to image resolution! Film with 8 2 resolution Same film with 16 2 resolution
Generating a Camera Ray • Pixel location in the world coordinates: pixel = x � u + y � v + z � w + � e • Camera ray in the world coordinates: origin = ~ e origin − pixel direction = || origin − pixel ||
Generating a Camera Ray ~ v ~ u ~ w ~ e
More Realistic Cameras [Kolb et al.] • “A realistic camera model for computer graphics” • Ray tracing with actual lens geometry • Distortion Full Simulation Thin Lens Approximation
More Realistic Cameras [Hanika et al.] • “Efficient Monte Carlo Rendering with Realistic Lenses” • Polynomial approximation of a lens system
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 ) }
Goal ~ d ~ o o + t ~ ~ p = ~ ~ d p ~ n What we want to know
Ray-Sphere Intersection • Sphere with center and radius c = ( c x , c y , c z ) ~ r c ) || 2 = r 2 || ( ~ p − ~
Ray-Sphere Intersection • Sphere with center and radius c = ( c x , c y , c z ) ~ r c ) || 2 = r 2 || ( ~ p − ~ o + t ~ Substitute ~ p = ~ d
Ray-Sphere Intersection • Sphere with center and radius c = ( c x , c y , c z ) ~ r c ) || 2 = r 2 v || 2 = ~ || ( ~ || ~ v · ~ p − ~ v o + t ~ Substitute p = ~ ~ d o + t ~ o + t ~ c ) = r 2 ( ~ c ) · ( ~ d − ~ d − ~
Ray-Sphere Intersection • Sphere with center and radius c = ( c x , c y , c z ) ~ r c ) || 2 = r 2 v || 2 = ~ || ( ~ || ~ v · ~ p − ~ v o + t ~ Substitute ~ p = ~ d o + t ~ o + t ~ c ) = r 2 ( ~ c ) · ( ~ d − ~ d − ~ dt 2 + 2 ~ c ) − r 2 = 0 d · ~ ~ d · ( ~ c ) t + ( ~ c ) · ( ~ o − ~ o − ~ o − ~ Quadratic equation of t Solve for t
Ray-Sphere Intersection • can have (considering only real numbers) t • 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
Ray-Sphere Intersection • Two hit points - take the closest ~ d ~ o ~ p 1 ~ p 0
Normal Vector ~ d ~ o n = ~ p − ~ c ~ ~ p r ~ c ) � r 2 � � ( ~ c ) · ( ~ = 2( ~ c ) p � ~ p � ~ p � ~ r ·
Ray-Implicit Surface Intersection • Generalized to any implicit surface Intersection point: Solve f ( ~ p ( t )) = 0 c ) || 2 − r 2 = 0 e.g., || ( ~ p ( t ) − ~ Normal vector: ~ r · f ( ~ p ( t )) n = ~ || ~ r · f ( ~ p ( t )) ||
Ray-Implicit Surface Intersection • can be f ( ~ p ( t )) = 0 • Linear: Plane • Quadratic: Sphere • Cubic: Bézier (cubic) • Quartic: Phong tessellation • ...and anything
Ray-Implicit Surface Intersection • Procedural geometry www.iquilezles.org
Ray-Implicit Surface Intersection • Subdivision surfaces “Direct Ray Tracing of Full-Featured Subdivision Surfaces with Bezier Clipping”
Triangle Mesh • Approximate shapes with triangles http://www.graphics.rwth-aachen.de/publication/149/
Barycentric Coordinates • Ratios of areas of the sub-triangles ~ A a a α = A a A b + A c + A b β = A a A b + A c + A c ~ γ = c ~ A a A b + A c + b
Barycentric Coordinates • Parametric description of a point in a triangle a + �~ ~ p = ↵~ b + �~ c ~ a 0 < α < 1 0 < β < 1 ~ p 0 < γ < 1 α + β + γ = 1 ~ ~ c b
Barycentric Coordinates • Interpolate values at the vertices ~ n a ~ n p = ↵~ n a + �~ n b + �~ n c Normalize it before use ~ n p ~ n c ~ n b
Interpolation
Interpolation
Ray-Triangle Intersection • Calculate as fast as possible ( t, α , β , γ ) • Modification of ray-plane intersection • Direct methods • Cramer’s rule • Signed volumes
Ray-Triangle Intersection Cramer’s Rule a + �~ ~ p = ↵~ b + �~ c
Ray-Triangle Intersection Cramer’s Rule o + t ~ a + �~ ~ ~ p = ↵~ b + �~ d c
Ray-Triangle Intersection Cramer’s Rule o + t ~ a + �~ a + �~ (1 − � − � ) ~ b + �~ ~ ~ p = ↵~ b + �~ d c c
Ray-Triangle Intersection Cramer’s Rule o + t ~ a + �~ a + �~ (1 − � − � ) ~ b + �~ ~ ~ p = ↵~ b + �~ d c c o x + td x = (1 − β − γ ) a x + β b x + γ c x o y + td y = (1 − β − γ ) a y + β b y + γ c y o z + td z = (1 − β − γ ) a z + β b z + γ c z 3 equations for 3 unknowns
Ray-Triangle Intersection Cramer’s Rule o + t ~ a + �~ a + �~ (1 − � − � ) ~ b + �~ ~ p = ↵~ ~ b + �~ d c c a x − b x a x − c x d x β a x − o x a y − b y a y − c y d y γ a y − o y = a z − b z a z − c z d z t a Z − o z Solve the equation with Cramer’s Rule ⇣ ⌘ ⇣ ⌘ a, ~ a × ~ ~ b, ~ ~ · ~ det c = b c
Ray-Triangle Intersection Cramer’s Rule • Accept the solution only if t closest > t > 0 1 > β > 0 1 > γ > 0 1 > 1 − β − γ > 0 t closest : the smallest positive t values so far
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
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
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 ) }
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
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 ) }
Recommend
More recommend