Ray Tracing.

Slides:



Advertisements
Similar presentations
GR2 Advanced Computer Graphics AGR
Advertisements

13.1 si31_2001 SI31 Advanced Computer Graphics AGR Lecture 13 An Introduction to Ray Tracing.
1 05/10/2014 Computer Graphics Lecture 10 Global Illumination 1: Ray Tracing and Radiosity Taku Komura.
Computer Graphics In4/MSc Computer Graphics Lecture Notes #15 Illumination III View Independent Rendering.
3D Graphics Rendering and Terrain Modeling
Ray Tracing & Radiosity Dr. Amy H. Zhang. Outline  Ray tracing  Radiosity.
Course Website: Computer Graphics 18: Ray-tracing.
Advanced Computer Graphics (Spring 2005) COMS 4162, Lecture 15: Ray Tracing/Global Illumination Ravi Ramamoorthi Slides.
CS6500 Adv. Computer Graphics © Chun-Fa Chang, Spring 2003 Object-Order vs. Screen-Order Rendering April 24, 2003.
Computer Graphics (Fall 2005) COMS 4160, Lecture 21: Ray Tracing
RAY TRACING.
1 Advanced Ray Tracing Mani Thomas CISC 440/640 Computer Graphics.
CSCE 641: Computer Graphics Ray Tracing Jinxiang Chai.
Parallelizing Raytracing Gillian Smith CMPE 220 February 19 th, 2008.
Global Illumination May 7, Global Effects translucent surface shadow multiple reflection.
Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 14: Ray Tracing
Ray Tracing Jerry Sui Adam Conner. Part I – Introduction to Ray Tracing Final Product.
1 7M836 Animation & Rendering Global illumination, ray tracing Arjan Kok
Ray Tracing CS Nov. 19, Turner Whitted.
CSCE 641: Computer Graphics Ray Tracing Jinxiang Chai.
Basic Ray Tracing CMSC 435/634. Visibility Problem Rendering: converting a model to an image Visibility: deciding which objects (or parts) will appear.
CSC418 Computer Graphics n Raytracing n Shadows n Global Illumination.
Ray Tracing Primer Ref: SIGGRAPH HyperGraphHyperGraph.
An Introduction to Ray Tracing CS /27/1998 Vic Baker.
Technology and Historical Overview. Introduction to 3d Computer Graphics  3D computer graphics is the science, study, and method of projecting a mathematical.
Project Raytracing. Content Goals Idea of Raytracing Ray Casting – Therory – Practice Raytracing – Theory – Light model – Practice Output images Conclusion.
Ray Tracing Sang Il Park SEjong University With lots of slides stolen from Jehee Lee, Doug James, Steve Seitz, Shree Nayar, Alexei Efros, Fredo Durand.
Ray Tracing Chapter CAP4730: Computational Structures in Computer Graphics.
Raytracing and Global Illumination Intro. to Computer Graphics, CS180, Fall 2008 UC Santa Barbara.
CS447/ Realistic Rendering -- Radiosity Methods-- Introduction to 2D and 3D Computer Graphics.
Ray Tracing Jian Huang, CS 594, Fall, 2002 This set of slides are used at Ohio State by Prof. Roger Crawfis.
University of Texas at Austin CS384G - Computer Graphics Fall 2008 Don Fussell Ray Tracing.
1 Dr. Scott Schaefer Ray Tracing. 2/42 Ray Tracing Provides rendering method with  Refraction/Transparent surfaces  Reflective surfaces  Shadows.
Basic Ray Tracing CMSC 435/634. Visibility Problem Rendering: converting a model to an image Visibility: deciding which objects (or parts) will appear.
1 Ray-Tracing ©Anthony Steed Overview n Recursive Ray Tracing n Shadow Feelers n Snell’s Law for Refraction n When to stop!
Recursion and Data Structures in Computer Graphics Ray Tracing 1.
CSE 681 DISTRIBUTED RAY TRACING some implementation notes.
Basic Ray Tracing CMSC 435/634.
CS380: Computer Graphics Distributed Ray Tracing TA Course URL:
Ray-tracing.
Ray Tracing Fall, Introduction Simple idea  Forward Mapping  Natural phenomenon infinite number of rays from light source to object to viewer.
CSCE 441: Computer Graphics Ray Tracing
COMPUTER GRAPHICS CS 482 – FALL 2015 SEPTEMBER 29, 2015 RENDERING RASTERIZATION RAY CASTING PROGRAMMABLE SHADERS.
1. Ray Casting Surface intersection Visible surface detection Ray Tracing Bounce the ray Collecting intensity Technique for global reflection and transmission.
More on Ray Tracing Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, April 14, 2004.
Global Illumination (3) Path Tracing. Overview Light Transport Notation Path Tracing Photon Mapping.
CSL 859: Advanced Computer Graphics Dept of Computer Sc. & Engg. IIT Delhi.
Distributed Ray Tracing. Can you get this with ray tracing?
CS552: Computer Graphics Lecture 36: Ray Tracing.
Basic Ray Tracing CMSC 435/634.
Advanced Computer Graphics
CSE 167 [Win 17], Lecture 15: Ray Tracing Ravi Ramamoorthi
Photorealistic Rendering vs. Interactive 3D Graphics
3D Graphics Rendering PPT By Ricardo Veguilla.
RAY TRACING.
Ray Tracer Project CSE 557.
(c) 2002 University of Wisconsin
Ray Tracing.
Visibility Computations
Distributed Ray Tracing
(c) 2002 University of Wisconsin
Image.
Monte Carlo Rendering Central theme is sampling:
CSCE 441: Computer Graphics Ray Tracing
Distributed Ray Tracing
GR2 Advanced Computer Graphics AGR
CS Advanced Computer Graphics
Ray Tracer Project CSE 557.
Ray Tracing Sung-Eui Yoon (윤성의) CS580: Course URL:
CSC418 Computer Graphics Raytracing Shadows Global Illumination.
Presentation transcript:

