#include <stdio.h>

//#include "EPSG/Vec3.h"
#include "EPSG/Scene.h"
#include "EPSG/Entity.h"
#include "EPSG/Sphere.h"
#include "EPSG/Camera.h"
#include "EPSG/Material.h"
#include "EPSG/Transform.h"
#include "EPSG/LightSource.h"
#include "EPSG/Mesh.h"

#include "EPSGV/Application.h"

int main( int argc, char* args[] )
{
	EPSG::Scene* scene = new EPSG::Scene();

	// Build a bunny
	EPSG::Entity* bunny = new EPSG::Entity();
	EPSG::Mesh* bunnyMsh = new EPSG::Mesh("../data/bunny.msh");
	EPSG::Material* bunnyMat = new EPSG::Material();
	bunnyMat->setAmbient( 1, 0.1f, 0.1f, 1 );
	bunnyMat->setDiffuse( 1, 0.1f, 0.1f, 1 );
	bunnyMat->setSpecular( 1, 1, 1, 1 );
	bunnyMat->setShininess( 50 );
	bunny->setShape( bunnyMsh );
	bunny->setAppearance( bunnyMat );
	scene->addChild( bunny );

	// Get bunny bounding box
	float xmin, xmax, ymin, ymax, zmin, zmax;
	bunnyMsh->getBBox( &xmin, &xmax, &ymin, &ymax, &zmin, &zmax );
	float xcenter = ( xmin + xmax ) / 2;
	float ycenter = ( ymin + ymax ) / 2;
	float zcenter = ( zmin + zmax ) / 2;
	float zdist = 2 * ( zmax - zmin );
	
	// Build camera and position centralizing to bunny
	EPSG::Camera* c = new EPSG::Camera();
	c->setZPlanes( 0.1f, 50 );
	c->setEye( EPSG::Vec3<float>( xcenter, ycenter, zcenter-zdist ) );
	c->setCenter( EPSG::Vec3<float>( xcenter, ycenter, zcenter ) );
	c->setUp( EPSG::Vec3<float>( 0, 1, 0 ) );
	scene->addChild( c );

	// Build light source
	EPSG::LightSource* l = new EPSG::LightSource();
	l->setAmbient( 0.6f, 0.6f, 0.6f, 0.8f );
	l->setDiffuse( 0.2f, 0.2f, 0.2f, 1 );
	l->setSpecular( 0.2f, 0.2f, 0.2f, 1 );
	l->setPosition( EPSG::Vec3<float>( 0, 10, 0 ) );
	l->setDirectional( true );
	scene->addChild( l );

	//// Build a transformation
	//EPSG::Transform* t = new EPSG::Transform();
	//scene->addChild( t );

	//// Start graph viewer
	EPSGV::Application::loadScene( scene );
	EPSGV::Application::execute( argc, args );
	return 1;
}