Example #1
0
/******************************************************************************
 * Determine color contribution of a lightsource (Phong model)
 * Specular part is returned in seperate parameter and added later
 *****************************************************************************/
const ntlColor
ntlLightObject::getShadedColor(const ntlRay &reflectedRay, const ntlVec3Gfx lightDir,
															 ntlMaterial *surf, ntlColor &highlight) const
{
  gfxReal ldot = dot(lightDir, reflectedRay.getNormal()); /* equals cos( angle(L,N) ) */
  ntlColor reflected_color = ntlColor(0.0);  /* adds up to total reflected color */
	if(mpGlob->getDebugOut() > 5) errorOut("Lighting dir:"<<lightDir<<"  norm:"<<reflectedRay.getNormal()<<"  "<<ldot );

  /* lambertian reflection model */
  if (ldot > 0.0) {
		//ldot *= -1.0;
    reflected_color += surf->getDiffuseRefl() * (getColor() * ldot );

    /* specular part */
    /* specular reflection only makes sense, when the light is facing the surface,
       as the highlight is supposed to be a reflection of the lightsource, it cannot
       be reflected on surfaces with ldot<=0, as this means the arc between light 
       and normal is more than 90 degrees. If this isn't done, ugly moiree patterns appear
       in the highlights, and refractions have strange patterns due to highlights on the
       inside of the surface */
    gfxReal spec = dot(reflectedRay.getDirection(), lightDir); // equals cos( angle(R,L) )
    if((spec > 0.0) && (surf->getSpecular()>0)) {
      spec = pow( spec, surf->getSpecExponent() ); /* phong exponent */
      highlight += getColor() * surf->getSpecular() * spec;
			//errorOut( " "<< surf->getName() <<" S "<<highlight<<" "<<spec<<" "<<surf->getSpecular()<<" "<<surf->getSpecExponent() );
    }

  }

  return ntlColor(reflected_color);
}