Heckbert Heckbert’ ’s s Business Card Ray Tracer Business Card Ray Tracer Computer Graphics (Fall 2008) Computer Graphics (Fall 2008) COMS 4160, Lectures 16, 17: Nuts and bolts of Ray Tracing Ravi Ramamoorthi http://www.cs.columbia.edu/~cs4160 Acknowledgements: Thomas Funkhouser and Greg Humphreys 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 ; } Ray Casting Finding Ray Direction Finding 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 Virtual Viewpoint � 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], fov Virtual Screen Objects Multiple intersections: Use closest one (as does OpenGL) Ray misses all objects: Pixel colored black Ray intersects object: shade using color, lights, materials
Similar to gluLookAt gluLookAt derivation derivation Constructing a coordinate frame? Similar to Constructing a coordinate frame? � gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, We want to associate w with a , and v with b upy, upz) � But a and b are neither orthogonal nor unit norm � Camera at eye, looking at center, with up direction being up � And we also need to find u a Up vector = w a × b w = u Eye × b w = × v w u Center Slide 20 from 4160 lecture 2 From 4160 lecture 4 on deriving gluLookAt Camera coordinate frame Camera coordinate frame Canonical viewing geometry Canonical viewing geometry × a b w = = = × w u v w u × a b w � We want to position camera at origin, looking down –Z dirn β v α + β − u v w � Hence, vector a is given by eye – center = + ray eye α u -w α + β − u v w � The vector b is simply the up vector Up vector Eye − − fovx j ( width / 2) fovy ( height / 2) i α = tan × β = tan × Center 2 width / 2 2 height / 2 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 ; }
Ray- -Sphere Intersection Sphere Intersection Ray- -Sphere Intersection Sphere Intersection Ray Ray � � � � � � ≡ = + ≡ = + ray P P Pt ray P P Pt 0 1 0 1 � � � � � � � � ≡ − − − 2 = ≡ − − − 2 = sphere ( P C ) ( i P C ) r 0 sphere ( P C ) ( i P C ) r 0 Substitute � � � ≡ = + ray P P Pt 0 1 � � � � � � ≡ + − + − − 2 = sphere ( P Pt C ) ( i P Pt C ) r 0 0 1 0 1 C Simplify � � � � � � � � � 2 + − + − − − 2 = t ( P P i ) 2 t P i ( P C ) ( P C ) ( i P C ) r 0 P 0 1 1 1 0 0 0 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 � Intersection point: 1 1 1 0 0 0 0 1 Solve quadratic equations for t � Normal (for sphere, this is same as coordinates in sphere frame of reference, useful other tasks) � 2 real positive roots: pick smaller root � � − P C = normal � � � Both roots same: tangent to sphere − P C � One positive, one negative root: ray origin inside sphere (pick + root) � Complex roots: no intersection (check discriminant of equation first) Ray Ray- -Triangle Intersection Triangle Intersection Ray- Ray -Triangle Intersection Triangle Intersection � One approach: Ray-Plane intersection, then check if � One approach: Ray-Plane intersection, then check if inside triangle B inside triangle B A − × − A − × − ( C A ) ( B A ) ( C A ) ( B A ) = = n n � Plane equation: � Plane equation: − × − − × − ( C A ) ( B A ) ( C A ) ( B A ) � � � � � � � � ≡ − = ≡ − = plane P n i A n i 0 plane P n i A n i 0 � Combine with ray equation: C C � � � � � � � ≡ = + ray P P Pt − A n i P n i 0 1 � � � = � � 0 t � � + = P n i ( P Pt n ) i A n i 0 1 1
Ray inside Triangle Ray inside Triangle Ray inside Triangle Ray inside Triangle � Once intersect with plane, still need to find if in triangle = α + β + γ P A B C B A β α ≥ 0, β ≥ 0, γ ≥ 0 � Many possibilities for triangles, general polygons (point α α + β + γ = P 1 in polygon tests) γ � We find parametrically [barycentric coordinates]. Also useful for other applications (texture mapping) C P = α A + β B + γ C B − = β − + γ − P A ( B A ) ( C A ) A β α ≥ β ≥ γ ≥ 0, 0, 0 α ≤ β ≤ ≤ γ ≤ 0 1 , 0 1 α + β + γ = 1 P β + γ ≤ 1 γ C Other primitives Other primitives Ray Scene Intersection Ray Scene Intersection � 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 � Consult chapter in Glassner (handed out) for more details and possible extra credit Outline Outline Transformed Objects Transformed Objects � Camera Ray Casting (choosing ray directions) [2.3] � E.g. transform sphere into ellipsoid � Ray-object intersections [2.4] � Could develop routine to trace ellipsoid (compute parameters after transformation) � Ray-tracing transformed objects [2.4] � May be useful for triangles, since triangle after � Lighting calculations [2.5] transformation is still a triangle in any case � Recursive ray tracing [2.6] � But can also use original optimized routines
Transformed Objects Outline Transformed Objects Outline � Consider a general 4x4 transform M � Camera Ray Casting (choosing ray directions) [2.3] � Will need to implement matrix stacks like in OpenGL � Ray-object intersections [2.4] � Apply inverse transform M -1 to ray � Ray-tracing transformed objects [2.4] � Locations stored and transform in homogeneous coordinates � Vectors (ray directions) have homogeneous coordinate set to � Lighting calculations [2.5] 0 [so there is no action because of translations] � Recursive ray tracing [2.6] � 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 -t n. Do all this before lighting Shadows Outline in Code Outline in Code Light Source 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 ; Shadow ray to light is unblocked: object visible Shadow ray to light is blocked: object in shadow } Shadows: Numerical Issues Lighting Model Lighting Model • Numerical inaccuracy may cause intersection to be � Similar to OpenGL below surface (effect exaggerated in figure) � Lighting model parameters (global) • Causing surface to incorrectly shadow itself � Ambient r g b (no per-light ambient as in OpenGL) • Move a little towards light before shooting shadow ray � Attenuation const linear quadratic (like in OpenGL) L = L 0 + + const lin d * quad * d 2 � Per light model parameters � Directional light (direction, RGB parameters) � Point light (location, RGB parameters)
Recommend
More recommend