#include "stdafx.h"
#include "models.h"
#include "ModelMovements.h"

void draw_parallelepiped(GLfloat sizes[3], GLubyte colors[6][3], GLuint textures[6])
{
	// Спереди	 
	if(textures[0] != -1)
	{
		glColor3ub(255, 255, 255);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, textures[0]);	

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glEnd(); 

		glDisable(GL_TEXTURE_2D);
	}
	else
	{
		glColor3ub(colors[0][0], colors[0][1], colors[0][2]);
		glBegin(GL_QUADS);
		glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glEnd(); 
	}	

	// Сверху	
	if(textures[1] != -1)
	{
		glColor3ub(255, 255, 255);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, textures[1]);	

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glEnd(); 

		glDisable(GL_TEXTURE_2D);
	}
	else
	{
		glColor3ub(colors[1][0], colors[1][1], colors[1][2]); 
		glBegin(GL_QUADS);
		glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glEnd(); 
	}

	// справа	
	if(textures[2] != -1)
	{
		glColor3ub(255, 255, 255);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, textures[2]);	

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glEnd(); 

		glDisable(GL_TEXTURE_2D);
	}
	else
	{
		glColor3ub(colors[2][0], colors[2][1], colors[2][2]); 
		glBegin(GL_QUADS);
		glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glEnd(); 
	}	

	// Сзади	
	if(textures[3] != -1)
	{
		glColor3ub(255, 255, 255);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, textures[3]);	

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glEnd(); 

		glDisable(GL_TEXTURE_2D);
	}
	else
	{
		glColor3ub(colors[3][0], colors[3][1], colors[3][2]); 
		glBegin(GL_QUADS);
		glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glEnd();
	}
	
	// Снизу	
	if(textures[4] != -1)
	{
		glColor3ub(255, 255, 255);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, textures[4]);	

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glEnd(); 

		glDisable(GL_TEXTURE_2D);
	}
	else
	{
		glColor3ub(colors[4][0], colors[4][1], colors[4][2]); 
		glBegin(GL_QUADS);
		glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glEnd(); 
	}	

	// слева	
	if(textures[5] != -1)
	{
		glColor3ub(255, 255, 255);
		glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, textures[5]);	

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glEnd(); 

		glDisable(GL_TEXTURE_2D);
	}
	else
	{
		glColor3ub(colors[5][0], colors[5][1], colors[5][2]); 
		glBegin(GL_QUADS);
		glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
		glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
		glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
		glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
		glEnd(); 
	}
}

void draw_parallelepiped(GLfloat sizes[3], GLubyte colors[6][3])
{
	glBegin(GL_QUADS);

	// Спереди
	glColor3ub(colors[0][0], colors[0][1], colors[0][2]); 
	glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
	glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
	glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
	glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);

	// Сверху
	glColor3ub(colors[1][0], colors[1][1], colors[1][2]); 
	glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
	glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
	glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
	glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);


	// справа
	glColor3ub(colors[2][0], colors[2][1], colors[2][2]); 
	glVertex3f( sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
	glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
	glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
	glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);

	// Сзади
	glColor3ub(colors[3][0], colors[3][1], colors[3][2]); 
	glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
	glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
	glVertex3f( sizes[0]/2,  sizes[1]/2, -sizes[2]/2);
	glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);


	// Снизу
	glColor3ub(colors[4][0], colors[4][1], colors[4][2]); 
	glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
	glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
	glVertex3f( sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
	glVertex3f( sizes[0]/2, -sizes[1]/2,  sizes[2]/2);

	// слева
	glColor3ub(colors[5][0], colors[5][1], colors[5][2]); 
	glVertex3f(-sizes[0]/2,  sizes[1]/2,  sizes[2]/2);
	glVertex3f(-sizes[0]/2, -sizes[1]/2,  sizes[2]/2);
	glVertex3f(-sizes[0]/2, -sizes[1]/2, -sizes[2]/2);
	glVertex3f(-sizes[0]/2,  sizes[1]/2, -sizes[2]/2);

	glEnd(); 
}


