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