Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 325 Introduction to Computer Graphics 04 / 26 / 2010 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 325 Introduction to Computer Graphics 04 / 26 / 2010 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 325 Introduction to Computer Graphics 04 / 26 / 2010 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 325 - Spring 2010 Today’s Topics Questions? Reducing Intersection calculations (betweens Rays and Spheres) in ray tracing Review Intersection of Rays w/ polygons

3 Reducing Intersection calcs Our text says that ray-object intersection calculations can make up up-to 95% of the processing time of the ray tracer. Reducing this would obviously be worth the effort.

4 Reducing Intersection calcs enclose objects that are near each other within a bounding volume (e.g. a sphere, a cube, etc.)‏ do this for all the clusters of objects that are in your world then when testing for ray object intersections, first determine which bounding volumes the ray intersects with then only among the objects in those bounding volumes that the ray intersects with do we try to compute the ray-object intersection –all the objects in all the bounding volumes that do not intersect the ray can be ignored picture on the board

5 Reducing Intersection calcs Space subdivision methods –picture the world in a large cube which is subdivided into smaller cubes cubes that contain surfaces can be subdivided into 8 smaller cubes can do this until you get some programmer-defined maximum numbers of allowable surfaces inside a cube –can store these cubes in a binary partition tree or octtree --- so we can efficiently process the cubes –shoot a ray and find out which small cube it first intersects with if the cube doesn't contain any surfaces continue down the ray to find the next cube it hits and so on if there are surface(s) inside the cube determine if there's an intersection if so, that's the one we use; if not, continue with next cube... picture on the board

6 Reducing Intersection calcs smaller maximum number of surfaces per cube implies less ray-object intersection calculations however, this leads to an increase in the number of cubes which increases the calculation to determine the ray path through the cubes

7 Ray / Sphere Intersection A ray is a “line” starting at some point and continuing out to infinity. P(s) = P 0 + R d s where P 0 is the starting point of the ray, R d is a unit directional vector and s is the parameter which represents the distance from P 0 Let's find the intersection of P(s) with a sphere with radius r and center point P c.

8 Ray / Sphere Intersection Earlier we saw an algebraic solution to the ray-sphere intersection. Here (with the accompanying drawings on the board) is a geometric solution that allows you to find out earlier in the computations if the ray and sphere do not intersect (thereby reducing unneeded computations.)‏ 1. Determine if the ray's origin (P 0 ) is outside or inside of the sphere by: P 0 P c = P c – P 0. If the length squared of P 0 P c < r 2 then P 0 is inside sphere (skip to step 4)‏ otherwise the it is outside the sphere and we continue onto step 2. 2. Find the length L, of the ray from P 0 to the point on the ray that is closest to the center of the sphere. Notice |R d | = 1, so we can multiply by it... L = | P 0 P c | cosA = |P 0 P c | |R d | cosA = P 0 P c R d If L < 0, then ray does not intersect with the sphere, done. Otherwise go to step 3.

9 Ray / Sphere Intersection 3. Find the square of the distance, E 2 from the point on the ray that is closest to the center of the sphere to the intersection of the the ray with the sphere. From pythagorean theorem, r 2 = E 2 + D 2 so E 2 = r 2 – D 2 Also from pythagorean theorem, D 2 = |P 0 P c | 2 – L 2 So, E 2 = r 2 – (|P 0 P c | 2 – L 2 )‏ If E 2 < 0, ray does not intersect the sphere, done. Otherwise go to step 4. 4. Find s, the parameter of the intersection to use for the ray equation P(s) = P 0 + R d s If ray originates outside the sphere then s = L – E If ray originates inside the sphere then s = L + E 5. Calculate the intersection using s from step 4, (x i, y i, z i ) = P 0 + R d s

10 Ray / Sphere Intersection 6. Calculate the normal vector at the intersection If ray's origin was outside the sphere, N = [(x i -x c )/r, (y i -y c )/r, (z i -z c )/r] If inside the sphere, N = [(x c -x i )/r, (y c -y i )/r, (z c -z i )/r] (this procedure is based on Drew Kessler's notes from Lehigh, 1999)‏

