Ray Tracing I
Reading Hill, Chapter 14, Sections 14.1 to 14.5 Hill, Chapter 14, Sections and
References Hill Chapter 14 Specialized books: Online resources
Outline Introduction Camera and Ray Generation Ray-Sphere Intersection Ray-Plane Intersection Writing a Ray Tracer Shadows Recursive Ray Tracing
BRDF Dan Lemmon - Global Illumination: A Brief Overview
Phong Illumination
Phong vs. Reality
Arnold Ray Tracer
Global Illumination in Games
Ray Casting For every pixel Construct a ray from the eye For every object in the scene Find intersection with the ray Keep if closest
Durer’s Ray casting machine Albrecht Durer, 16 th century
Durer’s Ray casting machine Albrecht Durer, 16 th century
Arthur Appel, 1968 “On calculating the illusion of reality”, 1968 Cast one ray per pixel (ray casting). For each intersection, trace one ray to the light to check for shadows Only a local illumination model Developed for pen-plotters V V L L
Ray Casting Image by Henrik Wann Jensen
Turner Whitted, 1980 “An Improved Illumination Model for Shaded Display”, 1980 First global illumination model. An object’s color is influenced by lights and other objects in the scene First to simulate specular reflection and refractive transmission V T L R L R T R R
Ray Tracing Original Ray-traced image by Whitted Image computed using the Dali ray tracer by Henrik Wann Jensen
Ray Tracing Image computed using the Dali ray tracer from Henrik Wann Jensen Model Stephen Duck
Outline Introduction Camera and Ray Generation Ray-Sphere Intersection Ray-Plane Intersection Writing a Ray Tracer Shadows Recursive Ray Tracing
Cameras For every pixel Construct a ray from the eye For every object in the scene Find intersection with the ray Keep if closest
Ray Representation A parametric line: Origin Direction (normalized is better) p(t) = origin + t * direction origin direction p(t)
Pinhole Camera Box with a tiny hole Inverted image Pure geometric optics
Simplified Pinhole Camera Eye-image pyramid (frustum)
Durer’s Ray Casting Machine Albrecht Durer, 16 th century
Perspective Camera Eye point Look-At Point Up Vector Horizontal FOV (degrees) Look-At Up Eye
Perspective Ray Generation
Outline Introduction Camera and Ray Generation Ray-Sphere Intersection Ray-Plane Intersection Writing a Ray Tracer Shadows Recursive Ray Tracing
Implicit Sphere Equation Implicit sphere equation: ||P|| 2 = r 2 Assume sphere is centered at the origin - easy to translate origin direction
Ray-Sphere Intersection Implicit sphere equation: ||P|| 2 = r 2 Parametric ray equation: p(t) = O + t d with || d || = 1 Intersection means both are satisfied origin direction
Ray-Sphere Intersection
This is just a quadratic at 2 + bt + c = 0, where a = 1 b = 2 d O c = O O – r 2 With discriminant and solutions
Ray-Sphere Intersection Three cases, depending on sign of b 2 – 4ac Which root (t+ or t-) should you choose? Closest positive! (usually t-)
Ray-Sphere Intersection So easy that all ray-tracing images have spheres!
Geometric Ray-Sphere Intersection Try to shortcut (easy reject) E.g., if the ray is facing away from the sphere Geometric considerations can help In general, early reject is important r S
Geometric Ray-Sphere Intersection What geometric information is important? Inside / outside Closest point Direction O r S d
Geometric Ray-Sphere Intersection Check if the ray’s origin is outside the sphere ||O|| 2 > r 2 If inside, it intersects If on the sphere, it does not intersect Avoid degeneracy with epsilon test (later) O r S d
Geometric Ray-Sphere Intersection If the ray’s origin is outside the sphere Find the closest point P to the sphere center t P = OS d If t P < 0: Ray points away from the sphere, no hit O r S d tPtP P
Geometric Ray-Sphere Intersection Else find squared distance e 2 Pythagoras: e 2 = OS 2 - t P 2 if e 2 > r 2 : Ray misses sphere, no hit O r S d P tPtP e
Geometric Ray-Sphere Intersection t’ 2 + e 2 = r 2 If ray starts outside sphere: t = t P - t’ If ray starts inside sphere: t = t P + t’ O r S d P tPtP e t t’
Geometric vs. Algebraic Algebraic was simpler (and more generic) Geometric is more efficient Timely tests In particular for outside and pointing away
Normal Simply (I(t)-S) / r O r S d t n I(t)
Precision Problems In ray tracing, the origin of (secondary) rays is often on the surface of objects Theoretically, t = 0 for these rays In practice, calculation imprecision creeps in, and the origin of the new ray is slightly beneath the surface This could lead to a situation where a surface area is shadowing itself…
to the rescue… Check if t is within some epsilon tolerance: if abs(t) < eps // eps = 0.01 point is on the sphere else point is inside/outside Choose the epsilon tolerance empirically Move the intersection point by epsilon along the surface normal so it is outside of the object Check if point is inside/outside surface by checking the sign of the implicit (sphere etc.) equation
Outline Introduction Camera and Ray Generation Ray-Sphere Intersection Ray-Plane Intersection Writing a Ray Tracer Shadows Recursive Ray Tracing
Implicit Plane Equation Given a point A on the plane and the normal n, any point P on the plane must satisfy: n A P v
Implicit Plane Equation Suppose n = [a, b, c] T and P=[x, y, z] T : Every plane perpendicular to n = [a, b, c] T has an implicit equation: The value d = 0 gives a plane through the origin Different values of d give parallel planes
Implicit Plane Equation Implicit plane equation ax + by + cz + d = 0 Plane defined by P 0 = [x, y, z, 1] T n = [a, b, c, 0] T
Line-Plane Intersection Insert parametric equation of the line into the implicit equation of the plane Origin Direction P(t)
Additional House Keeping Verify that intersection is closer than previous Verify that it is in the allowed range t<0: intersection is behind the camera O d p(t)
Headed towards plane? Dot product with normal if t n = 0: Ray parallel to plane; no intersection if t n < 0: Normal is pointing away from ray (may be culled)* if t n > 0: Ray hits plane *Reverse plane normal so that ray intersects:
Intersection Calculation Ray expression: Substitute into plane equation: Solve for t: If t < 0: ray intersects plane behind O
The Algorithm
Normal Use the normal to the plane from the implicit equation Normal Origin Direction P(t)
Outline Introduction Camera and Ray Generation Ray-Sphere Intersection Ray-Plane Intersection Writing a Ray Tracer Shadows Recursive Ray Tracing
Object-Oriented Design We want to be able to add primitives easily Inheritance and virtual methods Even the scene is derived from Object3D Object3D bool intersect(Ray, Hit, tmin) Plane bool intersect(Ray, Hit, tmin ) Sphere bool intersect(Ray, Hit, tmin) Triangle bool intersect(Ray, Hit, tmin) Group bool intersect(Ray, Hit, tmin)
Ray ////////// class Ray { ////////// Ray () {} Ray (const Vec3f &dir, const Vec3f &orig) {_dir =dir; _orig=orig;} Ray (const Ray& r) {*this=r;} const Vec3f &origin() {return _orig;} const Vec3f &direction() {return &dir;} Vec3f pointAtParameter (float t) {return _orig+t*_dir;} private: Vec3f _dir; Vec3f _orig; };
Hit Stores intersection point and various information ///////// class Hit { ///////// float _t; Vec3f _color; Material *_material; Vec3f _normal; };
Shaders ( Material Class) Functions executed when light interacts with a surface Constructor: Set shader parameters Inputs: Incident radiance (lights) Incident & reflected viewing directions Output: Reflected radiance
Outline Introduction Camera and Ray Generation Ray-Sphere Intersection Ray-Plane Intersection Writing a Ray Tracer Shadows Recursive Ray Tracing
Ray Tracing with Shadows Image Henrik Wann Jensen
Outline Introduction Camera and Ray Generation Ray-Sphere Intersection Ray-Plane Intersection Writing a Ray Tracer Shadows Recursive Ray Tracing
Turner Whitted, 1980 “An Improved Illumination Model for Shaded Display”, 1980 First global illumination model. An object’s color is influenced by lights and other objects in the scene First to simulate specular reflection and refractive transmission V T L R L R T R R
Does it ever end? Stopping criteria: Recursion depth: Stop after a number of bounces Ray contribution: Stop if reflected / transmitted contribution becomes too small 1 recursion0 recursion2 recursions