Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 655 – Advanced Computer Graphics

Similar presentations


Presentation on theme: "CS 655 – Advanced Computer Graphics"— Presentation transcript:

1 CS 655 – Advanced Computer Graphics
Ray-Object Intersections

2 Ray-Object Intersections
Many objects can be represented as implicit surfaces: Sphere (with center at c and radius R): fsphere(p) = ||p – c||2 - R2 = 0 Plane (with normal n and distance to origin d): fplane(p) = p · n + D = 0 To determine where a ray intersects an object: Need to find the intersection point p of the ray and the object The ray is represented explicitly in parametric form: R(t) = Ro + Rdt Plug the ray equation into the surface equation and solve for t: f(R(t)) = 0 Substitute t back into ray equation to find intersection point p: p = R(t) = Ro + Rdt

3 Recall: 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 R(3) R(2) Ro + Rd = R(1) Ro = R(0) R(–1)

4 Ray-Sphere Intersections
To find the intersection points of a ray with a sphere: Sphere is represented as: center (point): Sc = (xc, yc, zc) and a radius (float): r The surface of the sphere is the set of all points (x, y, z) such that: (x-xc)2 + (y - yc)2 + (z - zc)2 = r2 In this form (implicit form), it is difficult to directly generate surface points However, given a point, it is easy to see if the point lies on the surface To solve the ray-surface intersection, substitute the ray equation into the sphere equation.

5 Ray-Sphere Intersections
First, split the ray into its component equations: x = xo + xdt y = yo + ydt z = zo + zdt Next 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

