Docsity

Ray Tracing: Recursive Algorithm for Pixel Color and Light Intensity, Study notes of Computer Graphics

Toggle sidebar
bg1
Lecture 17: Recursive Ray Tracing
Where is the way where light dwelleth? Job 38:19
1. Raster Graphics
Typical graphics terminals today are raster displays. A raster display renders a picture scan
line by scan line. A scan line is a horizontal line consisting of many small, tightly packed dots
called pixels. To display a scene using a raster display, we need to compute the color and intensity
of the light at each pixel. The purpose of this lecture is to explain how to compute the color and
intensity of light for each pixel given a 3-dimensional model of the objects in the scene and the
location, color, and intensity of the light sources.
2. Recursive Ray Tracing
For realistic rendering, we need the following setup: an eye point E, a viewing screen S, and
one or more point light sources with known location, color, and intensity (see Figure 1). We also
need to specify the locations and the shapes of all the objects in the scene, together with their
reflectivity, transparency, and indices of refraction. Our goal is to display the 2-dimensional
projection onto the screen of the 3-dimensional scene as viewed from the eye point, much like an
artist painting the scene or a still photographer capturing a vista. We shall accomplish this task by
tracing light rays from the scene back to the eye. Below is the basic ray tracing algorithm.
Ray Tracing Algorithm
For each pixel:
i. Find all the intersections of the ray from the eye to the pixel with every object in the scene.
ii. Keep the intersection closest to the eye.
iii. Compute the color and intensity of the light at this intersection point.
Render the scene by displaying the color and intensity at each pixel.
Step i of the ray tracing algorithm requires us to compute the intersections of a straight line
with lots of different surfaces. The difficulty of this intersection problem depends on the
complexity of the surfaces in the scene. For planar surfaces these intersections are easy to
compute: we simply solve one linear equation in one unknown. But for more complicated surfaces,
we shall need more sophisticated tools. We will study line-surface intersection algorithms for a
variety of different surfaces in Chapters 18 and 19.
Step ii of the intersection algorithm is straightforward; in this chapter we shall concentrate on
Step iii of the algorithm.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Ray Tracing: Recursive Algorithm for Pixel Color and Light Intensity and more Study notes Computer Graphics in PDF only on Docsity!

Lecture 17: Recursive Ray Tracing Where is the way where light dwelleth? Job 38:

1. Raster Graphics Typical graphics terminals today are raster displays. A raster display renders a picture scan line by scan line. A scan line is a horizontal line consisting of many small, tightly packed dots called pixels. To display a scene using a raster display, we need to compute the color and intensity of the light at each pixel. The purpose of this lecture is to explain how to compute the color and intensity of light for each pixel given a 3-dimensional model of the objects in the scene and the location, color, and intensity of the light sources. 2. Recursive Ray Tracing For realistic rendering, we need the following setup: an eye point E , a viewing screen S , and one or more point light sources with known location, color, and intensity (see Figure 1). We also need to specify the locations and the shapes of all the objects in the scene, together with their reflectivity, transparency, and indices of refraction. Our goal is to display the 2-dimensional projection onto the screen of the 3-dimensional scene as viewed from the eye point, much like an artist painting the scene or a still photographer capturing a vista. We shall accomplish this task by tracing light rays from the scene back to the eye. Below is the basic ray tracing algorithm. Ray Tracing Algorithm For each pixel: i. Find all the intersections of the ray from the eye to the pixel with every object in the scene. ii. Keep the intersection closest to the eye. iii. Compute the color and intensity of the light at this intersection point. Render the scene by displaying the color and intensity at each pixel. Step i of the ray tracing algorithm requires us to compute the intersections of a straight line with lots of different surfaces. The difficulty of this intersection problem depends on the complexity of the surfaces in the scene. For planar surfaces these intersections are easy to compute: we simply solve one linear equation in one unknown. But for more complicated surfaces, we shall need more sophisticated tools. We will study line-surface intersection algorithms for a variety of different surfaces in Chapters 18 and 19. Step ii of the intersection algorithm is straightforward; in this chapter we shall concentrate on Step iii of the algorithm.

€

screen

eye

