Build your own 3D renderer - Simulating illumination using Phong shading

We now know when the camera sees an object, but how do we know what color the corresponding pixel is? To answer this, we need to simulate how light interacts with the object. In general, this is a hard problem, but we can approximate it with local illumination. We will consider only the object and the lights in the scene, ignoring the effects of other objects in the scene. One way to do this is with the Phong reflectance model, which consists of three parts:

Adding all of these contributions from all lights, we get the color of the object at a given point, when viewed from a particular direction.

The last piece of the puzzle is how to calculate the surface normal for an object. For a sphere, the normal at a point `vec bbp` can be found by normalizing the vector between the center of the sphere, `vec bbc`, and the point: `hat bbN = {vec bbp - vec bbc} / ||vec bbp - vec bbc||`

A vector pointing from a sphere's center to a point on the surface, giving us the surface normal

To find the surface normal `hat bbN` at point `vec bbp`, take the vector between `vec bbp` and the sphere's center `vec bbc`, then normalize it.