title: Raytracing - how to combine diffuse and specular color?
body: <p>I've been reading numerous articles about ray tracing and shading, but my ray traced image does not look too good. I'm talking about the very bright green area near the specular highlight. The result green color is maxed out here, it looks like.
  How to adjust colors and/or shading calculations to make this look correct?</p>
  
  <p>(Never mind the silly code, I'm just trying to get the principles correct first).</p>
  
  <p>Here's how it looks:</p>
  
  <p><img src="https://i.stack.imgur.com/N64Zo.png" alt="enter image description here"></p>
  
  <p>Here is the diffuse component only:</p>
  
  <p><img src="https://i.stack.imgur.com/GL3AP.png" alt="enter image description here"></p>
  
  <p>Here is the specular component only:</p>
  
  <p><img src="https://i.stack.imgur.com/tIKpV.png" alt="enter image description here"></p>
  
  <p>EDIT: Changing diffuse to Color diffuseColor = ColorMake(0.0f, 0.6f, 0.0f);
  Then the image looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/SKZTM.png" alt="enter image description here"></p>
  
  <pre><code>Point lightPosition = PointMake(-100.0f, 100.0f, -100.0f);
  Color diffuseColor  = ColorMake(0.0f, 1.0f, 0.0f);
  Color specularColor = ColorMake(1.0f, 1.0f, 1.0f);
  Color pixelColor    = ColorMake(0.0f, 0.0f, 0.0f);
  
  //  Trace...
  
              // Diffuse
              Point intersectionPosition = PointMake(x, y, z);
              Vector intersectionNormal = VectorMake((x - xs) / rs, (y - ys) / rs, (z - zs) / rs);
              Vector intersectionNormalN = VectorNormalize(intersectionNormal);
              Vector lightVector          = VectorSubtract(lightPosition, intersectionPosition);
              VectorlightVectorN         = VectorNormalize(lightVector);
              float      cosTheta        = VectorDotProduct(intersectionNormalN, lightVectorN);
              if (cosTheta &lt; 0.0f)
              {
                  cosTheta = 0.0f;
              }
  
              pixelColor = ColorMultScalar(diffuseColor, cosTheta);
  
              // Specular
              Vector incomVector    = VectorSubtract(intersectionPosition, lightPosition);
              Vector incomVectorN   = VectorNormalize(incomVector);
  
              float myDot = - VectorDotProduct(incomVectorN, intersectionNormalN);
              float myLen = 2.0f * myDot;
  
              Vector tempNormal     = VectorMultScalar(intersectionNormalN, myLen);
              Vector reflectVector  = VectorAdd(tempNormal, incomVectorN);
              Vector reflectVectorN = VectorNormalize(reflectVector);
  
              float mySpec = MAX(-VectorDotProduct(reflectVectorN, incomVectorN), 0);
              mySpec       = powf(mySpec, 5);
  
              specularColor = ColorMultScalar(specularColor, mySpec);
              pixelColor    = ColorAdd(pixelColor, specularColor);
              pixelColor    = ColorClamp(pixelColor);
  
              [self putPixelatX:i andY:j andR:pixelColor.r andG:pixelColor.g andB:pixelColor.b];
  </code></pre>