//
// Simple example of using ARB vertex/fragment programs in OpenGL
//
// Author: Alex V. Boreskoff <steps3d@narod.ru>,<alexboreskoff@mtu-net.ru>
//

#include    <gl/glut.h>
#include    <stdio.h>
#include    <stdlib.h>
    
#include    "libTexture.h"
#include    "TypeDefs.h"
#include    "Vector3D.h"
#include    "Vector2D.h"
#include	"Vector4D.h"
#include	"VertexProgram.h"
#include	"FragmentProgram.h"

VertexProgram	vertexProgram;
FragmentProgram	fragmentProgram;

//////////////////////// here goes main program data ///////////////////////////////

Vector3D	eye   ( 7, 5, 7 );			// camera position
Vector3D	light ( 5, 0, 4 );			// light position
Vector3D	rot ( 0, 0, 0 );
float	 	angle     = 0;
int			mouseOldX = 0;
int			mouseOldY = 0;

unsigned	decalMap;

/////////////////////////// here goes main glut part ///////////////////////////////

void init ()
{
    glClearColor ( 0.0, 0.0, 0.0, 1.0 );
    glEnable     ( GL_DEPTH_TEST );
    glEnable     ( GL_TEXTURE_2D );
    glDepthFunc  ( GL_LEQUAL     );

    glHint       ( GL_POLYGON_SMOOTH_HINT,         GL_NICEST );
    glHint       ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}

void display ()
{
	glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

												// draw the light
	glMatrixMode ( GL_MODELVIEW );
	glPushMatrix ();

	glDisable       ( GL_TEXTURE_2D );
	glTranslatef    ( light.x, light.y, light.z );
	glColor4f       ( 1, 1, 1, 1 );
	glutSolidSphere ( 0.1f, 15, 15 );

	glPopMatrix  ();
	glEnable     ( GL_TEXTURE_2D );

	glMatrixMode ( GL_MODELVIEW );
	glPushMatrix ();

    glRotatef    ( rot.x, 1, 0, 0 );
    glRotatef    ( rot.y, 0, 1, 0 );
    glRotatef    ( rot.z, 0, 0, 1 );

	vertexProgram.enable ();
	vertexProgram.bind   ();

	fragmentProgram.enable ();
	fragmentProgram.bind   ();

	glBindTexture  ( GL_TEXTURE_2D, decalMap );

	glutSolidTeapot ( 1.5 );

	vertexProgram  .disable ();
	fragmentProgram.disable ();

    glPopMatrix ();

	glutSwapBuffers ();
}

void reshape ( int w, int h )
{
   glViewport     ( 0, 0, (GLsizei)w, (GLsizei)h );
   glMatrixMode   ( GL_PROJECTION );
   											// factor all camera ops into projection matrix
   glLoadIdentity ();
   gluPerspective ( 60.0, (GLfloat)w/(GLfloat)h, 1.0, 60.0 );
   gluLookAt      ( eye.x, eye.y, eye.z,	// eye
					0, 0, 0,				// center
					0, 0, 1 );				// up

   glMatrixMode   ( GL_MODELVIEW );
   glLoadIdentity ();
}

void motion ( int x, int y )
{
    rot.y -= ((mouseOldY - y) * 180.0f) / 200.0f;
    rot.z -= ((mouseOldX - x) * 180.0f) / 200.0f;
    rot.x  = 0;

    if ( rot.z > 360 )
		rot.z -= 360;

	if ( rot.z < -360 )
		rot.z += 360;

    if ( rot.y > 360 )
		rot.y -= 360;

	if ( rot.y < -360 )
        rot.y += 360;

    mouseOldX = x;
    mouseOldY = y;

	glutPostRedisplay ();
}

void mouse ( int button, int state, int x, int y )
{
    if ( state == GLUT_DOWN )
    {
        mouseOldX = x;
        mouseOldY = y;
	}
}

void key ( unsigned char key, int x, int y )
{
	if ( key == 27 || key == 'q' || key == 'Q' )	//	quit requested
    exit(0);
}

void	animate ()
{
	angle  = 0.004f * glutGet ( GLUT_ELAPSED_TIME );

	light.x = 4*cos ( angle );
	light.y = 4*sin ( angle );
	light.z = 3 + 0.3 * sin ( angle / 3 );

	vertexProgram.enable    ();
	vertexProgram.bind      ();
	vertexProgram.local [0] = eye;
	vertexProgram.local [1] = light;
	vertexProgram.disable   ();

	glutPostRedisplay ();
}

int main ( int argc, char * argv [] )
{
								// initialize glut
	glutInit            ( &argc, argv );
	glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
	glutInitWindowSize  ( 500, 500 );


								// create window
	glutCreateWindow ( "Example of vertex and fragment assembly shaders" );

								// register handlers
	glutDisplayFunc  ( display );
	glutReshapeFunc  ( reshape );
	glutKeyboardFunc ( key     );
	glutMouseFunc    ( mouse   );
	glutMotionFunc   ( motion  );
	glutIdleFunc     ( animate );

	init           ();
	initExtensions ();
	printfInfo     ();

	assertExtensionsSupported ( "GL_ARB_vertex_program GL_ARB_fragment_program" );

	decalMap  = createTexture2D ( true, "wood1.bmp" );

	if ( !vertexProgram.load ( "example.vp" ) )
	{
		printf ( "Error loading vertex program:\n %s\n", vertexProgram.getErrorString ().c_str () );

		return 1;
	}

	if ( !fragmentProgram.load ( "example.fp" ) )
	{
		printf ( "Error loading fragment program:\n %s\n", fragmentProgram.getErrorString ().c_str () );

		return 2;
	}

    glutMainLoop ();

	return 0;
}
