To Do
Computer Graphics (Spring 2008) Start early on raytracer assignment (assn 4)
COMS 4160, Lectures 17, 18: Nuts and bolts of Ray Tracing Ravi Ramamoorthi
http://www.cs.columbia.edu/~cs4160
Acknowledgements: Thomas Funkhouser and Greg Humphreys
Heckbert’s Business Card Ray Tracer Outline
Camera Ray Casting (choosing ray directions) [2.3] Ray-object intersections [2.4] Ray-tracing transformed objects [2.4] Lighting calculations [2.5] Recursive ray tracing [2.6]
Outline in Code Ray Casting 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 ; RayMultiple intersectsmisses intersections: all object:objects: shade UsePixel closest us coloreding color,one black (as lights, does materialsOpenGL) }
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) Camera in canonical frame, transform objects (OpenGL) Up vector 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 4160 lecture 4 on deriving gluLookAt
Constructing a coordinate frame? Camera coordinate frame a bw× We want to associate w with a, and v with b w = u = vwu=× But a and b are neither orthogonal nor unit norm a bw× And we also need to find u a We want to position camera at origin, looking down –Z dirn w = a Hence, vector a is given by eye – center The vector b is simply the up vector Up vector bw× u = bw× Eye vwu=× Center Slide 20 from 4160 lecture 2
Canonical viewing geometry Outline
Camera Ray Casting (choosing ray directions) [2.3] Ray-object intersections [2.4] βv Ray-tracing transformed objects [2.4] αuvw+ β − ray=+ eye -w αu αβuvw+ − Lighting calculations [2.5] Recursive ray tracing [2.6]
fovx j−−(/2) width fovy ( height /2) i αβ=×tan =× tan 2/22width height /2
2 Outline in Code Ray-Sphere Intersection Image Raytrace (Camera cam, Scene scene, int width, int height) ray≡=+ P P01 Pt { 2 Image image = new Image (width, height) ; sphere≡ ()()0 P−−−= Ci P C r for (int i = 0 ; i < height ; i++) for (int j = 0 ; j < width ; j++) { Ray ray = RayThruPixel (cam, i, j) ; Intersection hit = Intersect (ray, scene) ; C image[i][j] = FindColor (hit) ; P } 0 return image ; }
Ray-Sphere Intersection Ray-Sphere Intersection 22 ray≡=+ P P Pt tPP()2(11ii+ tPP 1 0−+ C )()() P 0 − C i P 0 −−= C r 0 01 sphere≡−()()0 P Ci P −−= C r 2 Solve quadratic equations for t Substitute 2 real positive roots: pick smaller root ray≡=+ P P Pt 01 Both roots same: tangent to sphere spherePPtCPPtCr≡()()0 +−i +−−=2 01 01 One positive, one negative root: ray Simplify origin inside sphere (pick + root) 22 tPP()2(11ii+−+−−−= tPP 1 0 C )()() P 0 C i P 0 C r 0 Complex roots: no intersection (check discriminant of equation first)
Ray-Sphere Intersection Ray-Triangle Intersection Intersection point: ray≡=+ P P01 Pt One approach: Ray-Plane intersection, then check if inside triangle B Normal (for sphere, this is same as coordinates in A ()()CA− ×− BA n = sphere frame of reference, useful other tasks) Plane equation: ()()CA−×− BA P − C normal = plane≡ Pii n−= A n 0 P − C C
3 Ray-Triangle Intersection Ray inside Triangle Once intersect with plane, still need to find if in triangle One approach: Ray-Plane intersection, then check if inside triangle B Many possibilities for triangles, general polygons (point A ()()CA−×− BA n = in polygon tests) Plane equation: ()()CA−×− BA We find parametrically [barycentric coordinates]. Also plane≡−= Pii n A n 0 useful for other applications (texture mapping)
Combine with ray equation: C B P =++α ABCβγ A ray≡=+ P P Pt α β αβγ≥≥≥0, 0, 0 01 AiinPn− 0 t = P αβγ++=1 ()PPtnAn01+=ii Pn1i γ
C
Ray inside Triangle Other primitives
B P =++α ABCβγ Much early work in ray tracing focused on ray- A α β αβγ≥≥≥0, 0, 0 primitive intersection tests P αβγ++=1 Cones, cylinders, ellipsoides γ Boxes (especially useful for bounding boxes) C General planar polygons P −=ABACAβ ()() − +γ − Many more 01,01≤≤β ≤≤γ βγ+≤1 Consult chapter in Glassner (handed out) for more details and possible extra credit
Ray Scene Intersection Outline
Camera Ray Casting (choosing ray directions) [2.3] Ray-object intersections [2.4] Ray-tracing transformed objects [2.4] Lighting calculations [2.5] Recursive ray tracing [2.6]
4 Transformed Objects Transformed Objects
E.g. transform sphere into ellipsoid Consider a general 4x4 transform M Will need to implement matrix stacks like in OpenGL Could develop routine to trace ellipsoid (compute parameters after transformation) Apply inverse transform M-1 to ray Locations stored and transform in homogeneous coordinates May be useful for triangles, since triangle after Vectors (ray directions) have homogeneous coordinate set to transformation is still a triangle in any case 0 [so there is no action because of translations] But can also use original optimized routines 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) [2.3] { Ray-object intersections [2.4] Image image = new Image (width, height) ; Ray-tracing transformed objects [2.4] for (int i = 0 ; i < height ; i++) Lighting calculations [2.5] for (int j = 0 ; j < width ; j++) { Ray ray = RayThruPixel (cam, i, j) ; Recursive ray tracing [2.6] 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 • Move a little towards light before shooting shadow ray
Virtual Viewpoint
Virtual Screen Objects
Shadow ray to light is blocked:unblocked: object object in visibleshadow
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 (no per-light ambient as in OpenGL) Attenuation const linear quadratic (like in OpenGL) Shininess s L L = 0 Emission (r g b) const++ lin** d quad d 2 Per light model parameters All as in OpenGL Directional light (direction, RGB parameters) Point light (location, RGB parameters)
Shading Model Outline
n s Camera Ray Casting (choosing ray directions) [2.3] IK=++ae K∑Vi LK id( max ( ln iii ,0) + K s (max( hn i ,0)) ) i=1 Ray-object intersections [2.4] Global ambient term, emission from material Ray-tracing transformed objects [2.4] For each light, diffuse specular terms Lighting calculations [2.5] Note visibility/shadowing for each light (not in Recursive ray tracing [2.6] OpenGL) 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 s IK=++ae K∑Vi L id(max(,0)(max(,0)))KlnKhn iii + s i +KIs R + KITT Trace Primary Eye Ray, find intersection i=1
Trace Secondary Shadow Ray(s) to all light(s) Color = Visible ? Illumination Model : 0 ; Highlighted terms are recursive specularities [mirror 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
Reflection rays may be traced forever •(Soft)Shadows • Reflections (Mirrors and Glossy) • Transparency (Water, Glass) Generally, set maximum recursion depth • Interreflections (Color Bleeding) • Complex Illumination (Natural, Area Light) Same for transmitted rays (take refraction into • Realistic Materials (Velvet, Paints, Glass) account)
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 grid Testing each object for each ray is slow of n x n point lights Fewer Rays Use jittering: Randomize direction of shadow ray within Adaptive sampling, depth control small box for given light source direction Generalized Rays Beam tracing, cone tracing, pencil tracing etc. Jittering also useful for antialiasing shadows when shooting Faster Intersections primary rays Optimized Ray-Object Intersections More complex reflectance models Fewer Intersections Simply update shading model But at present, we can handle only mirror global illumination calculations
7 Acceleration Structures Bounding Volume Hierarchies 1
Bounding boxes (possibly hierarchical) If no intersection bounding box, needn’t check objects
Bounding Box
Ray
Spatial Hierarchies (Oct-trees, kd trees, BSP trees)
Bounding Volume Hierarchies 2 Bounding Volume Hierarchies 3
Acceleration Structures: Grids Uniform Grid: Problems
8 Octree Octree traversal
Other Accelerations Interactive Raytracing
Ray tracing historically slow Now viable alternative for complex scenes Key is sublinear complexity with acceleration; need not process all triangles in scene Allows many effects hard in hardware OpenRT project real-time ray tracing (http://www.openrt.de)
Raytracing on Graphics Hardware
Modern Programmable Hardware general streaming architecture Can map various elements of ray tracing Kernels like eye rays, intersect etc. In vertex or fragment programs Convergence between hardware, ray tracing [Purcell et al. 2002, 2003] http://graphics.stanford.edu/papers/photongfx
9