#include <iostream>
using namespace std;

#include <GL/gl.h>

#include <graphmisc.h>
#include <vrmlshape.h>


#ifndef NDEBUG
//#define DEBUG_VRMLSHAPE
#endif



void vrmllines::writeP() const
{
  assert((point.size()%6)==0);

  myLightingTurnOff temp;

  uintc imax = point.size();
  glBegin(GL_LINES);
  for (uint i=0; i<imax; i+=6)
  {
    glVertex3f(point[i],point[i+1],point[i+2]);
    glVertex3f(point[i+3],point[i+4],point[i+5]);
  }
  glEnd();
}


void vrmllines::writePC() const
{
  assert(point.size()==color.size());
  assert((point.size()%6)==0);

  myLightingTurnOff temp;

  uintc imax = point.size();
  glBegin(GL_LINES);
  for (uint i=0; i<imax; i+=6)
  {
    glColor3f(color[i],color[i+1],color[i+2]);
    glVertex3f(point[i],point[i+1],point[i+2]);
    glColor3f(color[i+3],color[i+4],color[i+5]);
    glVertex3f(point[i+3],point[i+4],point[i+5]);
  }
  glEnd();
}


ostream & vrmlshape::print( ostream & os ) const
{
  os << "diffuseColor: ";
  os << diffuseColor[0] << " ";
  os << diffuseColor[1] << " ";
  os << diffuseColor[2] << " ";
  os << endl;

  os << "point:" << endl;

  for (uint i=0; i<point.size(); ++i)
  {
    os << point[i] << " ";
    if ((i+1)%3==0)
      os << endl;
  }
  os << endl;

  os << "normal:" << endl;

  for (uint i=0; i<normal.size(); ++i)
  {
    os << normal[i] << " ";
    if ((i+1)%3==0)
      os << endl;
  }
  os << endl;

  os << "color:" << endl;

  for (uint i=0; i<color.size(); ++i)
  {
    os << color[i] << " ";
    if ((i+1)%3==0)
      os << endl;
  }
  os << endl;

  return os;
}


void vrmlshape::diffuseColorWrite() const
{
  assert(diffuseColor[0]>=0.0);

  glColor3f
  (
    diffuseColor[0],
    diffuseColor[1],
    diffuseColor[2]
  );
}


void vrmlshape::writeP() const
{
  uintc sz = point.size();

  if (sz==0)
    return;

//<TODO> look at this code, does writeP() word?
  glBegin(GL_LINES);
//  glBegin(GL_TRIANGLES);

  diffuseColorWrite();

  assert((point.size()%3)==0);

  for (uint i=0; i<sz; i+=9 )
  {
    glVertex3f(point[i+0],point[i+1],point[i+2]);
    glVertex3f(point[i+3],point[i+4],point[i+5]);
    glVertex3f(point[i+3],point[i+4],point[i+5]);
    glVertex3f(point[i+6],point[i+7],point[i+8]);
    glVertex3f(point[i+6],point[i+7],point[i+8]);
    glVertex3f(point[i+0],point[i+1],point[i+2]);
  }

  glEnd();
}

void vrmlshape::writePN() const
{
  uintc sz = point.size();

  if (sz==0)
    return;

  assert(sz==normal.size());


#ifdef DEBUG_VRMLSHAPE
axes(0.25);
#endif


  glBegin(GL_TRIANGLES);

  diffuseColorWrite();

  for (uint i=0; i<sz; i+=3 )
  {
    glNormal3f(normal[i],normal[i+1],normal[i+2]);
    glVertex3f(point[i],point[i+1],point[i+2]);
  }

  glEnd();
}

void vrmlshape::writePNC() const
{
  uintc sz = point.size();

  if (sz==0)
    return;

  assert(sz==normal.size());
  assert(sz==color.size());

  glBegin(GL_TRIANGLES);

  for (uint i=0; i<sz; i+=3 )
  {
    glNormal3f(normal[i],normal[i+1],normal[i+2]);
    glColor3f(color[i],color[i+1],color[i+2]);
    glVertex3f(point[i],point[i+1],point[i+2]);
  }

  glEnd();
}

void vrmlshape::writePwind() const
{
  uintc sz = point.size();

  if (sz==0)
    return;

  glBegin(GL_TRIANGLES);

  uint k;

  for (uint i=0; i<sz; i+=3 )
  {
    k = i/3;
    k %= 3;
    switch (k)
    {
      case 0: glColor3f(1.0,0.0,0.0); break;
      case 1: glColor3f(0.0,1.0,0.0); break;
      default: glColor3f(0.0,0.0,1.0); break;
    }

    glVertex3f(point[i],point[i+1],point[i+2]);
  }

  glEnd();
}






