Ejemplo n.º 1
0
Vector4* findReflectedRayDirection(Vector4 &N, Vector4& L){

	Vector4* R;
	Vector4* Rtemp = new Vector4();
	Rtemp->SetVector4(0,0,0,1);
	Rtemp->GetCopyOf(N);
	double N_dot_L = N.DotProduct(N,L);
	Rtemp->Elongate(2*N_dot_L);
	R = Rtemp->Subtract(*Rtemp,L);
	delete Rtemp;
	return R;
}
Ejemplo n.º 2
0
Color* getColor(Vector4& viewVector, double x, double y, double z, int depth){

	Color* color = new Color(0,0,0);
	int red = 0;
	int green = 0;
	int blue = 0;

	double ka_red = 0.0;
	double ka_green = 0.0;
	double ka_blue = 0.0;

	double kd_red = 0.0;
	double kd_green = 0.0;
	double kd_blue = 0.0;

	double ks_red = 0.0;
	double ks_green= 0.0;
	double ks_blue = 0.0;

	double shininess = 0.0;

	//The view vector from the point of intersection to where the user is standing.
	Vector4* V = findViewDirection(x,y,z);
	V->ConvertToUnit();

	//The normal vector to the surface obtained by interpolating the normals.
	Vector4* N = new Vector4();
	N->SetVector4(0,0,0,1);

	if(iType == SPHERE){
		N->GetCopyOf( *findNormalToSphere(x,y,z,spheres[index_sphere]) );
		N->ConvertToUnit();

		kd_red = spheres[index_sphere].color_diffuse[0];
		kd_green = spheres[index_sphere].color_diffuse[1];
		kd_blue = spheres[index_sphere].color_diffuse[2];

		ks_red = spheres[index_sphere].color_specular[0];
		ks_green = spheres[index_sphere].color_specular[1];
		ks_blue = spheres[index_sphere].color_specular[2];

		shininess = spheres[index_sphere].shininess;

	}
	if(iType == TRIANGLE){

		N->GetCopyOf( *findNormalToTriangle(x,y,z,triangles[index_triangle]) );
		N->ConvertToUnit();

		Vector4* Kd = interpolateKd(x,y,z, triangles[index_triangle]);
		Vector4* Ks = interpolateKs(x,y,z, triangles[index_triangle]);

		kd_red = Kd->getX();
		kd_green = Kd->getY();
		kd_blue = Kd->getZ();

		ks_red = Ks->getX();
		ks_green =  Ks->getY();
		ks_blue =  Ks->getZ();

		shininess = interpolateShininess(x,y,z, triangles[index_triangle]);

		delete Kd;
		delete Ks;
	}

	ka_red = kd_red;
	ka_green = kd_green;
	ka_blue = kd_blue;

	color->addMoreRed((int)getAmbientRed(ka_red));
	color->addMoreGreen((int)getAmbientGreen(ka_green));
	color->addMoreBlue((int)getAmbientBlue(ka_blue));

	int i =0;
	for(i = 0; i<num_lights; i++){
		//Light Vector (From the point of intersection to the light source)
		auto_ptr<Vector4> L(findLightDirection(x,y,z,lights[i]));
		L->ConvertToUnit();

		//Reflected Ray Vector
		auto_ptr<Vector4> R(findReflectedRayDirection(*N, *L));
		R->ConvertToUnit();

		double LN = L->DotProduct(*L, *N);
		double RV = R->DotProduct(*R, *V);

//		if(isShadowed(x,y,z,lights[i]) and LN>=0){
		if(isShadowed(x,y,z,lights[i])){
			red = 0;
			green = 0;
			blue = 0;

		}else{

			red = getRed(lights[i].color[0], ka_red, kd_red, ks_red, LN, RV, shininess);
			green = getGreen(lights[i].color[1], ka_green, kd_green, ks_green, LN, RV, shininess);
			blue = getBlue(lights[i].color[2], ka_blue, kd_blue, ks_green, LN, RV, shininess);

		}

//		cout << "more Red "<<red<<", ";/////////////////////////////////////////////////////////
//		cout << "more Green "<<green<<", ";
//		cout << "more Blue "<<blue<<endl;

		color->addMoreRed(red);
		color->addMoreGreen(green);
		color->addMoreBlue(blue);

//		if(depth < MAX_DEPTH){
//			auto_ptr<Ray> reflRay(new Ray(x,y,z,R->getX(), R->getY(), R->getZ()));
//			Color* newColor = traceRay(*reflRay, depth+1);
//
//			int newRed = (int)ks_red*newColor->getRed();
//			int newGreen = (int)ks_green*newColor->getGreen();
//			int newBlue = (int)ks_blue*newColor->getBlue();
//
//			color->addMoreRed(newRed);
//			color->addMoreGreen(newGreen);
//			color->addMoreBlue(newBlue);
//
//			delete newColor;
//		}
	}



	if(depth < MAX_DEPTH){
		auto_ptr<Vector4> reflVector(findReflectedRayDirection(*N, viewVector));
		reflVector->ConvertToUnit();

		auto_ptr<Ray> reflRay(new Ray(x,y,z,-1,-1,-1));
		reflRay->setXd(reflVector->getX());
		reflRay->setYd(reflVector->getY());
		reflRay->setZd(reflVector->getZ());

		double t_new = findIntersect(*reflRay);

		if(t_new != NO_INTERSECTION){
			double new_x_boom = getXIntersect(*reflRay, t_new);
			double new_y_boom = getYIntersect(*reflRay, t_new);
			double new_z_boom = getZIntersect(*reflRay, t_new);

			Color* newColor = getColor(*reflVector, new_x_boom, new_y_boom, new_z_boom, depth+1);
			int newRed = (int)(ks_red*newColor->getRed());
			int newGreen = (int)(ks_green*newColor->getGreen());
			int newBlue = (int)(ks_blue*newColor->getBlue());

			//cout<<"new colors"<<newRed<<", "<<newGreen<<", "<<newBlue<<endl;

			color->addMoreRed(newRed);
			color->addMoreGreen(newGreen);
			color->addMoreBlue(newBlue);

			delete newColor;
		}

	}




	delete N;
	iType = NONE;

	return color;
}