11 Ray / Polygon Intersection The first step in Ray/Polygon intersection is to compute the intersection of the ray with the plane that the polygon lives on. At this point we have –the plane equation Ax+By+Cz+D = 0 –the point of intersection P I = (x I, y I, z I ) of the ray and plane –the vertices V = (x j, y j, z j ) of the polygon on that plane A technique that makes computation easier at this point is to orthographically project the polygon onto either the x-y, y-z, or z-x plane. To do this we just have to ignore the same coordinate of all of the vertices of the polygon and we end up with 2d points. The best coordinate to ignore is the one whose corresponding coefficient in the plane equation is dominant (largest absolute value.)‏

12 Ray / Polygon Intersection The best coordinate to ignore is the one whose corresponding coefficient in the plane equation is dominant (largest absolute value.)‏ It gives us a polygon in 2d with the largest area of the 3 choices.

13 Ray / Polygon Intersection Note: this technique is a combination of ideas from –Computer Graphics Principles and Practive by Foley, Van Dam, Feiner and Hughes, 1996 Addison-Wesley –Dr. G. Drew Kessler's csc313 course 1999, Lehigh Univ. and –Dr. Xiaoyu Zhang's Advanced Computer Graphics & Visualization page http://courses.csusm.edu/cs697exz/ray_polygon.htm Example: If the plane equation is –0.2 x +0.4 y –0.8945 z +5 = 0 we would orthographically project the polygon onto the x-y plane (ignore the z coordinate of each vertex) This will yield the largest (area) projection. Once we have done this projection, we can translate the intersection point P I to the origin of this new 2d space (call it u-v coordinates). Then determine whether the origin is inside or outside the polygon by counting the number of polygon edge crossings with the positive u axis.

14 Ray / Polygon Intersection The next algorithm distinguishes between several different situations to determine when to add 1 to the number of times the polygon's edges cross the positive u axis. For some edge –1) if the signs of the v coordinate in both vertices are the same, then that edge DOES NOT cross the positive u-axis –2) if the signs of the v coordinate in both vertices are different AND both u coordinates are positive then that edge DOES cross the positive u-axis. –3) if the signs of the v coordinate in both vertices are different AND one of the u coordinates is positive then compute the intersection of the line with v=0. If the u coordinate of this intersection is positive then that edge DOES cross the positive u-axis. –4) if the signs of the v coordinate in both vertices are different AND one of the u coordinates is positive then compute the intersection of the line with v=0. If the u coordinate of this intersection is not positive then that edge DOES NOT cross the positive u-axis. –For cases 2 and 3 above, add 1 to a counter. Do this for all edges in the polygon in order and then check the value of the counter --- if it is odd then the point is in the interior of the polygon and hence the ray intersects with the polygon.

15 Ray / Polygon Intersection The n vertices of the polygon are (u i, v i ) where i goes from 0 to n-1. Pseudocode to determine if the origin is inside the polygon initialize numCross to 0 if v 0 > 0 then signHold1 = +1 else signHold1 = -1 for each edge (u a, v a ) to (u b, v b ) { if v b > 0, signHold2 = +1, else signHold2 = -1 if signHold1 != signHold2 { if u a and u b are both > 0, then numCross++ if one of u a or u b is > 0, then { if (crossPosUAxis(u a, v a, u b, v b ) then numCross++ } } signHold1 = signHold2 } if numCross is odd, then the ray and polygon intersect

16 Ray / Polygon Intersection pseudocode for crossPosUAxis recall the parametric equations of a line: u = u b + (u a - u b )t v = v b + (v a - v b )t = 0 (because we're looking for intersection with u axis)‏ so, use the second equation to solve for t then solve the first for u and get u = (u b + ( u a - u b ) * ( v b / (v b -v a ) ) )‏ crossPosUAxis(u a, v a, u b, v b )‏ if ( (u b + ( u a - u b ) * ( v b / (v b -v a ) ) ) > 0 ) return true else return false


Download ppt "CS 325 Introduction to Computer Graphics 04 / 26 / 2010 Instructor: Michael Eckmann."

Similar presentations


Ads by Google