University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2013 Tamara Munzner Advanced Rendering Advanced Rendering http://www.ugrad.cs.ubc.ca/~cs314/Vjan2013 2 Reading for This Module Global Illumination Models • simple lighting/shading methods simulate • FCG Sec 8.2.7 Shading Frequency local illumination models • FCG Chap 4 Ray Tracing • no object-object interaction • FCG Sec 13.1 Transparency and Refraction • global illumination models • more realism, more computation • Optional: FCG Chap 24 Global Illumination • leaving the pipeline for these two lectures! • approaches • ray tracing • radiosity • photon mapping • subsurface scattering 3 4
Ray Tracing Simple Ray Tracing • simple basic algorithm • view dependent method • cast a ray from viewer’s • well-suited for software rendering eye through each pixel • flexible, easy to incorporate new effects • compute intersection of • Turner Whitted, 1990 ray with first object in scene pixel positions • cast ray from on projection projection plane intersection point on reference point object to light sources 5 6 Reflection Refraction n d n • mirror effects • happens at interface between transparent object • perfect specular reflection θ θ θ 1 and surrounding medium • e.g. glass/air boundary θ 2 t • Snell ’ s Law • c sin c sin θ = θ 1 1 2 2 • light ray bends based on refractive indices c 1 , c 2 7 8
Recursive Ray Tracing Basic Algorithm • ray tracing can handle • reflection (chrome/mirror) for every pixel p i { • refraction (glass) generate ray r from camera position through pixel p i • shadows for every object o in scene { • spawn secondary rays if ( r intersects o ) • reflection, refraction compute lighting at intersection point, using local • if another object is hit, normal and material properties; store result in p i recurse to find its color pixel positions else • shadow on projection projection p i = background color plane reference • cast ray from intersection } point point to light source, check if intersects another object } 9 10 Basic Ray Tracing Algorithm Algorithm Termination Criteria • termination criteria RayTrace (r,scene) obj := FirstIntersection (r,scene) • no intersection if (no obj) return BackgroundColor; • reach maximal depth else begin if ( Reflect (obj) ) then • number of bounces reflect_color := RayTrace ( ReflectRay (r,obj)); • contribution of secondary ray attenuated else below threshold reflect_color := Black; if ( Transparent (obj) ) then • each reflection/refraction attenuates ray refract_color := RayTrace ( RefractRay (r,obj)); else refract_color := Black; return Shade (reflect_color,refract_color,obj); end; 11 12
Ray Tracing Algorithm Ray-Tracing Terminology • terminology: Light Image Plane • primary ray: ray starting at camera Eye Source • shadow ray • reflected/refracted ray Shadow Reflected Rays Ray • ray tree: all rays directly or indirectly spawned off by a single primary ray • note: • need to limit maximum depth of ray tree to ensure termination of ray-tracing process! Refracted Ray 13 14 Ray Trees Ray Tracing • all rays directly or indirectly spawned off by a single • issues: primary ray • generation of rays • intersection of rays with geometric primitives • geometric transformations • lighting and shading • efficient data structures so we don’t have to test intersection with every object www.cs.virginia.edu/~gfx/Courses/2003/Intro.fall.03/slides/lighting_web/lighting.pdf 15 16
Ray Generation Ray Generation • camera coordinate system • other parameters: u • distance of camera from image plane: d • origin: C (camera position) • image resolution (in pixels): w, h • viewing direction: v u v • left, right, top, bottom boundaries C • up vector: u x in image plane: l , r, t, b • x direction: x= v × u • then: v O C d v l x b u • note: • lower left corner of image: = + ⋅ + ⋅ + ⋅ C x • pixel at position i, j ( i=0..w-1, j=0..h-1 ) : • corresponds to viewing r l t b transformation in rendering pipeline − − P j O i x j u = + ⋅ ⋅ − ⋅ ⋅ i , w 1 h 1 • like gluLookAt − − O i x x j y y = + ⋅ Δ ⋅ − ⋅ Δ ⋅ 17 18 Ray Generation Ray Tracing • ray in 3D space: • issues: • generation of rays R ( t ) C t ( P C ) C t v • intersection of rays with geometric primitives = + ⋅ − = + ⋅ i , j i , j i , j • geometric transformations • lighting and shading where t= 0… ∞ • efficient data structures so we don’t have to test intersection with every object 19 20
Ray - Object Intersections Ray Intersections: Spheres • inner loop of ray-tracing • spheres at origin • must be extremely efficient • implicit function • task: given an object o, find ray parameter t , such that R i,j ( t ) is a point on the object 2 2 2 2 S ( x , y , z ) : x y z r + + = • such a value for t may not exist • solve a set of equations • ray equation • intersection test depends on geometric primitive c v c t v • ray-sphere + ⋅ & # & # & # x x x x $ ! $ ! $ ! • ray-triangle R ( t ) C t v c t v c t v = + ⋅ = + ⋅ = + ⋅ $ ! $ ! $ ! i , j i , j y y y y • ray-polygon $ ! $ ! $ ! c v c t v + ⋅ % " % " % " z z z z 21 22 Ray Intersections: Spheres Ray Intersections: Other Primitives • implicit functions • to determine intersection: • spheres at arbitrary positions • insert ray R i,j ( t ) into S(x,y,z): • same thing • conic sections (hyperboloids, ellipsoids, paraboloids, cones, cylinders) 2 2 2 2 ( c t v ) ( c t v ) ( c t v ) r + ⋅ + + ⋅ + + ⋅ = x x y y z z • same thing (all are quadratic functions!) • polygons • solve for t (find roots) • first intersect ray with plane • simple quadratic equation • linear implicit function • then test whether point is inside or outside of polygon (2D test) • for convex polygons • suffices to test whether point in on the correct side of every boundary edge • similar to computation of outcodes in line clipping (upcoming) 23 24
Ray-Triangle Intersection Ray-Triangle Intersection • method in book is elegant but a bit complex • check if ray inside triangle • check if point counterclockwise from each edge (to • easier approach: triangle is just a polygon its left) • intersect ray with plane • check if cross product points in same direction as normal (i.e. if dot is positive) normal: n = ( b − a ) × ( c − a ) e c ray : x = e + t d ( b − a ) × ( x − a ) ⋅ n ≥ 0 d c plane : ( p − x ) ⋅ n = 0 ⇒ x = p ⋅ n n n (c − b) × (x − b) ⋅ n ≥ 0 n CCW x a x a (a − c) × (x − c) ⋅ n ≥ 0 p ⋅ n = e + t d ⇒ t = − ( e − p ) ⋅ n b n d ⋅ n b p is a or b or c • more details at http://www.cs.cornell.edu/courses/cs465/2003fa/homeworks/raytri.pdf • check if ray inside triangle 25 26 Ray Tracing Geometric Transformations • issues: • similar goal as in rendering pipeline: • modeling scenes more convenient using different • generation of rays coordinate systems for individual objects • intersection of rays with geometric primitives • problem • geometric transformations • not all object representations are easy to transform • lighting and shading • problem is fixed in rendering pipeline by restriction to polygons, which are affine invariant • efficient data structures so we don’t have to • ray tracing has different solution test intersection with every object • ray itself is always affine invariant • thus: transform ray into object coordinates! 27 28
Geometric Transformations Ray Tracing • ray transformation • issues: • for intersection test, it is only important that ray is in • generation of rays same coordinate system as object representation • intersection of rays with geometric primitives • transform all rays into object coordinates • geometric transformations • transform camera point and ray direction by inverse of model/view matrix • lighting and shading • shading has to be done in world coordinates (where • efficient data structures so we don’t have to light sources are given) test intersection with every object • transform object space intersection point to world coordinates • thus have to keep both world and object-space ray 29 30 Local Lighting Local Lighting • local surface information • local surface information (normal … ) • alternatively: can interpolate per-vertex • for implicit surfaces F ( x,y,z ) =0: normal n ( x,y,z ) information for triangles/meshes as in can be easily computed at every intersection rendering pipeline point using the gradient • now easy to use Phong shading! F ( x , y , z ) / x ∂ ∂ & # $ ! • as discussed for rendering pipeline n ( x , y , z ) F ( x , y , z ) / y = ∂ ∂ $ ! • difference with rendering pipeline: $ ! F ( x , y , z ) / z ∂ ∂ % " • interpolation cannot be done incrementally 2 2 2 2 F ( x , y , z ) x y z r = + + − • have to compute barycentric coordinates for • example: 2 x & # every intersection point (e.g plane equation for $ ! triangles) n ( x , y , z ) 2 y = needs to be normalized! $ ! $ ! 2 z % " 31 32
Recommend
More recommend