Download presentation

Presentation is loading. Please wait.

Published bySebastian Reeves Modified over 2 years ago

1

2
2 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.

3
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 3

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

5
5 Ray Traced Images

6
6

7
7 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 Ray Tracing Eye Image plane Light

8
8 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. Eye Image plane Light

9
9 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

10
10 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

11
11 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

12
12 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 Eye Light Opaque object Transparent object P2P2 P1P1

13
13 Secondary Rays: Sent from the point at which the ray intersects an object Multiple types: Ray Tracing: Types of Rays Eye Light Opaque object Transparent object P R1R1 R2R2 R3R3 T1T1 T2T2 S3S3 S1S1 S2S2 Reflection (R): sent in the direction of reflection, and used in the Phong illumination model Transmission (T): sent in the direction of refraction Shadow (S): sent toward a light source to determine if point is in shadow or not.

14
14 Ray Tracing: Types of Rays Eye Light Opaque object Transparent object P = Primary rays R = Reflected rays T = Transmitted rays S = Shadow rays R1R1 R2R2 R3R3 T1T1 T2T2 S3S3 S1S1 S2S2 P

15
15 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.

16
08/04/ Ray tree depth 1. Note only ambient shade on mirror and teapot

17
08/04/ Ray tree depth 2. Note only ambient shade on reflection of mirror and teapot.

18
08/04/ Ray tree depth 3. Note only ambient shade on reflection of mirror in teapot.

19
08/04/ Ray tree depth 4. Note ambient shade on reflection of teapot in reflection of mirror in teapot.

20
08/04/ Ray tree depth 5.

21
08/04/ Ray tree depth 6.

22
08/04/ Ray tree depth 7.

23
23 Ray Tracing: Ray Tree Example Ray tree is evaluated from bottom up: Depth-first traversal The node colour is computed based on its childrens colours. Eye P O1O1 R2R2 BG R3R3 T2T2 O1O1 R1R1 T1T1 O1O1 O2O2 Eye Light Opaque object Transparent object P R1R1 R2R2 R3R3 T1T1 T2T2 S3S3 S1S1 S2S2 O2O2 O1O1 O3O3

24
24 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.

25
25 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. Tracing Rays

26
26 The primary ray (or viewing ray) for a point s on the view plane (i.e., screen) is computed as: Origin: r o = eye Direction: r d = 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) Viewing Ray r o = eye s r d = s – r o Window

27
27 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 Viewing Ray: Screen Point

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

29
29 (0,0) Move the ray from World Space to Object Space Object Space World Space r = 1 r major r minor (x,y) p WS = M p OS p OS = M -1 p WS

30
30 New origin: New direction: origin OS = M -1 origin WS direction OS = M -1 (origin WS + 1 * direction WS ) - M -1 origin WS origin OS origin WS direction OS direction WS Object Space World Space q WS = origin WS + t WS * direction WS q OS = origin OS + t OS * direction OS direction OS = M -1 direction WS

31
31 If M includes scaling, direction OS ends up NOT being normalized after transformation Two solutions: Normalize the direction Don't normalize the direction

32
32 t OS = t WS convenient! But you should not rely on t OS being true distance during intersection routines Object Space World Space t WS t OS Highly recommended

33
33 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): View-reference to World transform x y z u v w Window s

34
34 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: View-reference to World transform or s World = LookAt + u s u + v s v + w s w x y z u v w Window s

35
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

36
36 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) = r o + r d t

