COMP 175 | COMPUTER GRAPHICS Lecture 12: Recursive Ray Tracer2: Advanced Techniques and Data Structures COMP 175: Computer Graphics April 7, 2016 Remco Chang 12 – Recursive Ray Tracer 2 1/49 COMP 175 | COMPUTER GRAPHICS Review Ray Intersect (Assignment 4): questions / comments? Review of Recursive Ray Tracer: Two (or three) types of secondary rays Lighting equation Blending and texture mapping The value of Epsilon… Remco Chang 12 – Recursive Ray Tracer 2 2/49 COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 12 – Recursive Ray Tracer 2 3/49 COMP 175 | COMPUTER GRAPHICS Glowing Ball Now that you see how complicated (and slow) real rendering is, how do you do some common techniques that appear “cool” but run in real time? For example, a glowing floating ball? https://www.youtube.com/watch?v=kAG1MbDeDps Short answer: fake it with texture maps Remco Chang 12 – Recursive Ray Tracer 2 4/49 COMP 175 | COMPUTER GRAPHICS Advanced Techniques (Shadows 1/2) Total hack: Project object on ground plane and render it as black polygon. Eye less sensitive to exact geometry than to contrast of shadow Projective shadows (shadow mapping) Use orthographic or perspective matrix as CTM to project object (shadow caster) against another object; render the shadow object as another primitive Good for single light scenes with shadows cast on planes Shadow volumes Extrude silhouette volume from object (shadow caster) to infinity in the direction opposite the light source Silhouette edge is made up of adjacent polygons, any point within the volume is in shadow Implemented by former cs123 ta Kevin Egan and former PhD student and book co-auther Prof. Morgan McGuire, on nVidia chip Remco Chang 12 – Recursive Ray Tracer 2 5/49 COMP 175 | COMPUTER GRAPHICS Advanced Techniques (Shadows 2/2) Shadow Maps Transform camera to each directional light source Render the scene from the light source POV, keeping the same far clipping plane, update z-buffer only Read the z-buffer back and apply as a texture (shadow map) to the scene projected from the light’s POV Render the scene from the eye point. At each pixel, if distance from light is greater than in the shadow map, the object is in shadow. Major aliasing problem, but implemented in hardware Light Shadow Map Remco Chang By keeping the same far clipping plane, relative distances in Z are preserved June 12 –21, Recursive 2016 Ray Tracer 2 6 6/49 COMP 175 | COMPUTER GRAPHICS Advanced Techniques (Environment Mapping) A way to render reflections (also called reflection mapping) Simulates reflections with a texture map applied to the enclosing sphere or cube of an object Texture map can either be an existing image or a rendering of the scene from the object’s perspective Shiny metal effect using environment map. Cube environment map is created by stitching together 6 textures, each generated by rendering the scene from the object’s POV Ray from eye is reflected over surface normal and intersected with the environment map to get texture coordinates. Remco Chang 12 – Recursive Ray Tracer 2 7/49 COMP 175 | COMPUTER GRAPHICS Advanced Techniques (Surface Detail) Overview: There are several hacks to approximate the lighting and shading of complex geometry without actually describing the geometry – only used for fine detail Perturb geometric normals with bump map Desired Geometry Approximate geometry at render time with displacement map Replace normals of simpler geometry with normal map Remco Chang 12 – Recursive Ray Tracer 2 8/49 COMP 175 | COMPUTER GRAPHICS Surface Detail (Bump Mapping) Texture mapping a rough surface onto an object doesn’t achieve desired effect. Illumination is too uniform Blinn’s technique: Use an array of values (stored in a texture called the “bump map”) to perturb the surface normals by calculating gradient of bump map and adding it to the surface normal Evaluate lighting model with perturbed normals Effect is convincing, but objects still look smooth at silhouette edges Spherical Surface Remco Chang 12 – Recursive Ray Tracer 2 nVidia 9/49 COMP 175 | COMPUTER GRAPHICS Bump Mapping Example The tin foil on this Hershey’s Kiss looks too smooth Remco Chang 12 – Recursive Ray Tracer 2 10/49 COMP 175 | COMPUTER GRAPHICS Bump Mapping Example Bump Mapped: Tin foil look with no increase in model complexity. Edges still don’t look quite right Remco Chang 12 – Recursive Ray Tracer 2 11/49 COMP 175 | COMPUTER GRAPHICS Normal Mapping Similar to bump mapping, but better for highly detailed surfaces with drastically varying normals Level of detail is now limited by normal map resolution, not number of triangles Bump mapping uses a single channel height field to perturb normals, while normal mapping uses a three channel map to actually replace existing surface normal x, y, and z components of the desired normal, stored in the r, g, and b channels of an image Runtime surface normal determination is as easy as a texture lookup Limitations: Same as bump map, silhouette edges lack the added detail Though easy to use at runtime, normal maps are difficult to generate Remco Chang 12 – Recursive Ray Tracer 2 12/49 COMP 175 | COMPUTER GRAPHICS Normal Mapping Examples Render showing actual, simple underlying geometry When a normal map is applied, there appears to be much more detail in the model than actually is Normal mapping can completely alter the perceived geometry of a model Image courtesy of www.anticz.com Remco Chang 12 – Recursive Ray Tracer 2 13/49 COMP 175 | COMPUTER GRAPHICS Normal Map Creation (1/3) While bump maps can be simply drawn, normal maps hold meaningful data in three overlapping channels Normal map for a face. Red, green, and blue channels correspond to x, y, and z normal components www.bitmanagement.com Normal map for brick street www3.telus.net Normal maps are not visually intuitive like height maps. How are they created? Remco Chang 12 – Recursive Ray Tracer 2 14/49 COMP 175 | COMPUTER GRAPHICS Normal Map Creation (2/3) Normal maps must be generated by specialized software, such as Pixologic’s Zbrush (www.pixologic.com) A high resolution model is created by an artist and its normals are used to generate a normal map for a low resolution model. 3D geometry Low-res mesh (~5,000 polygons) Normal Map High-resolution model (~2 million polygons) Remco Chang 12 – Recursive Ray Tracer 2 Final render with normal map applied to low-res mesh (and lighting and texture maps). This is only 5,000 triangles! 15/49 COMP 175 | COMPUTER GRAPHICS Normal Mapping Creation (3/3) How it works: Artist creates a high resolution model with complex geometry (video) High resolution model simplified using mesh decimation to a model with much fewer triangles Normal map generation: Let 𝑀 be the complex original mesh and 𝑆 the simplified mesh To generate the normal to map to a point 𝑃𝑆 on 𝑆, find the point 𝑃𝑀 on 𝑀 such that the Euclidean distance between 𝑃𝑆 and 𝑃𝑀 is minimal. 𝑃𝑀 can be found by having each face of M stored in a grid and indexing into this grid with coordinates collected during mesh simplification. 𝑆 We find the minimum perpendicular distance from 𝑃𝑆 to each of 𝑀’s nearby facets 𝑃𝑆 Compute the normal at 𝑃𝑀 by averaging normals at surrounding vertices. Place this value in triangular normal map for the single face. 𝑃𝑀 Finally, stitch these triangular normal maps together to create a normal map for the entire simple surface. 𝑀 Note: The resolution at which normals are sampled is user defined See SIGGRAPH paper: “A general method for preserving attribute values (again, time vs quality tradeoff) on simplified meshes” by Cignoni, Montani, Rocchini, Scopigno Remco Chang 12 – Recursive Ray Tracer 2 16/49 COMP 175 | COMPUTER GRAPHICS Other Surface Detail Techniques (1/3) Displacement Map: The actual geometric position of points over the surface are displaced along the surface normal according to the values in a texture The displacement map used to render the scene. The intensity value indicates the height at a given point. http://www.creativemac.com/2004/02_feb/tutorials/koc4ddisplace040224.htm Remco Chang 12 – Recursive Ray Tracer 2 17/49 COMP 175 | COMPUTER GRAPHICS Other Surface Detail Techniques (2/3) Parallax Mapping: Displaces texture coordinates of a surface point by a function of the view angle relative to the surface normal and the value on the height map at that point Steep Parallax Mapping: Similar scheme to produce self-occlusion self-shadows of parallax mapped surface; More efficient and reduces swim effect for high frequency maps (Morgan McGuire Ph.D. ’06 and Max McGuire) Texture Mapped Remco Chang 12 – Recursive Ray Tracer 2 Texture and Parallax Mapped 18/49 COMP 175 | COMPUTER GRAPHICS Other Surface Detail Techniques (3/3) Texture Mapped Normal Mapped: Increased depth in rock wall Remco Chang Parallax Mapped: Increased depth of whole texture but unwanted “swim” Steep Parallax Mapped: Very realistic depth in effect. Textures look like they whole picture. Lion head, columns cast self should be 3D but they stay shadows embedded in surface, unsettling to the eye when moving http://graphics.cs.brown.edu/games/SteepParallax/index.h tml 12 – Recursive Ray Tracer 2 19/49 COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 12 – Recursive Ray Tracer 2 20/49 COMP 175 | COMPUTER GRAPHICS How to Speed Up My Ray Tracer?! Remco Chang 12 – Recursive Ray Tracer 2 21/49 COMP 175 | COMPUTER GRAPHICS What Would You Do? In the example below, we have to compute 8 intersection tests to find that we intersected with nothing Is there a way to speed this up? Ray Remco Chang 12 – Recursive Ray Tracer 2 22/49 COMP 175 | COMPUTER GRAPHICS Screen-Based Approach We can project the objects onto the screen, and compute the bounding boxes in 2D. No need to cast rays where the screen is empty If we use openGL to render the scene once, we can (roughly) determine what object(s) we need to intersect with! Remco Chang 12 – Recursive Ray Tracer 2 23/49 COMP 175 | COMPUTER GRAPHICS What Would You Do? In the example below, we have to compute 8 intersection tests to find the nearest object Is there a way to speed this up? Ray Remco Chang 12 – Recursive Ray Tracer 2 24/49 COMP 175 | COMPUTER GRAPHICS Bounding Volumes Use bounding volumes (that are easier/ cheaper to compute intersection tests for) Spheres and boxes are most common It’s typical to use axis-aligned bounding boxes (makes math easier – no coordinate transform) Works very well for tight bounding boxes Do this recursively to get a set of hierarchical bounding boxes (bottom-up or top-down) Can be applied to the scene graph if the objects in the scene are organized spatially Remco Chang 12 – Recursive Ray Tracer 2 25/49 COMP 175 | COMPUTER GRAPHICS Creating a Bounding Volume Hierarchy (BVH) Using a bottom-up approach Remco Chang 12 – Recursive Ray Tracer 2 26/49 COMP 175 | COMPUTER GRAPHICS Bounding Volume Hierarchy (BVH) Example Remco Chang 12 – Recursive Ray Tracer 2 27/49 COMP 175 | COMPUTER GRAPHICS Different Ways To Partition a Space Bounding volume hierarchy Grid Octree kd-Tree (k-dimensional tree) BSP-Tree (binary space partitioning trees) Remco Chang 12 – Recursive Ray Tracer 2 28/49 COMP 175 | COMPUTER GRAPHICS Grids Divide the world into uniform cells Each cell stores the objects contained within it Use line-scanning algorithm to traverse Intersect with each cell along the way 0 1 2 3 4 5 6 0 Remco Chang 1 2 3 12 – Recursive Ray Tracer 2 4 5 6 7 8 29/49 COMP 175 | COMPUTER GRAPHICS Grids Problems: Choosing the right number of cells isn’t the most obvious Too many cells -> need to check a lot of each ray Too few cells -> speed up can be reduced (too many objects in each one) Some cells could be empty, while others have tons of objects within it Note that this approach is not hierarchical, which means that we don’t get the nice 𝑂(log 𝑛) benefit Need to be careful in deciding what objects belong to a cell (vertex, edge, face) But very useful for many reasons: Easy to implement in hardware Good for an animated scene (an object moving to a different cell doesn’t affect the other objects or other cells) Remco Chang 12 – Recursive Ray Tracer 2 30/49 COMP 175 | COMPUTER GRAPHICS Octree Basically this is an adaptive grids approach Generate more cells when needed, but leave the white spaces alone Cells are still axis-aligned Cells will no longer have the same size Remco Chang 12 – Recursive Ray Tracer 2 2-D example (called a quadtree) 31/49 COMP 175 | COMPUTER GRAPHICS Octree Constructed using a top-down approach Recursively split the world into 8 cells Terminates when the depth is too high, or Terminates when the number of objects per cell falls below a threshold Similar problem as grids, need to be careful in determining which objects belong to which cells Remco Chang 12 – Recursive Ray Tracer 2 32/49 COMP 175 | COMPUTER GRAPHICS Octree Start at the root node (cell), if it intersects, recursively check all 8 children (or 4 for quadtrees) If a leaf node (cell) is empty, terminate. Otherwise, check all objects within the cell Remco Chang 12 – Recursive Ray Tracer 2 33/49 COMP 175 | COMPUTER GRAPHICS Octree For each ray, the expected runtime is 𝑂(log 𝑛) Problems: Updating an octree could be tricky (for an animated scene) Finding neighbors of a cell isn’t trivial For scenes that have objects distributed non-uniformly, the depth could become very high Remco Chang 12 – Recursive Ray Tracer 2 34/49 COMP 175 | COMPUTER GRAPHICS Kd-Tree Improves upon octrees in that it is a little more “intelligent” in finding the split points Skips large swaths of empty spaces early Zeros in on areas with high complexity (lots of objects) In the worst case, a kd-Tree will be exactly like a quadtree (for k=2), or an octree (for k=3) Like grids and octrees, kd-trees also use axis-aligned bounding boxes (for checking intersection is fast) Unlike octrees, kd-Trees split in one dimension at a time Remco Chang 12 – Recursive Ray Tracer 2 Octree kd-Tree 35/49 COMP 175 | COMPUTER GRAPHICS kd-Tree: Choosing a Split Plane y=1 0 x=1 (Recursively) splitting a middle results in a quadtree During traversal, cost of entering the right side is much higher Remco Chang 12 – Recursive Ray Tracer 2 36/49 COMP 175 | COMPUTER GRAPHICS kd-Tree: Choosing a Split Plane y=1 0 x=1 Splitting at the medium balances out the cost, but During traversal, the chance of entering the left hand side is much higher (the area is larger) Remco Chang 12 – Recursive Ray Tracer 2 37/49 COMP 175 | COMPUTER GRAPHICS kd-Tree: Choosing a Split Plane y=1 0 x=1 Cost-optimized split: attempts to balance the cost of entering and the probability of entering the two sides Isolates the complex geometry quickly, creating large, (mostly) empty spaces that can be efficiently computed and discarded Remco Chang 12 – Recursive Ray Tracer 2 38/49 COMP 175 | COMPUTER GRAPHICS kd-Tree: Choosing a Split Plane y=1 0 x=1 Further subdivision does not result in big gains (compared to octrees) But the point is that we isolated the complex geometry in one split (which would have taken octree much longer to find) Remco Chang 12 – Recursive Ray Tracer 2 39/49 COMP 175 | COMPUTER GRAPHICS kd-Tree: Caution with Edge Cases Notice the ray from left to right When it enters F, it will do an intersection test with the green rectangle You will be tempted to say that you’re done and return the green rectangle as the nearest object (because F is in front of G) However, one can see that the clear nearest object is the yellow rectangle… Solution, check the t-value against the bounding box t-value Which will show that the t-value for the green rectangle is behind F. Remco Chang 12 – Recursive Ray Tracer 2 40/49 COMP 175 | COMPUTER GRAPHICS Cells BVH Octree Remco Chang kd-Tree 12 – Recursive Ray Tracer 2 41/49 COMP 175 | COMPUTER GRAPHICS Binary Space Partition (BSP) Tree Not really used for ray tracer, but for visible surface determination This algorithm replaces the use of z-buffers Fast to construct. Used in video games (like Quake 3) Similar to kd-Tree, but NOT axis-aligned BSP-0: Initial Scene Remco Chang 12 – Recursive Ray Tracer 2 42/49 COMP 175 | COMPUTER GRAPHICS BSP Trees BSP-1: Choose any polygon (e.g., polygon 3) and subdivide others by its plane, splitting polygons when necessary Remco Chang 12 – Recursive Ray Tracer 2 43 43/49 COMP 175 | COMPUTER GRAPHICS BSP Trees BSP-2: Process front sub-tree recursively Remco Chang 12 – Recursive Ray Tracer 2 44 44/49 COMP 175 | COMPUTER GRAPHICS BSP Trees BSP-3: Process back sub-tree recursively Remco Chang 12 – Recursive Ray Tracer 2 45 45/49 COMP 175 | COMPUTER GRAPHICS BSP Trees BSP-4: An alternative BSP tree with polygon 5 at the root Remco Chang 12 – Recursive Ray Tracer 2 46 46/49 COMP 175 | COMPUTER GRAPHICS BSP-Trees Remember that this is used for determining the order in which objects need to be drawn on screen So the order in which the objects are drawn will be from back to front, but takes into account of the direction of the face and where the eye is (recursively) Remco Chang 12 – Recursive Ray Tracer 2 47/49 COMP 175 | COMPUTER GRAPHICS BSP-Trees void BSP_displayTree(BSP_tree* tree) { location = tree->find_location(eye); if ( location > 0 ) //eye in front of object if ( viewer is in front of root ) { BSP_displayTree(tree->backChild); displayPolygon(tree->root); BSP_displayTree(tree->frontChild); } else { //eye behind the object BSP_displayTree(tree->frontChild); // ignore next line if back-face // culling desired displayPolygon(tree->root); BSP_displayTree(tree->backChild); } } } Remco Chang 12 – Recursive Ray Tracer 2 48/49 COMP 175 | COMPUTER GRAPHICS Summary Which data structure would be best to use in the following situations? An animation where only the camera moves? A fast-paced video game with lots of interactive, moving objects? octree Rendering scenes in a Pixar-like movie? regular grid or BSP-Tree A very balanced and even scene with objects uniformly distributed throughout the whole scene? kd-Tree depends on the shot; most likely a combination of techniques are used Your ray-tracing assignment if you are really fed up with your rendering speed? kd-Tree is best, but balance your decision with difficulty in implementing the solution… Remco Chang 12 – Recursive Ray Tracer 2 49/49