COMP 175 COMPUTER GRAPHICS. Ray Casting. COMP 175: Computer Graphics April 26, Erik Anderson 09 Ray Casting

Similar documents
Ray Tracer I: Ray Casting Due date: 12:00pm December 3, 2001

Raycasting. Chapter Raycasting foundations. When you look at an object, like the ball in the picture to the left, what do

Ray Tracing Basics I. Computer Graphics as Virtual Photography. camera (captures light) real scene. photo. Photographic print. Photography: processing

INTRODUCTION TO COMPUTER GRAPHICS. It looks like a matrix Sort of. Viewing III. Projection in Practice. Bin Sheng 10/11/ / 52

Ray tracing. Computer Graphics COMP 770 (236) Spring Instructor: Brandon Lloyd 3/19/07 1

Assignment 6: Ray Tracing

CS 563 Advanced Topics in Computer Graphics Lecture 2: Bare-Bones Raytracer. by Emmanuel Agu

CS354 Computer Graphics Ray Tracing. Qixing Huang Januray 24th 2017

Lecture 8 Ray tracing Part 1: Basic concept Yong-Jin Liu.

INTRODUCTION TO COMPUTER GRAPHICS. cs123. It looks like a matrix Sort of. Viewing III. Projection in Practice 1 / 52

1999, Denis Zorin. Ray tracing

So far, we have considered only local models of illumination; they only account for incident light coming directly from the light sources.

THE VIEWING TRANSFORMATION

Basics of Computational Geometry

Ray Tracing Part 1. CSC418/2504 Introduction to Computer Graphics. TA: Muhammed Anwar & Kevin Gibson

Motivation. Sampling and Reconstruction of Visual Appearance. Effects needed for Realism. Ray Tracing. Outline

Geometric Queries for Ray Tracing

Ray Tracer Due date: April 27, 2011

2D/3D Geometric Transformations and Scene Graphs

Computer Graphics and Image Processing Ray Tracing I

3D Rendering and Ray Casting

3D Rendering and Ray Casting

Topics and things to know about them:

COMP 175 COMPUTER GRAPHICS. Lecture 11: Recursive Ray Tracer. COMP 175: Computer Graphics April 9, Erik Anderson 11 Recursive Ray Tracer

CSE528 Computer Graphics: Theory, Algorithms, and Applications

Computer Graphics Ray Casting. Matthias Teschner

CSE328 Fundamentals of Computer Graphics

Ray Tracing. Outline. Ray Tracing: History

Last Time? Ray Casting. Administrivia: Lab & Office Hours. Notes on Assignments. Ray Casting. Overview of Today

CS Computer Graphics: Introduction to Ray Tracing

CS Computer Graphics: Introduction to Ray Tracing

Game Architecture. 2/19/16: Rasterization

Ray Tracing I. History

Photorealism: Ray Tracing

8.1 Geometric Queries for Ray Tracing

Movie: Geri s Game. Announcements. Ray Casting 2. Programming 2 Recap. Programming 3 Info Test data for part 1 (Lines) is available

CS 130 Exam I. Fall 2015

LECTURE 7. Announcements

Effects needed for Realism. Ray Tracing. Ray Tracing: History. Outline. Foundations of Computer Graphics (Spring 2012)

CS 130 Exam I. Fall 2015

Lecture 11. More Ray Casting/Tracing

Math background. 2D Geometric Transformations. Implicit representations. Explicit representations. Read: CS 4620 Lecture 6

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

Ray Casting. Outline. Similar to glulookat derivation. Foundations of Computer Graphics

Notes on Assignment. Notes on Assignment. Notes on Assignment. Notes on Assignment

CS 4620 Midterm 1. Tuesday 22 October minutes

Ray Tracing. Foley & Van Dam, Chapters 15 and 16

Ray Tracing Foley & Van Dam, Chapters 15 and 16

Ray Tracing. Rendering and Techniques CS337 INTRODUCTION TO COMPUTER GRAPHICS. 1 of 50. Bin Sheng October 25, 2016

Perspective Mappings. Contents

Polar Coordinates. 2, π and ( )

Viewing with Computers (OpenGL)

Introduction Ray tracing basics Advanced topics (shading) Advanced topics (geometry) Graphics 2010/2011, 4th quarter. Lecture 11: Ray tracing