Ray Tracing

Ray Tracing What is ray tracing? Trace the path of a ray of light. Model its interactions with the scene. When a ray intersects an object, send off secondary rays (reflection, shadow, transmission) and determine how they interact with the scene.

Ray Tracing Capabilities Basic algorithm allows for: Hidden surface removal (like z-buffering) Multiple light sources Reflections Transparent refractions Hard shadows Extensions can achieve: Soft shadows Motion blur Blurred reflections (glossiness) Depth of field (finite apertures) Translucent refractions and more

Ray Tracing Produces realistic images Strengths: Weaknesses: Specular reflections Transparency Weaknesses: colour bleeding (diffuse reflections) Time consuming References: “An Improved Illumination Model for Shaded Display,” Turner Whitted, CACM, June 1980. “Distributed Ray Tracing,” Cook, Porter, and Carpenter, Computer Graphics, July 1984, pp. 137-145. Green, S.A., Paddon, D.J. Exploiting Coherence for “Multiprocessor Ray Tracing”. IEEE Computer Graphics Journal, 9,6, Nov 1989, pp 12-26.

Ray Traced Images

Ray Tracing “Backward” ray tracing: Traces the ray forward (in time) from the light source through potentially many scene interactions Problem: most rays will never even get close to the eye Very inefficient since it computes many rays that are never seen Light Eye Image plane

Ray Tracing “Forward” ray tracing: Traces the ray backward (in time) from the eye, through a point on the screen More efficient: computes only visible rays (since we start at eye) Generally, ray tracing refers to forward ray tracing. Light Eye Image plane

Ray Tracing Ray tracing is an image- precision algorithm: Visibility determined on a per-pixel basis Trace one (or more) rays per pixel Compute closest object (triangle, sphere, etc.) for each ray Produces realistic results Computationally expensive

