#include "view.h"
#include <stdio.h>


_main(argc,argv)
int argc;
char **argv;
{
	Vertex *carVertex;
	FaceList *facelist;
	Surface *surface;
	View *view;
	Index *index;
	Tree *t;
	Lighting *lighting;
	Light ambient,light;
	Colour colour;
	register int i,r,s,j,n;
	register Face *face;
	long tickCount1,tickCount2;
	REAL lightX = -30.0, lightY = -50.0, lightZ = 30.0;
	
	carVertex = newVertex(8000);
	facelist = newFaceList(7000);
	
	/*initialise the viewing stuff*/
	initialiseDrawables();
	surface = newSurface(10,10,410,410);
	view = newView(surface);
	
	/*read the file*/
	colour.red = 0.8; colour.green = 0.4; colour.blue = 0.0;
	readShape("HARD DISK:Graphics:DATA:x29.geom",&carVertex,facelist,&colour);
	
	/*initialise the lighting*/
	setIntensityOfLight(&ambient,0.5,0.5,0.5);
	lighting = newLighting(&ambient);

	setPositionOfLight(&light,lightX,lightY,lightZ);
	setIntensityOfLight(&light,0.5,0.5,0.5);
	addLightToLighting(lighting,&light);

	/*set the view*/
	setVRP(view,-20.0,-40.0,20.0);
	setVPN(view,1.0,2.0,-1.0);
	setCOP(view,0.0,0.0,-3.0);
	setVPDView(view,0.0);
	setClipView(view,0.0,1000.0);
	setVUV(view,0.0,1.0,0.0);
	setVPWindowView(view,-2.0,2.0,-2.0,2.0);
	setView(view);
	
	/*set the plane equations and the lighting*/
	for(i=0;i<NEXTFreeFaceList(facelist);++i){
		setPlaneEqOfFace(atFaceList(facelist,i));
		
		setAmbientLightingOnFace((face=atFaceList(facelist,i)),&AMBIENT(lighting));
		for(j=0;j<NOOfLightsIn(lighting);++j)
			setCentreColourOfFace(face,&LIGHTOf(lighting,j),j);
	}
	
	/*make the tree*/
	
	
	index = makeSetFromFaceList(facelist);
	
	printf("Number of polygons is %d\n",NEXTFreeFaceList(facelist));
	tickCount1 = TickCount();
	
	t = makeTree(facelist,index);
	
	tickCount2 = TickCount();
	printf("Time for tree is %f\n",((REAL)tickCount2 - (REAL)tickCount1)/60.0);
	n = NEXTFreeFaceList(facelist);
	printf("and number of polygons is %d\n",NEXTFreeFaceList(facelist));
	
	/*set the shadows*/
	tickCount1 = TickCount();
	/*
	setShadowsOnTree(t,facelist,lighting);
	*/
	tickCount2 = TickCount();
	printf("Time for shadows is %f\n",((REAL)tickCount2 - (REAL)tickCount1)/60.0);
	printf("and number of polygons is %d\n",NEXTFreeFaceList(facelist));
	
	displayTreeOnView(view,facelist,t);
	
	mainEventLoop(view,facelist,t);
}