Viewing and Ray Tracing. CS 4620 Lecture 4

Ray Tracing I. Internet Raytracing Competition

CS 4204 Computer Graphics

Viewing and Ray Tracing

Problem Set 4 Part 1 CMSC 427 Distributed: Thursday, November 1, 2007 Due: Tuesday, November 20, 2007

CS 4620 Midterm, March 21, 2017

Lecture 4. Viewing, Projection and Viewport Transformations

Homework #2. Hidden Surfaces, Projections, Shading and Texture, Ray Tracing, and Parametric Curves

Geometric optics. The University of Texas at Austin CS384G Computer Graphics Don Fussell

Overview. By end of the week:

Lecture 11: Ray tracing (cont.)

Questions??? Announcements Assignment 3 due today

COSC579: Scene Geometry. Jeremy Bolton, PhD Assistant Teaching Professor

Institutionen för systemteknik

Ray Tracing. Last Time? Reading for Today. Reading for Today

Transforms. COMP 575/770 Spring 2013

LECTURE 6. Announcements

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S. Raytracing Basics. rendered in a matter of seconds with Travis Fischer s 09 raytracer

Movie: For The Birds. Announcements. Ray Tracing 1. Programming 2 Recap. Programming 3 Info Test data for part 1 (Lines) is available

2D rendering takes a photo of the 2D scene with a virtual camera that selects an axis aligned rectangle from the scene. The photograph is placed into

Visualisation Pipeline : The Virtual Camera

3D Viewing. CS 4620 Lecture 8

Ray casting. Ray casting/ray tracing

Lecture 3 Sections 2.2, 4.4. Mon, Aug 31, 2009

Revision Problems for Examination 2 in Algebra 1

Today. Rendering algorithms. Rendering algorithms. Images. Images. Rendering Algorithms. Course overview Organization Introduction to ray tracing

Geometric Transformations

Reading. Ray Tracing. Eye vs. light ray tracing. Geometric optics. Required: Watt, sections , (handout) Further reading:

PITSCO Math Individualized Prescriptive Lessons (IPLs)

CS 130 Final. Fall 2015

Intersecting Simple Surfaces. Dr. Scott Schaefer

Ray Casting. Connelly Barnes CS 4810: Graphics

To be a grade 1 I need to

Camera Placement for Ray Tracing

Introduction to Homogeneous coordinates

A Little Background. Motivation. Motivation

3D Viewing Episode 2

INFOGR Computer Graphics. Jacco Bikker - April-July Lecture 3: Ray Tracing (Introduction) Welcome!

Ray Tracing. CS 4620 Lecture 5

CPSC GLOBAL ILLUMINATION

Intro to Ray-Tracing & Ray-Surface Acceleration

Therefore, after becoming familiar with the Matrix Method, you will be able to solve a system of two linear equations in four different ways.

CMSC427 Final Practice v2 Fall 2017

AQA GCSE Maths - Higher Self-Assessment Checklist

6.837 Introduction to Computer Graphics Final Exam Tuesday, December 20, :05-12pm Two hand-written sheet of notes (4 pages) allowed 1 SSD [ /17]

SUMMARY. CS380: Introduction to Computer Graphics Ray tracing Chapter 20. Min H. Kim KAIST School of Computing 18/05/29. Modeling

B.Stat / B.Math. Entrance Examination 2017

Transcription:

Ray Casting COMP 175: Computer Graphics April 26, 2018 1/41

Admin } Assignment 4 posted } Picking new partners today for rest of the assignments } Demo in the works } Mac demo may require a new dylib I will provide } Lab07 posted } Physics Equation Cheat Sheet posted } Any Questions about the schedule? 2/41

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? 3/41

Origin of Ray Casting / Ray Tracing } Albrecht Dürer, 1525 } http://www.youtube.com/watch?v=8s1lzirwbe8 } Durer: record string intersection from center of projection (eye) to the object as points on a 2D plane. } Points created are perspective projection of 3D object onto 2D plane 4/41

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. 5/41

