I am writing a shader according to the Phong Model . I am trying to implement this equation:

where n is normal, l is the direction of light, v is the direction to the camera, r is the reflection of light. The equations are described in more detail in the Wikipedia article.
As of now, I am only testing on directional light sources, so there is no recession r ^ 2. The external term is added beyond the function below, and it works well. The maxDot3 function returns 0 if the point product is negative, as is usually done in the Phong model.
Here is my code that implements the above equation:
#include "PhongMaterial.h" PhongMaterial::PhongMaterial(const Vec3f &diffuseColor, const Vec3f &specularColor, float exponent,const Vec3f &transparentColor, const Vec3f &reflectiveColor,float indexOfRefraction){ _diffuseColor = diffuseColor; _specularColor = specularColor; _exponent = exponent; _reflectiveColor = reflectiveColor; _transparentColor = transparentColor; } Vec3f PhongMaterial::Shade(const Ray &ray, const Hit &hit, const Vec3f &dirToLight, const Vec3f &lightColor) const{ Vec3f n,l,v,r; float nl; l = dirToLight; n = hit.getNormal(); v = -1.0*(hit.getIntersectionPoint() - ray.getOrigin()); l.Normalize(); n.Normalize(); v.Normalize(); nl = n.maxDot3(l); r = 2*nl*(nl); r.Normalize(); return (_diffuseColor*nl + _specularColor*powf(v.maxDot3(r),_exponent))*lightColor; }
Unfortunately, the mirror term seems to disappear for some reason. My conclusion:

The correct conclusion:

The first sphere has only diffuse and ambient shading. It looks right. The rest have mirror expressions and give incorrect results. What is wrong with my implementation?
source share