COMP 175 COMPUTER GRAPHICS Lecture 09 Ray Casting

  • Slides: 47
Download presentation
COMP 175 | COMPUTER GRAPHICS Lecture 09: Ray Casting COMP 175: Computer Graphics March

COMP 175 | COMPUTER GRAPHICS Lecture 09: Ray Casting COMP 175: Computer Graphics March 10, 2020 Remco Chang 09 – Ray Casting 1/41

COMP 175 | COMPUTER GRAPHICS Coordinate Transform (Recap) � Remco Chang 09 – Ray

COMP 175 | COMPUTER GRAPHICS Coordinate Transform (Recap) � Remco Chang 09 – Ray Casting 2/41

COMP 175 | COMPUTER GRAPHICS Coordinate Transform (Recap) � Remco Chang 09 – Ray

COMP 175 | COMPUTER GRAPHICS Coordinate Transform (Recap) � Remco Chang 09 – Ray Casting 3/41

COMP 175 | COMPUTER GRAPHICS Coordinate Transform (Recap) � Remco Chang 09 – Ray

COMP 175 | COMPUTER GRAPHICS Coordinate Transform (Recap) � Remco Chang 09 – Ray Casting 4/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 5/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 5/41

COMP 175 | COMPUTER GRAPHICS Motivation �So far, you have learned to build a

COMP 175 | COMPUTER GRAPHICS Motivation �So far, you have learned to build a “passive” renderer � Input: vertices and geometric shapes � Output: an image �What happens when a user wants to manipulate the objects in the scene? � Say, a user clicks on an object on screen, how do you know: � A. which object the user clicked on? � B. where on the object the user clicked on? Remco Chang 09 – Ray Casting 6/41

COMP 175 | COMPUTER GRAPHICS Origin of Ray Casting / Ray Tracing � Albrecht

COMP 175 | COMPUTER GRAPHICS Origin of Ray Casting / Ray Tracing � Albrecht Dürer, 1525 � http: //www. youtube. com/watch? v=8 s 1 Lz. Ir. Wb. E 8 Durer: record string intersection from center of projection (eye) to the object as points on a 2 D plane. � Points created are perspective projection of 3 D object onto 2 D plane � Remco Chang 09 – Ray Casting 7/41

COMP 175 | COMPUTER GRAPHICS What is a Ray Tracer? � A ray tracer

COMP 175 | COMPUTER GRAPHICS What is a Ray Tracer? � A ray tracer is a mapping of rays from the camera (eye) through each pixel to the objects in the scene � Each pixel returns either: Ray intersection with the nearest object in the scene � No intersection � � Unlike Durer’s method, a typical ray tracer also takes into account of lighting (and material) information � To render a scene, a ray is cast from each pixel, and the returned value (color) is recorded. Remco Chang 09 – Ray Casting 8/41

COMP 175 | COMPUTER GRAPHICS Ray Tracing Fundamentals �There are three parts to a

COMP 175 | COMPUTER GRAPHICS Ray Tracing Fundamentals �There are three parts to a ray tracer: 1. Generating rays � 2. Ray-Object intersection � � 3. Shoot a ray from the camera’s eye point to each pixel on the film plane For each ray, compute the intersection with each object in the scene Record the closest intersection point to the eye (if one exists) Calculate lighting (i. e. color) � � For each ray, use illumination model to determine direct contribution from light sources (For recursive ray tracer), recursively generate secondary rays that might contribute to the color of the pixel (e. g. , reflective surfaces or mirrors need to reflect other objects in the scene) Remco Chang 09 – Ray Casting 9/41

COMP 175 | COMPUTER GRAPHICS Ray Tracing vs. Scan Conversion �How is ray tracing

COMP 175 | COMPUTER GRAPHICS Ray Tracing vs. Scan Conversion �How is ray tracing different from scan conversion? � Shapes and Scene. View both use scan conversion to render objects in a scene and follow the pseudocode: for each object in the scene for each triangle in the object pass vertex geometry and colors to Open. GL tell Open. GL to render/map the triangle onto screen Remco Chang 09 – Ray Casting 10/41

