Document Sample

Ray Tracing G. Drew Kessler 1 An Ideal Ray Tracer Cast light rays from all light sources. Follow them to the eye point. Reflect off of solid objects. Pass through transparent objects (refraction). Obtain color by light absorption. Diffuse reflection/refraction multiplies rays. For rays that “hit” the eye point, intersect with window on view plane, which corresponds to a screen pixel. Add the light ray’s color intensities to the pixel. Problem: It could take forever to draw an image! G. Drew Kessler, Larry F. Hodges 2 Ray Tracing That Works Observation: We are only interested in rays that cross the eye point. Idea: Start at eye point and trace backwards. Light Source Shadow Ray Eye Object Eye Ray Shadow Ray Object Light Source G. Drew Kessler, Larry F. Hodges 3 Visible Surface Determination Ray tracing can be used as a visible surface determination algorithm Often called ray casting. (A ray is a uni-directional line segment). The algorithm: For each pixel Determine the world coordinates of the pixel’s center. Cast a ray from the eye through the pixel’s center. Check for intersection of this ray with any object. For closest intersection, determine color of object at that point. G. Drew Kessler, Larry F. Hodges 4 Color Determination To determine the color at the intersection of an eye ray with an object, we can: Simply assign the color of the object (no shading). Determine the shading of the object. Calculate the contribution of a texture at that point. Some combination of the above Shading: Cast ray from intersection point to each light source. The shadow ray. Light contributes to shading only if the shadow ray does not intersect an object in the scene. G. Drew Kessler, Larry F. Hodges 5 Intersections Ray tracing algorithms are dominated by intersection testing and determination Need efficient methods to test for intersection Does the eye ray or shadow ray intersect an object? Need efficient method to calculate intersection points and normal vectors (for lighting calculations). Ray/Sphere Ray/Plane Ray/Polygon G. Drew Kessler, Larry F. Hodges 6 Ray/Sphere Intersection (Algebraic Solution) Ray is defined by R(t) = Ro + Rd*t where t > 0. Ro = Origin of ray at (xo, yo, zo) Rd = Direction of ray [xd, yd, zd] (unit vector) Sphere's surface is defined by the set of points {(xs, ys, zs)} satisfying the equation: (xs - xc)2 + (ys - yc)2 + (zs - zc)2 - rs2 = 0 Center of sphere: (xc, yc, zc) Radius of sphere: rs G. Drew Kessler, Larry F. Hodges 7 Possible Cases of Ray/Sphere Intersection 1. Ray intersects 1 sphere twice with t>0 2 2. Ray tangent to sphere 3. Ray intersects sphere with t<0 3 4. Ray originates 4 inside sphere 5 5. Ray does not intersect sphere G. Drew Kessler, Larry F. Hodges 8 Solving For t Substitute the basic ray equation: x = xo + xd*t y = yo + yd*t z = zo + zd*t into the equation of the sphere: (x0 + xdt - xc)2 + (y0 + ydt - yc)2 + (z0 + zdt - zc)2 - rs2 = 0 This is a quadratic equation in t: At2 + Bt + C = 0, where A = xd2 + yd2 + zd2 B = 2[xd(x0 - xc) + yd(y0 - yc) + zd(z0 - zc)] C = (x0 - xc)2 + (y0 - yc)2 + (z0 - zc)2 - rs2 Note: A=1 G. Drew Kessler, Larry F. Hodges 9 Relation of t to Intersection We want the smallest positive t - call it ti t0 t1 Discriminant = 0 t0 t0 t1 t1 Discriminant < 0 G. Drew Kessler, Larry F. Hodges 10 Actual Intersection Intersection point, (xi, yi, zi) = (xo+xd*ti, yo+yd*ti, zo+zd*ti) Unit vector normal to the surface at this point is N = [(xi - xc) / rs, (yi - yc) / rs, (zi - zc) / rs] If the ray originates inside the sphere, N should be negated so that it points back toward the center. N N G. Drew Kessler, Larry F. Hodges 11 Summary (Algebraic Solution) 1. Calculate A, B and C of the quadratic 2. Calculate discriminant (If < 0, then no intersection) 3. Calculate t0 4. If t0 < 0, then calculate t1 (If t1 < 0, no intersection point on ray) 5. Calculate intersection point 6. Calculate normal vector at point Helpful pointers: Precompute rs2 Precompute 1/rs If computed t is very small then, due to rounding error, you may not have a valid intersection G. Drew Kessler, Larry F. Hodges 12 Ray/Sphere Intersection (Geometric Solution) In the case of the sphere there are a number of tests that we can make to determine that there is not an intersection. This approach allows us to avoid calculations and result in a faster algorithm. 1. Find if the ray's origin is outside the sphere. Ro OC rs Origin to center vector, OC = C - Ro C Length squared of OC = ||OC||2 If ||OC||2 < rs2 then the ray origin is inside the sphere (go to step 4) Else if ||OC||2 > rs2 then the ray origin is outside the sphere G. Drew Kessler, Larry F. Hodges 13 R/S Geometric Solution (cont.) 2. Find the length of the ray to the point along the ray closest to the sphere's center. L R(t) ß rs Ro OC C L = ||OC|| cos ß = ||OC|| ||Rd|| cos ß = OC•Rd If the ray is outside and points away from the sphere (L < 0), the ray must miss the sphere. G. Drew Kessler, Larry F. Hodges 14 R/S Geometric Solution (cont.) 3. Else find the squared distance from the closest approach along the ray to the sphere's center to the sphere's surface (HC2 = half chord distance squared). HC R(t) L D rs D2 = ||OC||2 - L2 Ro OC C HC2 = rs2 - D2 = rs2 - ||OC||2 + L2 If HC2 < 0, the ray misses the sphere. G. Drew Kessler, Larry F. Hodges 15 R/S Geometric Solution (cont.) 4. Else find t for the intersection ti = L - HC for rays originating outside the sphere ti = L + HC for rays originating inside or on the sphere 5. Calculate intersection point (xi, yi, zi) by plugging ti into Ro + tRd 6. Calculate normal vector [(xi - xc)/rs (yi - yc)/rs, (zi - zc)/rs] (negate if ray originates inside sphere) G. Drew Kessler, Larry F. Hodges 16 Summary (Geometric Solution) 1. Find distance squared between ray origin and center of sphere, test for ray outside sphere. 2. Calculate ray distance which is closest to the center of the sphere. Test if the ray points away from the sphere. 3. Find square of half chord intersection distance. Test if square is negative. 4. Calculate intersection distance. 5. Find intersection point. 6. Calculate normal vector at point. These techniques are no slower than the algebraic solution and often allow you to determine that there is no intersection using fewer calculations. G. Drew Kessler, Larry F. Hodges 17 Ray/Plane Intersection Ray is defined by R(t) = Ro + Rd*t where t > 0 Ro = Origin of ray at (xo, yo, zo) Rd = Direction of ray [xd, yd, zd] (unit vector) Plane is defined by [A, B, C, D] Ax + By + Cz + D = 0 for a point in the plane Normal Vector, N = [A, B, C] (unit vector) A2 + B2 + C2 = 1 G. Drew Kessler, Larry F. Hodges 18 Ray/Plane (cont.) Substitute the ray equation into the plane equation: A(xo + xdt) + B(yo + ydt) + C(zo +zdt) + D = 0 Solve for t: t = -(Axo + Byo + Czo + D) / (Axd + Byd + Czd) t = -(N • Ro + D) / (N • Rd) Note: The normal vector of the plane should usually (except for backface removal) be for the surface facing the ray. If it isn't then it should be reversed before illumination is calculated. If N • Rd > 0 then use N = -[A, B, C] G. Drew Kessler, Larry F. Hodges 19 What Can Happen? N • Rd = 0 N • Rd > 0 Ro Ro t<0 G. Drew Kessler, Larry F. Hodges t>0 20 Ray/Plane Summary Intersection point: (xi, yi, zi) = (xo + xdti, yo + ydti, zo + zdti) 1. Calculate N • Rd and compare it to zero. 2. Calculate ti and compare it to zero. 3. Compute intersection point. 4. Compare N • Rd to zero and reverse normal if appropriate G. Drew Kessler, Larry F. Hodges 21 Ray/Polygon Intersection A polygon is defined by a set of p points: Gn = {(xn, yn, zn) | n = 0, 1, ..., (p-1)} The polygon is in a plane, Ax + By + Cz + D = 0, Normal N = [A, B, C] 1. Find the intersection of the ray with the plane of the polygon. 2. Throw away the coordinate of each point for which the corresponding normal component is of the greatest magnitude (dominant coordinate). 3. You now have a 2-D polygon defined by the set of points {(un, vn) | n = 0, 1, .., p-1} 4. Translate the polygon so that the intersection point of the ray with the plane of the polygon goes to the origin of the u,v system. Call these translated points {(u´n, v´n) | n = 0, 1, .., p-1} 5. Determine whether the origin is within the 2D polygon. G. Drew Kessler, Larry F. Hodges 22 Ray/Polygon (cont.) Y V V´ X U Z U´ G. Drew Kessler, Larry F. Hodges 23 Ray/Polygon Algorithm To determine whether the origin is within a 2D polygon, we only need to count the number of times the polygon’s edges cross the positive u´-axis as we walk around the polygon. Odd number crossings: origin is within. num_crossings = 0; sign_holder = sign(v´0); for(a=0; a < p; a++){ b = (a+1)% p; next_sign_holder = sign(v´b) if (sign_holder != next_sign_holder) { if (u´a > 0 and u´b > 0) num_crossings = num_crossings + 1; else if (u´a > 0 or u´b > 0){ int = u´a - v´a(u´b - u´a) / (v´b - v´a) if(int > 0) num_crossings = num_crossings + 1; } sign_holder = next_sign_holder; } } if(num_crossings % 2 == 1) ray intersects polygon G. Drew Kessler, Larry F. Hodges 24

DOCUMENT INFO

Shared By:

Categories:

Tags:

Stats:

views: | 8 |

posted: | 8/16/2011 |

language: | English |

pages: | 24 |

OTHER DOCS BY liaoqinmei

How are you planning on using Docstoc?
BUSINESS
PERSONAL

By registering with docstoc.com you agree to our
privacy policy and
terms of service, and to receive content and offer notifications.

Docstoc is the premier online destination to start and grow small businesses. It hosts the best quality and widest selection of professional documents (over 20 million) and resources including expert videos, articles and productivity tools to make every small business better.

Search or Browse for any specific document or resource you need for your business. Or explore our curated resources for Starting a Business, Growing a Business or for Professional Development.

Feel free to Contact Us with any questions you might have.