/*
*	Introduction to OpenGL, sample code #2 [ GLUT ]
*	1st obtain a copy of glut from course website
*		for example  if you are using Microsoft Visual Studio
*		then copy the header file where you desire
*		in VSDIR\VC\includes\
*		and then copy the library file ( .lib extension ) to
*					   VSDIR\VC\lib\
*
*/

#include <windows.h>
//#include <gl/gl.h>
#include <glut.h>	// The header file for GLUT
#include <math.h>
#include <iostream>
using namespace std;

/*
*	write your simulation method here.
*	the functionality of GL and GLUT is the same. so you shouldn't have any 
*	problems porting your program from GL to GLUT.
*/

static float theta = 0.0f ;
int step = 0 ;
void simulation(int step){
            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
            //glClear (GL_COLOR_BUFFER_BIT);     
            glPushMatrix ();
			glRotatef (theta, 0.0f, 0.0f, 1.0f);
            int circle_points = 3000 ;
            double angle;
            float c = step / 1000 ;
            glBegin(GL_POINTS); 
                double R = 1 ;
                for (int i = 0; i < circle_points; i++) {    
                angle = 2*3.1415*i/circle_points; 
                glColor3f(0.1+(1.0/3000)*i, 0.2+c, 1-c);
                glVertex2f(R*cos(angle), R*sin(angle)); 
                R *= 0.92 ;
            } 
            glEnd();
			
            glPopMatrix ();
			step++;
            theta += 1.0f;
}


void Rotate(){           
    glRotatef (theta, 0.0f, 0.0f, 1.0f);
	theta++ ;
}


/*
*	used for controlling keyboard.
*/
void keyBoard( unsigned char t, int x, int y ){
	switch(t){
		case 'q':
			exit(0);
			break;
		case 'a':
			Rotate();
			glutPostRedisplay();
			break;
		case 'z':	// goto next simulation step.
			simulation(step);
			glutPostRedisplay();
		default:
			simulation(0);
			
	}
}

/*
*	if you need to get any input from user use this method.
*	it'll be called before the initiallization of glut.
*/
void input(){
	cout << "Motion-Simulation" << endl
		 << "-----------------" << endl ;
}

/*
*	at the end of your program print the meaningfull variables trough here
*/
void output(){
}

/*
*	pre-display method, do all initiallization here
*/
void init(){
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	
	glShadeModel(GL_FLAT);
}

/*
*	Display method for GLUT, it runs before the glutMainLoop, so we call the simulation method from here.
*/
void display(){
	//glClear(GL_COLOR_BUFFER_BIT);
		simulation(0);
	glutSwapBuffers();
}

/*
*	the main loop.
*/
int main(int argc, char** argv){
	input();	
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
	glutInitWindowSize(600, 400 );          // declare the size
	glutInitWindowPosition(100, 100);       // and position
	glutCreateWindow("Motion-simulation");  // and the name of GLUT window.
	init();
	glutDisplayFunc(display);				// two interesting function.
	glutKeyboardFunc(keyBoard);				// take notice that their argument is a function name! not variables.
	glutMainLoop();     // an infinity loop that keeps window open.
	output();
	system("PAUSE");
	return 0;
}