Ray Tracing Fundamentals } There are three parts to a ray tracer: 1. Generating rays } Shoot a ray from the eye through a sample point on the film plane that corresponds to a pixel } Repeat the process for each pixel 2. Ray-Object intersection } For each object in the scene, compute their intersection point (if one exists) } Record the closest intersection point to the eye 3. Calculate lighting (i.e. color) } 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) 6/41

Ray Tracing vs. Scan Conversion } How is ray tracing different from scan conversion? } Shapes and SceneView 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 OpenGL tell OpenGL to render/map the triangle onto screen 7/41

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 8/41

Step 1: Generating a Ray (Solution 1 World Coord) } Ray Origin } Let s look at the geometry in the world space coordinate } We ll start the ray from the camera s position (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 (eye point) } d is the unit vector in the direction of the ray } t is a non-negative real value 9/41

Step 1: Generating a Ray (Solution 1 World Coord) } We know the value of the eye point, but what about the look direction? } Consider the simple case: } Finding the look direction means finding the look at point, call it Q, then the ray direction is just: d = normalize(q-p) } Recall that P is the eye point } If Q is right on the look vector, that s pretty straight forward: Q = P + nearplane * lookv Q 10/41

Step 1: Generating a Ray (Solution 1 World Coord) } Now that we have the point Q, can we find this other point S? } S = Q + au + bv Q S } How do we find a and b? (a, b) Q S 11/41

Step 1: Generating a Ray (Solution 1 World Coord) (a, b) Q S } First, we need to find the width (W) and height (H) of the film plane in the world coordinates: } If we place the film plane on the near clipping plane, let s say that the plane extends from W to W in the u direction, and H to H in the v direction } For a view angle of θ, and the aspect ratio of (w/h) } H = N tan (θ/2) and W = H (w/h) 12/41

Step 1: Generating a Ray (Solution 1 World Coord) (a, b) Q S } Given H and W, finding a and b is pretty easy: } a = W + 2W 4 56789, for c = 0, 1,, ncols 1 } b = H + 2H H 5I7J9, for r = 0, 1,, nrows 1 13/41

Step 1: Generating a Ray (Solution 1 World Coord) } The origin of a ray is at the eye point P } The direction of the ray is S P, where S is a 3D point on the film plane. } S has the form of Q + au + bv } Q is P + near*lookv } a = W + 2W 4 56789 } b = H + 2H H 5I7J9 14/41

Questions? 15/41

Step 1: Generating a Ray (Solution 2 Camera Coord) } The second approach to computing a ray is to think of the ray in the camera s own coordinate system. } In this system, the camera s position is at the origin (0, 0, 0), and the look vector is always z (0, 0, -1) } The four corners of the far plane have the coordinates of (-1, 1, -1), (1, 1, -1), (-1, -1, -1), (1, -1, -1) canonical view volume Any plane z = k, -1<= k < 0 can be the film plane 16/41

Step 1: Generating a Ray (Solution 2 Camera Coord) } In this coordinate system, finding the lookat point is pretty easy. The x value ranges from [-1, 1], and the y value ranges from [-1, 1]. The z value is always -1. } So the lookat point will have the form of (-1+2/nCols, - 1+2/nRows, -1) } Once we have the lookat point, we need to apply the inverse of the camera s normalization matrices. } Recall that in building a synthetic camera, we map the camera in world coordinate into the camera s coordinate space. The steps are: } Translate, Rotate, Scale, Unhinge (M MM S OPQ R RSJTOPQ T RSJ ) } Here, we do not need to do the Unhinge step, but we do need to invert the first three parts: } UnScale, UnRotate, UnTranslate (T RSJ VW VW R RSJTOPQ S VW OPQ ) 17/41

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 point on the far plane } Don t forget to normalize the resulting ray vector 18/41

Step 1: Generating a Ray (Solution 2 Camera Coord) } In Summary: } Start the ray at center of projection ( eye point ) } Map 2D integer screen-space point (x, y) onto 3D 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 3D film plane point (mapped pixel) to an untransformed world-space point } need to undo normalizing transformation } Construct the direction vector } point minus point is a vector } direction = (world-space point (mapped pixel)) (eye point (in world space)) 19/41

Questions? 20/41