6 Ray-Sphere Intersections
Next, simplify the equation: (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

7 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 Then solve using the quadratic equation:

8 Ray-Sphere Intersections
If the discriminant is negative, the ray misses the sphere The smaller positive root (if one exists) is the closer intersection point We can save some computation time by computing the smaller root first, then only computing the second root if necessary

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

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

11 Ray-Sphere Intersections
Computation time: 17 adds/subtracts 17 multiplies 1 square root for each ray/sphere test Can we reduce the number of intersection calculations? use a geometric approach

12 Ray-Sphere Intersections - Geometric
tca thc d Sc OC R0

13 Ray-Sphere Intersections - Geometric
Determine whether the ray’s origin is outside the sphere if R0 - Sc < r, then the point is inside the sphere Call the vector between R0 and Sc OC Find the closest approach of the ray to the sphere’s center. let d = distance from Sc to the ray let tca = distance from R0 to closest approach of ray to Sc tca = Rd · OC tca thc d Sc OC R0

14 Ray-Sphere Intersections - Geometric
If tca < 0 and R0 lies outside the sphere, the ray does not intersect the sphere Otherwise, compute thc, the distance from the closest approach to the sphere’s surface thc2 = r2 - d2 d2 = |OC|2 - tca2, so thc2 = r2 - |OC|2 + tca2 tca thc d Sc OC R0

15 Ray-Sphere Intersections - Geometric
If thc2 < 0 the ray does not intersect the sphere Otherwise, calculate the intersection distance If R0 is outside the sphere: t = tca - thc If R0 is inside the sphere: t = tca + thc tca thc d Sc OC R0

16 Ray-Sphere Intersections - Geometric
Calculate the intersection point: (xi, yi, zi) = (xo + xdt, yo + ydt, zo + zdt) Calculate the normal at the intersection point: This reduces the computation (worst case) by 4 multiplies and 1 add Even fewer computations if the ray misses the sphere tca thc d Sc OC R0

17 Example Find the intersection point of the ray and the sphere

18 Example First: Normalize ray 1. Compute OC
1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal First: Normalize ray 1. Compute OC Since > r, Ro is outside the sphere

19 Example 2. Find the closest approach of the ray
1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal 2. Find the closest approach of the ray to the sphere’s center.

20 Example 3. If tca < 0 and R0 lies outside the sphere,
1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal 3. If tca < 0 and R0 lies outside the sphere, the ray does not intersect the sphere R0 does lie outside the sphere, but tca > 0, so continue

21 Example the closest approach to the sphere’s surface
1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal 4. Compute thc, the distance from the closest approach to the sphere’s surface

22 Example 1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal 5. See if thc2 < 0 thc2 > 0, so the ray must hit the sphere

23 Example 6. Calculate the intersection distance
1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal 6. Calculate the intersection distance Since R0 is outside the sphere: so the ray intersects the sphere at t = 3.744

24 Example 7. Calculate the intersection point
1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal 7. Calculate the intersection point

25 Example intersection point 8. Calculate the normal at the Done!
1. Check ray origin inside/outside sphere Compute OC 2. Find closest approach to sphere’s center. 3. Check for non intersection 4. Compute thc 5. Check thc2 < 0 6. Calculate the intersection distance 7. Calculate intersection point 8. Calculate normal 8. Calculate the normal at the intersection point Done!

26 Computing the Reflection Ray
Angle of incidence = angle of reflection N I R I = incoming ray N = Surface normal R = Reflected ray (generally want to keep I and N normalized)

27 Computing the Reflection Ray
Knowing I and N, how do we compute R? N I R 1. qI = q R 2. I, N, and R are coplanar 3. R = aI + bN 4. -I ● N = cos(qI) 5. N ● R = cos(q R) 6. Using 1, 4, and 5, we get: 7. -I ● N = N ● R, so 8. -I ● N = N ● (aI + bN) 9. -I ● N = (N ● aI) + (N ● bN) 10. -I ● N = a(N ● I) + b (N ● N) 11. -I ● N = a(N ● I) + b, since N is normalized 12. Without loss of generality, let a = 1, then 13. b = -2 (N ● I), so 14. R = I - 2(N ● I)N

28 Refraction (transparency)
When an object is transparent, it transmits light Light travels through different materials at different speeds. Thus, we get light “bending”. E.g., pole in water: Keep this in mind if you ever get stuck after a plane crash and have to use a bow and arrow to fish for your food (See Hatchet by G. Paulson p. 125)

29 Refraction (transparency)
The manner in which light travels through a material is accounted for by the object’s “index of refraction” the ratio of the speed of light through the material to the speed of light in a vacuum Light bend computed by Snell’s law: I n Medium 1 T Medium 2

30 Refraction Indices Index of refraction for various materials:
Material Index Vacuum 1.0 Air Water 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

31 Refraction How do we compute T? 1. Let nIT = n1/n2 = sin(f)/sin(q)
2. then nIT2 = sin2(f)/sin2(q) 3. sin2(q) nIT2 = sin2(f) 4. since sin2(q) + cos2(q) = 1, we get 5. (1 - cos2(q)) nIT2 = 1 - cos2(f), so 6. (1 - cos2(q)) nIT2 - 1 = - cos2(f) 7. (1 - cos2(q)) nIT2 - 1 = - (-N ● T)2 8. (1 - cos2(q)) nIT2 - 1 = - (-N ● (aI + bN))2 9. (1 - cos2(q)) nIT2 - 1 = - (-a(N ● I) + b(-N ● N))2 10. (1 - cos2(q)) nIT2 - 1 = -(a(cos(f)) - b)2 11. We want T normalized, so T ● T = 1, so 12. 1 = (aI + bN) ● (aI + bN) 13. 1 = a2(I ● I) + 2ab(I ● N) ● b2(N ● N) 14. 1 = a2 + 2abcos(q) ● b2 I n Medium 1 T Medium 2

32 Refraction How do we compute T?
15. Solve equations 10 and 14 simultaneously: 16. (1 - cos2(q)) nIT2 - 1 = -(a(cos(f)) - b)2 1 = a2 + 2abcos(q) ● b2 17. After solving, we get: 18. So, n Medium 1 T Medium 2

33 Refraction Example Compute T for: Water T Glass

34 Refraction Example Glass Water T

35 Shadows The intensity of what we see at at pixel will depend on whether or not a light actually hits the object The point of intersection between ray 1 and the object is in direct illumination The point of intersection between ray 2 and the object is blocked from the light source (in shadow) The final pixel intensity must be computed taking this into account Light ray 1 ray 2 Eye Image plane

36 Shadows Approach for computing shadows (hard shadows)
For each ray generated, find the first object intersected From that intersection point, send a ray directly to each light source If the ray intersects an object en route to the light source, the object is occluded from that light source For shadow rays, we don’t care which object the ray intersects, just the fact that it does intersect

37 Ray-Plane Intersections
To find the intersection points of a ray with an infinite plane: Ray equation: Origin: ro = (xo, yo, zo) Direction: rd = (xd, yd, zd) Plane equation: ax +by +cz +d = 0 with a2 + b2 +c2 = 1 normal vector pn = (a, b, c) distance from (0, 0, 0) to plane is d 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)

38 Ray-Plane Intersections
In vector form we have: If the ray is parallel to the plane and does not intersect If the normal of the plane is pointing 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 point: p = ro + rdt

