//*****************************************************************************************
// Disclaimer, Authorship and License
/*
	This code has been written to serve as a learning tool for the course MAE 410-574, 
	Virtual Reality Applications and Research, Spring 2007.
	
	Large parts of the code has been adapted from the examples given in the OpenGL 
	Programming Guide by Woo et. al and also from Computer Graphics Using OpenGL by FS Hill
	Some parts of the code has also been adapted from various resources available through 
	out the internet.I thank all those whose code and resources I have used to write these 
	examples. You may use this code for any non-commerical purpose. Feel free to include 
	and use parts of this code for your own application, but remember that this code is not
	entirely bug free, use it at your own risk..If you plan on using the code for your any 
	academic purpose please drop me an email. I would like to attach a link to your course 
	from my home page.

	The Author of this code is Govindarajan Srimathveeravalli, Dept. of Mech and Aero Eng. ,
	University at Buffalo.
*/
//******************************************************************************************
// Demonstrates the drawing of different types of polygons based on the key choice
// pressed. The location is taken based on the mouse click when the key is pressed.
// The color is added randomly.
// Try the following
// 1. scale the objects to some random value, use the rand() function used for the color code to
//    achieve this.


#include <stdlib.h>
#include <math.h>
#include <glut.h>


void drawTriangle( float x, float y )
{
	glColor3f( rand() %11/10., rand() %11/10., rand() %11/10. );
	float scale = 1.;
	if( scale > 0 )
	{	
		glBegin( GL_TRIANGLES );
			glVertex3f( scale*x, scale*y, 0. );
			glVertex3f( scale*x+50., scale*y, 0. );
			glVertex3f( scale*x+25., scale*y+25., 0. );
		glEnd();
	}
}

void drawRectangle( float x, float y )
{
	glColor3f( rand() %11/10., rand() %11/10., rand() %11/10. );
	float scale = 1.;
	if( scale > 0 )
	{
		glBegin( GL_POLYGON );
			glVertex3f( scale*x, scale*y, 0. );
			glVertex3f( scale*x+100., scale*y, 0. );
			glVertex3f( scale*x+100., scale*y+50., 0. );
			glVertex3f( scale*x, scale*y+50., 0. );
		glEnd();
	}
}

void keyBoardFunc( unsigned char key, int x, int y )
{
	switch( key )
	{
	case 't':
		{
			drawTriangle( x, y );
		} break;
	case 'r':
		{
			drawRectangle( x, y );
		} break;
	}
	glutSwapBuffers();
}

void reshape( int w, int h)
{
	// nothing to see here, please move along

}

void display( )
{

	// Clear the background before drawing
	glClear( GL_COLOR_BUFFER_BIT );

	// Coloring
	glColor3f( 1., 1., 1. );

	// Ensure all drawing commands are executed
	glutSwapBuffers( );
}

void init ( )
{
	// Set the background 
	glClearColor( 0., 0., 0. , 0. );

	// Setting up a rudimentary camera
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );
	gluOrtho2D( 0, 640, 480, 0 );
}

int main(int argc, char* argv[])
{

	// Initialize a window with command line arguments
	glutInit( &argc, argv );
	// Provide window with buffering, coloring information
	//glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
	// Position and Size the window
	glutInitWindowPosition( 50, 50 );
	glutInitWindowSize( 640, 480 );
	// Create the window with the given features with a 'suitable title'
	glutCreateWindow( "Hello World" );

	// do some initialization like setting up background color, camera, projection type etc.
	init( );

	// Assign a display 'callback function'
	glutDisplayFunc( display );

	// The reshape function is called everytime the window is relocated or resized ! duh
	glutReshapeFunc( reshape );

	// add the function to handle keyboard calls
	glutKeyboardFunc( keyBoardFunc );

	// Enter the graphics loop
	glutMainLoop( );

	return 0;
}


