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



#define SCREEN_WIDTH 864
#define SCREEN_HEIGHT 480

#define SCENE_DURATION 250

#define TIME_INTERVAL_MS 15

GLdouble eyeX = 2.0;
GLdouble eyeY = 1.0;
GLdouble eyeZ = 3.0;

int current_time = 0;

bool firsTime = true;

GLuint textureGround;
GLuint leftRobotTextures[6];
GLuint rightRobotTextures[6];

void printCoordinates()
{
	system("cls");
	printf("eyeX = %f\neyeY = %f\neyeZ = %f", eyeX, eyeY, eyeZ);
}


GLuint LoadTextureToMemory(char* filePath)
{
	GLubyte *pBytes;
	GLint iWidth, iHeight, iComponents;
	GLenum eFormat;

	GLuint texture;

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	pBytes = (GLubyte *)gltLoadTGA(filePath, &iWidth, 
		&iHeight, &iComponents, &eFormat);

	glGenTextures( 1, &texture );
	glBindTexture(GL_TEXTURE_2D, texture);	

	glTexImage2D(GL_TEXTURE_2D,0,iComponents,iWidth,iHeight,0,eFormat,GL_UNSIGNED_BYTE,pBytes);

	/*gluBuild2DMipmaps( GL_TEXTURE_2D, iComponents, iWidth, iHeight,
		eFormat, GL_UNSIGNED_BYTE, pBytes );*/

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	free(pBytes);

	return texture;
}

void LoadTextures()
{
	textureGround = LoadTextureToMemory("textures//moon//moon2.tga");

	GLuint textureLeftRobot = LoadTextureToMemory("textures//mosaic_5.tga");
	GLuint textuteRightRobot = LoadTextureToMemory("textures//mosaic_40.tga");

	leftRobotTextures[0] = -1;
	leftRobotTextures[1] = -1;
	leftRobotTextures[2] = textureLeftRobot;
	leftRobotTextures[3] = -1;
	leftRobotTextures[4] = -1;
	leftRobotTextures[5] = textureLeftRobot;

	rightRobotTextures[0] = -1;
	rightRobotTextures[1] = -1;
	rightRobotTextures[2] = textuteRightRobot;
	rightRobotTextures[3] = -1;
	rightRobotTextures[4] = -1;
	rightRobotTextures[5] = textuteRightRobot;
}



void DrawGround()
{	
	glBindTexture( GL_TEXTURE_2D, textureGround);

	glEnable(GL_TEXTURE_2D);	
	
	glBegin(GL_QUADS);		
	glColor3ub(255, 255, 255);

	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-4.0f, 0.0f,  4.0f);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f( 4.0f, 0.0f,  4.0f);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f( 4.0f, 0.0f,  -4.0f);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(-4.0f, 0.0f,  -4.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void display()
{
	GLfloat progress = (GLfloat)current_time / (GLfloat)SCENE_DURATION;

	//glClearColor(progress/3, progress/3, progress/3, 0.0f);			// Set background color to black and opaque
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);				// Clear the color buffer (background)	

	glLoadIdentity();
	gluLookAt(eyeX, eyeY, eyeZ, 0, 0, 0, 0, 1, 0);	

	//glutWireCube(2.4f); // Render the primitive  
	
	glPushMatrix();
		glTranslatef(0.0f, -body_Ysize / 2.0f - leg_Ysize, 0.0f);  // Трава
		DrawGround();
	glPopMatrix();

	// Robo left 
	glPushMatrix();
		glTranslatef(-1.4f, 0.0f, 0.0f);  // центр левого робота		
		draw_leftRobot(progress, leftRobotTextures);
	glPopMatrix();

	//-----------------------------------------------------------

	// Robo right
	glPushMatrix();
		glTranslatef(1.4f, 0.0f, 0.0f);  // центр правого робота
		draw_rightRobot(progress, rightRobotTextures);
	glPopMatrix();

	glTranslatef(-5.0f, 1.5f, -20.0f);  // центр правого робота
	glColor3ub(255, 255, 0);
	gluSphere(gluNewQuadric(), 1.0, 30, 30);

	glutSwapBuffers();
}

void keyboardFunc(int key, int x, int y) 
{
	switch (key)
	{

	case GLUT_KEY_LEFT:	// left arrow
		eyeX -= 0.1;
		break;

	case GLUT_KEY_RIGHT:	// right arrow
		eyeX += 0.1;
		break;

	case GLUT_KEY_UP:	// up arrow
		eyeY += 0.1;
		break;

	case GLUT_KEY_DOWN:	// down arrow
		eyeY -= 0.1;
		break;

	default:
		break;
	}
}



void keyboardFunc(unsigned char key, int x, int y) 
{
	switch (key)
	{
	case 27:     // Esc
		exit(0);
		break;

	case '8':	// numpad 8 (up)
		eyeZ += 0.1;
		break;

	case '2':	// numpad 2 (down)
		eyeZ -= 0.1;
		break;

	case '0':
		eyeX = 2.0; eyeY = 1.0; eyeZ = 3.0;
		break;

	default:
		break;
	}
}

void reshapeFunc(int w, int h)
{
	GLfloat fAspect;

	// Prevent a divide by zero
	if(h == 0)
		h = 1;

	// Set Viewport to window dimensions
	glViewport(0, 0, w, h);

	fAspect = (GLfloat)w/(GLfloat)h;

	// Reset coordinate system
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// Produce the perspective projection
	gluPerspective(35.0f, fAspect, 0.1, 40.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void timerFunc(int i) 
{
	static int time_delta = 1;

	current_time = i;

	if (i == SCENE_DURATION || i == -1) 
	{
		time_delta = -time_delta;
		//i = -1;
	}

	glutPostRedisplay();
	glutTimerFunc(TIME_INTERVAL_MS, timerFunc, i + time_delta);
}

int main(int argc, char **argv) 
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
	glEnable(GLUT_MULTISAMPLE);

	glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	glutCreateWindow("Robots");
	glEnable(GL_DEPTH_TEST);

	glutInitWindowPosition(0, 0);
	glutDisplayFunc(display);
	glutReshapeFunc(reshapeFunc);
	glutKeyboardFunc(keyboardFunc);
	glutSpecialFunc(keyboardFunc);
	glutTimerFunc(TIME_INTERVAL_MS, timerFunc, 0);

	glutFullScreen();

	LoadTextures();
	glutMainLoop();
	return 0;
}
