To Do

Computer Graphics (Spring 2008) ƒ Start early on raytracer assignment (assn 4)

COMS 4160, Lectures 17, 18: Nuts and bolts of 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 ()

ƒ 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 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