//void draw_body()
//{
//	GLfloat size[3] = {body_Xsize, body_Ysize, body_Zsize};
//	GLubyte colors[6][3] = {  
//		{41, 5, 183},   // Спереди
//		{64, 59, 11},   // Сверху
//		{102, 51, 0},   // Справа
//		{41, 5, 183},   // Сзади
//		{64, 59, 11},   // Снизу
//		{102, 51, 50},   // Слева
//	};
//
//	draw_parallelepiped(size, colors);
//}

void draw_body(GLuint textures[6])
{
	GLfloat size[3] = {body_Xsize, body_Ysize, body_Zsize};
	GLubyte colors[6][3] = {  
		{41, 5, 183},   // Спереди
		{64, 59, 11},   // Сверху
		{102, 51, 0},   // Справа
		{41, 5, 183},   // Сзади
		{64, 59, 11},   // Снизу
		{102, 51, 50},   // Слева
	};

	draw_parallelepiped(size, colors, textures);
}

void draw_LeftPart_body(GLuint textures[6])
{
	GLfloat size[3] = {body_Xsize, body_Ysize, body_Zsize / 2.0f};
	GLubyte colors[6][3] = {  
		{98, 29, 29},   // Спереди
		{64, 59, 11},   // Сверху
		{102, 51, 0},   // Справа
		{98, 29, 29},   // Сзади
		{64, 59, 11},   // Снизу
		{102, 51, 50},   // Слева
	};

	draw_parallelepiped(size, colors, textures);
}

void draw_RightPart_body(GLuint textures[6])
{
	GLfloat size[3] = {body_Xsize, body_Ysize, body_Zsize / 2.0f};
	GLubyte colors[6][3] = {  
		{41, 5, 183},   // Спереди
		{64, 59, 11},   // Сверху
		{102, 51, 0},   // Справа
		{41, 5, 183},   // Сзади
		{64, 59, 11},   // Снизу
		{102, 51, 50},   // Слева
	};

	draw_parallelepiped(size, colors, textures);
}

void draw_rectangle(GLfloat sizes[2], GLubyte color[3])
{
	glBegin(GL_QUADS);

	glColor3ub(color[0], color[1], color[2]);

	glVertex3f(-sizes[0]/2, -sizes[1]/2,  0);
	glVertex3f( sizes[0]/2, -sizes[1]/2,  0);
	glVertex3f( sizes[0]/2,  sizes[1]/2,  0);
	glVertex3f(-sizes[0]/2,  sizes[1]/2,  0);

	glEnd();
}

void draw_nose(GLubyte color[3])
{
	glBegin(GL_TRIANGLES);

	glColor3ub(color[0], color[1], color[2]);

	glVertex3f(-nose_Xsize / 2.0f, -nose_Ysize / 2.0f,  0);
	glVertex3f( nose_Xsize / 2.0f, -nose_Ysize / 2.0f,  0);
	glVertex3f( 0.0f, nose_Ysize / 2.0f,  0);

	glEnd();
}

void draw_eye(GLubyte color[3])
{
	GLfloat sizes[3] = {eye_Xsize, eye_Ysize};

	draw_rectangle(sizes, color);
}

void draw_mouth(GLubyte color[3])
{
	GLfloat sizes[3] = {mouth_Xsize, mouth_Ysize};

	draw_rectangle(sizes, color);
}

// Рисуется из центра
void draw_head(bool isLeftRobot)
{
	GLfloat size[3] = {head_Xsize, head_Ysize, head_Zsize};
	GLubyte colors[6][3] = {  
		{45, 69, 92},   // Спереди
		{64, 59, 11},   // Сверху
		{43, 128, 0},   // Справа
		{45, 69, 92},   // Сзади
		{64, 59, 11},   // Снизу
		{43, 128, 0},   // Слева
	};

	draw_parallelepiped(size, colors);	

	GLubyte color[3] = {212, 162, 0};
	
	// Рисуем глаза, нос и рот

	GLfloat X_translate = head_Xsize / 2.0f + 0.0001f;
	GLfloat angle = 90.0f;

	if(! isLeftRobot)
	{
		X_translate *= -1.0f;
		angle *= -1.0f;
	}

	// правый глаз
	glPushMatrix();
	glTranslatef(X_translate, head_Ysize / 5.0f, head_Zsize / 5.0f);		
	glRotatef(angle, 0.0f, 1.0f, 0.0f);
	draw_eye(color);
	glPopMatrix();

	// левый глаз
	glPushMatrix();
	glTranslatef(X_translate, head_Ysize / 5.0f, -head_Zsize / 5.0f);		
	glRotatef(angle, 0.0f, 1.0f, 0.0f);
	draw_eye(color);
	glPopMatrix();

	// нос
	/*	glPushMatrix();
	glTranslatef(X_translate, head_Ysize / 20.0f, 0.0f);		
	glRotatef(angle, 0.0f, 1.0f, 0.0f);
	draw_nose(color);
	glPopMatrix();*/

	// Рот
	glPushMatrix();
	glTranslatef(X_translate, -head_Ysize / 5.0f, 0.0f);		
	glRotatef(angle, 0.0f, 1.0f, 0.0f);
	draw_mouth(color);
	glPopMatrix();
}

