
#include <d2arrow.h>
#include <d2simplex.h>
#include <d2simplextest.h>
#include <gobj.h>
#include <graphmisc.h>
#include <mathlib.h>
#include <menusystem.h>
#include <primitivewindow.h>
#include <zpr.h>


typedef point2<double> pt2;
typedef point2<double> const pt2c;  



void d2simplextest::keyboard(unsigned char key, int x, int y)
{ 
  menu->read(key); 
}

d2simplextest::d2simplextest(int & argc, char** argv)
  : myglutgui(argc,argv), si(A,B)
{
  OpenGLinitialisation();
  
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);

  globalSet();
  menu = new d2simplextestmenu01(this); 

  xGraphics.set();
  xGraphics.push( menu );
  xGraphics.push( & si );
}

void d2simplextest::test01()
{
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt
  (
    2.0,0.0,3.0, 
    2.0,0.0,0.0, 
    0.0,1.0,0.0
  );

  zz.update();


/*
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  //glFrustum(2.0,10.5,-1.0,6.0,1.5,20.1);

  double z0=0.4;
  double z1=10.1;
  cout << SHOW(z1/z0) << endl;
  glFrustum(500.5,15.5,-2.5,6.0,z0,z1);
  
  glMatrixMode(GL_MODELVIEW);

*/


  //zpr::readProjection();
  //zpr::printInfo();


  //zpr::setViewVolume(-0.5,3.5,-0.5,1.0,0.1,4.1);
  //zpr::setViewVolume(2.0,3.5,-1.0,8.0,0.9,4.0);

//cout << SHOW(zpr::zNear) << endl;
//cout << SHOW(zpr::zFar) << endl;

  A.v[0] = pt2(0.0,0.0);
  A.v[1] = pt2(1.0,0.0);
  A.v[2] = pt2(0.5,0.5);

  B.v[0] = pt2(.2,.23);
  B.v[1] = pt2(.4,.23);
  B.v[2] = pt2(.4,.5);

  B.translate( pt2(2.0,0.0) );


  //zpr::printInfo();

  glutPostRedisplay();

  glutMainLoop();
}

d2simplextest::~d2simplextest()
{
  delete menu;
  menu = 0;
}


void d2simplextest::display()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  //menu.draw();
  //si.draw();

  xGraphics.draw();

  glerrordisplay();
  glutSwapBuffers();
}

void d2simplextest::intersection()
{
  si.intersection = A.intersects(B);
//cout << SHOW(si.intersection) << endl;
}





d2simplextestmenu01::d2simplextestmenu01(d2simplextest * _test)
  : menusystem(this,0,true,point2<int>(60,30),20), test(_test)
{
  assert(test!=0);

  addfont12("Simplex and Simplex Intersection");
  addnewline();
  addnewline();
  addfont10("'j' 'J'  Rotate A anti clockwise and clockwise");
  addnewline();
  addfont10("'k' 'K'  Translate A right and left");
  addnewline();
  addfont10("'l' 'L'  Translate A up and down");
  addnewline();
  addfont10("'u' 'U'  Rotate B anti clockwise and clockwise");
  addnewline();
  addfont10("'i' 'I'  Translate B right and left");
  addnewline();
  addfont10("'o' 'O'  Translate B up and down");

  addnewline();
  addnewline();
  addfont10("ESC      Quit");
}


void d2simplextestmenu01::readImmediate(char const key)
{
  static double delta = 0.02;
  static double theta = 0.1;

  switch (key)
  {
    case 27:
      exit(0);
      break;

    case 'j':
      test->A.rotate(theta);
      break;

    case 'J':
      test->A.rotate(-theta);
      break;

    case 'k':
      test->A.translate( pt2(delta,0.0) );
      break;

    case 'K':
      test->A.translate( pt2(-delta,0.0) );
      break;


    case 'l':
      test->A.translate( pt2(0.0,delta) );
      break;

    case 'L':
      test->A.translate( pt2(0.0,-delta) );
      break;

    case 'u':
      test->B.rotate(theta);
      break;

    case 'U':
      test->B.rotate(-theta);
      break;

    case 'i':
      test->B.translate( pt2(delta,0.0) );
      break;

    case 'I':
      test->B.translate( pt2(-delta,0.0) );
      break;


    case 'o':
      test->B.translate( pt2(0.0,delta) );
      break;

    case 'O':
      test->B.translate( pt2(0.0,-delta) );
      break;

  }

  test->intersection();

  glutPostRedisplay();
}
