39 Ray/Plane Intersection
Basic Algorithm: Compute vd = pn · rd If vd > 0 and assuming 1 sided planes then return If vd = 0 then return (ray parallel to plane) Compute vo = -(pn · rd + d) Compute t = vo / vd If t < 0 return If vd > 0 reverse the plane’s normal Return r = (xo + xdt, yo + ydt, zo + zdt)

40 Ray/Triangle Intersection
Simpler than Ray/Polygon intersection Every planar polygon can be reduced to a finite set of triangles. Determine if the ray intersects the plan containing the triangle. Project the triangle onto a plane. Find the Barycentric coordinates of the intersection point in 2D on the plane. Reproject back into world coordinates.

41 Ray/Triangle: The plane
v1 v2 N v0 N = norm((v1 - v0) x (v2 - v0))

42 Ray/Triangle: The plane
v1 v2 N v0 N = norm((v1 - v0) x (v2 - v0)) Plane (x,y,z) = ax + by + cz + d = Nx(x) + Ny(y) + Nz(z) + d = 0 What’s d? d = distance from origin to closest point on the plane.

43 Ray/Triangle: The plane
v1 v2 N v0 N = norm((v1 - v0) x (v2 - v0)) Plane (x,y,z) = ax + by + cz + d = Nx(x) + Ny(y) + Nz(z) + d = 0 What’s d? d = distance from origin to closest point on the plane. = projection of v0 (as a vector) onto N. = v0 dot N

44 v0 N (v0-0rigin) N

45 v0 N v0 dot N (v0-0rigin) N

46 Ray/Triangle: The plane
v1 v2 N v0 N = norm((v1 - v0) x (v2 - v0)) Plane (x,y,z) = ax + by + cz + d = Nx(x) + Ny(y) + Nz(z) + d = 0 What’s d? d = distance from origin to closest point on the plane. = projection of v0 (as a vector) onto N. = v0 dot N

47 Ray/Triangle: Ray/Plane
Now know the plane equation. Know the ray equation. Compute t then intersection point I

48 Ray/Triangle: Projection
v1 v2 N v0 Project the ray, plane and triangle s.t. drop a coordinate. Now we only have to work in 2D. If N = (2,3,1) then y (middle coordinate) is dominant. Drop all y coordinates in v0, v1, v2 and I.

49 Ray/Triangle: Is I in triangle?
v1 I z v2 v0 x Find the Barycentric coordinates for I. If all are less than 1, then I is in the triangle.

50 Ray/Polygon Intersection
Assume planar polygons First perform the ray/plane intersection with the plane in which the polygon lies Next, determine whether the intersection point lies within the polygon

51 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

52 Ray/Polygon Intersection
a set of N points Gn = (xn, yn, zn), n = 0, 1, … N-1 Plane: Ax + By + Cz + D = 0, with normal Pn = (A, B, C) Intersection point: Ri = (xi, yi, zi), Ri on the plane

53 Ray/Polygon Intersection Algorithm
Step 1: Project the polygon onto a 2D plane one of the coordinate planes simply discard one of the coordinates this will project the polygon onto the plane defined by the other two coordinates topology preserving but not area preserving throw away the coordinate whose magnitude in the plane normal is greatest e.g., if Pn = (3, -1, -5) we would throw away all z coordinates

54 Ray/Polygon Intersection Algorithm
Step 2: Translate the polygon so that the intersection point is at the origin apply this translation to all points in the polygon Step 3: Send a ray down one of the coordinate axes and count the number of intersections of that ray with the polygon This is done in practice by looking at each polygon edge, one at a time, to see if it crosses the coordinate axis

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

56 Ray/Polygon Intersection Algorithm
How do we handle these cases? In essence, shift the vertex or edge up by e so that it will lie in the plane just above the axis This is done by checking for a y value > 0 What about if the intersection point is exactly a vertex? Perform a similar e shift What if the intersection point lies exactly on an edge? Specify it as either in or out - as long as we do the specification consistently, it will work okay If the edge is shared by two polygons, the point will be “in” one polygon and “out” of the other.

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

58 The Actual Algorithm - part 1
Determine the dominant coordinate as the largest magnitude component of Pn For each vertex (xn, yn, zn), n = 0, 1, …, N-1 of the polygon, project the vertex onto the dominant coordinate axis, giving (un, vn) vertices. Project the intersection point (xint, yint, zint) onto the same coordinate plane as the vertices. Translate all polygon vertices by (-uint, -vint), giving (un’, vn’) vertices. Set numCrossings = 0

