Example #1
0
Vector3f shade(const Ray &ray, const Hit &hit, SceneParser * Scene )
{
	Material * material=hit.getMaterial();

	//need to get a specific diffuse color depending on whether or not we have triangle object
	//and whether or not this triangle object contains a texture 

	//as default set as diffuse color
	Vector3f _diffuseColor=material->getDiffuseColor();

	Vector2f uv; 
	Texture * tex; 

	if(hit.hasTex())
	{
		if((*material).isLoaded())
			cout << "texture should have been loaded" <<endl; 
		else
			cout << "Material is not loaded!" <<endl;

		uv=hit.getTexCoord();
		tex=(*material).getTexture();
		cout << "texture coordinates" << uv[0] << " " << uv[1] <<endl; 
		_diffuseColor=(*tex).getTexel(uv[0],uv[1]); //set diffuse color based on texture map
	}

	Vector3f _specularColor=material->getSpecularColor();
	float _shininess=material->getShininess(); 

        //first we need to figure out where this section of material is located
        Vector3f n=hit.getNormal();
        Vector3f dray=ray.getDirection();
        Vector3f p=ray.pointAtParameter(hit.getT());
        Vector3f c_amb=Scene->getAmbientLight();
        int num=Scene->getNumLights();

        Vector3f color=_diffuseColor*c_amb; //* is direct vector product

        Light * bob;
        Vector3f ldir, col,ref;
        float dist,val,val2;

        //iterate through light sources
        for(int i=0; i<num; i++)
        {
                bob=Scene->getLight(i);
                bob->getIllumination(p,ldir,col,dist);

                //compute contribution from diffuse reflection
                val=ldir.dot(ldir,n);
                if(val<0) //clamp value
                        val=0;
                color=color+_diffuseColor*col*val;

                //compute contribution from specular reflection
                ref=ldir-2*ldir.dot(ldir,n)*n;
                val2=pow(dray.dot(dray,ref),_shininess); //as dray moves off reflection we suppress 
                if(dray.dot(dray,ref)<0) //clamp value
                        val2=0;

                color=color+_specularColor*col*val2;
        }

        return color;
}