Heckbert ’ s Business Card Ray Tracer Computer Graphics CSE 167 [Win 19], Lectures 16, 17: Nuts and bolts of Ray Tracing Ravi Ramamoorthi http://viscomp.ucsd.edu/classes/cse167/wi19 Acknowledgements: Thomas Funkhouser and Greg Humphreys To Do Outline § START EARLY on HW 4 § Camera Ray Casting (choose ray directions) § Milestone is due on Mar 8 § Ray-object intersections § Ray-tracing transformed objects § Lighting calculations § Recursive ray tracing Ray Casting Outline in Code Image Raytrace (Camera cam, Scene scene, int width, int height) { Image image = new Image (width, height) ; for (int i = 0 ; i < height ; i++) for (int j = 0 ; j < width ; j++) { Ray ray = RayThruPixel (cam, i, j) ; Virtual Viewpoint Intersection hit = Intersect (ray, scene) ; image[i][j] = FindColor (hit) ; } Virtual Screen Objects return image ; Ray misses all objects: Pixel colored black Ray intersects object: shade using color, lights, materials Multiple intersections: Use closest one (as does OpenGL) } 1
Finding Ray Direction Similar to gluLookAt derivation § gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, § Goal is to find ray direction for given pixel i and j upy, upz) § Many ways to approach problem § Camera at eye, looking at center, with up direction being up § Objects in world coord, find dirn of each ray (we do this) Up vector § Camera in canonical frame, transform objects (OpenGL) § Basic idea § Ray has origin (camera center) and direction Eye § Find direction given camera params and i and j § Camera params as in gluLookAt § Lookfrom[3], LookAt[3], up[3], fov Center From earlier lecture on deriving gluLookAt Constructing a coordinate frame? Camera coordinate frame u = b × w w = a We want to associate w with a , and v with b v = w × u b × w § But a and b are neither orthogonal nor unit norm a § And we also need to find u w = a § We want to position camera at origin, looking down –Z dirn a § Hence, vector a is given by eye – center § The vector b is simply the up vector u = b × w Up vector b × w Eye v = w × u Center From basic math lecture - Vectors: Orthonormal Basis Frames Canonical viewing geometry Outline § Camera Ray Casting (choosing ray directions) § Ray-object intersections β v § Ray-tracing transformed objects ray = eye + α u + β v − w α u -w α u + β v − w § Lighting calculations § Recursive ray tracing ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ α = tan fovx j − ( width / 2) β = tan fovy ⎟ × ( height / 2) − i ⎟ × ⎜ ⎜ ⎟ ⎜ ⎜ ⎟ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ 2 width / 2 2 height / 2 2
Outline in Code Ray-Sphere Intersection Image Raytrace (Camera cam, Scene scene, int width, int height) ≡ P = 0 + ray P P 1 t { C ) − r 2 = 0 sphere ≡ ( P − C ) i ( P − Image image = new Image (width, height) ; for (int i = 0 ; i < height ; i++) for (int j = 0 ; j < width ; j++) { Ray ray = RayThruPixel (cam, i, j) ; C Intersection hit = Intersect (ray, scene) ; image[i][j] = FindColor (hit) ; P 0 } return image ; } Ray-Sphere Intersection Ray-Sphere Intersection C ) − r 2 = 0 t 2 ( 1 ) + 2 t 0 − C ) + ( 0 − 0 − P 1 i P P 1 i ( P P C ) i ( P ≡ P = 0 + ray P P 1 t C ) − r 2 = 0 Solve quadratic equations for t sphere ≡ ( P − P − C ) i ( Substitute § 2 real positive roots: pick smaller root ≡ P = 0 + ray P P 1 t § Both roots same: tangent to sphere C ) − r 2 = 0 sphere ≡ ( 0 + 1 t − 0 + 1 t − P P C ) i ( P P § One positive, one negative root: ray Simplify origin inside sphere (pick + root) C ) − r 2 = 0 t 2 ( 1 ) + 2 t 0 − C ) + ( 0 − 0 − P 1 i P P 1 i ( P P C ) i ( P § Complex roots: no intersection (check discriminant of equation first) Ray-Sphere Intersection Ray-Triangle Intersection ≡ P = 0 + ray P P 1 t § Intersection point: § One approach: Ray-Plane intersection, then check if inside triangle B § Normal (for sphere, this is same as coordinates A n = ( C − A ) × ( B − A ) in sphere frame of reference, useful other tasks) § Plane equation: ( C − A ) × ( B − A ) P − P i A i C plane ≡ n − n = 0 = normal P − C C 3
Ray-Triangle Intersection Ray inside Triangle § Once intersect with plane, still need to find if in § One approach: Ray-Plane intersection, then triangle check if inside triangle B A n = ( C − A ) × ( B − A ) § Many possibilities for triangles, general polygons § Plane equation: ( C − A ) × ( B − A ) (point in polygon tests) P i A i plane ≡ n − n = 0 § We find parametrically [barycentric coordinates]. Also useful for other applications (texture mapping) § Combine with ray equation: C A i 0 i B ray ≡ P = P 0 + P 1 t n − P n P = α A + β B + γ C A t = β 1 i 1 t ) i A i α 0 + n = ( P P n P n α ≥ 0, β ≥ 0, γ ≥ 0 P α + β + γ = 1 γ C Ray inside Triangle Other primitives P = α A + β B + γ C § Much early work in ray tracing focused on ray- B A β α ≥ 0, β ≥ 0, γ ≥ 0 primitive intersection tests α α + β + γ = 1 P § Cones, cylinders, ellipsoids γ § Boxes (especially useful for bounding boxes) C § General planar polygons P − A = β ( B − A ) + γ ( C − A ) § Many more 0 ≤ β ≤ 1 , 0 ≤ γ ≤ 1 β + γ ≤ 1 § Consult chapter in Glassner (handed out) for more details and possible extra credit Ray Scene Intersection Outline § Camera Ray Casting (choosing ray directions) § Ray-object intersections § Ray-tracing transformed objects § Lighting calculations § Recursive ray tracing 4
Transformed Objects Ray-Tracing Transformed Objects § E.g. transform sphere into ellipsoid We have an optimized ray-sphere test § But we want to ray trace an ellipsoid … § Could develop routine to trace ellipsoid Solution: Ellipsoid transforms sphere (compute parameters after transformation) § Apply inverse transform to ray, use ray-sphere § May be useful for triangles, since triangle after § Allows for instancing (traffic jam of cars) transformation is still a triangle in any case § Same idea for other primitives § But can also use original optimized routines Transformed Objects Outline § Consider a general 4x4 transform M § Camera Ray Casting (choosing ray directions) § Will need to implement matrix stacks like in OpenGL § Ray-object intersections § Apply inverse transform M -1 to ray § Locations stored and transform in homogeneous § Ray-tracing transformed objects coordinates § Vectors (ray directions) have homogeneous coordinate § Lighting calculations set to 0 [so there is no action because of translations] § Recursive ray tracing § Do standard ray-surface intersection as modified § Transform intersection back to actual coordinates § Intersection point p transforms as Mp § Distance to intersection if used may need recalculation § Normals n transform as M -t n. Do all this before lighting Shadows Outline in Code Light Source Image Raytrace (Camera cam, Scene scene, int width, int height) { Image image = new Image (width, height) ; for (int i = 0 ; i < height ; i++) for (int j = 0 ; j < width ; j++) { Ray ray = RayThruPixel (cam, i, j) ; Virtual Viewpoint Intersection hit = Intersect (ray, scene) ; image[i][j] = FindColor (hit) ; } Virtual Screen Objects return image ; Shadow ray to light is unblocked: object visible Shadow ray to light is blocked: object in shadow } 5
Shadows: Numerical Issues Lighting Model � Numerical inaccuracy may cause intersection to be § Similar to OpenGL below surface (effect exaggerated in figure) § Lighting model parameters (global) � Causing surface to incorrectly shadow itself § Ambient r g b � Move a little towards light before shooting shadow ray § Attenuation const linear quadratic L 0 L = const + lin * d + quad * d 2 § Per light model parameters § Directional light (direction, RGB parameters) § Point light (location, RGB parameters) § Some differences from HW 2 syntax Material Model Shading Model § Diffuse reflectance (r g b) n ∑ L i ( K d max ( l i i n ,0) + K s (max( h i i n ,0)) s ) I = K a + K e + V i § Specular reflectance (r g b) i = 1 § Shininess s § Global ambient term, emission from material § Emission (r g b) § For each light, diffuse specular terms § All as in OpenGL § Note visibility/shadowing for each light (not in OpenGL) § Evaluated per pixel per light (not per vertex) Mirror Reflections/Refractions Outline § Camera Ray Casting (choosing ray directions) § Ray-object intersections § Ray-tracing transformed objects § Lighting calculations Virtual Viewpoint § Recursive ray tracing Virtual Screen Objects Generate reflected ray in mirror direction, Get reflections and refractions of objects 6
Recommend
More recommend