#include <stdio.h>
#include <stdlib.h> 
//#include <glut.h>
#include <gl/freeglut.h>
#include <GL/GL.h>



#define ESC_KEY 27
#define UP_KEY 38
#define LEFT_KEY 37
#define RIGHT_KEY 39
#define DOWN_KEY 40
#define A_KEY 97

GLfloat rotate = 0.0f;
GLfloat angley = 0.0f;
GLfloat anglex = 0.0f;
GLfloat selfRotateAngle = 0.0f;

unsigned int mouseXCoord, mouseYCoord;
bool mouseDown = false; 
unsigned int width, height;

void mousePress(int button, int state, int x, int y ){

  switch(button) 
  {
  case GLUT_LEFT_BUTTON:
    switch(state) {
  case GLUT_DOWN:
      mouseDown = true;
    break;
  case GLUT_UP:
    mouseDown = false;
    break;
   }
  }
}
void mouseCord(int x, int y ){



  mouseXCoord = x;
  mouseYCoord = y;
  //printf("%d, %d\n",x,y);
}


void drawMouseTracker()
{
  //glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glPointSize(10.0f);
  glColor4f(1.0,1.0,1.0,0.01);
  if (mouseDown == true) {
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
  
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
    glBegin(GL_POINTS);
  
    //glVertex3f(0.0,0.0,0.0);
    glVertex3f(-1+2*(float)mouseXCoord/width,1-(2*(float)mouseYCoord/height),-1.0);
  
    glEnd();
  glPopMatrix();
  glPopMatrix();
  }
  glutSwapBuffers();
}

void drawCleanSheet() {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glutSwapBuffers();
}



void reshape(int width_, int height_)   
{
  width = width_;
  height = height_;
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);  
  glLoadIdentity();              
  if (height==0) 
  {
     height=1;
     gluPerspective ( 75, float(width), 1, 500.0 );
  }
  else
     gluPerspective ( 75, float(width)/height, 1, 500.0 );
  glMatrixMode(GL_MODELVIEW);
 }

void keyboard ( unsigned char key, int x, int y )  
{
  switch ( key ) 
  {
    case ESC_KEY:   
      printf("Hej");
		  exit(0);
      break;
    case A_KEY:
      drawCleanSheet();
      printf("clean sheet");
      break;
    default:        
      break;
  }
  glutPostRedisplay();
}

void processSpecialKeys(int key, int x, int y) {
  switch( key ) {
    case GLUT_KEY_LEFT:
      rotate = -270.0f;
      angley -= 10.0f;
      printf("rotate %f\n",rotate);
      break;
    case GLUT_KEY_RIGHT:
      angley += 10.0f;
      rotate = -90.0f;
      printf("rotate %f\n",rotate);
      break;
    case GLUT_KEY_UP:
      rotate = 0.0f;
      anglex -= 10.0f;
      printf("rotate %f\n",rotate);
      break;
    case GLUT_KEY_DOWN:
      rotate = 180.0f;
      anglex += 10.0f;
      printf("rotate %f\n",rotate);
      break;
    default:
      printf("unknown key\n");
      break;
  }
  glutPostRedisplay();
}


void main(int argc, char* argv[])  
{
 

  glutInit(&argc, argv); 
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  glutInitWindowSize(500, 500 );
  glutCreateWindow("My GLUT application");
  /*glutDisplayFunc(drawMultiColorCubes);
  glutIdleFunc(drawMultiColorCubes);*/
  glutDisplayFunc(drawMouseTracker);
  glutIdleFunc(drawMouseTracker);
  glutMouseFunc( mousePress );
  
  glutMotionFunc( mouseCord ); // When mouse button is down and moved
  glutPassiveMotionFunc( mouseCord ); // When mouse button is not down and moved
  
  glutKeyboardFunc(keyboard);
  glutReshapeFunc(reshape);
  glutSpecialFunc(processSpecialKeys);
  glutMainLoop();
  
  
  
}