University of British Columbia Reading for Last and This Time News Review: Shading Models CPSC 314 Computer Graphics • flat shading • FCG Chap 10 Ray Tracing • signup sheet for P2 grading Jan-Apr 2007 • compute Phong lighting once for entire • only 10.1-10.7 • Mon 11-12, 2-3, 5-5:30 polygon Tamara Munzner • FCG Chap 25 Image-Based Rendering • Tue 11-1 • Gouraud shading • Wed 11-12, 2-3, 5-5:30 • compute Phong lighting at the vertices and Advanced Rendering II interpolate lighting values across polygon • Phong shading Week 7, Fri Mar 2 • compute averaged vertex normals • interpolate normals across polygon and http://www.ugrad.cs.ubc.ca/~cs314/Vjan2007 perform Phong lighting across polygon 2 3 4 Review/Clarification: Specifying Normals Review: Recursive Ray Tracing Review: Reflection and Refraction • ray tracing can handle n • OpenGL state machine • refraction: mirror effects • reflection (chrome/mirror) • uses last normal specified • refraction (glass) • perfect specular reflection θ θ Light • if no normals specified, assumes all identical • shadows Eye Image Plane Source • one primary ray per pixel • per-vertex normals • spawn secondary rays glNormal3f(1,1,1); Advanced Rendering II • refraction: at boundary glVertex3f(3,4,5); • reflection, refraction glNormal3f(1,1,0); Reflected • if another object is hit, recurse to n Shadow • Snell’s Law d glVertex3f(10,5,2); Ray find its color Rays • per-face normals • shadow • light ray bends based on • cast ray from intersection point to refractive indices c 1 , c 2 θ 1 glNormal3f(1,1,1); glVertex3f(3,4,5); light source, check if intersects another object glVertex3f(10,5,2); c sin c sin Refracted θ = θ θ 2 1 1 2 2 • termination criteria • normal interpreted as direction from vertex location Ray t • no intersection (ray exits scene) • can automatically normalize (computational cost) • max bounces (recursion depth) glEnable(GL_NORMALIZE); 5 • attenuated below threshold 6 7 8 Ray Trees Ray Tracing Ray Generation Ray Generation • all rays directly or indirectly spawned off by a single • issues: • camera coordinate system • other parameters: u u primary ray • distance of camera from image plane: d • generation of rays • origin: C (camera position) • image resolution (in pixels): w, h • intersection of rays with geometric primitives • viewing direction: v u u v v • left, right, top, bottom boundaries C C • geometric transformations • up vector: u x x in image plane: l , r, t, b • lighting and shading • x direction: x= v × u • then: v v • efficient data structures so we don’t have to • note: O C d v l x b u • lower left corner of image: = + ⋅ + ⋅ + ⋅ C C x x test intersection with every object • corresponds to viewing • pixel at position i, j ( i=0..w-1, j=0..h-1 ) : 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 = + ⋅ Δ ⋅ − ⋅ Δ ⋅ www.cs.virginia.edu/~gfx/Courses/2003/Intro.fall.03/slides/lighting_web/lighting.pdf 9 10 11 12 Ray Generation Ray Tracing Ray - Object Intersections Ray Intersections: Spheres • ray in 3D space: • issues: • inner loop of ray-tracing • spheres at origin • must be extremely efficient • generation of rays • implicit function • task: given an object o, find ray parameter t , such R ( t ) C t ( P C ) C t v = + ⋅ − = + ⋅ • intersection of rays with geometric primitives i , j i , j i , j that R i,j ( t ) is a point on the object S ( x , y , z ) : x 2 y 2 z 2 r 2 + + = • geometric transformations • such a value for t may not exist • lighting and shading • ray equation where t= 0… ∞ • solve a set of equations • efficient data structures so we don’t have to c v c t v • intersection test depends on geometric primitive + ⋅ x x x x test intersection with every object • ray-sphere R ( t ) C t v c t v c t v = + ⋅ = + ⋅ = + ⋅ i , j i , j y y y y • ray-triangle c v c t v + ⋅ z z z z • ray-polygon 13 14 15 16
Ray Intersections: Spheres Ray Intersections: Other Primitives Ray-Triangle Intersection Ray-Triangle Intersection • implicit functions • method in book is elegant but a bit complex • check if ray inside triangle • to determine intersection: • spheres at arbitrary positions • check if point counterclockwise from each edge (to • easier approach: triangle is just a polygon • insert ray R i,j ( t ) into S(x,y,z): • same thing its left) • conic sections (hyperboloids, ellipsoids, paraboloids, cones, • intersect ray with plane • check if cross product points in same direction as cylinders) 2 2 2 2 ( c t v ) ( c t v ) ( c t v ) r + ⋅ + + ⋅ + + ⋅ = normal: n = ( b − a ) × ( c − a ) normal (i.e. if dot is positive) • same thing (all are quadratic functions!) x x y y z z e c • polygons ray : x = e + t d • solve for t (find roots) ( b − a ) × ( x − a ) ⋅ n ≥ 0 d • first intersect ray with plane c plane : ( p − x ) ⋅ n = 0 ⇒ x = p ⋅ n n • simple quadratic equation • linear implicit function n (c − b) × (x − b) ⋅ n ≥ 0 • then test whether point is inside or outside of polygon (2D test) n CCW x a a x (a − c) × (x − c) ⋅ n ≥ 0 • for convex polygons p ⋅ n = e + t d ⇒ t = − ( e − p ) ⋅ n • suffices to test whether point in on the correct side of every b boundary edge n d ⋅ n b • similar to computation of outcodes in line clipping (upcoming) p is a or b or c • more details at • check if ray inside triangle http://www.cs.cornell.edu/courses/cs465/2003fa/homeworks/raytri.pdf 17 18 19 20 Ray Tracing Geometric Transformations Geometric Transformations Ray Tracing • issues: • similar goal as in rendering pipeline: • ray transformation • issues: • modeling scenes more convenient using different • for intersection test, it is only important that ray is in • generation of rays • generation of rays coordinate systems for individual objects same coordinate system as object representation • intersection of rays with geometric primitives • intersection of rays with geometric primitives • problem • transform all rays into object coordinates • geometric transformations • geometric transformations • transform camera point and ray direction by inverse of • not all object representations are easy to transform • lighting and shading model/view matrix • lighting and shading • problem is fixed in rendering pipeline by restriction to • shading has to be done in world coordinates (where polygons, which are affine invariant • efficient data structures so we don’t have to • efficient data structures so we don’t have to light sources are given) • ray tracing has different solution test intersection with every object test intersection with every object • transform object space intersection point to world • ray itself is always affine invariant coordinates • thus: transform ray into object coordinates! • thus have to keep both world and object-space ray 21 22 23 24 Local Lighting Local Lighting Global Shadows Global Reflections/Refractions • local surface information • local surface information (normal…) • approach • approach • alternatively: can interpolate per-vertex • send rays out in reflected and refracted direction to • for implicit surfaces F ( x,y,z ) =0: normal n ( x,y,z ) • to test whether point is in shadow, send out information for triangles/meshes as in gather incoming light can be easily computed at every intersection shadow rays to all light sources rendering pipeline • that light is multiplied by local surface color and point using the gradient • if ray hits another object, the point lies in added to result of local shading • now easy to use Phong shading! F ( x , y , z ) / x ∂ ∂ shadow • 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! needs to be normalized! 2 z 25 26 27 28 Total Internal Reflection Ray Tracing Optimized Ray-Tracing Example Images • issues: • basic algorithm simple but very expensive • optimize by reducing: • generation of rays • number of rays traced • intersection of rays with geometric primitives • number of ray-object intersection calculations • geometric transformations • methods • lighting and shading • bounding volumes: boxes, spheres • efficient data structures so we don’t have to • spatial subdivision test intersection with every object • uniform • BSP trees • (more on this later with collision) http://www.physicsclassroom.com/Class/refrn/U14L3b.html 29 30 31 32
Recommend
More recommend