Ray-Object Intersections } How do we determine if (and where) two mathematical objects intersect? } For example, how do we know where two lines intersect? } y = 3x + 1 } y = 2x + 4 } In concept, the two objects intersect where their values are the same. In this case, we can say } 3x + 1 = 2x +4 } x = 3 } put back into either equation, we get y = 10 } So the two lines intersect at (3, 10) 21/41

Ray-Object Intersection } Intersection between a ray and a 3d 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 3d object can be: translated, rotated, and scaled } It s a pain when the mathematical definition of the object is different for every object in the scene! 22/41

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 equation to always be: } y = 0 } Then, in our original equations } y = 3x + 1 } y = 2x + 4 } We would then: } Subtract 1 from both equations (shift both lines down by 1) } For both equations, divide both sides by 3x } Subtract 1 from both equations } This results in: } y/x = 0 } y/x = -1/3 + 1/x } Now we can solve for x 23/41

In 3D } Our trick is basically the same we want to solve intersection for each Shape once, and we do this in the Object Coordinate Space where: } The object is always centered at (0, 0, 0) } The object is not rotated } The object is bounded within -0.5 to 0.5 in x, y, and z directions } This means that we need to transform the ray from World Coordinate Space into Object Coordinate Space 24/41

Example } In Assignment 3 (SceneView), after flattening the scene graph hierarchy, each object is associated with a single 4x4 transform matrix, M } This matrix is an Object to World transform (transforms from the object space to the world space) } If this is confusing, think about how the point (0, 0, 0) gets transformed after multiplying by this matrix This implies that the origin in object coordinate space gets mapped to the a point in the world coordinate } Since our ray is in the World Coordinate Space, we need to invert M such that the matrix goes from World to Object coordinates 25/41

Ray from World to Object Coordinate } Recall that our ray is defined by two components: } The eye point P } The direction dy } This means that converting the Ray from World Coordinate into Object Coordinate requires two transforms: } P 7[\]4^ = M VW P J7H8_ } dy 7[\]4^ = M VW dy J7H8_ 26/41

Questions? 27/41

Intersection in Object Space } Now that we have the Ray in the Object Coordinate: } P ]P]7[\]4^ + tdy 7[\]4^ } Given a t value, this equation describes a 3D point in the object coordinate space: } P 7[\]4^ = P ]P]7[\]4^ + tdy 7[\]4^ } Then we can describe our 3d geometric objects in the same way that we did in Assignment 1, in that our equation can assume: } Object is at 0, 0, 0 (no translation) } Object is not deformed in some way (no scaling) } Object is axis-aligned (no rotation. Think cube) 28/41

Example: Sphere } The equation for a 3D sphere is: } (x x 4 ) T +(y y 4 ) T +(z z 4 ) T = r T } Where r is the radius of the sphere } x 4, y 4, and z 4 represent the coordinate of the center of the sphere } Using vector notation, we can rewrite this as: } P 9 P 4 T = r T } Where P 9 is any given point on the surface of the sphere } P 4 is the origin of the sphere } The double bar means to take the length of the vector 29/41

Ray Sphere Intersection } Recall that to find intersection between two mathematical objects, we just have to set the two equations to be equal } Our two equations are: } Ray: P 9 = P ]P] + tdy (note, this is in object space) } Sphere: P 9 P 4 T = r T } Substitute P 9 into the Sphere equation: } P ]P] + tdy P 4 T = r T } We can drop P 4 because it is (0, 0, 0) in the object space } Recall that the (square of the) length of a vector is the same as the dot product of the vector onto itself } If this is unclear, think about what the dot product is } Now we can rewrite the equation as } P ]P] + tdy d P ]P] + tdy = r T 30/41

Ray Sphere Intersection } From the last slide: } P ]P] + tdy d P ]P] + tdy = r T } Multiply it out, we get } P ]P] d P ]P] + 2 P ]P] d tdy + t T dy d dy r T = 0, or } t T dy d dy + t 2 P ]P] d dy + P ]P] d P ]P] r T = 0 } This should look familiar as this is a simple quadratic equation Ax T + Bx + C = 0. In our case: } A = dy d dy } B = 2 P ]P] d dy } C = P ]P] d P ]P] r T } Our goal is to solve for t: } t = Vg± gi Vjk6 Tk 31/41

