/////////////////----------- g_display.cpp ------------/////////////////////

void display(){
	int i,j;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	GLfloat mat_shiny[] = {4.0};
	GLfloat mat_specular[]= {0.9,0.9,0.9,1.0};
	GLfloat mat_ambient[] = {0.1,0.1,0.1,1.0};	

	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shiny);
	glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_ambient);
  	
	GLfloat light_position[] = {0,0,0,1};	
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	GLfloat light_position1[] = {0,-10,0,1};	
	glLightfv(GL_LIGHT1, GL_POSITION, light_position1);

	GLint box[] =   {-10,10,10, -10,-10,10, -10,-10,-10, -10,10,-10,  10,-10,10, 10,-10,-10,  -10,-10,-10, -10,-10,10,  10,10,-10, -10,10,-10, -10,-10,-10, 10,-10,-10};
	GLint nbox[] =  {1,0,0, 0,1,0, 0,0,1};
	
	// ================ DRAWS CO-ORD BOX =====================
	glBegin(GL_QUADS);
	for(i=0; i<3; i++){
		glNormal3iv(&nbox[3*i]);
		for(j=0; j<4; j++)	glVertex3iv(&box[12*i + 3*j]);
	}
	glEnd();
	// =======================================================
	
	unsigned int mode, vertex_per_face;
	int* indices;
	float* vertex;
	float d, d1[3], d2[3], norm[3];
	
// ===================== ACTION!! ============================
	for (const Hedron* p = Hedron::list; p; p=p->next)
		 {															//declare p const so we
		indices = p->indices; 										//can't modify *Hedron
		vertex = p->vertex;
		vertex_per_face = p->vertex_per_face;
		
		GLfloat hedron_ambient[] = {p->red,p->green,p->blue,1.0};	//important one
		glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, hedron_ambient);
			
		switch (vertex_per_face) {
		case 4: mode = 7;break;
		case 3: mode = 4;break;
		exit(1);
		}
		
		glBegin(mode);
		for (i=0; i < p->no_of_faces; i++) {

	// ===== CALCULATES FACE NORMALS FROM VERTEX DATA ======
			for (j=0; j<3; j++) {
				d1[j] = vertex[indices[vertex_per_face*i] * 3 + j] - vertex[indices[vertex_per_face*i+1] * 3 + j];
				d2[j] = vertex[indices[vertex_per_face*i+1] * 3 + j] - vertex[indices[vertex_per_face*i+2] * 3 + j];
			}		
		
			norm[0] = d1[1]*d2[2] - d1[2]*d2[1];
			norm[1] = d1[2]*d2[0] - d1[0]*d2[2];
 			norm[2] = d1[0]*d2[1] - d1[1]*d2[0];
			d = sqrt(norm[0]*norm[0]+norm[1]*norm[1]+norm[2]*norm[2]);
			if(d==0) exit(1);
			norm[0] /= d; norm[1]/= d; norm[2] /= d;
			glNormal3fv(norm);
	// =====================================================
  						
			glVertex3fv(&vertex[indices[vertex_per_face*i] * 3]);	
			glVertex3fv(&vertex[indices[vertex_per_face*i+1] * 3]);	
			glVertex3fv(&vertex[indices[vertex_per_face*i+2] * 3]);	
			if (mode == 7)
			glVertex3fv(&vertex[indices[vertex_per_face*i+3] * 3]);	//added for cube
		}
	glEnd();
	}
// ==========================================================
	glutSwapBuffers();
}

void reshape(int w, int h){
	
	glViewport(0,0,(GLsizei) w, (GLsizei) h); 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(80,(GLfloat) w/ (GLfloat) h,1.0,60.0);
	
	// =================== CAMERA ==========================
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(15,15,15,0,0,0,0,0,1);
	// =====================================================
}




void renderscene(int argc,char** argv)
{

	glutInit(&argc,argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (450,450);
	glutInitWindowPosition (0,0);
	glutCreateWindow ("display");
	
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glShadeModel(GL_SMOOTH);
	glEnable (GL_DEPTH_TEST);
	
	// ================== LIGHTS ============================

	GLfloat slight[] = {0.8,0.8,0.8,0.0};
	GLfloat alight[] = {0.8,0.8,0.8,1.0};
	glLightfv(GL_LIGHT0, GL_AMBIENT, alight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, alight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, slight);
	glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.04);


	GLfloat slight1[] = {0.6,0.6,0.6,0.0};
	GLfloat alight1[] = {0.8,0.8,0.8,1.0};
	glLightfv(GL_LIGHT1, GL_AMBIENT, alight1);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, alight1);
	glLightfv(GL_LIGHT1, GL_SPECULAR, slight1);
	glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.04);

	glEnable (GL_LIGHTING);
	glEnable (GL_LIGHT0);
	glEnable (GL_LIGHT1);

	// =====================================================
	
	glutReshapeFunc(reshape);   // Register callback functions
	glutDisplayFunc(display);	//

	glutMainLoop();		
}


