#include "define.h"

int		localviewer = 0;
int		twoside = 0;

GLfloat mat_ambient[] = { 0.91, 0.1, 0.11, 1.0};
GLfloat mat_diffuse[] = { 0.43, 0.37, 0.57, 1.0 };
GLfloat mat_specular[] = { 0.33, 0.33, 0.52, 1.0 };
GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat mat_shininess[] = { 10.0 };

GLfloat light_Kambient[] = { 0.2, 0.2, 0.2, 1.0};
GLfloat light_position[] = { 5.0, 5.0, 5.0, 1.0 };
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[] ={ 1.0, 1.0, 1.0, 1.0};
GLfloat light_spotDir[] = { -1.0, -1.0, -1.0 };

CObject3D* g_pCar     = new CObject3D("data\\porsche.obj");
CCamera*   g_pCamera  = new CCamera();
CCubeMap*  g_pCubeMap = new CCubeMap(); 

bool	  g_bMoveState = true;
bool	  g_bCameraState =true;


void myInit(void)
{    
	glClearColor(0.25, 0.25, 0.25, 1.0);
	glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,localviewer);
	glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,twoside);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT,light_Kambient);
	glLightfv(GL_LIGHT0,GL_POSITION,light_position);
	glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
	glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
	glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
	glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,light_spotDir);
	glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,180.0f);
	glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,30.0f);	
	glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
	glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
	glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
	glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
	glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_CULL_FACE);		
	glEnable(GL_COLOR_MATERIAL);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);//*/
	g_pCubeMap->initTexture();
}


void myDisplay (void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	gluLookAt(g_pCamera->getEye().x, g_pCamera->getEye().y, g_pCamera->getEye().z, 
			  g_pCamera->getLookAt().x, g_pCamera->getLookAt().y, g_pCamera->getLookAt().z, 
			  g_pCamera->getUp().x, g_pCamera->getUp().y, g_pCamera->getUp().z);
			  //*/
	
	glPushMatrix();
	g_pCubeMap->drawCube(2000.f, g_pCar->getPos());
	glPushMatrix();
	glTranslatef(g_pCar->getPos().x,g_pCar->getPos().y,g_pCar->getPos().z);	
	glRotatef(g_pCar->getRotate(),0.f,1.f,0.f);
	g_pCar->RenderObject();
	glPopMatrix();
	glPopMatrix();
	
	glFlush();
	glutSwapBuffers();
}

void myResize (int width, int height)
{
	if (!height) return;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60., (float)width/height, .1, 1000.0);
}

void inputCheck()
{
	if(GetAsyncKeyState(VK_LEFT))
		g_pCar->leftRotate();
	if(GetAsyncKeyState(VK_RIGHT))
		g_pCar->rightRotate();
}

void myIdle()
{
	g_pCar->MoveObject();
	g_pCamera->setCamera(g_pCar->getPos(),g_bCameraState);
	glutPostRedisplay();
}

void mySKeyboard (int key, int x, int y)
{
	switch (key) {
		case GLUT_KEY_LEFT : 
			g_pCar->leftRotate();
			break;
		case GLUT_KEY_RIGHT : 
			g_pCar->rightRotate();
			break;
		default : return;
	}
	glutPostRedisplay();
}

void myKeyboard (unsigned char key, int x, int y)
{
	switch(key)
	{
	case 'z':
		{
			g_pCar->calSpeed(true);
		}break;
	case 'x':
		{
			g_pCar->calSpeed(false);
		}break;
	case 'q':
		{
			g_bCameraState=true;
		}break;
	case 'w':
		{
			g_bCameraState=false;
		}break;
	}
}

void main()
{
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(800,600);
	glutCreateWindow("RidingCar");
	glutReshapeFunc(myResize);
	glutDisplayFunc(myDisplay);
	glutKeyboardFunc(myKeyboard);  
	glutSpecialFunc(mySKeyboard);
	glutIdleFunc(myIdle);
	myInit();	
	glutMainLoop();

}