Minimal Ray Tracer A basic (minimal) ray tracer is simple to implement: The code can even fit on a 3×5 card (code courtesy of Paul Heckbert): typedef struct{double x,y,z}vec;vec U,black,amb={.02,.02,.02};struct sphere{ vec cen,colour;double rad,kd,ks,kt,kl,ir}*s,*best,sph[]={0.,6.,.5,1.,1.,1.,.9, .05,.2,.85,0.,1.7,-1.,8.,-.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8, 1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,0.,.6,1.5,-3.,-3.,12.,.8,1., 1.,5.,0.,0.,0.,.5,1.5,};yx;double u,b,tmin,sqrt(),tan();double vdot(A,B)vec A ,B;{return A.x*B.x+A.y*B.y+A.z*B.z;}vec vcomb(a,A,B)double a;vec A,B;{B.x+=a* A.x;B.y+=a*A.y;B.z+=a*A.z;return B;}vec vunit(A)vec A;{return vcomb(1./sqrt( vdot(A,A)),A,black);}struct sphere*intersect(P,D)vec P,D;{best=0;tmin=1e30;s= sph+5;while(s-->sph)b=vdot(D,U=vcomb(-1.,P,s->cen)),u=b*b-vdot(U,U)+s->rad*s ->rad,u=u>0?sqrt(u):1e31,u=b-u>1e-7?b-u:b+u,tmin=u>=1e-7&&u<tmin?best=s,u: tmin;return best;}vec trace(level,P,D)vec P,D;{double d,eta,e;vec N,colour; struct sphere*s,*l;if(!level--)return black;if(s=intersect(P,D));else return amb;colour=amb;eta=s->ir;d= -vdot(D,N=vunit(vcomb(-1.,P=vcomb(tmin,D,P),s->cen )));if(d<0)N=vcomb(-1.,N,black),eta=1/eta,d= -d;l=sph+5;while(l-->sph)if((e=l ->kl*vdot(N,U=vunit(vcomb(-1.,P,l->cen))))>0&&intersect(P,U)==l)colour=vcomb(e ,l->colour,colour);U=s->colour;colour.x*=U.x;colour.y*=U.y;colour.z*=U.z;e=1-eta* eta*(1-d*d);return vcomb(s->kt,e>0?trace(level,P,vcomb(eta,D,vcomb(eta*d-sqrt (e),N,black))):black,vcomb(s->ks,trace(level,P,vcomb(2*d,N,D)),vcomb(s->kd, colour,vcomb(s->kl,U,black))));}main(){puts(“P3\n32 32\n255”);while(yx<32*32) U.x=yx%32-32/2,U.z=32/2-yx++/32,U.y=32/2/tan(25/114.5915590261),U=vcomb(255., trace(3,black,vunit(U)),black),printf("%.0f %.0f %.0f\n",U);}/*minray!*/

