Ray Tracing

# Ray Tracing

Télécharger la présentation

## Ray Tracing

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Ray Tracing • Rendering Methods • Rays • Rays for screen images • Shadows • Recursion

2. Visible Surface Raytracing ObjectCoordinates ModelviewMatrix Eye coordinates IntersectionData Structure Raytracing then utilizes this data structure to build the image.

3. Raytracing • Invented by Arthur Appel in 1968 at the IBM T.J. Watson Research Center. • Not considered a major accomplishment since it was deemed impractical given its enormous computational requirements. • Languished in obscurity until the late 1970’s. SIGGRAPH’77 saw many significant developments, including the first looks at modeling reflection and shadows.

4. More History • A true recursive ray tracer was presented for the first time by Turner Whitted in the classic article “An improved illumination model for shaded display” in the June, 1980 Communications of the ACM. • Note that the article is more concerned with the model for illumination than the ray tracing algorithm.

5. That Pinhole Camera Model After eyespace transformation Projection Plane at z=-d

6. Raytracing • If we shoot a line from the center of projection through the center of a pixel and off into space… • What does it hit first? • If it hits an object, compute the color for that point on the object • That’s the pixel color

7. Rays • A Ray is a vector and a point • Point – The starting point of the ray • Vector – The ray direction • This describes an infinite line starting at the point and going in the ray direction • Ray t values • A distance along the ray t=1.5

8. t values • Any point along the ray with origin (xs,ys,zs) and direction(xd,yd,zd) can be described using t: • x = xs + t xd • y = ys + t yd • z = zs + t zd • Input: ray • Output: color

9. Where will the viewplane beand how big is it? We describe projection with: Field of view (angle) Aspect Ratio (x/y)

10. Viewplane z location y -z Z location is unimportant, field of view is what matters. We’ll use z=-1 Could be defined using fov or as a frustum

11. Viewplane size y y (in + direction) -z 1 (x,y) y = tan(fov/2) height = 2y x = aspect*y width = 2x (-x,-y)

12. What if I have a frustum? • glFrustum(left, right, bottom, top, znear, zfar) (r,t,n) (l,b,n)

13. What if I have a frustum? • glFrustum(left, right, bottom, top, znear, zfar) y = bottom/znear height = (top – bottom)/znear xleft = left/znear width = (right-left)/znear (r,t,n) (l,b,n)

14. The View Plane as a Pixel Grid (x,y) (11,7) 8 tall (-x,-y) (0,0) 12 wide

15. What we want • We want to shoot a ray • Starting point: (0,0,0) (center of projection) • Direction: through the center of a pixel • What we need is the coordinates of the center of a pixel

16. (x,y) (11,7) Pixel Centers (4,5) 8 tall (4,5) (-x,-y) (0,0) 12 wide

17. Visible Surface Raytracing // width – Screen width, height – Screen height// fov – Field of view in degrees, aspect – Aspect ratiodouble ey = tan(fov / 2. * DEGTORAD); double ehit=2 * ey;double ex = ey * aspect; double ewid = 2 * ex;for(int r=0; r<height; r++) for(int c=0; c<width; c++) { ray.start.Set(0,0,0); ray.dir.Set(-ex + ewid * (c + 0.5) / width, -ey + ehit * (r + 0.5) / height, -1.); ray.dir.Normalize(); // Compute color in ray direction // Save color as image pixel }

18. How do we figure out what we will hit? • Ray intersection tests • Reduces to solving for t • x = xs + t xd • y = ys + t yd • z = zs + t zd • Easiest is the sphere • (x-x0)2+(y-y0)2+(z-z0)2=r2 • Substitute ray equation in and solve for t • Note: Text uses two points on the line

19. Ray intersection with a sphere Questions: Why two t values? How do we tell if there is an intersection?

20. Intersections with a plane • Plane equation • Ax+By+Cz+D=0 • (A,B,C) is the plane normal • We can compute the normal and, given a single point, compute D Questions: Can t be negative? Zero? Why is this “more difficult” than a sphere?

21. Plane Intersection to Polygon Intersection • Given a plane intersection, the question is: • Is this point inside the polygon?

22. Polygon Interior Tests • In a 2D projection: • Find all edges that overlap in one dimension • Determine intersection of a line parallel with an axis and the overlapping edges • How many intersections are to the left of the point?

23. But, what projection? • Project the polygon for the maximum area • What is the largest component of the normal? • If x: Use a Y/Z projection • If y: Use an X/Z projection • If z: Use an X/Y projection • Why?

24. Another Way… • Suppose you know the intersection and know a normal for each edge that points towards the inside of the polygon How do we compute these normals? What is the characteristic of points inside the polygon?

