#include "SpacePlanetGameInterface.h"
#include "./math/Mathematics.h"

using namespace GEOMETRY;
using namespace ANALYTICAL_GEOMETRY;

//#include "texture\TextureManager.h"

SpacePlanetGame::SpacePlanetGame( SoftProjRender &renderer, ShaderManager &shader ): renderer( renderer), shader( shader )
{
	this->basicViewer.observerData.maxVerticalAngle = 80;
	this->basicViewer.observerData.minVerticalAngle = 15;

	this->basicViewer.observerData.nearCameraLimit = 25;
	this->basicViewer.observerData.farCameraLimit = 75;

	this->basicViewer.camera.look = MAKE_VECTOR<float>(0,0,-1);
	this->basicViewer.camera.look.normalize();
	this->basicViewer.camera.pos = this->basicViewer.observerData.cameraPivot - this->basicViewer.camera.look * basicViewer.observerData.farCameraLimit ;
	this->basicViewer.camera.right = MAKE_VECTOR<float>(1,0,0);
	this->basicViewer.camera.recalculate_up();

	this->basicViewer.observerData.calibrateCamera( this->basicViewer.camera); //to clamp camera angle into the (minVerticalAngle, maxVerticalAngle)

}

void SpacePlanetGame::setModels(  vector< Model*> &models )
{
	this->models = models;
}
void SpacePlanetGame::setLights(  vector< Light*> &lights )
{
	this->lights = lights;
}

void SpacePlanetGame::simulate( float dt )
{
	this->cloudySky.simulate( dt );
}
void SpacePlanetGame::render( void )
{
	glEnable( GL_CULL_FACE );
	glCullFace( GL_BACK );
	glFrontFace( GL_CCW );

	Camera<double> cam;
	cam.pos = CONVERT_TO_DOUBLE<float>( this->basicViewer.camera.pos );
	cam.look = CONVERT_TO_DOUBLE<float>( this->basicViewer.camera.look );
	cam.right = CONVERT_TO_DOUBLE<float>( this->basicViewer.camera.right );

	this->renderer.beginFrame( cam );

	
		this->shader.unbind(); //disable shader

		this->skyBox.render(  this->renderer );
		this->cloudySky.render( this->renderer );

		if ( this->renderer.isFragmentShaderValid() && this->renderer.isVertexShaderValid() )
		{
			this->shader.bind();	//enable shader 
			this->renderer.renderPhongModels( this->lights, this->models );
		}
		else
		{
			this->renderer.renderModels( this->models );
		}

	this->renderer.endFrame();

}
void SpacePlanetGame::handleKeyboard( Keyboard &keyboard )
{
	this->basicViewer.handleKeyboard( keyboard );
}
void SpacePlanetGame::handleMouse( Mouse &mouse )
{
	this->basicViewer.handleMouse( mouse );
}