/*
 * test bird renderer
 */

#include "bird_renderer.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

int num_sides = 4;		// number of sides in a tetrahedron

GLdouble tetrahedron[12][3] =	// defn of a tetrahedron
{
  
  { 1,  4, -1},
  { 2,  0,  0},
  {-1, -4,  3},
  
  { 1,  4, -1},
  { 2,  0,  0},
  { 4,  5,  7},
  
  { 1,  4, -1},
  {-1, -4,  3},
  { 4,  5,  7},
  
  { 2,  0,  0},
  {-1, -4,  3},
  { 4,  5,  7},
  
  
};

/* colors for our sides */
GLdouble colors[4][3] = 
{
  {1.0, 0.0, 0.0},		/* red */
  {0.0, 1.0, 0.0},		/* blue */
  {0.0, 0.0, 1.0},		/* green */
  {1.0, 1.0, 1.0},		/* white */
};

/* setup our test birds in a 3x3x3 cube (28 birds in all) */
void fill_shapes (bird* shapes[]) {

  int i;
  GLdouble x, y, z;
  vec3 my_vector;

  i = 0;
  for (x=-30.0; x<31.0; x+=30.0) {
    for (y=-30.0; y<31.0; y+=30.0) {
      for (z=-30.0; z<31.0; z+=30.0) {
	shapes[i] = malloc (sizeof(bird));
	my_vector.x = x;
	my_vector.y = y;
	my_vector.z = z;
	bird_set_position(shapes[i], &my_vector);
	my_vector.x = 0.0;
	my_vector.y = 0.0;
	my_vector.z = 0.0;
	bird_set_heading(shapes[i], &my_vector);
	i++;
      }
    }
  }
}

/*
 * Make an OpenGL object resident
 */

GLuint store_gl_object () {

  int i, j;
  GLuint my_object;

  my_object = glGenLists(1);	/* create a new list for our object */
  glNewList(my_object, GL_COMPILE); /* tell opengl to compile this object */
  glBegin (GL_TRIANGLES);	/* use triangles */
  for (i = 0; i < num_sides; i ++) /* for each side */
    {
      glColor3dv (colors[i]);
      for (j = 0; j < 3; j++) {	/* for each vertex */
	glVertex3d (tetrahedron[(i*3)+j][0],
		    tetrahedron[(i*3)+j][1],
		    tetrahedron[(i*3)+j][2]);      
      }
    }
  glEnd ();			/* end object */
  glEndList();			/* end compilation loop for this list */

  return my_object;
}

/*
 * Main routine
 */

int main (int argc, char **argv)
{
  
  /* Window width and height */
  winW = INITIAL_W;
  winH = INITIAL_H;
  
  /* Relative positions for our tetrahedrons */
  num_objects = malloc (sizeof(int));
  *num_objects = 27;
  shapes = (bird**) malloc(sizeof(void*) * (*num_objects));
  if (shapes == NULL) {
    perror ("unable to malloc : ");
    exit (1);
  }

  fill_shapes(shapes);
  
  printf("Press h for help\n");
  
  glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowSize (winW,winH);
  glutCreateWindow (APP_NAME);
  glutDisplayFunc (glutDisplay);
  if (FULL_SCREEN == 1)
    glutFullScreen();
  glutReshapeFunc (glutResize);
  glutKeyboardFunc (glutKeyboard);
  if (MOUSE_SUPPORT == 1)
    glutPassiveMotionFunc(glutMouse);
  glInit ();
  glObject = store_gl_object();	/* instantiate our object */

  glutMainLoop();
  
  exit (0);
}