COMP 175 | COMPUTER GRAPHICS Ray Tracing vs. Scan Conversion � Ray tracing uses

COMP 175 | COMPUTER GRAPHICS Ray Tracing vs. Scan Conversion � Ray tracing uses the following pseudocode: for each sample (pixel) on the film plane 1. determine the closest object in the scene hit by a ray through that pixel 2. set the color based on the calculation of the illumination model for the intersected object � Note the distinctions: � Ray tracing iterates over PIXELS � Scan conversion iterates over VERTICES Remco Chang 09 – Ray Casting 11/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 12/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 12/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World Coord) � Let’s start by looking at the geometry in world space � Ray Origin � We’ll start the ray from the camera’s position in world space (eye point). Call that point P � Shoot a ray from P towards a point on the film plane (which can be thought of as the UV plane in the camera’s UVN (or UVW) space). Call this directional vector d. � In parametric form, any point along the ray can be described using this form: P + td , where: P is the ray’s origin in world space (eye point) � d is the unit vector in the direction of the ray � t is a non-negative real value � Remco Chang 09 – Ray Casting 13/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World Coord) �We know the value of the eye point, but how can we calculate our directional vector(s) d ? �To find d we just subtract the eye point P from the “look. At” point (Q): � d = normalize(Q - P) �When Q is on the camera’s Q look vector, then Q = P + near. Plane * look. V Remco Chang 09 – Ray Casting 14/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World Coord) � Given that we have the (world space) point Q that is intersected by the camera’s look vector, how can we find this other point S? Q S � S = Q + aû + bv � How do we find a and b (a, b) S Q Remco Chang 09 – Ray Casting 15/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World Coord) (a, b) S Q � Remco Chang 09 – Ray Casting 16/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World Coord) (a, b) S Q � Remco Chang 09 – Ray Casting 17/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 1 – World Coord) � Remco Chang 09 – Ray Casting 18/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 19/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 19/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera Coord) � The second approach to computing a ray is to think of the ray in camera space. � In this coordinate space, the camera’s position is at the origin (0, 0, 0), and the look vector is always <0, 0, -1> � The four corners of the far plane have the coordinates: � (-1, 1, -1), (-1, -1), (1, -1) canonical view volume (camera space) Any plane z = k, -1<= k < 0 can be the film plane Remco Chang 09 – Ray Casting 20/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera Coord) � Remco Chang 09 – Ray Casting 21/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera Coord) �Remember that we need to apply this inverse matrix to the two points that make up the ray � The eye point P � The camera-space look. At point on the far plane �Don’t forget to normalize the resulting ray vector Remco Chang 09 – Ray Casting 22/41

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera

COMP 175 | COMPUTER GRAPHICS Step 1: Generating a Ray (Solution 2 – Camera Coord) � In Summary: � Start the ray at center of projection (world space “eye point”) � � Map 2 D integer screen-space point (x, y) onto 3 D film plane � � scale x, y to fit between -1 and 1 set z to -1 so points lie on the far clip plane film plane = far clip plane when z = -1 Transform 3 D film plane point (mapped pixel) to a world-space point � � You might think of this as the (0, 0, 0) point transformed by the inverse camera matrix. need to apply the inverse scaling, rotation, and translation components of camera’s transformation matrix to move the camera-space pixel point to world space. Construct the direction vector d � d = normalize(world-space film-plane point (mapped pixel) – (world space eye point) Remco Chang 09 – Ray Casting 23/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 24/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 24/41

COMP 175 | COMPUTER GRAPHICS Ray-Object Intersection � How do we determine if (and

COMP 175 | COMPUTER GRAPHICS Ray-Object Intersection � How do we determine if (and where) two mathematical objects intersect? � For example, how do we know where two lines intersect? � y = 3 x + 1 � y = 2 x + 4 � In concept, the two objects intersect where their values are the same. In this case, we can say � 3 x + 1 = 2 x +4 � x = 3 � put back into either equation, we get y = 10 � So the two lines intersect at (3, 10) Remco Chang 09 – Ray Casting 25/41

COMP 175 | COMPUTER GRAPHICS Ray-Object Intersection �Intersection between a ray and a 3

COMP 175 | COMPUTER GRAPHICS Ray-Object Intersection �Intersection between a ray and a 3 d geometric object is the same. � We know the mathematical definitions for sphere, cylinder, cube, and cone. � We also know the mathematical definition for a ray � This is from your Assignment 1 -- more on this later �However, in our scene graph, our 3 d object can be translated, rotated, and/or scaled… � It’s a pain when the mathematical definition of the object is different for every object in the scene! Remco Chang 09 – Ray Casting 26/41

COMP 175 | COMPUTER GRAPHICS “Fixing the Frame of Reference” � For example, in

COMP 175 | COMPUTER GRAPHICS “Fixing the Frame of Reference” � For example, in the original problem with 2 lines, we might be able to “fix” one of the equation so that it’s always easily solvable. For instance, let’s say that we want one of the equations to always be: � � Then, in our original equations � � � Subtract 1 from both equations (shift both lines down by 1) For both equations, divide both sides by 3 x Subtract 1 from both equations This results in: � � � y = 3 x + 1 y = 2 x + 4 We would then: � � y = 0 y/x = -1/3 + 1/x Now we can solve for x Remco Chang 09 – Ray Casting 27/41

COMP 175 | COMPUTER GRAPHICS In 3 D �Our trick is basically the same

COMP 175 | COMPUTER GRAPHICS In 3 D �Our trick is basically the same – we want to solve intersection for each “Shape” once. We do this in object space where: � The object is centered at (0, 0, 0) � The object is not rotated � The object is bounded by -0. 5 and 0. 5 in x, y, and z directions �This means that we need to transform the ray from world space into object space Remco Chang 09 – Ray Casting 28/41

COMP 175 | COMPUTER GRAPHICS Example �In Assignment 3 (Scene. View), after “flattening” the

COMP 175 | COMPUTER GRAPHICS Example �In Assignment 3 (Scene. View), after “flattening” the scene graph hierarchy, each object is associated with a single 4 x 4 transform matrix, M � This matrix transforms the object from object space, where it lives within -0. 5 to 0. 5 in each direction, to world space, where it is scaled, rotated, and translated to its position in the scene �To transform our ray from world space to object space, we need to invert M. Remco Chang 09 – Ray Casting 29/41

COMP 175 | COMPUTER GRAPHICS Ray from World to Object Coordinate � Remco Chang

COMP 175 | COMPUTER GRAPHICS Ray from World to Object Coordinate � Remco Chang 09 – Ray Casting 30/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 31/41

COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 09 – Ray Casting 31/41

COMP 175 | COMPUTER GRAPHICS Intersection in Object Space � Remco Chang 09 –

COMP 175 | COMPUTER GRAPHICS Intersection in Object Space � Remco Chang 09 – Ray Casting 32/41

COMP 175 | COMPUTER GRAPHICS Example: Sphere � Remco Chang 09 – Ray Casting

COMP 175 | COMPUTER GRAPHICS Example: Sphere � Remco Chang 09 – Ray Casting 33/41

COMP 175 | COMPUTER GRAPHICS Ray – Sphere Intersection � Remco Chang 09 –

COMP 175 | COMPUTER GRAPHICS Ray – Sphere Intersection � Remco Chang 09 – Ray Casting 34/41

COMP 175 | COMPUTER GRAPHICS Ray – Sphere Intersection � Remco Chang 09 –

COMP 175 | COMPUTER GRAPHICS Ray – Sphere Intersection � Remco Chang 09 – Ray Casting 35/41

COMP 175 | COMPUTER GRAPHICS Algebra Reminder � Remco Chang 09 – Ray Casting

COMP 175 | COMPUTER GRAPHICS Algebra Reminder � Remco Chang 09 – Ray Casting 36/41

COMP 175 | COMPUTER GRAPHICS Finding the Intersection Point � Remco Chang 09 –

COMP 175 | COMPUTER GRAPHICS Finding the Intersection Point � Remco Chang 09 – Ray Casting 37/41

COMP 175 | COMPUTER GRAPHICS For All Other Geometric Shapes � Remco Chang 09

COMP 175 | COMPUTER GRAPHICS For All Other Geometric Shapes � Remco Chang 09 – Ray Casting 38/41

COMP 175 | COMPUTER GRAPHICS Summary P = world space eye. Point for each

COMP 175 | COMPUTER GRAPHICS Summary P = world space eye. Point for each pixel of image Compute the world space ray, d for each object 1) convert P and d into object space 2) intersect ray P+td with object and find closest intersection point 3) convert the point to world space and determine its distance to eye point (in world space) Select the nearest object Compute normal at that point in object space Transform normal to world space Use world space normal for lighting computations Remco Chang 09 – Ray Casting 39/41

