I am student doing the typical ray tracer in C on Sun Sparc. One of the features is glossy reflections, where the reflected angle is slightly skewed a number of times depending on a variable, and the resulting color is averaged. The problem is, all the glossy reflections come out pure white. You may think the color is not being averaged properly, but the strange part is, when a put a printf statement in the code to check the value of the color, it works. When the printf is commented out, the value is Not a Number (NaN), producing the white.... How can a printf statement cause this?
//check for reflections
if (hit.obj->specular.x > 0 || hit.obj->specular.y > 0 ||
hit.obj->specular.z > 0) {
int g,k;
g = scene->glossRays; //this value is 1.
Color newCol = {0,0,0}; // Color is 3 doubles for rgb
for (k = 0; k < g; k++) {
Ray rray; //ray is a vector and direction
if (hit.obj->glossy > 0.0) {
rray = reflectedRayG(ray, &normal, &hitPt, hit.obj->glossy);
} //glossy reflections call
else {
rray = reflectedRay(ray, &normal, &hitPt);
} //non glossy reflections
Color rc = shootRay(scene, &rray, bounce +1);
v3Print(&rc); // this is the printf to stderr that prints the color
// if commented out, newCol is NaN
// if left in, it works properly
newCol.x += hit.obj->specular.x * rc.x;
newCol.y += hit.obj->specular.y * rc.y;
newCol.z += hit.obj->specular.z * rc.z;
}// for k
v3Print(&newCol); //error checking color
newCol = v3Scale(&newCol, 1/(float)g);
c = v3Sum(&c, &newCol);
}//if reflections