Solution (source code)

= Solution

At a surface <point> $p$, let $\hat N$ be the outward <unit normal vector>, $\hat V$ the <unit vector> from $p$ toward the viewer, and $\hat L$ the <unit vector> from $p$ toward the light. The incoming propagation direction is therefore $-\hat L$. The <specular reflection> direction $\hat R$ points away from the surface along the ideal reflected ray.

Resolve $\hat L$ into its normal and tangential components:
$$
\hat L=(\hat N\cdot\hat L)\hat N+\bigl(\hat L-(\hat N\cdot\hat L)\hat N\bigr).
$$
Reflection of the incoming direction preserves its tangential component and reverses its normal component. Equivalently, the outgoing direction has the same normal component as $\hat L$ and the opposite tangential component, giving
$$
\boxed{\hat R=2(\hat N\cdot\hat L)\hat N-\hat L.}
$$
The <dot product> identity shows $\|\hat R\|=1$ when $\|\hat L\|=\|\hat N\|=1$.

In the <Phong reflection model>, ambient lighting approximates unmodelled indirect illumination and is independent of these directions. The diffuse term models <Lambertian reflectance> and varies as $\max(0,\hat N\cdot\hat L)$. The specular term models a highlight around $\hat R$ and varies as $\max(0,\hat R\cdot\hat V)^n$, where $n>0$ controls its sharpness. For a visible front surface, a convenient per-colour-channel equation is
$$
\boxed{I=k_a I_a+I_L\left[k_d\max(0,\hat N\cdot\hat L)+k_s\,\chi\,\max(0,\hat R\cdot\hat V)^n\right],}
$$
where $I_a$ is ambient illumination, $I_L$ is the source strength at the surface including any chosen attenuation, $k_a,k_d,k_s\geq0$ are material weights, and $\chi$ is one when $\hat N\cdot\hat L>0$ and zero otherwise. The weights can be colour-dependent. An idealized point source's incident strength decreases as inverse distance squared; the empirical equation can also use a prescribed attenuation law.

In degenerate geometric cases the effective weights switch off. A light behind or tangent to the surface contributes neither direct diffuse nor specular illumination. If $\hat R\cdot\hat V\leq0$, the specular term is zero. A viewpoint on the back side is rejected by <back-face culling> for a one-sided surface. A material with no highlight has $k_s=0$; with no diffuse response, $k_d=0$; without ambient illumination the ambient term vanishes. These zeros do not require renormalizing the other weights, and the empirical <Phong reflection model> does not impose a universal condition $k_a+k_d+k_s=1$. At coincident light/surface or eye/surface positions a direction cannot be normalized; such singular configurations require a separate convention, rather than substituting an undefined <unit vector> into the formula.