CMPS-3480 Computer Graphics

Multiple lights in ray tracer



How to correct your multiple lights.

When you loop to collect the diffuse light, do not add
the ambient light inside that loop.

Code from my lab10m.cpp written in class...

//Code at the bottom of the trace function

for (int i=0; i<g.nlights; i++) {
    //vector to the light source
    Vec v;
    v.diff(g.light_pos[i], closehit.p);
    Flt len = v.normalize();
    Flt sdot = closehit.norm.dotProduct(v);
    int shadow = 0;
    if (sdot > 0.0) {
        //are we in a shadow?
        shadow = getShadow(closehit.p, v, len);
    }
    //surface normal, dot product
    o->normal.normalize();
    Flt dot = v.dotProduct(o->normal);
    if (dot < 0.0)
        dot = 0.0;
    //mult diffuse light by the dot
    Vec diff(g.diffuse[i]);
    diff.scale(dot);
    Vec strength;
    if (!shadow)
        strength.add(diff);
    rgb.x += closehit.color.x * strength.x * weight;
    rgb.y += closehit.color.y * strength.y * weight;
    rgb.z += closehit.color.z * strength.z * weight;
}
rgb.add(g.ambient);  //<---- move ambient light to here
rgb.clamp(0.0, 1.0);