// Рисуется из центра
void draw_leg()
{
	GLfloat size[3] = {leg_Xsize, leg_Ysize, leg_Zsize};
	GLubyte colors[6][3] = {  
		{20, 19, 22},   // Спереди
		{64, 59, 11},   // Сверху
		{33, 10, 5},   // Справа
		{20, 19, 22},   // Сзади
		{64, 59, 11},   // Снизу
		{33, 10, 5},   // Слева
	};

	draw_parallelepiped(size, colors);
}

// Рисуется с основания руки
void draw_hand(bool isLeft)
{
	GLfloat size[3] = {hand_Xsize, hand_Ysize, hand_Zsize};
	GLubyte colors[6][3] = {  
		{45, 45, 45},   // Спереди
		{10, 10, 10},   // Сверху
		{20, 20, 20},   // Справа
		{45, 45, 45},   // Сзади
		{10, 10, 10},   // Снизу
		{20, 20, 20},   // Слева
	};

	glPushMatrix();
	if(isLeft)
		glTranslatef(hand_Xsize / 2.0f, 0.0f, 0.0f);
	else
		glTranslatef(-hand_Xsize / 2.0f, 0.0f, 0.0f);

	draw_parallelepiped(size, colors);
	glPopMatrix();
}

// Из основания конуса
void draw_sword()
{
	glColor3ub(255, 0, 0); 
	GLUquadricObj *quadObj = gluNewQuadric();
	gluCylinder(quadObj, sword_radius, 0, sword_height, 10, 10);
}

// Из основания конуса
void draw_horns()
{
	glPushMatrix();
	glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
	glColor3ub(220, 30, 40); 
	GLUquadricObj *quadObj = gluNewQuadric();
	gluCylinder(quadObj, horn_radius, 0, horn_height, 10, 10);
	glPopMatrix();
}



void draw_leftRobot(GLfloat curTime, GLuint textures[])
{
	glPushMatrix();
	{
		PerformLeftTransition(curTime);

		draw_body(textures);
			
		// Head
		// Перемещаемся в центр головы
		glTranslatef(0.0f, body_Ysize / 2.0f + head_Ysize / 2.0f, 0.0f);
		draw_head(true);

		// Left leg
		glPushMatrix();
			// Перемещаемся из центра головы в центр ноги
			glTranslatef(- body_Xsize / 2.0f + leg_Xsize / 2.0f, -head_Ysize / 2.0f - body_Ysize - leg_Ysize / 2.0f, -body_Zsize / 4.0f);
			PerformLegsTurns(curTime, true, true);
			draw_leg();
		glPopMatrix();

		// Right leg
		glPushMatrix();
			// Перемещаемся из центра головы в центр ноги
			glTranslatef( body_Xsize / 2.0f - leg_Xsize / 2.0f, -head_Ysize / 2.0f - body_Ysize - leg_Ysize / 2.0f, body_Zsize / 4.0f);
			PerformLegsTurns(curTime, true, false);
			draw_leg();
		glPopMatrix();

		// Left Hand
		glPushMatrix();
			// Перемещаемся из центра головы в начало руки
			glTranslatef( 0.0f, -head_Ysize / 2.0f - hand_Ysize, -body_Zsize / 2.0f - hand_Zsize / 2.0f);
			glRotatef(-70.0f, 0.0f, 0.0f, 1.0f);	
			draw_hand(true);
		glPopMatrix();

		// Right Hand
		glPushMatrix();
			// Перемещаемся из центра головы в основание руки
			glTranslatef( 0.0f, -head_Ysize / 2.0f - hand_Ysize, body_Zsize / 2.0f + hand_Zsize / 2.0f);
			PerformLeftHandRotation(curTime);	
			draw_hand(true);

			glTranslatef(hand_Xsize, 0.0f, 0.0f);
			glRotatef(90.0f, 0.0f, 1.0f, 0.0f);

			draw_sword();
		glPopMatrix();

		// Рога
		glPushMatrix();
			// Перемещаемся из центра головы в основание рога 1
			glTranslatef( 0.0f, head_Ysize / 2.0f, head_Zsize / 4.0f);	
			//glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
			draw_horns();
			// В основание рога 2
			//glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
			glTranslatef( 0.0f, 0.0f, -head_Zsize / 2.0f);	
			//glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
			draw_horns();
		glPopMatrix();
	}
	glPopMatrix();
}