€ reflection € refraction € light € light €

  • € shadow € pixel Figure 1: To determine the color and intensity at a pixel, a ray is cast from the eye point through the pixel. The color and intensity of the point where the ray first strikes a surface is computed by summing the contributions from the direct illumination of the unobstructed light sources and the contributions of reflected and refracted rays. To find the color and intensity of the light at a surface point, we need to consider not only the light shining directly on the surface from the light sources, but also the light reflected and refracted from these sources off of other surfaces. Thus the total intensity I at a surface point is given by € I = Idirect + ks Ireflected + kt Irefracted , (2.1) where, as we observed in Chapter 16, € Idirect = Iambiant + Idiffuse + Ispecular (2.2) and € 0 ≤ ks , kt ≤ 1 are the reflection and refraction (transparency) coefficients. These coefficients depend on the physical characteristics of the surface, which can be determined experimentally or simply set by the programmer. Again the light reflected and refracted from other surfaces depends on the light shining directly on these surfaces as well as on the light reflected and refracted onto these surfaces from other surfaces. These observations lead to the binary light tree depicted in Figure 2. 2

light source, then the surface point lies in shadow; otherwise the surface point is illuminated by the light source. Below we summarize this basic shadow casting algorithm. Shadow Casting Algorithm For each surface point visible to the eye, cast a virtual ray to each light source. i. If the virtual ray hits an opaque object before the virtual ray hits a light source, then omit the contribution of this light source. That is, for this light source, € Idiffuse = Ispecular = 0. ii. If the virtual ray hits a transparent or semi-transparent object before the virtual ray hits the light source, then scale the contribution of this light source and continue to look for further intersections. Note that in step i, an object may be self shadowed -- that is, a point on an object (e.g. a sphere) may lie in the object’s own shadow -- so we must compute intersections of the virtual ray even with the object containing the point of interest. Since we need to compute the intersections of the virtual ray with every object in the scene, shadow computations can be quite time consuming. To speed up shadow computations, we can take advantage of shadow coherence. The idea behind shadow coherence is that if a point P lies in the shadow of an object O , then a nearby point Q is also likely to lie in the shadow of the same object O. Therefore to speed up the shadow calculations, we store the current shadowing object and we test first for intersections with this object when we cast the virtual ray from the next point in the scene. Shadow rays are not refracted; if shadow rays were refracted, these rays would not hit the light source! We could solve this problem by casting rays starting from the light sources instead of from the surface points taking refraction into account, but this approach is very expensive and not generally done. Typically this problem is ignored, since the resulting scenes still appear realistic.

4. Reflection To find the color and intensity of the light at a surface point, we need to calculate the light reflected off this point from other surfaces in the scene. To compute the color and intensity of this reflected light, we consider the surface point as a virtual eye and we cast a secondary ray from the surface point treating the surface as a mirror (see Figure 3). As in the standard ray casting algorithm, we find all the intersections of this secondary ray with every object in the scene, keep the closest intersection point, and calculate the color and intensity of the light at this intersection point recursively. We then add a scaled value of this color and intensity to the color and intensity at the original point. Below we summarize this reflection algorithm. 4

Reflection Algorithm For each visible point on a reflecting surface: i. Use the law of mirrors -- angle of incidence = angle of reflection -- to calculate a reflected secondary ray (see Figure 3). ii. Find all the intersections of this reflected secondary ray with every object in the scene. iii. Keep the intersection closest to the surface point. iv. Compute the color and intensity of the light at this intersection point recursively and add a scaled value of this contribution, € ks Ireflected , to the color and intensity at the original point. To find the reflected secondary ray € R ( t ) , let €

V =

E − P

| E − P |

denote the unit vector from the point P to the eye point E , and let € V || and € V ⊥ denote the components of V parallel and perpendicular to the surface normal N at the point P (see Figure 3). Then €

V = V || + V ⊥ ,

so the reflected vector W is given by €

W = V || − V ⊥.

But €

V || = ( V • N ) N

V ⊥ = V − V || = V − ( V • N ) N.

Hence €

W = 2 ( V • N ) N − V

and € R ( t ) = P + tW. Note that the calculation here of the reflected vector W is the same as the calculation of the reflected vector R for specular reflection (see Chapter 16, Section 5). N θ

V

Surface

W

V ||

V ⊥

Eye

− V ⊥

P

Figure 3: Law of the mirror: angle of incidence = angle of reflection. Thus if V is the unit vector from the point P to the Eye, then the reflected vector W is given by €

W = V || − V ⊥.

N

€ θ 1 € Surface € θ 2 € W € Eye

− N

€ P €

€ − V € θ 1 − θ 2 € V € V ⊥ € V || € W || € W ⊥ Figure 4: The unit vector V from the point P to the eye, the surface normal N at the point P , and the refracted vector W. Here € θ 1 is the angle between the surface normal N and the vector V, and € θ 2 is the angle between the refracted vector W and the vector € − N. By Snell’s Law if the indices of refraction on either side of the surface are € c 1 and € c 2 , then € c 2 c 1

sin( θ 2 ) sin(θ 1 )