59 The Actual Algorithm - part 2
If v0’ < 0, set signHolder = -1, otherwise set signHolder = 1 For i = 0 to N-1 (note - when i = N-1, i+1 should be 0) if vi+1’ < 0 set nextSignHolder = -1 else set nextSignHolder = 1 if (signHolder <> nextSignHolder) if (ui’ > 0 and ui+1’ > 0) this edge crosses +u’ so increment numCrossings else if (ui’ > 0 or ui+1’>0) the edge might cross +u’, so compute the intersection with the u’ axis ucross= ui’ -vi’ * (ui+1’ -ui’)/(vi+1’ -vi’) if ucross > 0 the edge crosses +u so increment numCrossings set signHolder = nextSignHolder If numCrossings is odd, the point is inside the polygon

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

61 Example Step 1: Get the plane normal, determine dominant coordinate
Pn 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 G0 G1 G2 v1 = G0 - G1 = (-3, -3, 7) - (3, -4, 3) = (-6, 1, 4) v2 = G2 - G1 = (4, -5, 4) - (3, -4, 3) = (1, -1, 1)

62 Example The plane normal is then v1 x v2 (assuming clockwise vertex specification) Pn = (-6, 1, 4) x (1, -1, 1) = (5, 10, 5) So the dominant coordinate is y G0 G1 G2

63 Example Step 2: Project the vertices
Step 3: Project the intersection point G0 = proj of (-3, -3, 7) Þ (-3, 7) G1 = proj of (3, -4, 3) Þ (3, 3) G2 = proj of (4, -5, 4) Þ (4, 4) G1 Ri G2 G0 Ri = proj of (-2, -2, 4) Þ (-2, 4)

64 Example Step 4: Translate the vertices
G0’ = (-3, 7) - (-2, 4) Þ (-1, 3) G1’ = (3, 3) - (-2, 4) Þ (5, -1) G2’ = (4, 4) - (-2, 4) Þ (6, 0) Ri’ = (-2, 4) - (-2, 4) Þ (0, 0) G1’ Ri’ G2’ G0’

65 Example Step 5: Set numCrossings = 0 Step 6: v0’= 3, so signHolder = 1
Ri’

66 Example G1’ = (5, -1) Step 7: Ri’ u G2’ = (6, 0) G0’ = (-1, 3) v
i signHolder nextSignHolder numCrossings intersection point u v +1 -1 -1 1 -1-3*(5-(-1))/(-1-3) = 3.5 1 +1 +1 2 2 +1 Since numCrossings is even, the point is outside the polygon

67 Ray/Box Intersection i.e., intersecting with bounding boxes
We will deal with the case of boxes with parallel sides with normals parallel to the coordinate axes. Box: Minimum extent Bl = (xl, yl, zl) Maximum extent Bh = (xh, yh, zh) Ray R(t) = Ro + Rdt Ro = (xo, yo, zo) Rd = (xd, yd, zd)

68 Ray/Box Intersection (2)
Algorithm: Set tnear = -∞, tfar = ∞ For the pair of X planes: If xd = 0, the ray is parallel to the planes If xo < xl or xo > xh then return FALSE (origin not between planes) Else the ray is not parallel to the planes, so calculate intersection distances of planes t1 = (xl - xo) / xd (time at which the ray intersects the minimum x plane) t2 = (xh - xo) / xd (time at which the ray intersects the maximum x plane) If t1 > t2 swap t1 and t2 If t1 > tnear , set tnear = t1 If t2 < tfar , set tfar = t2 If tnear > tfar box is missed so return FALSE If tfar < 0 box is behind ray so return FALSE Repeat step 2 for Y then Z All tests survived, so return TRUE

69 Ray/Box Intersection example 1
tfar t2x t1x tnear yh t2y yl t1y Ro xl xh

70 Ray/Box Intersection example 2
tfar tnear yh t2x t2y t1x yl t1y Ro xl xh

71 Ray/Box Intersection Example 3
Given the ray Ro = (0, 4, 2) Rd = (0.213, , 0.873) And the box Bl = (-1, 2, 1) Bh = (3, 3, 3) Does the ray hit the box?

72 Ray/Box Intersection Example 3
t1x = (-1 - 0) / = -4.59 t2x = (3 - 0) / = 13.8 So tnear = -4.59, tfar = 13.8 t1y = (2 - 4) / (-.436) = 4.59 t2y = (3 - 4) / (-.436) = 2.29 t1y > t2y so swap Then tnear = 2.29, tfar = 4.59 t1z = (1 - 2) / (0.873) = -1.15 t2z = (3 - 2) / (0.873) = 1.15 tnear = stays the same tfar = 1.15 So, tnear > tfar so the ray misses the box


Download ppt "CS 655 – Advanced Computer Graphics"

Similar presentations


Ads by Google