#include <math.h>
#include <GL/glut.h>
#include "Drawing_Routines.h"
#include <iostream.h>

// rough routines for OpenGL to simplify drawing 

void draw_polygon( GLfloat* coord, int num )
{
  GLfloat v1[3] = { coord[3] - coord[0],
		   coord[4] - coord[1],
		   coord[5] - coord[2] };
  GLfloat v2[3] = { coord[6] - coord[3],
		    coord[7] - coord[4],
		    coord[8] - coord[5] };

  GLfloat v3[3] = { v1[1] * v2[2] - v2[1] * v1[2],
		    v1[2] * v2[0] - v2[2] * v1[0],
		    v1[0] * v2[1] - v2[0] * v1[1] };
  
  float d = sqrt( v3[0] * v3[0] + v3[1] * v3[1] + v3[2] * v3[2]);
  glNormal3f( v3[0] / d, v3[1] / d,  v3[2] / d );

  glBegin(GL_POLYGON);
  for(int i = 0; i < num ; i += 3 ) {
    glVertex3f( coord[i], coord[i+1], coord[i+2]);
  }
  glEnd();
}


void draw_box( GLfloat* size, GLfloat* pos)
{
  GLfloat coord[12];

  //drawing facet1
  coord[0]  = pos[0] - size[0] / 2;
  coord[1]  = pos[1] - size[1] / 2;
  coord[2]  = pos[2] - size[2] / 2;
  coord[3]  = pos[0] - size[0] / 2;
  coord[4]  = pos[1] + size[1] / 2;
  coord[5]  = pos[2] - size[2] / 2;
  coord[6]  = pos[0] + size[0] / 2;
  coord[7]  = pos[1] + size[1] / 2;
  coord[8]  = pos[2] - size[2] / 2;
  coord[9]  = pos[0] + size[0] / 2;
  coord[10] = pos[1] - size[1] / 2;
  coord[11] = pos[2] - size[2] / 2;
  draw_polygon( coord, 12);

  //drawing facet2
  coord[0]  = pos[0] - size[0] / 2;
  coord[1]  = pos[1] - size[1] / 2;
  coord[2]  = pos[2] + size[2] / 2;
  coord[3]  = pos[0] + size[0] / 2;
  coord[4]  = pos[1] - size[1] / 2;
  coord[5]  = pos[2] + size[2] / 2;
  coord[6]  = pos[0] + size[0] / 2;
  coord[7]  = pos[1] + size[1] / 2;
  coord[8]  = pos[2] + size[2] / 2;
  coord[9]  = pos[0] - size[0] / 2;
  coord[10] = pos[1] + size[1] / 2;
  coord[11] = pos[2] + size[2] / 2;
  draw_polygon( coord, 12);

  //drawing facet3
  coord[0]  = pos[0] + size[0] / 2;
  coord[1]  = pos[1] - size[1] / 2;
  coord[2]  = pos[2] - size[2] / 2;
  coord[3]  = pos[0] + size[0] / 2;
  coord[4]  = pos[1] + size[1] / 2;
  coord[5]  = pos[2] - size[2] / 2;
  coord[6]  = pos[0] + size[0] / 2;
  coord[7]  = pos[1] + size[1] / 2;
  coord[8]  = pos[2] + size[2] / 2;
  coord[9]  = pos[0] + size[0] / 2;
  coord[10] = pos[1] - size[1] / 2;
  coord[11] = pos[2] + size[2] / 2;
  draw_polygon( coord, 12);

  //drawing facet4
  coord[0]  = pos[0] - size[0] / 2;
  coord[1]  = pos[1] - size[1] / 2;
  coord[2]  = pos[2] - size[2] / 2;
  coord[3]  = pos[0] - size[0] / 2;
  coord[4]  = pos[1] - size[1] / 2;
  coord[5]  = pos[2] + size[2] / 2;
  coord[6]  = pos[0] - size[0] / 2;
  coord[7]  = pos[1] + size[1] / 2;
  coord[8]  = pos[2] + size[2] / 2;
  coord[9]  = pos[0] - size[0] / 2;
  coord[10] = pos[1] + size[1] / 2;
  coord[11] = pos[2] - size[2] / 2;
  draw_polygon( coord, 12);

  //drawing facet5
  coord[0]  = pos[0] - size[0] / 2;
  coord[1]  = pos[1] + size[1] / 2;
  coord[2]  = pos[2] - size[2] / 2;
  coord[3]  = pos[0] - size[0] / 2;
  coord[4]  = pos[1] + size[1] / 2;
  coord[5]  = pos[2] + size[2] / 2;
  coord[6]  = pos[0] + size[0] / 2;
  coord[7]  = pos[1] + size[1] / 2;
  coord[8]  = pos[2] + size[2] / 2;
  coord[9]  = pos[0] + size[0] / 2;
  coord[10] = pos[1] + size[1] / 2;
  coord[11] = pos[2] - size[2] / 2;
  draw_polygon( coord, 12);

  //drawing facet6
  coord[0]  = pos[0] - size[0] / 2;
  coord[1]  = pos[1] - size[1] / 2;
  coord[2]  = pos[2] - size[2] / 2;
  coord[3]  = pos[0] + size[0] / 2;
  coord[4]  = pos[1] - size[1] / 2;
  coord[5]  = pos[2] - size[2] / 2;
  coord[6]  = pos[0] + size[0] / 2;
  coord[7]  = pos[1] - size[1] / 2;
  coord[8]  = pos[2] + size[2] / 2;
  coord[9]  = pos[0] - size[0] / 2;
  coord[10] = pos[1] - size[1] / 2;
  coord[11] = pos[2] + size[2] / 2;
  draw_polygon( coord, 12);

}


void draw_cilinder(GLfloat R, GLfloat H, int N, GLfloat* pos)
{
  //drawing circles
  GLfloat points[20][2];
  int i;
  float delta =  - 2 * M_PI / N ;
  glNormal3f(0,0,-1);
  glBegin(GL_POLYGON);
  for( i = 0; i < N; i++ ){
    points[i][0] = cos(delta * i) * R + pos[0];
    points[i][1] = sin(delta * i) * R + pos[1];
    glVertex3f( points[i][0], points[i][1], pos[2] - H / 2 );
  }
  glEnd();
  glNormal3f(0,0,1);
  glBegin(GL_POLYGON);
  for( i = 1; i < N + 1; i++ ){
    glVertex3f( points[ N - i][0], points[N - i][1], pos[2] + H / 2 );
  }
  glEnd();

  for( i = 0; i < N - 1; i++ ){
    glNormal3f( cos( delta * (i + 0.5)), sin( delta * (i + 0.5)), 0);
    glBegin(GL_POLYGON);
    glVertex3f( points[i][0], points[i][1], pos[2] - H / 2 );
    glVertex3f( points[i][0], points[i][1], pos[2] + H / 2 );
    glVertex3f( points[i + 1][0], points[i + 1][1], pos[2] + H / 2 );
    glVertex3f( points[i + 1][0], points[i + 1][1], pos[2] - H / 2 );
    glEnd();
  }
    glNormal3f( cos( delta * (i + 0.5)), sin( delta * (i + 0.5)), 0);
    glBegin(GL_POLYGON);
    glVertex3f( points[i][0], points[i][1], pos[2] - H / 2 );
    glVertex3f( points[i][0], points[i][1], pos[2] + H / 2 );
    glVertex3f( points[0][0], points[0][1], pos[2] + H / 2 );
    glVertex3f( points[0][0], points[0][1], pos[2] - H / 2 );
    glEnd();

}
