Chapter 6
Light and Shading

Hot diggity! You are unstoppable. You just drew the silhouette of a three-dimensional sphere with nothing but some code and math! That’s, like, level-10 wizard stuff.

Still—sad, but true!—the results are not quite what most people think of as “3D rendered.” Time to fix that.

In this chapter, you’ll implement a model to simulate the reflection of light from a surface, which will finally allow you to draw that sphere and make it look three dimensional. In fact, by the end of the chapter, you’ll have rendered an image very much like this one:

images/lights/3d-sphere-doodled.png

To do this, you’ll add a source of light, and then implement a shading algorithm to approximate how brightly that light illuminates the surfaces it shines on. It might sound complicated, but it’s not. The truth is that most ray tracers favor approximations over physically accurate simulations, so that to shade any point, you only need to know four vectors. These are illustrated in the figure.

images/lights/shading-vectors-doodled.png

If P is where your ray intersects an object, these four vectors are defined as:

  • E is the eye vector, pointing from P to the origin of the ray (usually, where the eye exists that is looking at the scene).

  • L is the light vector, pointing from P to the position of the light source.

  • N is the surface normal, a vector that is perpendicular to the surface at P.

  • R is the reflection vector, pointing in the direction that incoming light would bounce, or reflect.

You already have the tools to compute the first two vectors:

  • To find E, you can negate the ray’s direction vector, turning it around to point back at its origin.

  • To find L, you subtract P from the position of the light source, giving you the vector pointing toward the light.

The surface normal and reflection vector, though…those are new. Before you can use those, we need to pause and talk about how to compute them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.54.63