Minimal Ray Tracer This code implements: Multiple spheres (with different properties) Multiple levels of recursion: Reflections Transparency: Refraction One point light source: Hard shadows Hidden surface removal Phong illumination model typedef struct{double x,y,z}vec;vec U,black,amb={.02,.02,.02};struct sphere{ vec cen,colour;double rad,kd,ks,kt,kl,ir}*s,*best,sph[]={0.,6.,.5,1.,1.,1.,.9, .05,.2,.85,0.,1.7,-1.,8.,-.5,1.,.5,.2,1.,.7,.3,0.,.05,1.2,1.,8.,-.5,.1,.8,.8, 1.,.3,.7,0.,0.,1.2,3.,-6.,15.,1.,.8,1.,7.,0.,0.,0.,.6,1.5,-3.,-3.,12.,.8,1., 1.,5.,0.,0.,0.,.5,1.5,};yx;double u,b,tmin,sqrt(),tan();double vdot(A,B)vec A ,B;{return A.x*B.x+A.y*B.y+A.z*B.z;}vec vcomb(a,A,B)double a;vec A,B;{B.x+=a* A.x;B.y+=a*A.y;B.z+=a*A.z;return B;}vec vunit(A)vec A;{return vcomb(1./sqrt( vdot(A,A)),A,black);}struct sphere*intersect(P,D)vec P,D;{best=0;tmin=1e30;s= sph+5;while(s-->sph)b=vdot(D,U=vcomb(-1.,P,s->cen)),u=b*b-vdot(U,U)+s->rad*s ->rad,u=u>0?sqrt(u):1e31,u=b-u>1e-7?b-u:b+u,tmin=u>=1e-7&&u<tmin?best=s,u: tmin;return best;}vec trace(level,P,D)vec P,D;{double d,eta,e;vec N,colour; struct sphere*s,*l;if(!level--)return black;if(s=intersect(P,D));else return amb;colour=amb;eta=s->ir;d= -vdot(D,N=vunit(vcomb(-1.,P=vcomb(tmin,D,P),s->cen )));if(d<0)N=vcomb(-1.,N,black),eta=1/eta,d= -d;l=sph+5;while(l-->sph)if((e=l ->kl*vdot(N,U=vunit(vcomb(-1.,P,l->cen))))>0&&intersect(P,U)==l)colour=vcomb(e ,l->colour,colour);U=s->colour;colour.x*=U.x;colour.y*=U.y;colour.z*=U.z;e=1-eta* eta*(1-d*d);return vcomb(s->kt,e>0?trace(level,P,vcomb(eta,D,vcomb(eta*d-sqrt (e),N,black))):black,vcomb(s->ks,trace(level,P,vcomb(2*d,N,D)),vcomb(s->kd, colour,vcomb(s->kl,U,black))));}main(){puts(“P3\n32 32\n255”);while(yx<32*32) U.x=yx%32-32/2,U.z=32/2-yx++/32,U.y=32/2/tan(25/114.5915590261),U=vcomb(255., trace(3,black,vunit(U)),black),printf("%.0f %.0f %.0f\n",U);}/*minray!*/

Ray Tracing: Types of Rays Primary rays: Sent from the eye, through the image plane, and into the scene May or may not intersect an object in the scene. No intersection: set pixel to background colour Intersects object: send out secondary rays and compute lighting model Light Opaque object Transparent object P1 P2 Eye

Ray Tracing: Types of Rays Secondary Rays: Sent from the point at which the ray intersects an object Multiple types: Light Transmission (T): sent in the direction of refraction S3 S2 T2 R2 Reflection (R): sent in the direction of reflection, and used in the Phong illumination model S1 T1 R3 R1 Opaque object Transparent object Shadow (S): sent toward a light source to determine if point is in shadow or not. P Eye

Ray Tracing: Types of Rays Light P = Primary rays R = Reflected rays T = Transmitted rays S = Shadow rays S2 S3 R2 T2 S1 T1 R1 R3 Opaque object Transparent object P Eye

Ray Tracing: Ray Tree Each intersection may spawn secondary rays: Rays form a ray tree. Nodes are the intersection points. Edges are the secondary rays. Rays are recursively spawned until: Ray does not intersect any object. Tree reaches a maximum depth. Light reaches some minimum value. Shadow rays are sent from every intersection point (to determine if point is in shadow), but they do not spawn additional rays.

Note only ambient shade on mirror and teapot Test Scene. Ray tree depth 1. Note only ambient shade on mirror and teapot 28/03/2017

Note only ambient shade on reflection of mirror and teapot. Test Scene. Ray tree depth 2. Note only ambient shade on reflection of mirror and teapot. 28/03/2017

Note only ambient shade on reflection of mirror in teapot. Test Scene. Ray tree depth 3. Note only ambient shade on reflection of mirror in teapot. 28/03/2017

Test Scene. Ray tree depth 4. Note ambient shade on reflection of teapot in reflection of mirror in teapot. 28/03/2017

Test Scene. Ray tree depth 5. 28/03/2017

Test Scene. Ray tree depth 6. 28/03/2017

Test Scene. Ray tree depth 7. 28/03/2017

Ray Tracing: Ray Tree Example Eye P O1 Light S3 S2 O3 T2 R2 R1 T1 O1 O2 S1 T1 O2 R3 R1 O1 Opaque object R2 BG R3 T2 O1 BG Transparent object P Eye Ray tree is evaluated from bottom up: Depth-first traversal The node colour is computed based on its children’s colours.

