Upcoming SlideShare
×

# Ray tracing

5,881 views

Published on

5 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
5,881
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
248
0
Likes
5
Embeds 0
No embeds

No notes for slide

### Ray tracing

1. 1. Ray Tracing 1
2. 2. What is ray tracing• Ray tracing is a technique for rendering three- dimensional graphics with very complex light interactions. This means you can create pictures full of mirrors, transparent surfaces, and shadows, with stunning results.• A very simple method to both understand and implement.• It is based on the idea that you can model reflection and refraction by recursively following the path that light takes as it bounces through an environment 2
3. 3. Raytraced Images PCKTWTCH by K evin Odhner, POV -Ray 3
4. 4. Kettle, MikeMiller, POV-Ray 4
5. 5. 5
6. 6. Ray Tracing ModelSource: wikipedia 6
7. 7. Ray tracing Shadow rays Reflection ray refracted ray 7
8. 8. Ray tracing algorithm• Builds the image pixel by pixel• Cast additional rays from the hit point to determine the pixel color – Shoot rays toward each light. If they hit something, the object is shadowed from that light, otherwise use “standard model” for the light – Reflection rays for mirror surfaces, to see what should be reflected in the mirror – Refraction rays to see what can be seen through transparent objects – Sum all the contributions to get the pixel color 8
9. 9. Recursive ray tracing• When a reflected or refraction ray hits a surface, repeat the whole process from that point – Send more out shadow rays – Send out new reflected rays (if required) – Send out a new refracted ray (if required) – Generally, reduce the weight of each additional ray when computing the contributions to the surface color – Stop when the contribution from a ray is too small to notice or maximum recursion level has been reached 9
10. 10. Ray tracing implementation• Ray tracing breakdown into two tasks – Constructing the ray to cast – Intersection rays with geometry• The former problem is simple vector arithmetic• Intersection calculation can be done in world coordinates or model coordinates 10
11. 11. Constructing Rays• Define rays by an initial point and a direction: x(t)=x0+td• Eye rays: Rays from the eye through a pixel – Construct using the eye location and the pixel’s location on the image plane. X0 = eye• Shadow rays: Rays from a point on a surface to the light. – X0 = point on surface• Reflection rays: Rays from a point on a surface in the reflection direction – Construct using laws of reflection. X0 = surface point• Transmitted rays: Rays from a point on a transparent surface through the surface – Construct using laws of refraction. X0 = surface point 11
12. 12. Ray-Object Intersections• Aim: Find the parameter value, ti , at which the ray first meets object i• Write the surface of the object implicitly: f(x)=0 – Unit sphere at the origin is x•x-1=0 – Plane with normal n passing through origin is: n•x=0• Put the ray equation in for x – Result is an equation of the form f(t)=0 where we want t – Now it’s just root finding 12
13. 13. Ray-Sphere Intersection Ray : x (t ) x0 td Sphere : x x 1 0 Substitute: x0 td x0 td 1 0 : d d t2 2 x0 d t x0 x0 1 0• Quadratic in t – 2 solutions: Ray passes through sphere - take minimum value that is > 0 – 1 solution: Ray is tangent - use it if >0 – 0 solutions: Ray does not hit sphere• Numerical stability is very important. For example, can a reflection ray hit the same sphere? 13
14. 14. Sphere IntersectionA sphere is defined by its center, s, and its radius r. The intersection of a raywith a sphere can be computed as follows: 14
15. 15. Ray-Plane Intersections Ray : x (t ) x0 td P lane: n x 0 Substit ute: n x0 td 0 : n d t n x0 0 n x0 :t n d• This is plane going through the origin – What about an arbitrary plane?• To do polygons, intersect with plane then do point-in-polygon test… 15
16. 16. Intersections• It is necessary to determine the intersection between a ray with objects to decide which pixel is• Intersection with a sphere• Intersection with a plane• … 16
17. 17. Ray Tracing Illumination Recursive L R E ˆ N V VreflectedI ( E, V ) I direct I reflected I transmitted I I reflected P I transmitted Vtransmitte d I reflected k r I ( P,Vreflected ) I transmitted kt I ( P,Vtransmitted ) I direct k a I ambient I light ˆ ˆ kd N L ks ˆ ˆ V R nshiny Check for shadowing (intersection with object along ray (P,L))
18. 18. The Ray Tree N3 T3 R Viewpoint 2 N2 T1 R R 3 1 L1 L2 N1 L3 L1 R T1 1 L2 L3 EyeNi surface normalRi reflected ray R R T3 2 3Li shadow rayTi transmitted (refracted) ray
19. 19. Reflection Rays• The laws of reflection 19
20. 20. Reflection• Reflection angle = view angle 20
21. 21. Reflection• The maximum depth of the tree affects the handling of refraction• If we send another reflected ray from here, when do we stop? 2 solutions (complementary) – Answer 1: Stop at a fixed depth. – Answer 2: Accumulate product of reflection coefficients and stop when this product is too small. 21
22. 22. Reflection 22
23. 23. Refraction ˆ N cos ˆ ˆ I N i sin Snell’s Law t i ˆ sin i t r ˆ I N cos i i ˆ MˆT sin ˆ M ˆ cos t N t ˆ ( N cos ˆ I) ˆ M i t ˆ sin ˆ N T iˆ sin t ˆ ˆ ˆT ( N cos i I ) cos t N Note that I is the negative of sin i the incoming rayˆT ( cos ˆ cos t ) N ˆ I r i r cos ˆ ˆ N I i cos 1 sin 2 1 2 sin 2 1 2 ˆ ˆ (1 ( N I ) 2 ) t t r i rTˆ ˆ ˆ (N I ) 1 2 ˆ ˆ ˆ (1 ( N I ) 2 ) N ˆ I r r r 23
24. 24. Refraction 24
25. 25. Shadow Rays• Shadows are important lighting effect that can be easily with ray tracing• If we wish to compute the illinumination with shadows for a point, we shoot an additional ray from the point to every light source• A light is only allowed to contribute to the final color if the ray doesn’t hit anything between the point and a light source 25
26. 26. Pseudo Code for Ray Tracingrgb lsou; // intensity of light sourcergb back; // background intensityrgb ambi; // ambient light intensityVector L // vector pointing to light sourceVector N // surface normalObject objects [n] //list of n objects in scenefloat Ks [n] // specular reflectivity factor for each objectfloat Kr [n] // refractivity index for each objectfloat Kd [n] // diffuse reflectivity factor for each objectRay r;void raytrace() { for (each pixel P of projection viewport in raster order) { r = ray emanating from viewer through P int depth = 1; // depth of ray tree consisting of multiple paths the pixel color at P = intensity(r, depth) }} 26
27. 27. rgb intensity (Ray r, int depth) { Ray flec, frac; rgb spec, refr, dull, intensity; if (depth >= 5) intensity = back; else { find the closest intersection of r with all objects in scene if (no intersection) { intensity =back; } else { Take closest intersection which is object[j] compute normal N at the intersection point if (Ks[j] >0) { // non-zero specular reflectivity compute reflection ray flec; refl = Ks[j]*intensity(flec, depth+1); } else refl =0; if (Kr[j]>0) { // non-zero refractivity compute refraction ray frac; refr = Kr[j]*intensity(frac, depth+1); } else refr =0; check for shadow; if (shadow) direct = Kd[j]*ambi else direct = Phong illumination computation; intensity = direct + refl +refr; } } return intensity; } 27
28. 28. Thank for attention! 28