Download - Ray tracing
1
Ray Tracing
2
What is ray tracing
• Ray tracing is a technique for rendering three-dimensional graphics with very com-plex 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 re-flection and refraction by recursively follow-ing the path that light takes as it bounces through an environment
3
PCKTWTCH by Kevin Odhner, POV-Ray
Raytraced Images
4
Kettle, Mike Miller, POV-Ray
5
6
Ray Tracing Model
Source: wikipedia
7
Ray tracing
Shadow rays
Reflection ray
refracted ray
8
Ray tracing algorithm
• Builds the image pixel by pixel• Cast additional rays from the hit point to de-
termine the pixel color– Shoot rays toward each light. If they hit some-
thing, 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
9
Recursive ray tracing
• When a reflected or refraction ray hits a sur-face, 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
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 coor-dinates
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 re-flection direction– Construct using laws of reflection. X0 = surface point
• Transmitted rays: Rays from a point on a transparent sur-face through the surface– Construct using laws of refraction. X0 = surface point
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
13
Ray-Sphere Intersection
• Quadratic in t– 2 solutions: Ray passes through sphere - take mini-
mum 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?
012:
01 :Substitute
01 :Sphere
)( :Ray
0002
00
0
xxdxdd
dxdx
xx
dxx
tt
tt
tt
14
Sphere Intersection
A sphere is defined by its center, s, and its radius r. The intersec-tion of a ray with a sphere can be computed as follows:
15
Ray-Plane Intersections
• This is plane going through the origin– What about an arbitrary plane?
• To do polygons, intersect with plane then do point-in-polygon test…
dn
xn
xndn
dxn
xn
dxx
0
0
0
0
:
0:
0 :Substitute
0 :Plane
)( :Ray
t
t
t
tt
16
Intersections
• It is necessary to determine the in-tersection between a ray with objects to decide which pixel is
• Intersection with a sphere• Intersection with a plane• …
Ray Tracing IlluminationRecursive
dtransmittereflecteddirect IIIVEI ),(
),( dtransmittetdtransmitte VPIkI
shinyn
sdlightambientadirect RVkLNkIIkI ˆˆˆˆ
I
V
E
PreflectedI
reflectedV
dtransmitteI dtransmitteV
N
L R
directIVEI ),( reflecteddirect IIVEI ),(
),( reflectedrreflected VPIkI
Check for shadowing (intersection with object along ray (P,L))
The Ray Tree
R2
R1
R3
L2
L1 L3 N1
N2
N3
T1
T3
Ni surface normal
Ri reflected ray
Li shadow ray
Ti transmitted (refracted) ray
View-point
L1
T3 R3
L3 L2
T1 R1
R2
Eye
19
Reflection Rays
• The laws of reflection
20
Reflection
• Reflection angle = view angle
21
Reflection
• The maximum depth of the tree affects the handling of re-fraction
• 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.
22
Reflection
23
Refraction
Snell’s Lawsin
sint i
ri t
NMT ttˆcosˆsinˆ
i
i INM
sin
)ˆcosˆ(ˆ
NINT tii
t ˆcos)ˆcosˆ(sin
sinˆ
INT rtirˆˆ)coscos(ˆ
))ˆˆ(1(1sin1sin1cos
ˆˆcos
22222 IN
IN
rirtt
i
INININT rrrˆˆ))ˆˆ(1(1)ˆˆ(ˆ 22
N
N
M
T
Ii
t
IN iˆcosˆ
iN cosˆ
Note that I is the nega-tive of the incoming ray
24
Refraction
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 any-thing between the point and a light source
26
Pseudo Code for Ray Tracingrgb lsou; // intensity of light source rgb back; // background intensity rgb ambi; // ambient light intensity
Vector L // vector pointing to light sourceVector N // surface normalObject objects [n] //list of n objects in scene float Ks [n] // specular reflectivity factor for each objectfloat Kr [n] // refractivity index for each object float Kd [n] // diffuse reflectivity factor for each object Ray 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)}
}
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; }
28
Thank for attention!