Basic Ray Tracing Algorithm Generate one ray per pixel. For each ray: Find the first object the ray intersects. Compute colour for the intersection point using an illumination model. If the surface is reflective, trace a reflection ray. If the surface is transparent, trace a transmission ray. Trace shadow ray. Combine results of the intensity computation, reflection, transmission, and shadow information. If the ray misses all objects, set to the background colour.

Tracing Rays Basic (non-recursive) ray tracing algorithm: 1. Send a ray from the eye through the screen 2. Determine which object that ray first intersects 3. Compute pixel colour Most (approx. 75%) of the time in step 2: Simple method: Compare every ray against every object and determine the closest object hit by each ray. Very time consuming: Several optimizations possible.

Viewing Ray Which coordinate space? The primary ray (or viewing ray) for a point s on the view plane (i.e., screen) is computed as: Origin: ro = eye Direction: rd = s – eye Which coordinate space? Want to define rays in terms world-space coordinates (x, y, z) Eye is already in specified in terms of (x, y, z) position Screen point s is easiest to define in terms of where it is on the window in viewing- space coordinates (u, v, w) s rd = s – ro ro = eye Window

Viewing Ray: Screen Point Given: Our scene in world-coordinates A camera position in world-coordinates (x, y, z) A pixel (i, j) in the viewport We need to: Compute the point on the view plane window that corresponds to the (i, j) point in the viewport Transform that point into world-coordinates

View-reference coordinates y LookAt point LookFrom point w x u z World coordinates View reference coordinates

Transform the Ray pWS = M pOS pOS = M-1 pWS World Space Object Space Move the ray from World Space to Object Space r minor r major (x,y) r = 1 World Space (0,0) Object Space pWS = M pOS pOS = M-1 pWS

Transform Ray World Space Object Space New origin: New direction: originOS = M-1 originWS directionOS = M-1 (originWS + 1 * directionWS) - M-1 originWS directionOS = M-1 directionWS originWS directionWS qWS = originWS + tWS * directionWS originOS directionOS qOS = originOS + tOS * directionOS World Space Object Space

What to do about the depth, t If M includes scaling, directionOS ends up NOT being normalized after transformation Two solutions: Normalize the direction Don't normalize the direction

Don't normalize direction Highly recommended Don't normalize direction tOS = tWS  convenient! But you should not rely on tOS being true distance during intersection routines tWS tOS World Space Object Space

View-reference to World transform Given the screen point in terms of viewing- space coordinates (u, v, w), transform to world-space (x, y, z): The viewing transform takes a point from world space to view space (change of basis, CoB): y v s w Window u x z

View-reference to World transform Given the screen point in terms of viewing-space coordinates (u, v, w), transform to world-space (x, y, z): We want to reverse this: y v s w Window u x z or sWorld = LookAt + usu + vsv + wsw

Ray/Object intersection Can intersect ray with many objects Planes, Spheres, Cylinders, Cones Boxes (axis-aligned or otherwise) Even procedural/implicit objects! Constructive solid geometry (CSG) Implicit surfaces / equations

Remember: Ray Representation A ray can be represented explicitly (in parametric form) as an origin (point) and a direction (vector): Origin: Direction The ray consists of all points: r(t) = ro + rdt

Ray-Object Intersections Many objects can be represented as implicit surfaces: Sphere (with center at c and radius R): fs (p) = ||p – c||2 - R2 = 0 Plane (with normal n and distance to origin d): fp (p) = p · n + D = 0 To determine where a ray intersects an object: Plug the ray equation into the surface equation and solve for t: f(ro + rdt) = 0 Substitute t back into ray equation to find intersection point p: p = r(t) = ro + rdt

Ray-Sphere Intersections To find the intersection points of a ray with a sphere: Sphere is represented as: center: c = (xc, yc, zc) radius: R The sphere is the set of all points (x, y, z) such that: (x-xc)2 + (y - yc)2 + (z - zc)2 = R2

Ray-Sphere Intersections First, split the ray into its component equations: x = xo + xdt y = yo + ydt z = zo + zdt Then substitute the ray equation into the sphere equation: (x-xc)2 + (y - yc)2 + (z - zc)2 = R2 Giving: (xo + xdt - xc)2 + (yo + ydt - yc)2 + (zo + zdt - zc)2 = R2