There are several ways to find the refracted vector W in terms of the known vectors N and V. For example, we could rotate € − N about the vector € N × V through the angle € θ 2 and then apply Snell’s Law to eliminate € θ 2 (see Exercise 4). Alternatively, we could compute € W = slerp (− N ,− V ,θ 2 / θ 1 ) , and again apply Snell’s Law to eliminate € θ 2 (see Exercise 3). Below we present a simpler method due to S. Schaefer that avoids both rotation and € slerp. Let € V || and € V ⊥ denote the parallel and perpendicular components of V relative to N , and let €

W ||

and € W ⊥ denote the parallel and perpendicular components of W relative to € − N (see Figure 4). Then by construction €

W || ||− V ||

W ⊥ ||− V ⊥.

Moreover, since V and W unit vectors € V || = cos( θ 1 ) V ⊥ = sin( θ 1 ) W || = cos( θ 2 ) W ⊥ = sin( θ 2 ).

Therefore by Equation (5.2), (5.3) and Snell’s Law (Equation (5.1)) €

W || =

cos( θ 2 ) cos( θ 1 )

(− V ||) = −

cos( θ 2 ) ( V • N )

V || (5.4)

W ⊥ =

sin( θ 2 ) sin( θ 1 )

(− V ⊥ ) = −

c 2 c 1

V ⊥. (5.5)

But recall that €

V || = ( V • N ) N

V ⊥ = V − ( V • N ) N.

Substituting these formulas for € V || and € V ⊥ into Equations (5.4) and (5.5) yields €

W || = −

cos( θ 2 ) ( V • N ) ( V • N ) N = −cos( θ 2 ) N €

W ⊥ = −

c 2 c 1 (^ V^ −^ ( V^ •^ N ) N ) , so €

W = W || + W ⊥ =

c 2 c 1 ( V • N ) − cos( θ 2 )

 N^ −^

c 2 c 1

V. (5.6)

It remains only to solve for € cos( θ 2 ) in terms of N and V. We proceed by applying the trigonometric identity € cos 2 ( θ) + sin 2 ( θ) = 1 together with Snell’s Law: € cos( θ 2 ) = 1 − sin 2 ( θ 2 ) = 1 − c 22 c 1 2 sin 2 ( θ 1 ) = 1 − c 22 c 1 2 (1−^ cos 2 θ 1 ) = 1 − c 2 c 1

2 1 − (N • V) 2 ( ). Thus we conclude that €

W =

c 2 c 1 ( N • V ) − cos( θ 2 )

 N^ −^

c 2 c 1

V , (5.7)

where € cos( θ 2 ) = 1 − c 2 c 1

2 1 − (N • V) 2 ( ).^ (5.8) The equation of the refracted ray is simply € R ( t ) = P + tW. Notice that for each color, we use the same index of refraction and therefore the same refracted ray. Technically this procedure is incorrect, but this tactic saves tremendously on computation time and the results still look realistic, so most ray tracing programs adopt this approach. There are some angles € θ 1 between the vector V to the eye and the normal vector N to the surface at which refraction cannot occur. This phenomenon is called total reflection. Total reflection may occur when € c 2 / c 1 > 1. In this case for some angles € θ 1 , 8

Total Reflection €

( N • V)

2

c 1 c 2

2 < 1 To implement recursive ray tracing, we need to be able to calculate two things: surface normals and line-surface intersections. We shall take up the calculation of these items for a variety of different surfaces in the next two chapters. Exercises:

  1. Show that the reflection vector W in Figure 3 can be computed in each of the following ways: a. By finding the mirror image of the vector V in the plane perpendicular to €

V ⊥.

b. By rotating the vector V by €

o around the normal vector N. c. By finding the mirror image of the vector V in the plane perpendicular to the normal vector N and then negating the result.

  1. Using the result of Exercise 1, part c, show that light following the law of the mirror -- angle of incidence = angle of reflection -- takes the shortest path from the light source through the mirror to the eye.
  2. Consider the refraction vector W in Figure 4. a. Show that € W = slerp (− N ,− V , t ) , for € t = θ 2 / θ 1 (see Chapter 11, Section 6). b. Use the result of part a together with Snell’s Law to derive Equation (5.7) for the refraction vector W.
  3. Consider the refraction vector W in Figure 4. a. Show that W is the result of rotating the vector € − N thorough the angle € θ 2 about the axis vector €

N × V

N × V

b. Using part a, show that €

W =

sin( θ 2 − θ 1 ) N − sin( θ 2 ) V sin( θ 1 )

c. Use the result of part b together with Snell’s Law to derive Equation (5.7) for the refraction vector W. 10