Algebra Reminder } There are a few things about this equation that should be familiar to you: } The discriminate (B T 4AC) should be positive, otherwise there is no real solution } There are two solutions (because of the + and -) } Both properties are meaningful in our solution: } If the discriminate is negative, there is no solution } In this case, the ray misses the sphere there is no intersection } If the discriminate is 0, we have one solution } In this case, the ray grazes the sphere (and ray becomes a tangent to the sphere) } If the discriminate is positive, we have two solutions } The two solutions represent the two points where the Ray intersect a Sphere (i.e., the front and back side of the sphere) 32/41

Finding the Intersection Point } Assuming that the discriminate is positive (that is, you have two solutions t0 and t1) } You should return the min(t0, t1) because the smaller value represents the intersection point that is closer to the eye point } Plug the smaller t value back into the Ray equation: } P 9 = P ]P] + tdy } And you have your intersection point (in object coordinate space) } To find this point in the World Coordinate Space, multiple Ps by the object s transform matrix M. } P 9 J7H8_ = M P 97[\]^ 33/41

For All Other Geometric Shapes } Cube: } Perform Ray Plane intersection 6 times, check to make sure that the intersection point occurs within the bounds (-0.5, 0.5) } Equation for a Plane is: P P 7Hnon5 d np = 0 } Where P is any point on the plane, P 7Hnon5 is the center point of the plane, and n is the normal } Sphere: } Discussed before } Cylinder: } Perform Ray Plane intersection for the two caps, check to make sure the intersection point occurs within the circle } Perform Ray Cylinder intersection } Equation for an (infinite) cylinder is: x T + z T = r T. Check for bounds such that y is between (-0.5, 0.5) } Cone } Perform the cap (same as cylinder) } Perform Ray Cone intersection } Equation for an (infinite) cone is: x T + z T = apex y T r T /height T. Check for bounds such that y is between (-0.5, 0.5) } Note that in our case, height of the cone is equal to 1, apex is the position of the pin point of the cone (0, apex, 0), which is 0.5 34/41

Summary P = eyepoint for each pixel of image Compute the 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 coordinate and determine its distance to eye point (in world coordinate) Select the nearest object Compute normal at that point in object coordinate Transform normal to world space Use world space normal for lighting computations 35/41

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. 36/41

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 37/41

Inside / Outside Test } Consider the Cross Product Between (B-A) and (Q-A) } If Q is inside vs. outside of the triangle, the two cross products will have different signs } More formally, we can compare this cross product with the normal of this triangle, n, such that if Q is inside of the triangle, then: } B A Q A d n 0 38/41

Inside Outside Test } To make sure that this is true for all 3 sides, we need to test all three conditions: } B A Q A d n 0 } C B Q B d n 0 } A C Q C d n 0 } If all three are satisfied, we know that Q is inside the triangle ABC 39/41

Barycentric Coordinate } The test gave us an inside / outside test, but it doesn t say exactly where the intersection occurs. To determine the intersection point, we need } For a point Q inside a triangle (ABC), the point Q can be described as: } Q = α A + β B + θ C } If Q is inside the triangle, then: } α 0 } β 0 } θ 0 } θ = 1 α β 40/41

Computing the Barycentric Coordinate } It turns out that one way to compute the barycentric coordinate of a point is to compare the sizes of the triangles. } Q = α A + β B + θ C } In particular: } α = kh]z({g6) kh]z(kg6) } β = kh]z(k{6) kh]z(kg6) } θ = kh]z(kg{) kh]z(kg6) 41/41

Computing Area } Recall that the area of a parallelogram made up of two vectors is the same as the length of their cross product } So the area of the triangle ABC would be: } Area ABC = gvk 6Vk T } We can further optimize the computation of the length of the cross product by dotting it with the normal vector: } Area ABC = B A C A d n } (see http://www.scratchapixel.com/old/lessons/3d-basic-lessons/lesson-9- ray-triangle-intersection/barycentric-coordinates/ for more detail of the proof) 42/41

Finally } Assuming that Q is inside the triangle ABC, we can write it all as: } Area kg6 = B A C A d n } Area {g6 = C B Q B d n } Area k{6 = A C Q C d n } Area kg{ = B A Q A d n } Which means: } α = kh]z }~ kh]z }~ } β = kh]z ~ kh]z }~ } θ = kh]z } kh]z }~ 43/41