Ray-Sphere Intersections Next multiply out the squared terms: (x0 + xdt - xc)2 + (y0 + ydt - yc)2 + (z0 + zdt - zc)2 = R2 Þ (xd2 + yd2 + zd2)t2 + 2(xdxo - xdxc+ ydyo - ydyc+ zdzo - zdzc)t + (xo2- 2xoxc+ xc2+ yo2- 2yoyc+ yc2+ zo2-2zozc+ zc2) = R2 How do we solve for t? Use the Quadratic Equation

Ray-Sphere Intersections Let A = xd2 + yd2 + zd2 = 1 B = 2(xdxo - xdxc+ ydyo - ydyc+ zdzo - zdzc) C = xo2- 2xoxc+ xc2+ yo2- 2yoyc+ yc2+ zo2-2zozc+ zc2 - r2 So At2 + Bt + C = 0 and we can solve this using the quadratic equation:

Ray-Sphere Intersections 3 possibilities: Case 1: B2 – 4C < 0 Zero real roots. No intersection. Case 2: B2 – 4C = 0 One real root t0 = t1 = -B/2 Case 3: B2 – 4C > 0 Two real roots Subcase a: t0 < 0, t1 > 0 t1 is the correct answer Subcase b: 0 < t0 < t1 t0 is the correct answer case 1 case 2 t1 case 3a t0 t0 t1 case 3b

Ray-Sphere Intersections If the discriminant B2 – 4C < 0, the ray misses the sphere. The smaller positive root (if one exists) is the closest intersection point. We can save time by computing the small root first and only computing the large root if necessary.

Ray-Sphere Intersections: Algorithm Algorithm for ray-sphere intersection: Calculate B and C of the quadratic Calculate the discriminant: D = B2 – 4C If D < 0 return false (no intersection point) Calculate smaller t-value t0: If t0  0 then calculate larger t-value t1: If t1  0 return false (intersection is behind ray) else set t = t1 else set t = t0 Return intersection point: p = r(t) = ro + rdt

Ray-Sphere Intersections: Normal The normal n at an intersection point p on a sphere is: n p R c

Ray-Sphere Intersections Computation time per ray-sphere test: 17 additions / subtractions 17 multiplies 1 square root Can we reduce the number of intersection calculations? Yes, use a geometric approach (not here).

Ray-Plane Intersections To find the intersection point of a ray with a plane: Plane equation: ax +by +cz +d = 0 with a2 + b2 +c2 = 1 normal n = (a, b, c) distance from (0, 0, 0) to plane is d Ray equation: Origin: ro = (xo, yo, zo) Direction: rd = (xd, yd, zd) Substitute the ray equation into the plane equation: a(x0 + xdt) + b(y0 + ydt) + c(z0 + zdt) + d = 0 Þ ax0 + axdt + by0 + bydt + cz0 + czdt + d = 0 Solving for t we get: t = -(ax0 + by0 + cz0 + d) / (axd + byd + czd)

Ray-Plane Intersections In vector form: If the ray is parallel to the plane and does not intersect If the plane’s normal points away from the ray and thus the plane is culled. If t  0 then intersection point is behind the ray, so no real intersection occurs Otherwise, compute intersection: p = ro + rdt

Ray/Plane Intersection Basic Algorithm: vd = n·rd if vd ³ 0 and plane has one side then return if vd = 0 then return (ray is parallel to plane) t = -(n·ro + d) / vd if t < 0 then return return r = (xo + xdt, yo + ydt, zo + zdt)

Ray/Polygon Intersection Assume planar polygons First, intersect the plane the polygon is on with the ray. Next, determine if the intersection is within the polygon. How do we quickly tell if the intersect is within the polygon?

Ray/Polygon Intersection Idea: shoot a ray from the intersection point in an arbitrary direction if the ray crosses an even number of polygon edges, the point is outside the polygon if the ray crosses an odd number of polygon edges, the point is inside the polygon 11 crossings - point inside polygon 4 crossings - point outside polygon

