

#include <GL/glut.h>

#include <cpsphere.h>
#include <gobj.h>
//#include <graphmisc.h>
#include <tetrahedron.h>
#include <tetrahedrondraw.h>


void cpsphere::stateinc()
{
//cout << "state=" << state << endl;
  ++state;
  state %= vi.size();

  update();
}

void cpsphere::statedec()
{
  uint sz = vi.size();
  if (sz==0)
    return;

  state += (sz-1);
  state %= sz;

  update();
}

void cpsphere::update()
{
  current->nuke();

  uintc imax = vi.size();

  if ((state<imax)==false)
    return;

  // Calculate the spheres details.

  uintc slices=30;
  uintc stacks=30;

  point3<double> const & P1( pts[ vi[state].x ] );
  point3<double> const & P2( pts[ vi[state].y ] );
  point3<double> const & P3( pts[ vi[state].z ] );
  point3<double> const & P4( pts[ vi[state].w ] );

  tetrahedron<point3<double>,double> const t(P1,P2,P3,P4);
  point3<double> c0;
  t.circumcenter(c0);
  point3<double> const c(c0);
  doublec radius = (c-P1).distance();

  // Write the graphics.

  current->push( new gobjglPushMatrix() );


  current->push( new gobjglEnable(GL_LIGHTING) );

/*
  current->push( new gobjglColor3f(1.0,0.0,0.0) );


  current->push( new gobjglTranslatef(c) );
  current->push( 
    new gobjglutSolidSphere(radius,slices,stacks) );
*/



  current->push( new gobjglDisable(GL_DEPTH_TEST) );
  current->push( new gobjglEnable(GL_BLEND) );
  current->push( 
    new gobjglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) );

  current->push( new gobjglColor4f(1.0,0.0,0.0,0.8) );

  current->push( new gobjglTranslatef(c) );
  current->push( 
    new gobjglutSolidSphere(radius,slices,stacks) );


  current->push( new gobjglPopMatrix() );


  //glerrordisplay();
}
  
cpsphere::cpsphere
(
  vector< point3<double> > const & pts_,
  vector< point4<uint> > const & vi_
)
  : pts(pts_), vi(vi_), state(0)
{
  current = new gobjContainer();
}

cpsphere::~cpsphere()
{
  delete current;
}





