#include "ModelDrawer.h"
#include <GL/glut.h>

float lpos[4] = { 4, 10, 0, 0 };
float ldiff[4] = { 1, 1, 1, 1 };
float lambdiff[4] = { 0.4, 0.4, 0.4, 1 };

void ModelDrawer::drawRuin( bool usingStructuredLine, float transparencyFactor )
{
	float r,g,b;

	if( !usingStructuredLine )
	{
		r = 0.95; g = 0.95; b = 0.0;
	}
	else
	{
		r = 0; g = 0; b = 1.0;
	}
	glLineWidth(4.0);
	drawPiece(0,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,1,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,2,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,3,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,4,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,5,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,6,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,7,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,8,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,9,0,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(0,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,0,1,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,0,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,0,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,0,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,0,5,1,1,r,g,b,1.0-transparencyFactor);


	drawPiece(0,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(4,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(5,0,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(6,0,0,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(0,1,1,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,1,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,2,1,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(1,1,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,2,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,2,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,2,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,3,0,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(1,0,1,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(4,0,1,1,1,r,g,b,1.0-transparencyFactor);
	glLineWidth(1.0);
}

void ModelDrawer::drawReconstructedRuin( bool usingStructuredLine, float transparencyFactor )
{
	float r,g,b;
	if( !usingStructuredLine )
	{
		r = 0.95; g = 0.95; b = 0.0;
	}
	else
	{
		r = 0; g = 0; b = 1.0;
	}
	drawPiece(0,1,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,1,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,1,3,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(0,2,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,2,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,2,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,2,2,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(0,3,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,3,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,3,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,3,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,3,1,1,1,r,g,b,1.0-transparencyFactor);	

	drawPiece(0,4,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,4,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,4,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,4,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,4,1,1,1,r,g,b,1.0-transparencyFactor);	

	drawPiece(1,3,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,3,0,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(0,5,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,5,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,5,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,5,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(0,5,1,1,1,r,g,b,1.0-transparencyFactor);	

	drawPiece(1,4,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,4,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,4,0,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(1,5,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,5,0,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,5,0,1,1,r,g,b,1.0-transparencyFactor);


// 	drawPiece(0,6,5,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(0,6,4,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(0,6,3,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(0,6,2,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(0,6,1,1,1,r,g,b,1.0-transparencyFactor);	
// 
// 
// 	drawPiece(1,6,5,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(1,6,4,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(1,6,3,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(1,6,2,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(1,6,1,1,1,r,g,b,1.0-transparencyFactor);	
// 	drawPiece(1,6,0,1,1,r,g,b,1.0-transparencyFactor);	
// 
// 
// 	drawPiece(2,6,5,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(2,6,4,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(2,6,3,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(2,6,2,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(2,6,1,1,1,r,g,b,1.0-transparencyFactor);	
// 	drawPiece(2,6,0,1,1,r,g,b,1.0-transparencyFactor);	
// 
// 
// 	drawPiece(3,6,5,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(3,6,4,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(3,6,3,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(3,6,2,1,1,r,g,b,1.0-transparencyFactor);
// 	drawPiece(3,6,1,1,1,r,g,b,1.0-transparencyFactor);	
// 	drawPiece(3,6,0,1,1,r,g,b,1.0-transparencyFactor);	

	//

	drawPiece(1,3,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,3,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,3,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,3,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,3,1,1,1,r,g,b,1.0-transparencyFactor);	
	drawPiece(1,3,0,1,1,r,g,b,1.0-transparencyFactor);	


	drawPiece(2,3,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,3,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,3,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,3,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,3,1,1,1,r,g,b,1.0-transparencyFactor);	
	drawPiece(2,3,0,1,1,r,g,b,1.0-transparencyFactor);	


	drawPiece(3,3,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,3,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,3,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,3,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,3,1,1,1,r,g,b,1.0-transparencyFactor);	

	//

	drawPiece(1,0,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,0,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(1,0,4,1,1,r,g,b,1.0-transparencyFactor);	
	drawPiece(1,0,5,1,1,r,g,b,1.0-transparencyFactor);	


	drawPiece(2,0,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,0,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,0,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,0,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(2,0,1,1,1,r,g,b,1.0-transparencyFactor);	

	drawPiece(3,0,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,0,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,0,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,0,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(3,0,1,1,1,r,g,b,1.0-transparencyFactor);	

	drawPiece(4,0,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(4,0,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(4,0,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(4,0,2,1,1,r,g,b,1.0-transparencyFactor);

	drawPiece(5,0,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(5,0,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(5,0,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(5,0,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(5,0,1,1,1,r,g,b,1.0-transparencyFactor);	

	drawPiece(6,0,5,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(6,0,4,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(6,0,3,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(6,0,2,1,1,r,g,b,1.0-transparencyFactor);
	drawPiece(6,0,1,1,1,r,g,b,1.0-transparencyFactor);	
}

void ModelDrawer::drawPiece( float x, float y, float z, float xSize, float zSize, float r, float g, float b, float a )
{
	// quads
	// 1 unit size cube
	float v_quad_strip[18][3] = 
	{
		{ -0.5, -0.5, 0.5 }, //1
		{  0.5, -0.5, 0.5 }, //2
		{ -0.5,  0.5, 0.5 }, //3
		{  0.5,  0.5, 0.5 }, //4
		{ -0.5,  0.5,-0.5 }, //5
		{  0.5,  0.5,-0.5 }, //6
		{ -0.5, -0.5,-0.5 }, //7
		{  0.5, -0.5,-0.5 }, //8
		{ -0.5, -0.5, 0.5 }, //9
		{  0.5, -0.5, 0.5 }, //10
		// face esquerda
		{ -0.5, -0.5,-0.5 }, //11
		{ -0.5, -0.5, 0.5 }, //12
		{ -0.5,  0.5,-0.5 }, //13
		{ -0.5,  0.5, 0.5 }, //14
		// face direita
		{  0.5, -0.5, 0.5 }, //15
		{  0.5, -0.5,-0.5 }, //16
		{  0.5,  0.5, 0.5 }, //17
		{  0.5,  0.5,-0.5 }, //18

	};

	//  normals
	float n_quad_strip[18][3] = {

		{ 0, 0, 1 },  //1
		{ 0, 0, 1 },  //2
		{ 0, 0, 1 },  //3
		{ 0, 0, 1 },  //4  <
		{ 0, 1, 0 },  //5
		{ 0, 1, 0 },  //6  < 
		{ 0, 0,-1 },  //7 
		{ 0, 0,-1 },  //8  <
		{ 0,-1, 0 },  //9
		{ 0,-1, 0 },  //10 <

		{-1, 0, 0 },  //11
		{-1, 0, 0 },  //12
		{-1, 0, 0 },  //13 
		{-1, 0, 0 },  //14 <

		{ 1, 0, 0 },  //15
		{ 1, 0, 0 },  //16
		{ 1, 0, 0 },  //17 
		{ 1, 0, 0 },  //18 <


	};

	unsigned int t[18] = 
	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15, 16, 17 };

	glPushAttrib( GL_SHADE_MODEL );
	glShadeModel( GL_FLAT );

	// Scale and positioning 
	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
	glTranslatef( CUBE_D*0.5*xSize + x*CUBE_D, (CUBE_D*0.5) + y*CUBE_D , (CUBE_D*0.5*zSize) + z*CUBE_D );
	glScalef( CUBE_D, CUBE_D , CUBE_D );
	
	glColor4f( r, g, b, a );
		 
	glVertexPointer( 3,GL_FLOAT, 0, v_quad_strip );
	glNormalPointer( GL_FLOAT, 0, n_quad_strip );
	glDrawElements ( GL_QUAD_STRIP, 10, GL_UNSIGNED_INT, t );

	// face esquerda
	glVertexPointer( 3,GL_FLOAT, 0, v_quad_strip );
	glNormalPointer( GL_FLOAT, 0, n_quad_strip );
	glDrawElements ( GL_QUAD_STRIP, 4, GL_UNSIGNED_INT,&t[10] );

	// face esquerda
	glVertexPointer( 3,GL_FLOAT, 0, v_quad_strip );
	glNormalPointer( GL_FLOAT, 0, n_quad_strip );
	glDrawElements ( GL_QUAD_STRIP, 4, GL_UNSIGNED_INT, &t[14] );

	glPopMatrix();

	glPopAttrib();
}