#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ppmIO.h"
#include "GraphicsEnv.h"
#include "colors.h"

#define USECPP 0
extern Matrix VTM, GTM;
extern Light_struct *GLOBAL_LIGHT_STRUCT;
extern Vector cop;

int main(int argc, char *argv[]) {
  int row = 400;
  int col = 400;
  Image_t *image;
  ViewStruct3D *myView;
  Matrix ltm;
  Structure *box, *tabletop, *table;

  /* light variables */
  Light_t ambient, light1; 
  Color_t Ia = {10,10,10};
  double Ks[] = {0.7, 0.2, 0.3};
  double Purple[] = {0.8, 0.2, 0.8};
  double Gray[] = {0.7,0.7,0.7};
  long x = 1;
  long y = 1;
  long z = 1;

  Point_t plane1[] = {{0,0,0}, {x,0,0}, {x,0,z}, {0,0,z}}; //bottom
  Point_t plane2[] = {{x,y,0}, {x,0,0}, {x,0,z}, {x,y,z}}; //front facing x
  Point_t plane3[] = {{0,y,0}, {x,y,0}, {x,y,z}, {0,y,z}}; //top
  Point_t plane4[] = {{0,y,0}, {0,y,z}, {0,0,z}, {0,0,0}}; //back facing -x
  Point_t plane5[] = {{x,y,0}, {0,y,0}, {0,0,0}, {x,0,0}}; //left
  Point_t plane6[] = {{x,y,z}, {0,y,z}, {0,0,z}, {x,0,z}}; //right

    
  /*surface Normals*/
  Vector ttop[]={{0.0,-1.0,0.0,1.0},{-1.0,0.0,0.0,1.0},{0.0,0.0,-1.0,1.0},
	       {1.0,0.0,0.0,1.0},{0.0,0.0,-1.0,1.0},{0.0,0.0,1.0,1.0}};

  Vector tbox[]={{0.0,0.0,-1.0,1.0},{0.0,0.0,-1.0,1.0},{0.0,1.0,0.0,1.0},
		{0.0,-1.0,0.0,1.0},{1.0,0.0,0.0,1.0},{1.0,0.0,0.0,1.0}};

  /* setting up viewStructure3D */
  myView = (ViewStruct3D *)malloc(sizeof(ViewStruct3D));
  
  identity(GTM);
  identity(VTM);
  identity(ltm);
 
  setVector(2.0, 5.0, -15.0, 0.0,myView->VRP);
  setVector(-2.0, -2.5, 8.0, 0.0,myView->VPN);
  setVector(0.0, 1.0, 0.0, 0.0,myView->VUP);
  setVector(0.0, 0.0, -4.0, 1.0,myView->COP);
  setVector(0.0, 0.0, -4.0, 1.0, cop);
  myView->umax = 1.5;
  myView->umin = -1.5;
  myView->vmax = 1.5;
  myView->vmin = -1.5;
  myView->screenX = 400;
  myView->screenY = 400;
  myView->f = 1;
  myView->b = 5;

  

  //initialize light source structure
  ambient.type = _AMBIENT_;
  ambient.color = Ia;
  makeLightStruct(ambient);
  light1.type = _POINT_;
  setVector(8.0, 8.0, -10.0, 0.0, light1.position);
  light1.color = white;
  insertLight(light1);
    
  /*allocate memory for new image */
  image= make_image(row, col, white);
    

  Set3DViewTransform(myView, VTM);
  
  printf("testing\n");
  box = makeBox(1.0, 1.0, 1.0, Purple, Ks, tbox, image);
  
  
  tabletop = beginStructure();
  identity(ltm);
  scale3D(5.0, 0.6, 7.0, ltm);
  translate3D(-3.0, 1.2, -5.0,ltm);
  fillPolygon(plane1, 4, Gray, Ks, ttop[0],ltm,image);
  identity(ltm);
  scale3D(5.0, 0.6, 7.0, ltm);
  translate3D(-3.0, 1.2, -5.0,ltm);
  fillPolygon(plane2, 4, Gray, Ks, ttop[1],ltm,image);
  identity(ltm);
  scale3D(5.0, 0.6, 7.0, ltm);
  translate3D(-3.0, 1.2, -5.0,ltm);
  fillPolygon(plane3, 4, Gray, Ks, ttop[2],ltm,image);
  identity(ltm);
  scale3D(5.0, 0.6, 7.0, ltm);
  translate3D(-3.0, 1.2, -5.0,ltm);
  fillPolygon(plane4, 4, Gray, Ks, ttop[3],ltm,image);
  identity(ltm);
  scale3D(5.0, 0.6, 7.0, ltm);
  translate3D(-3.0, 1.2, -5.0,ltm);
  fillPolygon(plane5, 4, Gray, Ks, ttop[4],ltm,image);
  identity(ltm);
  scale3D(5.0, 0.6, 7.0, ltm);
  translate3D(-3.0, 1.2, -5.0,ltm);
  fillPolygon(plane6, 4, Gray, Ks, ttop[5],ltm,image);
  endStructure();
  
  table = beginStructure();
  identity(ltm);
  drawStructure(box,image);
  identity(ltm);
  drawStructure(tabletop,image);  
  endStructure();

  drawStructure(table,image);
  /* write image and free memory */

  writePPM(image->image, row, col, 255, argv[1]);
  free(image->image);

  return(0);
}















