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?
(Never mind the silly code, I'm just trying to get the principles correct first).
Here's how it looks:
Here is the diffuse component only:
Here is the specular component only:
EDIT: Changing diffuse to Color diffuseColor = ColorMake(0.0f, 0.6f, 0.0f); Then the image looks like this:
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 < 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];
Ray Tracing: Recursive. Eye-screen ray is the primary ray. Backward tracking of photons that could have arrived along primary. Intersect with all objects in scene.
The Whitted algorithm is the classical example of an algorithm that uses ray-tracing to produce photo-realistic computer generated images. Many more advanced light transport algorithms have been developed since the paper was first published.
Forward ray tracing is capable of determining the color of each object however this technique is inefficient. This is due to the fact that many rays from a light source never come through view plane and into the eye.
"Global illumination," the more advanced form of ray tracing, adds to the local model by reflecting light from surrounding surfaces to the object. A global illumination model is more comprehensive, more physically correct, and it produces more realistic images.
This has long been an issue with the "specular + diffuse + ambient" model of illumination. Namely, it is a hack and therefore, has no guarantee of correctness.
If you're so keen on cementing your fundamentals first, take a look at the excellent book "Physically Based Ray Tracing" by Matt Pharr and Greg Humphreys.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With