## Phong reflectance

We will use the Phong reflectance model with Phong specular highlights. In most reflectance models, the reflectance coefficients are represented by \(k\). Phong reflectance uses three reflectance values: ambient, diffuse, and specular. The goal is to compute the intensities of each component and sum for a final intensity.

$$
I = I*\mathrm{a} + I*\mathrm{d} + I_\mathrm{s}
$$

We are interested in computing the intensity at a point using a reflectance function. There are several vectors that are useful in reflectance calculations:

- \(n\): the normal vector away from the surface, perpendicular to the tangent
- \(v\): the view vector to the camera position
- \(\ell\): the vector to the light

When we are interested in the angles between vectors, we will often use the dot product to get the cosine of the angle between. This assumes the vectors are normalized. You should definitely make sure you normalize vectors before you use the dot product to get angle information.

### Normal vectors

When computing the normal vector, we only have the vertices that for the surface. The vertex order defines the 'front' of the surface. We will define the 'front' using the right-hand rule; that is, the side where the vector order wraps counter-clockwise is the 'front'. Normal vectors can be computed using the cross product of vectors formed from the vertex positions.

For example, in a triangle with vertices \(\mathbf{a}\), \(\mathbf{b}\), and \(\mathbf{c}\) the vectors would be \(\mathbf{b} - \mathbf{a}\), \(\mathbf{c} - \mathbf{b}\), and \(\mathbf{a} - \mathbf{c}\). Taking the cross product of these vectors will give the normal. Use the right-hand rule to verify that this is correct.

### Ambient reflectance

This reflectance term approximates the high order reflections of global illumination. For an ambient light value \(L_a\), the intensity is:

$$
I*\mathrm{a} = k*\mathrm{a}L_\mathrm{a}
$$

This looks like:

### Diffuse reflectance

This is the reflectance for light that is scattered by the rough or matte part of a surface. In general, the less light per unit area, the lower the reflected intensity should be. Thus, when a surface directly faces a light, it should be brighter than when it is off-angle from the light. This is proportional to the cosine of the angle between the surface normal and the light. Assuming the vectors are normalized, then the diffuse reflectance is:

$$
I*\mathrm{d} = k*\mathrm{d}L_\mathrm{d}(\mathbf{\ell}\cdot\mathbf{n})
$$

This looks like:

### Specular reflectance

Smooth surfaces work a bit like mirrors in that they can have shiny reflections. Most objects are not as smooth as mirrors, so reflections details are lost. We still see reflections on such surfaces, but only the most intense reflections, often light sources. Just as in a mirror, the reflectance off smooth surfaces is a function of the viewer location and the reflected object's location (lights in our case). Mirror like reflections are called specular reflections.

Specular reflections are only visible when the viewer looks at the surface and sees the reflection of the light source. To compute specular reflectance, we need the reflection vector of the light source (or the viewer).

In general, reflection vectors can be computed from an outgoing vector \(\mathbf{t}\) and the surface normal \(\mathbf{n}\):

$$ \mathbf{r} = 2(\mathbf{t} \cdot \mathbf{n}) \mathbf{n} - \mathbf{t} $$

Or from an incident vector \(\mathbf{i}\) and the surface normal \(\mathbf{n}\):

$$ \mathbf{r} = \mathbf{i} - 2(\mathbf{n} \cdot \mathbf{i}) \mathbf{n} $$

We will control the 'sharpness' of the reflection similar to how the falloff of the spotlight is handled: an exponent \( \alpha \) on the cosine term. The higher the exponent, the faster the falloff and the sharper the reflection. If we compute the reflection of the light as \( \mathbf{\ell_r} \), then the specular reflectance is:

$$
I*\mathrm{s} = k*\mathrm{s}L_\mathrm{s}(\mathbf{\ell_r}\cdot\mathbf{v})^\alpha
$$

This looks like:

### Final value

Summing all the intensities gives:

$$
I = I*\mathrm{a} + I*\mathrm{d} + I_\mathrm{s}
$$

This looks like:

We can now compute the reflectance at a single point! For general images, the reflectance must often be computed thousands of times: once for each point used to create the image.

There is one significant issue that remains: the dot product can be negative. This indicates the vectors point away from each other. If negative, the surface is probably facing away from the light and the reflectance should be 0. You need to detect this and handle it for diffuse and specular reflectance. The book covers an approach using 'halfway vectors' that is faster to compute.