#include <GL/glut.h>
#include <math.h>
#include "types.h"
#include "globj.h"
#include "vecmath.h"
#include <stdlib.h>
#include "tergen.h"
#include "carpet.h"

void carpet_draw (globj_t*) ;
void carpet_init (void) ;
globj_t* carpet_factory () ;
void carpet_delete (globj_t*) ;

static globj_t carpet = 
{ carpet_draw, carpet_delete, "carpet", NULL } ;

globj_class_t globj_class_carpet = 
{ carpet_init, carpet_factory, "icosa", NULL } ;

void carpet_init (void) {}

globj_t* carpet_factory ()
{
  return &carpet ;
}

void carpet_delete (globj_t* carpet) { } /* do nothing (static allocation) */

void carpet_draw (globj_t* dummy)
{
  GLfloat sea[4]=  { 0.0f, 0.2f, 0.8f, 0.7f};
  int i,j,t = glutGet (GLUT_ELAPSED_TIME) / 10 ;
  glNormal3d(0.0,0.0,1.0);
  
#define XOFFSET 0.25 /* XSTEP/2 normally */
#define XSTEP 0.5
#define YSTEP 0.375
#define PUTVERTEX(x,y) glVertex3d(x,y,tergenGetZ(x,y,t))
  
  glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,sea);
  for (j=0; j<20; j++) {
    glBegin(GL_TRIANGLE_STRIP) ;
    for (i=0;i<20; i++) {
      PUTVERTEX(i*XSTEP,j*YSTEP);
      PUTVERTEX(i*XSTEP+XOFFSET,(j+1)*YSTEP);
    }
    glEnd();
  }
}