37
37 Many objects can be represented as implicit surfaces: Sphere (with center at c and radius R) : f s (p) = ||p – c|| 2 - R 2 = 0 Plane (with normal n and distance to origin d) : f p (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(r o + r d t) = 0 Substitute t back into ray equation to find intersection point p: p = r(t) = r o + r d t

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

39
39 First, split the ray into its component equations: x = x o + x d t y = y o + y d t z = z o + z d t Then substitute the ray equation into the sphere equation: (x-x c ) 2 + (y - y c ) 2 + (z - z c ) 2 = R 2 Giving: (x o + x d t - x c ) 2 + (y o + y d t - y c ) 2 + (z o + z d t - z c ) 2 = R 2

40
40 Next multiply out the squared terms: (x 0 + x d t - x c ) 2 + (y 0 + y d t - y c ) 2 + (z 0 + z d t - z c ) 2 = R 2 (x d 2 + y d 2 + z d 2 )t 2 + 2(x d x o - x d x c + y d y o - y d y c + z d z o - z d z c )t + (x o 2 - 2x o x c + x c 2 + y o 2 - 2y o y c + y c 2 + z o 2 -2z o z c + z c 2 ) = R 2 How do we solve for t?Use the Quadratic Equation

41
41 Let A = x d 2 + y d 2 + z d 2 = 1 B = 2(x d x o - x d x c + y d y o - y d y c + z d z o - z d z c ) C = x o 2 - 2x o x c + x c 2 + y o 2 - 2y o y c + y c 2 + z o 2 -2z o z c + z c 2 - r 2 So At 2 + Bt + C = 0 and we can solve this using the quadratic equation:

42
42 3 possibilities: Case 1: B 2 – 4C < 0 Zero real roots. No intersection. Case 2: B 2 – 4C = 0 One real root t 0 = t 1 = -B/2 Case 3: B 2 – 4C > 0 Two real roots Subcase a: t 0 0 t 1 is the correct answer Subcase b: 0 < t 0 < t 1 t 0 is the correct answer case 3a case 1 case 2 case 3b t1t1 t0t0 t1t1 t0t0

43
43 If the discriminant B 2 – 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.

44
44 Algorithm for ray-sphere intersection: Calculate B and C of the quadratic Calculate the discriminant: D = B 2 – 4C If D < 0 return false (no intersection point) Calculate smaller t-value t 0 : If t 0 0 then calculate larger t-value t 1 : If t 1 0 return false (intersection is behind ray) else set t = t 1 else set t = t 0 Return intersection point: p = r(t) = r o + r d t

45
45 The normal n at an intersection point p on a sphere is: n p c R

46
46 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).

47
47 To find the intersection point of a ray with a plane: Plane equation: ax +by +cz +d = 0 with a 2 + b 2 +c 2 = 1 normaln = (a, b, c) distance from (0, 0, 0) to plane is d Ray equation: Origin: r o = (x o, y o, z o ) Direction:r d = (x d, y d, z d ) Substitute the ray equation into the plane equation: a(x 0 + x d t) + b(y 0 + y d t) + c(z 0 + z d t) + d = 0 ax 0 + ax d t + by 0 + by d t + cz 0 + cz d t + d = 0 Solving for t we get: t = -(ax 0 + by 0 + cz 0 + d) / (ax d + by d + cz d )

48
48 In vector form: If the ray is parallel to the plane and does not intersect If the planes 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 = r o + r d t

49
49 Basic Algorithm: v d = n·r d if v d 0 and plane has one side then return if v d 0 then return (ray is parallel to plane) t = -(n·r o + d) / v d if t < 0 then return return r = (x o + x d t, y o + y d t, z o + z d t)

50
50 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?

51
51 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 4 crossings - point outside polygon 11 crossings - point inside polygon

52
52 Polygon: a set of N points P j = (x j, y j, z j ), j = 0, 1, … N-1 Plane: ax + by + cz + d = 0, with normal n = (a, b, c) Intersection point: p i = (x i, y i, z i ), p i on the plane

53
53 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 doesnt 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.

54
54 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.

55
55 Problems: Vertices that lie exactly on the ray Edges that lie exactly on the ray

56
56 How do we handle these cases? Essentially, shift the vertex or edge up by so that it will be just above the axis.

57
57 The effect of doing this shift is: These cases all work correctly now.

58
58 1.Find the coordinate of n with the largest magnitude. 2.For each vertex (x j, y j, z j ), 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 (u j, v j ). 3.Project the intersection point (x i, y i, z i ) onto the same coordinate plane as the vertices. 4.Translate all polygon vertices by (-u i, -v i ). (u j, v j ) = (u j, v j ) - (u i, v i ). 5.Set numCrossings = 0

