class Hedron{

	friend void display();

protected:
	
	short no_of_vertices, no_of_faces;
	float position[3], orientation[4];
	float size, red, green, blue;
	float* vertex; int* indices;
	unsigned int vertex_per_face;

	Hedron* next;
	static Hedron* list;
	
	void normalize(float& ax ,float& ay, float& az){
		double length = sqrt(ax * ax + ay * ay + az * az); 
		if (length != 0.0) { ax /= length; ay /= length; az /= length;}
	}
   
	void translate(float x, float y, float z) {
		position[0] += x; position[1] += y; position[2] += z;
		for (int i=0; i < 3 * no_of_vertices; i+=3){
			vertex[i] += x; vertex[i+1] += y; vertex[i+2] += z;
		}
	}		

// really should be called rotate_about_origin.  Uses rotation formula
// from Goldstein Classical Mechanics 2nd ed. (4-92).
	
	void rotate(float at, float ax, float ay, float az)	 {
		float at_rad = at * 3.141592653589793 / 180.0;
		float cos_at = cos(at_rad);
		float sin_at = sin(at_rad);
		normalize(ax, ay, az);
		for (int i=0; i < 3 * no_of_vertices; i+=3){
			float dot = ax * vertex[i] + ay * vertex[i+1] + az * vertex[i+2];
			float t0 = vertex[i]; float t1 = vertex[i+1]; float t2 = vertex[i+2];
			vertex[i]	= t0 * cos_at + ax * dot * ( 1.0 - cos_at ) + (t1 * az - t2 * ay) * sin_at;
			vertex[i+1] = t1 * cos_at + ay * dot * ( 1.0 - cos_at ) + (t2 * ax - t0 * az) * sin_at;
			vertex[i+2] = t2 * cos_at + az * dot * ( 1.0 - cos_at ) + (t0 * ay - t1 * ax) * sin_at;
		}
		
  	}

	void setdata(float tvertex[], int tindices[]){
	   		vertex = new float[3 * no_of_vertices];
		 	for(int i=0; i < (3 * no_of_vertices); i++) vertex[i] = tvertex[i]; 
			
			indices = new int[vertex_per_face * no_of_faces];
			for(int j=0; j < (vertex_per_face * no_of_faces); j++) indices[j] = tindices[j];
	}

public:

	Hedron(){}

	Hedron(float sz, int n, int f, unsigned int v):size(sz), no_of_vertices(n), no_of_faces(f), vertex_per_face(v) 
	{
		position[0] = 0.0; position[1] = 0.0; position[2] = 0.0;
	}	
	
// notice you have to translate object to origin, rotate it, and then 
// translate it back.

	void Rotate(float at, float ax, float ay, float az) {
		float x,y,z;
		x = position[0]; y = position[1]; z = position[2];
		
		translate(-x,-y,-z);
		rotate(at, ax, ay, az);
		translate(x,y,z);
	}

	void Translate(float x, float y, float z) {
		translate(x, y, z);
	}

	float* getVertex(){
		return vertex;
	}
	
	float* getPosition(){
		return position;
	}

	void addScene(float r=0.2, float g=0.2, float b=0.2)
	{
		red = r; green = g; blue = b;	// provide support for colors
		next = list;					// link list
		list = this;					//	""
	}
};



class Hexa: public Hedron {
public:
	
	Hexa(float sz, float x=0.0, float y=0.0, float z=0.0, float at=0.0, float ax=0.0, float ay=0.0, float az=0.0):Hedron(sz,8,6,4)
	{	
		float a = sz / sqrt(2.0);		
		float tvertex[] = {a,-a,-a,  a,a,-a,  a,a,a,  a,-a,a,  -a,-a,-a,  -a,a,-a,  -a,a,a,  -a,-a,a};
		int tindices[] = {0,1,2,3, 1,5,6,2, 5,4,7,6, 0,3,7,4, 0,4,5,1, 3,2,6,7};
		
		setdata(tvertex, tindices);
		rotate(at, ax, ay, az);
		translate(x, y, z);
	}

	~Hexa(){ delete [] vertex; delete [] indices;}

};

class Tetra: public Hedron {
public:

		Tetra(float sz, float x=0.0, float y=0.0, float z=0.0, float at=0.0, float ax=0.0, float ay=0.0, float az=0.0):Hedron(sz,4,4,3)
		{	
			float rad_30 = 30.0 * 3.141592653589793 / 180.0;
			float cos_30 = cos(rad_30); 
			float sin_30 = sin(rad_30);	
			float tvertex[] = {0,0,sz,  sz*cos_30,0,-sz*sin_30,  -sz*cos_30*sin_30,sz*cos_30*cos_30,-sz*sin_30,  -sz*cos_30*sin_30,-sz*cos_30*cos_30,-sz*sin_30};
			int tindices[] = {0,1,2,  0,2,3,  0,3,1,  1,3,2};
			
			setdata(tvertex, tindices);
			rotate(at, ax, ay, az);
			translate(x, y, z);
		}

		~Tetra()	 { delete [] vertex; delete [] indices; }

};

class Icosa: public Hedron {
public:

		Icosa(float sz, float x=0.0, float y=0.0, float z=0.0, float at=0.0, float ax=0.0, float ay=0.0, float az=0.0):Hedron(sz,12,20,3)
		{				
			float a = sz * 0.525731112119133606;
			float b = sz * 0.850650808352039932;
			float tvertex[] = {-a,0,b, a,0,b, -a,0,-b, a,0,-b, 0,b,a, 0,b,-a, 0,-b,a, 0,-b,-a, b,a,0, -b,a,0, b,-a,0, -b,-a,0};
			int tindices[] = {0,4,1, 0,9,4, 9,5,4, 4,5,8, 4,8,1, 8,10,1, 8,3,10, 5,3,8, 5,2,3, 2,7,3, 7,10,3, 7,6,10, 7,11,6, 11,0,6, 0,1,6, 6,1,10, 9,0,11, 9,11,2, 9,2,5, 7,2,11};
			
			setdata(tvertex,tindices);
			rotate(at, ax, ay, az);
			translate(x, y, z);
		}

		~Icosa()	 { delete [] vertex; delete [] indices; }

};

Hedron* Hedron::list = 0;
	




