
Heckbert’s Business Card Ray Tracer Computer Graphics CSE 167 [Win 17], Lectures 16, 17: Nuts and bolts of Ray Tracing Ravi Ramamoorthi http://viscomp.ucsd.edu/classes/cse167/wi17 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 MultipleRay missesintersects intersections: all object:objects: shade Pixel Use using coloredclosest color, black one lights, (as doesmaterials OpenGL) 1 Similar to gluLookAt derivation Constructing a coordinate frame? § gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz) We want to associate w with a, and v with b § 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 a Up vector w = a b × w Eye u = 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 a b × w w = u = v = w × u a b × w § We want to position camera at origin, looking down –Z dirn βv § αu + βv − w Hence, vector a is given by eye – center αu ray = eye + -w αu + βv − w § The vector b is simply the up vector Up vector Eye ⎛ fovx⎞ ⎛ j − (width / 2)⎞ ⎛ fovy ⎞ ⎛ (height / 2) − i ⎞ α = tan × β = tan × ⎝⎜ 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 ray ≡ P = P + Pt ray ≡ P = P + Pt 0 1 0 1 ≡ − − − 2 = ≡ − − − 2 = sphere (P C) i (P C) r 0 sphere (P C) i (P C) r 0 Substitute ray ≡ P = P + Pt 0 1 2 sphere ≡ (P0 + P1t − C) i (P0 + P1t − C) − r = 0 C Simplify t 2(P i P ) + 2t P i (P − C) + (P − C) i (P − C) − r 2 = 0 P0 1 1 1 0 0 0 Ray-Sphere Intersection Ray-Sphere Intersection 2 2 t (P1 i P1) + 2t P1 i (P0 − C) + (P0 − C) i (P0 − C) − r = 0 § Intersection point: ray ≡ P = P + Pt 0 1 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 § Both roots same: tangent to sphere normal = 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 (C − A) × (B − A) A (C − A) × (B − A) n = n = § (C − A) × (B − A) § (C − A) × (B − A) Plane equation: Plane equation: plane ≡ P i n − A i n = 0 plane ≡ P i n − A i n = 0 C § Combine with ray equation: C ray ≡ P = P0 + P1t A i n − P i n t = 0 (P + Pt) i n = A i n P i n 0 1 1 3 Ray inside Triangle Ray inside Triangle § Once intersect with plane, still need to find if in B P = αA + βB + γ C triangle A α β α ≥ 0, β ≥ 0,γ ≥ 0 P α + β + γ = § Many possibilities for triangles, general polygons 1 (point in polygon tests) γ § We find parametrically [barycentric coordinates]. Also C useful for other applications (texture mapping) − = β − + γ − B P A (B A) (C A) A P = αA + βB + γ C α β 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 We have an optimized ray-sphere test § Consider a general 4x4 transform M § But we want to ray trace an ellipsoid… § Will need to implement matrix stacks like in OpenGL -1 Solution: Ellipsoid transforms sphere § Apply inverse transform M to ray § § Apply inverse transform to ray, use ray-sphere Locations stored and transform in homogeneous 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-tn. 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 Light Source Shadows: Numerical Issues ' Numerical inaccuracy may cause intersection to be below surface (effect exaggerated in figure) ' Causing surface to incorrectly shadow itself P Move a little towards light before shooting shadow ray Virtual Viewpoint Virtual Screen Objects Shadow ray ray to to light light is isunblocked: blocked: object visible 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 § § Attenuation const linear quadratic Shininess s L 0 § Emission (r g b) L = 2 const + lin* d + quad * d § 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 s § Camera Ray Casting (choosing ray directions) I = Ka + Ke + ∑ViLi (Kd max (li i n,0) + Ks (max(hi i n,0)) ) i=1 § Ray-object intersections § 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 Generate reflected ray in mirror direction, Turner Whitted 1980 Get reflections and refractions of objects 6 Basic idea Recursive Shading Model n For each pixel I = K + K + V L (K max (l i n,0) + K (max(h i n,0))s ) + K I + K I § Trace Primary Eye Ray, find intersection a e ∑ i i d i s i s R T T i=1 § Trace Secondary Shadow Ray(s) to all light(s) § Highlighted terms are recursive specularities [mirror § Color = Visible ? Illumination Model : 0 ; reflections] and transmission (latter is extra credit) § Trace Reflected Ray § § Color += reflectivity * Color of reflected ray Trace secondary rays for mirror reflections and refractions, include contribution in lighting model § GetColor calls RayTrace recursively (the I values in equation above of secondary rays are obtained by recursive calls) Problems with Recursion Effects needed for Realism P (Soft) Shadows § Reflection rays may be traced forever P Reflections (Mirrors and Glossy) P Transparency (Water, Glass) § Generally, set maximum recursion depth P Interreflections (Color Bleeding) P Complex Illumination (Natural, Area Light) § Same for transmitted rays (take refraction into account) P Realistic Materials (Velvet, Paints, Glass) Discussed in this lecture so far Not discussed but possible with distribution ray tracing Hard (but not impossible) with ray tracing; radiosity methods Some basic add ons Acceleration § Area light sources and soft shadows: break into Testing each object for each ray is slow grid of n x n point lights § Fewer Rays Adaptive sampling,
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages12 Page
-
File Size-