#include "gleye.h"
#include <GL/glut.h>
#include <stdio.h>
#include "sphere.h"
#include "laminate.h"

extern globj_t* sphere ;

/*
gleyedata_t gleyedata = { 0, 0, 0, // triggers, mouse_x y
			  2.2, 26.2, 0.7, // camera
			  0,0,0, //speed
			  -268, -87, 164.5 };// angles
*/
gleyedata_t gleyedata = { 0, 0, 0, 2.9, 10.4, 0.7, 0, 0, 0, -265, -75, 164.5 } ;		  

/* handle mouse button pressing : sets an internal flag to keep track
   of pressed buttons, and start recording mouse position */
static void gleyeMouseFunc (int button, int state, int x, int y) {
  int bit;

  switch (button) {
  case GLUT_LEFT_BUTTON:     bit=GLEYE_LEFT_BUTTON ;        break;
  case GLUT_MIDDLE_BUTTON:   bit=GLEYE_MIDDLE_BUTTON ;      break;
  case GLUT_RIGHT_BUTTON:    bit=GLEYE_RIGHT_BUTTON ;       break;
  default: printf("unknown button") ; exit(1) ; 
  }

  switch (state) {
  case GLUT_DOWN:            
    SETBIT(gleyedata.triggers,bit) ;   
    gleyedata.mouse_last_x = x ;
    gleyedata.mouse_last_y = y ;
    break;
  case GLUT_UP:              
    CLEARBIT(gleyedata.triggers,bit) ; 
    break;
  default: printf ("unknown button state") ; exit(1) ;
  }

}

extern void drawFunc(void) ;

static void gleyeIdleFunc (void) {
  glMatrixMode(GL_MODELVIEW) ;
  glLoadIdentity () ;
  glRotatef(gleyedata.roll,0,0,1); 
  glRotatef(gleyedata.elevation,-1,0,0); 
  glRotatef(90+gleyedata.azimuth,0,1,0); 
  gluLookAt(gleyedata.camera_x,gleyedata.camera_y,gleyedata.camera_z,
	    gleyedata.camera_x,gleyedata.camera_y,gleyedata.camera_z-1,
	    0,1,0);
  drawFunc () ;

  glutPostRedisplay () ;
}

static void gleyeKeyboardFunc (unsigned char key, int x, int y) {
  if (key == 'x') exit(0) ;
  if (key == 'c') 
    printf ("x=%f, y=%f, z=%f, elevation=%f, azimuth=%f, roll=%f\n",
	    gleyedata.camera_x, gleyedata.camera_y, gleyedata.camera_z,
	    gleyedata.elevation, gleyedata.azimuth, gleyedata.roll) ;
  /* if (key == 'd')
     sphere_detail (sphere->data) ;*/
  if (key == 'l')
    sphere_adjust (sphere->data) ;
  /*  if (key == 'a')
  sphere_adjust (sphere -> data) ;*/
}

static void gleyeMotionFunc (int x, int y) {
  float delta_x = 0.1 * (x - gleyedata.mouse_last_x) ;
  float delta_y = 0.1 * (y - gleyedata.mouse_last_y) ;
  gleyedata.mouse_last_x = x ;
  gleyedata.mouse_last_y = y ;
  if (ISSET(gleyedata.triggers,GLEYE_RIGHT_BUTTON)) {
    gleyedata.camera_x += delta_x ;
    gleyedata.camera_y += delta_y ;
  }
  if (ISSET(gleyedata.triggers,GLEYE_LEFT_BUTTON)) {
    gleyedata.azimuth += delta_x ;
    gleyedata.elevation += delta_y ;
  }
  if (ISSET(gleyedata.triggers,GLEYE_MIDDLE_BUTTON)) {
    gleyedata.roll += delta_x ;
    gleyedata.camera_z += delta_y ;
  }

}

static void gleyeSpecialFunc (int code, int x, int y) {

}

callbacks_t gleyeCallbacks = { 
  gleyeIdleFunc,
  gleyeKeyboardFunc,
  gleyeMotionFunc,
  gleyeMouseFunc, 
  gleyeSpecialFunc
} ;

void gleyeSetInputCallbacks (callbacks_t c) {
  glutIdleFunc (c.idleFunc) ;
  glutKeyboardFunc (c.keyboardFunc) ;
  glutMouseFunc (c.mouseFunc) ;
  glutMotionFunc (c.motionFunc) ;
  glutSpecialFunc (c.specialFunc) ;
}