Ray/Polygon Intersection a set of N points Pj = (xj, yj, zj), j = 0, 1, … N-1 Plane: ax + by + cz + d = 0, with normal n = (a, b, c) Intersection point: pi = (xi, yi, zi), pi on the plane

Ray/Polygon Intersection Algorithm Step 1: Project the polygon onto a coordinate plane Simply discard one of the coordinates. This will project the polygon onto the plane defined by the other two coordinates. This doesn’t preserve area, but does preserve topology. Discard the coordinate whose magnitude in the plane normal is the largest. If n = (3, -1, -5) we would throw away the z coordinates.

Ray/Polygon Intersection Algorithm Step 2: Translate the polygon so that the intersection point is at the origin. Translate all its vertices. Step 3: Send a ray down a coordinate axis and count the number of times it intersects the polygon. This can be done simply by counting the number of edges that cross the coordinate axis.

Ray/Polygon Intersection Algorithm Problems: Vertices that lie exactly on the ray Edges that lie exactly on the ray

Ray/Polygon Intersection Algorithm How do we handle these cases? Essentially, shift the vertex or edge up by e so that it will be just above the axis.

Ray/Polygon Intersection Algorithm The effect of doing this e shift is: These cases all work correctly now.

The Complete Algorithm - part 1 Find the coordinate of n with the largest magnitude. For each vertex (xj, yj, zj), j = 0, 1, …, N-1 of the polygon, project the vertex onto the coordinate plane of the other two coordinates. This gives us a new coordinates system (uj, vj). Project the intersection point (xi, yi, zi) onto the same coordinate plane as the vertices. Translate all polygon vertices by (-ui, -vi). (uj’, vj’) = (uj, vj) - (ui, vi). 5. Set numCrossings = 0

The Complete Algorithm - part 2 If v0’ < 0, set sign = -1, otherwise set sign = 1 For i = 0 to N-1 (let uN = u0, vN = v0 ) if vi+1’ < 0 set nextSign = -1 else set nextSign = 1 if (sign ≠ nextSign) if (ui’ > 0 and ui+1’ > 0) numCrossings++ else if (ui’ > 0 or ui+1’>0) \\ the edge might cross +u’ \\ compute the intersection with the u’ axis uc = ui’ - vi’ * (ui+1’ -ui’)/(vi+1’ -vi’) if uc > 0 numCrossings++ sign = nextSign If numCrossings is odd, the intersection is inside the polygon.

Example Given a polygon: and intersection point Ri = (-2, -2, 4) Does the intersection point lie within the polygon? P0 P1 P2

Example Step 1: Get the plane normal, determine dominant coordinate n can be computed from the cross product of two vectors in the plane The vertices of the polygon can be used to compute vectors in the plane P0 P1 P2 P0 = (-3, -3, 7) P1 = (3, -4, 3) P2 = (4, -5, 4) Ri = (-2, -2, 4)

Example Compute the Normal: v1 = P0 - P1 = (-3, -3, 7) - (3, -4, 3) First, compute edge vectors from the vertices P0 P1 v1 = P0 - P1 = (-3, -3, 7) - (3, -4, 3) = (-6, 1, 4) P2 P0 = (-3, -3, 7) P1 = (3, -4, 3) P2 = (4, -5, 4) Ri = (-2, -2, 4) v2 = P2 - P1 = (4, -5, 4) - (3, -4, 3) = (1, -1, 1)

Example The plane normal is then v2 x v1 n = (-6, 1, 4) x (1, -1, 1) = (5, 10, 5) P0 n P1 So the dominant coordinate is y P2 P0 = (-3, -3, 7) P1 = (3, -4, 3) P2 = (4, -5, 4) Ri = (-2, -2, 4)

Example Step 2: Project the vertices Step 3: Project the intersection point project P0 = (-3, -3, 7) Þ (-3, 7) project P1 = (3, -4, 3) Þ (3, 3) project P2 = (4, -5, 4) Þ (4, 4) u’ P1’ Ri P2’ P0’ v’ project Ri = (-2, -2, 4) Þ (-2, 4)

