#include "RenderS.h"


void RenderS::setProjectionMatrix( const Matrix4X4<float> &projMatrix )
{
	this->projectionMatrix = projMatrix;
}
void RenderS::setViewMatrix( const Matrix4X4<float> &viewMatrix )
{
	this->viewMatrix = viewMatrix;
}
RenderS::RenderS( Context &context): AbstractRender(context)
{
}
void RenderS::beginFrame( const Matrix4X4<float> &viewMatrix )
{
	this->viewMatrix= viewMatrix;
	this->beginFrame();
}
void RenderS::beginFrame(void)
{
	this->bind();
	this->clearDepthbuffer();
	this->clearFramebuffer();
}
void RenderS::bind(void)
{
	AbstractRender::bind();
}
void RenderS::renderModels( vector<Model*> &models)
{
	this->bind();

	for (int i=0; i<models.size(); ++i)
	{
	
		Matrix4X4<float> tModelMatrix;
		models[i]->getModelMatrix(tModelMatrix);
		tModelMatrix.transpose();

		Matrix4X4<float> tProjectionMatrix = this->projectionMatrix;
		tProjectionMatrix.transpose();

		Matrix4X4<float> tViewMatrix = this->viewMatrix;
		tViewMatrix.transpose();


		Matrix4X4<float> finalMatrix;
		finalMatrix = tProjectionMatrix * tViewMatrix * tModelMatrix;
	
//TODO: can be optimalized ( we can precalculate PROJECTION * VIEW )

		glMatrixMode( GL_PROJECTION );
		glLoadIdentity();
		
		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();

		glMatrixMode(GL_MODELVIEW);
		glLoadTransposeMatrixf( finalMatrix.getPtr() );

		glColor3f(1,0,0);

		renderModel(*models[i], true );

	}
}



