Skip to main content

Simple Ray Tracer - Part 3

Specular Lighting


Specular lighting is the reflection of the light's color on the surface, and projected on the screen (or the viewers point of view).
In the Phong lighting model the Specular contribution to the light is defined as a relation between the vector from the camera to the surface, and the normal to the surface.
When the light hits the surface, it's reflected along the surface normal. Then, the angle formed between the reflected light vector and the screen vector is used to calculate the Specular component of the surface.
The Formula in the Phong model is expressed in the following snippet:


 Vec3 CalculateSpecular(Vec3 const& intersect, Vec3 const& n, Vec3 const& viewDir) const
 {
  Vec3 specular = color;
  Vec3 lDir(position.x - intersect.x, position.y - intersect.y, position.z - intersect.z);
  lDir.Normalize();

  double dDotN = 2 * dotP(lDir, n);
  Vec3 reflection = n;
  reflection*= dDotN;
  reflection.x = lDir.x - reflection.x;
  reflection.y = lDir.y - reflection.y;
  reflection.z = lDir.z - reflection.z;

  float spec = pow(max(dotP(viewDir, reflection), 0.0), 32);
  specular*= spec;
  return specular;
 }

Adding up this result to the diffuse color, we obtain the final result (in this example, with two lights):



Shadows

One of the advantages of Ray traced shadows vs other traditional methods is the accuracy and the simplicity of a basic shadow implementation.
Simply put, we will add a check before calculating the pixel's color. This validation traces a ray between the surface point (Hit by the previous ray trace), and the light source (considering it's a point light). If there's a hit (an object intersects the ray) the light will not contribute to the pixel's color.
The end result can be seen in the following image:



Comments