void draw_rightRobot(GLfloat curTime, GLuint textures[])
{
	glPushMatrix();
	{
		glPushMatrix();
		PerformRightBodyTransition(curTime, true);
		glTranslatef(0.0f, 0.0f, body_Zsize / 4.0f);
		draw_LeftPart_body(textures);
		glPopMatrix();

		glPushMatrix();
		PerformRightBodyTransition(curTime, false);
		glTranslatef(0.0f, 0.0f, - body_Zsize / 4.0f);
		draw_RightPart_body(textures);
		glPopMatrix();

		// Head
		// Перемещаемся в центр головы
		glPushMatrix();
		glTranslatef(0.0f, body_Ysize / 2.0f + head_Ysize / 2.0f, 0.0f);
		PerformRightHeadTransition(curTime);
		draw_head(false);
		glPopMatrix();

		// Left leg
		glPushMatrix();
		// Перемещаемся из центра тела в центр ноги
		glTranslatef(- body_Xsize / 2.0f + leg_Xsize / 2.0f, - body_Ysize / 2.0f - leg_Ysize / 2.0f, -body_Zsize / 4.0f);
		PerformLegsTransition(curTime, false, true);
		draw_leg();
		glPopMatrix();
		
		// Right leg
		glPushMatrix();
		// Перемещаемся из центра головы в центр ноги
		glTranslatef( body_Xsize / 2.0f - leg_Xsize / 2.0f, -body_Ysize / 2.0f - leg_Ysize / 2.0f, body_Zsize / 4.0f);
		PerformLegsTransition(curTime, false, false);
		draw_leg();
		glPopMatrix();
		
		// Left Hand
		glPushMatrix();
		// Перемещаемся из центра тела в начало руки		
		glTranslatef( 0.0f, body_Ysize / 2.0f - hand_Ysize, body_Zsize / 2.0f + hand_Zsize / 2.0f);
		PerformRightBodyTransition(curTime, true);
		glRotatef(70.0f, 0.0f, 0.0f, 1.0f);
		draw_hand(false);
		glPopMatrix();

		// Right Hand
		glPushMatrix();
		// Перемещаемся из центра тела в основание руки
		glTranslatef( 0.0f, body_Ysize / 2.0f - hand_Ysize, -body_Zsize / 2.0f - hand_Zsize / 2.0f);
		PerformRightHandTransitionAndRotation(curTime);	
		draw_hand(false);

		glTranslatef(-hand_Xsize, 0.0f, 0.0f);
		glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);

		draw_sword();
		glPopMatrix();

		// Рога
		glPushMatrix();
		// Перемещаемся из центра тела в основание рога 1
		glTranslatef( 0.0f, body_Ysize / 2.0f + head_Ysize, head_Zsize / 4.0f);		
		PerformRightHornsTransition(curTime, true);
		draw_horns();
		glPopMatrix();	

		// В основание рога 2
		glPushMatrix();		
		glTranslatef( 0.0f, body_Ysize / 2.0f + head_Ysize, -head_Zsize / 4.0f);	
		PerformRightHornsTransition(curTime, false);
		draw_horns();
		glPopMatrix();		
	}
	glPopMatrix();
}