//*****************************************************************************************
// 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.
*/
//******************************************************************************************
// This code demonstrates the basic mapping and principles behind 2D windows and a viewport
// Try the following
// 1. Relocate the "origin" of the world wrt to the window
// 2. What happens to drawn images when the window is not in a 1:1 ratio, how do you fix this
//    using the viewports?
// 3. Turn of the function where mouse clicks points are scaled according to the window size
// 4. Can you write an inverse function where from window points you can work back to points
//    in the world?


#include <stdlib.h>
#include <math.h>
#include <glut.h>
#include <iostream>

using namespace std;

typedef struct 
{
	float x;
	float y;
}point;


const int numPoints = 100;
unsigned int numClicks = 0;
point pointSet[ numPoints ];
unsigned int oldIndex = 0;

// what is the current window width and height? To achieve "correct scaling"
int currentWinW, currentWinH = 0;

void mouseClickFunc( int button, int state, int x, int y )
{
	if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
	{
		// note that our "origin (top left), remains the same
		x = x*(640./float(currentWinW));
		y = y*(480./float(currentWinH));
		cout << x << "  " << y << "\n";
		
		pointSet[ numClicks ].x = x;
		pointSet[ numClicks ].y = y;
		numClicks++;
	}
	glutPostRedisplay();
}


float aspectratio = 640./480.;

void reshape( int w, int h)
{
	// helps you "fill the specified area of the window
	glViewport( 0., 0., w, h );

	currentWinW = w;
	currentWinH = h;
	glutPostRedisplay();
}

void display( )
{
	// Clear the background before drawing
	glClear( GL_COLOR_BUFFER_BIT );

	// Coloring
	glColor3f( 1., 0., 0. );

	// without "scaling"
	glBegin( GL_POINTS );
		for( unsigned int i=0; i<numClicks; i++)
			glVertex3f( pointSet[i].x, pointSet[i].y, 0. );
	glEnd();

	for( unsigned int i=0; i<100; i++ )
	{
		float angle = 2*3.14*i/100;
		glBegin( GL_POINTS );
			glVertex3f( 200.+50.*cos(angle), 200.+50.*sin(angle), 0. );
		glEnd();
	}

	// drawing the sine curve here
	 
	glColor3f( 0., 1., 0. );
	for(unsigned int i=0; i<1000; i++)
	{
		glBegin( GL_POINTS );
			glVertex3f((float)i/10., 200.+100.*sin(3.14*i/180.), 0.);
		glEnd();
	} 

	// Ensure all drawing commands are executed
	glutSwapBuffers( );
}

void init( )
{
	// Set the background 
	glClearColor( 1., 1., 1. , 0. );
	glPointSize( 8.0 );

	// Setting up a rudimentary camera
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );

	// case 1: the world is set up with the origin at bottom left hand corner
	//gluOrtho2D( 0, 640., 0. , 480  );	

	// case 2: the world is set up with the origin at top left hand corner
	gluOrtho2D( 0, 640., 480. , 0.  );

	// case 3: the world is set up with the origin at the center of the screen
	//gluOrtho2D( -320, 320., -240. , 240  );

}

int main(int argc, char* argv[])
{

	// Initialize a window with command line arguments
	glutInit( &argc, argv );
	// Provide window with buffering, coloring information
	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 );

	// adding a function to handle mouse clicks
	glutMouseFunc( mouseClickFunc );

	// Enter the graphics loop
	glutMainLoop( );

	return 0;
}