59
59 6.If v 0 < 0, set sign = -1, otherwise set sign = 1 7.For i = 0 to N-1 (let u N = u 0, v N = v 0 ) if v i+1 < 0set nextSign = -1 else set nextSign = 1 if (sign nextSign) if (u i > 0 and u i+1 > 0) numCrossings++ else if (u i > 0 or u i+1 >0) \\ the edge might cross +u \\ compute the intersection with the u axis u c = u i - v i * (u i+1 -u i )/(v i+1 -v i ) if u c > 0 numCrossings++ sign = nextSign 8.If numCrossings is odd, the intersection is inside the polygon.

60
60 Given a polygon: P 0 = (-3, -3, 7) P 1 = (3, -4, 3) P 2 = (4, -5, 4) and intersection point R i = (-2, -2, 4) Does the intersection point lie within the polygon? P0P0 P1P1 P2P2

61
61 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 P 0 = (-3, -3, 7) P 1 = (3, -4, 3) P 2 = (4, -5, 4) R i = (-2, -2, 4) P0P0 P1P1 P2P2

62
62 Compute the Normal: First, compute edge vectors from the vertices v 1 = P 0 - P 1 = (-3, -3, 7) - (3, -4, 3) = (-6, 1, 4) v 2 = P 2 - P 1 = (4, -5, 4) - (3, -4, 3) = (1, -1, 1) P 0 = (-3, -3, 7) P 1 = (3, -4, 3) P 2 = (4, -5, 4) R i = (-2, -2, 4) P0P0 P1P1 P2P2

63
63 The plane normal is then v 2 x v 1 n = (-6, 1, 4) x (1, -1, 1) = (5, 10, 5) So the dominant coordinate is y P0P0 P1P1 P2P2 P 0 = (-3, -3, 7) P 1 = (3, -4, 3) P 2 = (4, -5, 4) R i = (-2, -2, 4) n

64
64 Step 2: Project the vertices Step 3: Project the intersection point project P 0 = (-3, -3, 7) project P 1 = (3, -4, 3) project P 2 = (4, -5, 4) project R i = (-2, -2, 4) P 0 P 1 P 2 RiRi u v

65
65 Step 4: Translate the vertices P 0 = (-3, 7) - (-2, 4) P 1 = (3, 3) - (-2, 4) P 2 = (4, 4) - (-2, 4) R i = (-2, 4) - (-2, 4) P 0 P 1 P 2 RiRi u v

66
66 Step 5: Set numCrossings = 0 Step 6: v 0 = 3, so sign = 1 P 0 P 1 P 2 RiRi v u

67
67 For i = 0 to N-1 \\ (let uN = u0, vN = v0 ) if vi+1 < 0set 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 Step 7: isignnextSignnumCrossingsintersection point *(5-(-1))/(-1-3) = 3.5 Since numCrossings is even, the point is outside the polygon P 0 =(-1,3) P 1 =(5,-1) P 2 = (6,0) RiRi u v (Figure is drawn upside-down. Sorry. The positive v axis points down. +1

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

69
69 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.

70
How to get the final pixel colour? 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 –ρ) c 1 + ρ 1 ( (1- ρ 2 )c 2 + ρ 2 (c 3 …. ))) Local colour also influenced by shadow rays!

71
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

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

73
73 Implement specular reflection with a recursive call: colour = ambient + diffuse obj + specular obj + k s reflectedcolour where k s 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

74
74 Light transmits through transparent objects. Light bends when moving from one medium to another according to Snells law: glass n1n1 air n2n2

75
75 MaterialIndex Vacuum1.0 Air Water1.33 Alcohol1.36 Fused quartz1.46 Crown glass1.52 Flint glass1.65 Sapphire1.77 Heavy flint glass1.89 Diamond2.42

76
76 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 glass n d air

77
77 Ambient Light only

78
78 With shadows

79
79 Primary rays only

80
80 With Phong shading

81
81 With reflections

82
82 With transmissions

83
83

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google