Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raytracing - how to combine diffuse and specular color?

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:

enter image description here

Here is the diffuse component only:

enter image description here

Here is the specular component only:

enter image description here

EDIT: Changing diffuse to Color diffuseColor = ColorMake(0.0f, 0.6f, 0.0f); Then the image looks like this:

enter image description here

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];
like image 374
OMH Avatar asked Mar 25 '13 16:03

OMH


People also ask

What is recursive ray tracing?

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.

What is Whitted ray tracing?

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.

Why is light tracing inefficient?

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.

Is ray tracing global illumination?

"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.


1 Answers

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.

like image 82
Rahul Banerjee Avatar answered Oct 10 '22 05:10

Rahul Banerjee