
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <svl/Vec2.h>
#include <svl/Vec3.h>

#include <pic.h>

const int WIDTH = 640;
const int HEIGHT = 480;
const int FOV = 40;

int g_iMenuId;

Vec2 g_vMousePos;
int g_iLeftMouseButton = 0;    // 1 if pressed, 0 if not.
int g_iMiddleMouseButton = 0;
int g_iRightMouseButton = 0;

enum CONTROLSTATE {ROTATE, TRANSLATE, SCALE};

CONTROLSTATE g_ControlState = ROTATE;

// state of the world
Vec3 g_vLandRotate(0,0,0);
Vec3 g_vLandTranslate(0,0,0);
Vec3 g_vLandScale(1,1,1);

Pic * g_pHeightData;


void myinit()
{
  //setup gl view here
  glClearColor(.0,.0,.0,.0);
}

void display()
{
  // draw 1x1 square about origin.
  // replace this code with your height field implementation
  // you may also want to precede it with your rotation/translation/scaling

  glBegin(GL_POLYGON);

  glColor3f(1.0,1.0,1.0);
  glVertex3f(-0.5, -0.5, 0.0);
  glColor3f(0.0,0.0,1.0);
  glVertex3f(-0.5,0.5, 0.0);
  glColor3f(0.0,0.0,0.0);
  glVertex3f(0.5,0.5, 0.0);
  glColor3f(1.0,1.0,0.0);
  glVertex3f(0.5,-0.5, 0.0);

  glEnd();
  
}

void menufunc(int value)
{
  switch (value)
  {
    case 0:
      exit(0);
      break;
  }
}

void doIdle()
{
  // do some stuff...

  // make the screen update
  glutPostRedisplay();
}

// converts mouse drags into information about rotation/translation/scaling
void mousedrag(int x, int y)
{
  Vec2 vMouseNew;
  Vec2 vMouseDelta;
  
  vMouseNew[0] = x;
  vMouseNew[1] = y;

  vMouseDelta = vMouseNew-g_vMousePos;
  
  switch (g_ControlState)
  {
    case TRANSLATE:  
      if (g_iLeftMouseButton)
      {
        g_vLandTranslate[0] += vMouseDelta[0]*0.01;
        g_vLandTranslate[1] -= vMouseDelta[1]*0.01;
      }
      if (g_iMiddleMouseButton)
      {
        g_vLandTranslate[2] += vMouseDelta[1]*0.01;
      }
      break;
    case ROTATE:
      if (g_iLeftMouseButton)
      {
        g_vLandRotate[0] += vMouseDelta[1];
        g_vLandRotate[1] += vMouseDelta[0];
      }
      if (g_iMiddleMouseButton)
      {
        g_vLandRotate[2] += vMouseDelta[1];
      }
      break;
    case SCALE:
      if (g_iLeftMouseButton)
      {
        g_vLandScale[0] *= 1.0+vMouseDelta[0]*0.01;
        g_vLandScale[1] *= 1.0-vMouseDelta[1]*0.01;
      }
      if (g_iMiddleMouseButton)
      {
        g_vLandScale[2] *= 1.0-vMouseDelta[1]*0.01;
      }
      break;
    
    
  }
  g_vMousePos = vMouseNew;

}

void mouseidle(int x, int y)
{
  Vec2 vMouseNew;
  
  vMouseNew[0] = x;
  vMouseNew[1] = y;


  g_vMousePos = vMouseNew;
}

void mousebutton(int button, int state, int x, int y)
{
  Vec2 vMouseNew;
  
  vMouseNew[0] = x;
  vMouseNew[1] = y;

  switch (button)
  {
    case GLUT_LEFT_BUTTON:
      g_iLeftMouseButton = (state==GLUT_DOWN);
      break;
    case GLUT_MIDDLE_BUTTON:
      g_iMiddleMouseButton = (state==GLUT_DOWN);
      break;
    case GLUT_RIGHT_BUTTON:
      g_iRightMouseButton = (state==GLUT_DOWN);
      break;
  }
 
  switch(glutGetModifiers())
  {
    case GLUT_ACTIVE_CTRL:
      g_ControlState = TRANSLATE;
      break;
    case GLUT_ACTIVE_SHIFT:
      g_ControlState = SCALE;
      break;
    default:
      g_ControlState = ROTATE;
      break;
  }

  g_vMousePos = vMouseNew;

}

int main (int argc, char ** argv)
{
  if (argc<2)
  {  
    printf ("usage: %s heightfield.jpg\n", argv[0]);
    exit(0);
  }

  g_pHeightData = jpeg_read(argv[1], NULL);
  if (!g_pHeightData)
  {
    printf ("error reading %s.\n", argv[1]);
    exit(1);
  }

  glutInit(&argc,argv);
  
  //create a window here..should be double buffered and use depth testing
  //window should be 640x480
  
  //the code past here will segfault if you don't have a window set up....
  //replace the exit once you add those calls.
  exit(0);

  //tells glut to use a particular display function to redraw
  glutDisplayFunc(display);
  
  // allow the user to quit using the right mouse button menu
  g_iMenuId = glutCreateMenu(menufunc);
  glutSetMenu(g_iMenuId);
  glutAddMenuEntry("Quit",0);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
  
  // replace with any animate code
  glutIdleFunc(doIdle);

  // callback for mouse drags
  glutMotionFunc(mousedrag);
  // callback for idle mouse movement
  glutPassiveMotionFunc(mouseidle);
  // callback for mouse button changes
  glutMouseFunc(mousebutton);

  // do initialization
  myinit();

  glutMainLoop();

}
