Beispiel #1
0
// Apply the phong model to this point on the surface of the object, returning
// the color of that point.
Vec3d Material::shade( Scene *scene, const ray& r, const isect& i ) const
{ 
	// YOUR CODE HERE

	// For now, this method just returns the diffuse color of the object.
	// This gives a single matte color for every distinct surface in the
	// scene, and that's it.  Simple, but enough to get you started.
	// (It's also inconsistent with the phong model...)

	// Your mission is to fill in this method with the rest of the phong
	// shading model, including the contributions of all the light sources.
    // You will need to call both distanceAttenuation() and shadowAttenuation()
    // somewhere in your code in order to compute shadows and light falloff.
	if( debugMode )
		std::cout << "Debugging Phong code..." << std::endl;

    //Intersection Point
    Vec3d P = r.at(i.t);
    Vec3d L = Vec3d(0,0,0);
    Vec3d V = -r.getDirection();

    //======[ Emission ]======
    L += ke(i);
    if(debugMode)
        std::cout << "ke(i): " << ke(i) <<"\n";

    //======[ Ambient ]======
    //missing Ka
    L += prod(ka(i),scene->ambient());
    if(debugMode){
        std::cout << "ambient: " << scene->ambient() <<"\n";
        std::cout << "ka(i): " << ka(i) <<"\n";
    }

    //iterate through lights
    for ( std::vector<Light*>::const_iterator litr = scene->beginLights(); 
         litr != scene->endLights(); 
         ++litr )
    {
        Vec3d Lc; //light contribution
        Light* pLight = *litr;
        Vec3d sa = pLight->shadowAttenuation(P);
        double da = pLight->distanceAttenuation(P);

        //======[ Diffuse ]======
        Vec3d direction = pLight->getDirection(P);
        Vec3d normal = i.N;
        if(debugMode)
        {
            std::cout << "D: ";
            clamp((normal*direction)*kd(i)).print();   
        }
        Lc =  clamp((normal*direction)*kd(i));
        
        //======[ Specular ]======
        Vec3d H = (V+direction)/2.0;
        if(debugMode)
        {
            std::cout << "S: ";
            clamp(ks(i)*pow((normal*H),shininess(i))).print();   
            std::cout <<"sa: ";
            sa.print();
        }
        Lc += clamp(ks(i)*pow((normal*H),shininess(i)));

        Lc = da * prod(Lc,sa);
        
        L+=Lc;
    }
	
    if(debugMode){
        std::cout <<"L: ";
        L.print();
    }
	return L;
}