ray casting
play

Ray Casting Outline in Code Outline in Code Image Raytrace (Camera - PDF document

To Do To Do Computer Graphics (Spring 2008) 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


  1. To Do To Do Computer Graphics (Spring 2008) 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 s Business Card Ray Tracer Business Card Ray Tracer Outline Outline Heckbert � 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] Ray Casting Outline in Code Outline in Code 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 ; Ray misses all objects: Pixel colored black Ray intersects object: shade using color, lights, materials Multiple intersections: Use closest one (as does OpenGL) }

  2. Finding Ray Direction Similar to gluLookAt gluLookAt derivation derivation Finding Ray Direction Similar to � 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) Up vector � Camera in canonical frame, transform objects (OpenGL) � 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? Constructing a coordinate frame? Camera coordinate frame Camera coordinate frame × a b w We want to associate w with a , and v with b = = = × w u v w u × a b w � But a and b are neither orthogonal nor unit norm � And we also need to find u � We want to position camera at origin, looking down –Z dirn a = w � Hence, vector a is given by eye – center a � The vector b is simply the up vector Up vector × b w = u × b w Eye = × v w u Center Slide 20 from 4160 lecture 2 Canonical viewing geometry Canonical viewing geometry Outline Outline � Camera Ray Casting (choosing ray directions) [2.3] � Ray-object intersections [2.4] β v � Ray-tracing transformed objects [2.4] α u + β v − w = + ray eye α u -w α + β − u v w � Lighting calculations [2.5] � Recursive ray tracing [2.6] −  −   fovx   j ( width / 2)   fovy  ( height / 2) i α = × β = × tan tan         2   width / 2   2   height / 2 

  3. Outline in Code Ray- -Sphere Intersection Sphere Intersection Outline in Code Ray � � � Image Raytrace (Camera cam, Scene scene, int width, int height) ≡ = + ray P P Pt 0 1 � � � � { ≡ − − − 2 = sphere ( P C ) ( i P C ) r 0 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) ; C Intersection hit = Intersect (ray, scene) ; image[i][j] = FindColor (hit) ; P 0 } return image ; } Ray Ray- -Sphere Intersection Sphere Intersection Ray Ray- -Sphere Intersection Sphere Intersection � � � � � � � � � � � � 2 + − + − − − 2 = t ( P P i ) 2 t P i ( P C ) ( P C ) ( i P C ) r 0 ≡ = + ray P P Pt 1 1 1 0 0 0 0 1 � � � � ≡ − − − 2 = Solve quadratic equations for t sphere ( P C ) ( i P C ) r 0 Substitute � 2 real positive roots: pick smaller root � � � ≡ = + ray P P Pt � Both roots same: tangent to sphere 0 1 � � � � � � ≡ + − + − − = sphere ( P Pt C ) ( i P Pt C ) r 2 0 � One positive, one negative root: ray 0 1 0 1 Simplify origin inside sphere (pick + root) � � � � � � � � � 2 + − + − − − 2 = t ( P P i ) 2 t P i ( P C ) ( P C ) ( i P C ) r 0 � Complex roots: no intersection (check 1 1 1 0 0 0 discriminant of equation first) Ray Ray- -Sphere Intersection Sphere Intersection Ray- Ray -Triangle Intersection Triangle Intersection � � � ≡ = + � Intersection point: ray P P Pt � One approach: Ray-Plane intersection, then check if 0 1 inside triangle B � Normal (for sphere, this is same as coordinates in A − × − ( C A ) ( B A ) = n sphere frame of reference, useful other tasks) � Plane equation: − × − ( C A ) ( B A ) � � � � � − P C � ≡ − = = plane P n i A n i 0 normal � � − P C C

  4. Ray- -Triangle Intersection Triangle Intersection Ray inside Triangle Ray 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 − × − ( C A ) ( B A ) in polygon tests) = n � Plane equation: ( C − A ) ( × B − A ) � We find parametrically [barycentric coordinates]. Also � � � � ≡ − = plane P n i A n i 0 useful for other applications (texture mapping) � Combine with ray equation: C P = α A + β B + γ C B � � � � � � � ≡ = + A β α ≥ β ≥ γ ≥ ray P P Pt 0, 0, 0 − A n i P n i α 0 1 = � � � t � � 0 � � α + β + γ = 1 P + = ( P Pt n ) i A n i P n i 0 1 1 γ C Ray inside Triangle Ray inside Triangle Other primitives Other primitives = α + β + γ P A B C � Much early work in ray tracing focused on ray- B A β α ≥ β ≥ γ ≥ 0, 0, 0 primitive intersection tests α α + β + γ = P 1 � Cones, cylinders, ellipsoides γ � Boxes (especially useful for bounding boxes) C � General planar polygons − = β − + γ − P A ( B A ) ( C A ) � Many more ≤ β ≤ ≤ γ ≤ 0 1 , 0 1 β + γ ≤ � Consult chapter in Glassner (handed out) for more 1 details and possible extra credit Ray Scene Intersection Ray Scene Intersection Outline 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]

  5. Transformed Objects Transformed Objects 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 � Apply inverse transform M -1 to ray parameters after transformation) � 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] � Do standard ray-surface intersection as modified � But can also use original optimized routines � 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 -t n. Do all this before lighting Outline Outline Outline in Code 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++) for (int j = 0 ; j < width ; j++) { � Lighting calculations [2.5] Ray ray = RayThruPixel (cam, i, j) ; � Recursive ray tracing [2.6] Intersection hit = Intersect (ray, scene) ; image[i][j] = FindColor (hit) ; } return image ; } Shadows Shadows: Numerical Issues Light Source • 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: object in shadow Shadow ray to light is unblocked: object visible

Recommend


More recommend