25. Computing the Normals • Let v1, v2 be two vertices (in counter clockwise order). Then the edge normal is N x (v2-v1) • Orthogonal to the edge and the polygon surface normal (N). Surface Normal v2 v1 Edge Normal

26. Interior Test • For an edge v1,v2 with edge normal e1, the intersection p is on the inside side if (p-v1)e1 is not negative.

27. Why is ray tracing sometimes considered “impractical”? • Suppose you do a ray for every pixel and test against very polygon: • O(WHP) • Then if we allow recursion…

28. Faster Intersection Tests • Grouping objects and using bounding volumes • Bounding boxes are easier to test for intersections. • Your chair object could have a box around each polygon and a box around the entire object. • We can create hierarchical levels of bounding boxes pretty easily (cluster tree) • Spheres are a common bounding volume

29. Bounding box intersection test • Assumes: Upright box • Sometimes called “generalized box” • Consider box to have 6 planes • Planes are orthogonal to some axis • Box with range: (0.5, 0.7, -1) to (1.3, 2.2, -4) • Ray: R((0,0,0),(1, 1, -1)) • What is “t” for intersections with box planes orthogonal to the X axis?

30. Bounding box tests… • Conditions: • Box with range: (0.5, 0.7, -1) to (1.3, 2.2, -4) • Ray: R((0,0,0),(1, 1, -1)) • Remember: • x = xs + t xd • y = ys + t yd • z = zs + t zd • So, tx1 = (0.5 – 0) / 1 = 0.5 tx2 = (1.3 – 0) / 1 = 1.3 • What’s the condition that indicates we intersected the box?

31. Bounding box tests 0.8 0.9 Max tnear = 4.3 Max tnear = 3.5 Min tfar = 4.0 4.0 3.5 y2 4.3 4.6 Min tfar = 4.6 7.5 6.3 y1 x1 x2

32. Bounding box tests 0.8 Max tnear > min tfarNo intersection 0.9 Max tnear = 4.3 Max tnear = 3.5 Min tfar = 4.0 4.0 3.5 y2 4.3 Max tnear <= min tfarIntersection 4.6 Min tfar = 4.6 7.5 6.3 y1 x1 x2

33. Algorithm • Compute tnear and tfar for the three planes • Let tnear’ = max(tnear1, tnear2, tnear3) • Let tfar’ = min(tfar1, tfar2, tfar3) • If tnear’ ≤ tfar’ we have an intersection • Note that we can short-circuit this test after only two planes in some cases

34. Ray Walking Algorithms • We can also subdivide the space into boxes. Then we trace the ray through the series of boxes.

35. Ray Walking Algorithms • Problem: Not the same as line drawing: = extras for ray walking = line draw

36. Cleary and Wyvill ray walking Dx Dy dx dy

37. Spatial subdivisions • Uniform divisions • All boxes are equal sized • Advantages? Disadvantages? • Non-uniform divisions • How? • Why?

38. Octtree-based spatial subdivision

39. Speed-up • Item buffer • A z-buffer which holds pointers to objects rather than colors • How can this speed up ray tracing? • Text calls this: First-hit speedup

40. How do we compute the color? • Intersection gives a point on a polygon • Apply the color model • How do we get • View direction? • Light direction? • Normal?

41. How do we do? • Shadows? • Reflections?

42. Terms to know • Shadow feeler • Casting a ray in the direction of the light • If intersection is less than distance to the light, light is shadowed • Self shadowing • Roundoff errors can make us intersect ourselves

43. Recursion • Reflections • Simple compute the color in the reflection direction • What does this do to the running time? • Was O(WHP)

44. Adaptive Depth Control • How deep to you recurse? • A room with mirrors? • A room with only specular reflection? • Keep track of how much a ray will contribute to the final color • Falls below some threshold, don’t recurse anymore…

45. How do we do? • Shadows? • Penumbra? • Depth of field? • Motion blur? • Antialiasing?

46. Stochastic Ray Tracing • Problem with ray tracing: Point sampling • We can introduce randomness all over the place • Jitter for antialiasing • Light area for penumbra • Depth of field and motion blur

47. Minimum distance Poisson distribution • Better matches distribution in the eye • Algorithm • Generate a random point • If less than specified distance to nearest point, discard, otherwise keep

48. Distribution Comparisons Poisson Uniform

49. Adaptive sampling • Determine if number of samples is enough • If not, increase the number • Works for uniform and stochastic sampling

50. Ray tracing from the light sources • In regular ray tracing we don’t see • Lighting reflected through mirrors • Surfaces lit by reflection from other surfaces • Large set of rays from each light • Computes light that hits surfaces • General solution for diffuse reflection? • Still requires tracing from the eye as pass 2