1Advanced Computer Graphics Advanced Computer Graphics (Fall 2009)(Fall 2009)CS 294-13, Rendering Lecture 1: Introduction and Basic Ray Tracing Ravi Ramamoorthihttp://inst.eecs.berkeley.edu/~cs294-13/fa09Some slides courtesy Thomas Funkhouser and Pat HanrahanTo DoTo Do Start working on raytracer assignment (if necessary) Start thinking about path tracer, final projectFirst AssignmentFirst Assignment In groups of two (find partners) Monte Carlo Path Tracer If no previous ray tracing experience, ray tracer first. See how far you go. Many extra credit items possible, fast multi-dim. rendering, imp. sampling… This lecture focuses on basic ray tracing Likely to be a review for most of you, go over fastRay Tracing HistoryRay Tracing HistoryRay Tracing HistoryRay Tracing HistoryImage courtesy Paul Heckbert 19832OutlineOutline Camera Ray Casting (choosing ray directions) Ray-object intersections Ray-tracing transformed objects Lighting calculations Recursive ray tracing Outline in CodeOutline in CodeImage 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) ;Intersection hit = Intersect (ray, scene) ;image[i][j] = FindColor (hit) ;}return image ;}Ray CastingVirtual ViewpointVirtual Screen ObjectsRay misses all objects: Pixel colored blackRay intersects object: shade using color, lights, materialsMultiple intersections: Use closest one (as does OpenGL)Finding Ray DirectionFinding Ray Direction Goal is to find ray direction for given pixel i and j Many ways to approach problem Objects in world coord, find dirn of each ray (we do this) Camera in canonical frame, transform objects (OpenGL) Basic idea Ray has origin (camera center) and direction Find direction given camera params and i and j Camera params as in gluLookAt Lookfrom[3], LookAt[3], up[3], fovSimilar to Similar to gluLookAtgluLookAtderivationderivation gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz) Camera at eye, looking at center, with up direction being upEyeUp vectorCenterConstructing a coordinate frame?Constructing a coordinate frame?awaWe want to associate w with a, and v with b But a and b are neither orthogonal nor unit norm And we also need to find ubwubwvwu3Camera coordinate frameCamera coordinate frameawa We want to position camera at origin, looking down –Z dirn Hence, vector a is given by eye – center The vector b is simply the up vectorbwubwvwuEyeUp vectorCenterCanonical viewing geometryCanonical viewing geometry-wαuβv(/2) ( /2)tan tan2/2 2 /2fovx j width fovy height iwidth height uvwray eyeuvwOutlineOutline Camera Ray Casting (choosing ray directions) Ray-object intersections Ray-tracing transformed objects Lighting calculations Recursive ray tracingOutline in CodeOutline in CodeImage 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) ;Intersection hit = Intersect (ray, scene) ;image[i][j] = FindColor (hit) ;}return image ;}RayRay--Sphere IntersectionSphere Intersection012()() 0ray P P Ptsphere P C P C r CP0RayRay--Sphere IntersectionSphere Intersection012()() 0ray P P Ptsphere P C P C rSubstitute01201 01()()0ray P P Ptsphere PPtCPPtCr Simplify2211 1 0 0 0()2( )( )( ) 0tPP tPP C P C P C r 4RayRay--Sphere IntersectionSphere Intersection2211 1 0 0 0()2( )( )( ) 0tPP tPP C P C P C r Solve quadratic equations for t 2 real positive roots: pick smaller root Both roots same: tangent to sphere One positive, one negative root: ray origin inside sphere (pick + root) Complex roots: no intersection (check discriminant of equation first)RayRay--Sphere IntersectionSphere Intersection Intersection point: Normal (for sphere, this is same as coordinates in sphere frame of reference, useful other tasks) 01ray P P Pt PCnormalPCRayRay--Triangle IntersectionTriangle Intersection One approach: Ray-Plane intersection, then check if inside triangle Plane equation: ABC()()()()CA BAnCA BA0plane P n A nRayRay--Triangle IntersectionTriangle Intersection One approach: Ray-Plane intersection, then check if inside triangle Plane equation: Combine with ray equation: ABC()()()()CA BAnCA BA0plane P n A n0101()ray P P PtPPtnAn01AnPntPnRay inside TriangleRay inside Triangle Once intersect with plane, still need to find if in triangle Many possibilities for triangles, general polygons (point in polygon tests) We find parametrically [barycentric coordinates]. Also useful for other applications (texture mapping)ABCPαβγ0, 0, 01PABCRay inside TriangleRay inside TriangleABCPαβγ0, 0, 01PABC()()PABACA01,0115Other primitivesOther primitives Much early work in ray tracing focused on ray-primitive intersection tests Cones, cylinders, ellipsoides Boxes (especially useful for bounding boxes) General planar polygons Many more Many references. For example, chapter in Glassnerintroduction to ray tracing (see me if interested)Ray Scene IntersectionRay Scene IntersectionOutlineOutline Camera Ray Casting (choosing ray directions) Ray-object intersections Ray-tracing transformed objects Lighting calculations Recursive ray tracing Transformed ObjectsTransformed Objects E.g. transform sphere into ellipsoid Could develop routine to trace ellipsoid (compute
View Full Document