COMP 175 | COMPUTER GRAPHICS Note about Ray � An important point about transforming

COMP 175 | COMPUTER GRAPHICS Note about Ray � An important point about transforming Ray from world space to object space: � Normalize the Ray after it’s created in world space. � However, do NOT normalize Ray after the transform into object space!! � Reason: � � If you normalize, then the value t you find will be in the object space. When there are many objects, it’s hard to figure out how to compare all the different t values across different object spaces If you do not normalize, then the value t will apply in the world space. That means that you can compare the t values derived from the different objects in the same coordinate. � Think about it a little bit… This is the only time in this class where you should not normalize a vector. Remco Chang 09 – Ray Casting 40/41

COMP 175 | COMPUTER GRAPHICS Intersection with a Triangle �If you would like to

COMP 175 | COMPUTER GRAPHICS Intersection with a Triangle �If you would like to render a mesh (made up of triangles), you would need to do Ray-Triangle intersection �Ray – Triangle intersection is basically the same as Ray – Plane intersection, except that you need to check if the intersection point is within the triangle Remco Chang 09 – Ray Casting 41/41

COMP 175 | COMPUTER GRAPHICS Inside / Outside Test � Remco Chang 09 –

COMP 175 | COMPUTER GRAPHICS Inside / Outside Test � Remco Chang 09 – Ray Casting 42/41

COMP 175 | COMPUTER GRAPHICS Inside Outside Test � Remco Chang 09 – Ray

COMP 175 | COMPUTER GRAPHICS Inside Outside Test � Remco Chang 09 – Ray Casting 43/41

COMP 175 | COMPUTER GRAPHICS Barycentric Coordinate � Remco Chang 09 – Ray Casting

COMP 175 | COMPUTER GRAPHICS Barycentric Coordinate � Remco Chang 09 – Ray Casting 44/41

COMP 175 | COMPUTER GRAPHICS Computing the Barycentric Coordinate � Remco Chang 09 –

COMP 175 | COMPUTER GRAPHICS Computing the Barycentric Coordinate � Remco Chang 09 – Ray Casting 45/41

COMP 175 | COMPUTER GRAPHICS Computing Area �Recall that the area of a parallelogram

COMP 175 | COMPUTER GRAPHICS Computing Area �Recall that the area of a parallelogram made up of two vectors is the same as the length of their cross product Remco Chang 09 – Ray Casting 46/41

COMP 175 | COMPUTER GRAPHICS Finally � Remco Chang 09 – Ray Casting 47/41

COMP 175 | COMPUTER GRAPHICS Finally � Remco Chang 09 – Ray Casting 47/41