Example Step 4: Translate the vertices Ri’ = (-2, 4) - (-2, 4) Þ (0, 0) P1’ Ri u’ P2’ P0’ v’

Example Step 5: Set numCrossings = 0 Step 6: v0’= 3, so sign = 1 P1’ Ri v’ P2’ P0’ u’

Example Since numCrossings is even, the point is outside the polygon (Figure is drawn upside-down. Sorry. The positive v’ axis points down. Step 7: i sign nextSign numCrossings intersection point For i = 0 to N-1 \\ (let uN = u0, vN = v0 ) if vi+1’ < 0 set nextSign = -1 else set nextSign = 1 if (sign ≠ nextSign) if (ui’ > 0 and ui+1’ > 0) numCrossings++ else if (ui’ > 0 or ui+1’>0) \\ the edge might cross +u’ \\ compute the intersection with the u’ axis uc = ui’ - vi’ * (ui+1’ -ui’)/(vi+1’ -vi’) if uc > 0 numCrossings++ sign = nextSign P1’=(5,-1) Ri u’ P2’ = (6,0) P0’=(-1,3) v’ +1 -1 1 -1-3*(5-(-1))/(-1-3) = 3.5 1 -1 +1 2 2 +1 +1 Since numCrossings is even, the point is outside the polygon

Shadows Send a shadow ray from intersection point to the light: Compute the following shadow ray properties: Shadow ray direction: sd = (l – p) / ||l – p|| Distance to the light from the intersection point: tl = ||l – p|| Test if shadow ray intersects an object before reaching the light: In theory, the possible range of t-values is t  [0, tl] Due to numerical (floating-point) error, test in the range t  [e, t1] where e is some small value (such as 2–15)

Recursive Ray Tracing Basic ray tracing results in basic Phong illumination plus hidden surfaces. Shadows require only one extra ray per light source Shadow rays do not reflect or refract. No need to find the closest object, only need to hit once before reaching the light. Reflection and refraction rays add a lot to the images. Reflection and refraction can spawn many new rays since light can keep bouncing.

Computing ray colours How to get the final pixel colour? End result: Every material has local colour c Define reflexivity, refraction factors ρ (i.e. between 1.0 (mirror) and 0.0 (no reflection) Each hit returns its colour down the chain End result: colour = (1 –ρ) c1 + ρ1 ( (1- ρ2)c2 + ρ2 (c3 …. ))) Local colour also influenced by shadow rays!

Computing reflection rays Reflection is pretty simple Incident angle = Exit angle Incident ray: x + t*d Reflection ray: a + t*(d – 2*n*dot(n,d)) (careful, assumes n and d normalized!) n a

Specular Reflection Perfect reflection: angle of reflection = angle of incidence Light continues to bounce: Typically, some energy is lost on each bounce n and l are unit vectors n r l

Specular Reflection Implement specular reflection with a recursive call: colour = ambient + diffuseobj + specularobj + ks reflectedcolour where ks represents how “perfect” the mirror is and reflected colour is the recursive call Limit recursion: max depth or when the contribution of a ray is negligible

Refraction (transparency) Light transmits through transparent objects. Light bends when moving from one medium to another according to Snell’s law: n1 air glass n2

Refraction Indices Material Index Vacuum 1.0 Air 1.0003 Water 1.33 Alcohol 1.36 Fused quartz 1.46 Crown glass 1.52 Flint glass 1.65 Sapphire 1.77 Heavy flint glass 1.89 Diamond 2.42

Refraction Total internal reflection When going from a dense to less dense medium, the angle of refraction becomes more shallow If the angle of incidence is too shallow, it can get trapped in the dense material Optical cable Diamonds Calculating the transmitted vector requires: Incident ray direction Normal Indices of refraction Some trigonometry air d glass n

Ray Tracing Ambient Light only

Ray Tracing With shadows

Ray Tracing Primary rays only

Ray Tracing With Phong shading

Ray Tracing With reflections

Ray Tracing With transmissions

Other Ray Traced Images