Ejemplo n.º 1
0
void testCrossProduct(){
	Vector4 a;
	Vector4 b;
	a.SetVector4(1,2,0,1);
	b.SetVector4(2,1,1,1);
	Vector4* c;
	c = a.CrossProduct(b,a);
	c->DisplayVector4();

	delete c;
}
Ejemplo n.º 2
0
void testVector(){
	Vector4 a;
	Vector4 b;
	a.SetVector4(1,0,0,1);
	b.SetVector4(2,0,0,2);
	Vector4* c;
	c = a.Add(b,a);
	c->DisplayVector4();

	delete c;
}
Ejemplo n.º 3
0
Vector4* findViewDirection(double xS, double yS, double zS){

	Vector4* V = new Vector4();
	V->SetVector4(eX-xS, eY-yS, eZ-zS,1.0);
	V->ConvertToUnit();
	return V;
}
Ejemplo n.º 4
0
/**
 * Calculates the direction of the light source.
 * @param xS
 * @param yS
 * @param zS
 * @return
 */
Vector4* findLightDirection(double xS, double yS, double zS, Light& light){
	double xL = light.position[0];
	double yL = light.position[1];
	double zL = light.position[2];

	Vector4* L = new Vector4();
	L->SetVector4(xL-xS, yL-yS, zL-zS,1.0);

	return L;
}
Ejemplo n.º 5
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.º 6
0
Vector4* findNormalToSphere(double xS, double yS, double zS, Sphere& sphere){

	//Coordinates of the centre of the sphere.
	double x_c = sphere.position[0];
	double y_c = sphere.position[1];
	double z_c = sphere.position[2];

	//radius of the sphere.
	double S_r = sphere.radius;

	Vector4* normal = new Vector4();
	normal->SetVector4(xS-x_c, yS-y_c, zS-z_c, 1.0);
	normal->ConvertToUnit();
	return normal;

}
Ejemplo n.º 7
0
/**
 * Finds a cross b.
 * a and b must be in dimension 3 represented as 4 length vector in homogeneous coordinates.
 * @param a
 * @param b
 * @return
 */
Vector4* Vector4::CrossProduct(Vector4 &u, Vector4 &v){

	u.Homogenize();
	v.Homogenize();

	double a1 = u.getX();
	double a2 = u.getY();
	double a3 = u.getZ();

	double b1 = v.getX();
	double b2 = v.getY();
	double b3 = v.getZ();

	double x = a2*b3 - a3*b2;
	double y = a3*b1 - a1*b3;
	double z = a1*b2 - a2*b1;

	Vector4* product = new Vector4();
	product->SetVector4(x,y,z,1);

	return product;
}
Ejemplo n.º 8
0
/**
 * Finds a cross b.
 * a and b must be in dimension 3 represented as 4 length vector in homogeneous coordinates.
 * @param a
 * @param b
 * @return
 */
Vector4* Vector4::CrossProduct(Vector4 &u, Vector4 &v){

	u.Homogenize();
	v.Homogenize();

	float a1 = u.getX();
	float a2 = u.getY();
	float a3 = u.getZ();

	float b1 = v.getX();
	float b2 = v.getY();
	float b3 = v.getZ();

	float x = a2*b3 - a3*b2;
	float y = a3*b1 - a1*b3;
	float z = a1*b2 - a2*b1;

	Vector4* product = new Vector4();
	product->SetVector4(x,y,z,1);

	return product;
}
Ejemplo n.º 9
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;
}