#include "Render2D.h"
#include "openGlSupport\FBO.h"

Render2D::Render2D( Context & context, CGprogram &vShader, CGprogram &fShader ): AbstractRender(context)
{
	this->vShader = vShader;
	this->fShader = fShader;
	this->setViewport(0,0, 640, 480 );
}
void Render2D::beginFrame(void)
{
	AbstractRender::beginFrame();
	this->bind();
	this->clearDepthbuffer();
	this->clearFramebuffer();

	//Bind shaders
	cgGLBindProgram( this->vShader );
	cgGLBindProgram( this->fShader );
}

void Render2D::bind(void)
{
	AbstractRender::bind();
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D( this->viewport.fromX, this->viewport.fromX + this->viewport.sizeX, this->viewport.fromY, this->viewport.fromY + this->viewport.sizeY );	// Calculate The Aspect Ratio Of The Window

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void Render2D::renderWholeViewportTexture(unsigned int textureID)
{
	float leftUpCornerPos[2]={ this->viewport.fromX ,this->viewport.fromY + this->viewport.sizeY };
	float size[2]={ this->viewport.sizeX ,this->viewport.sizeY};

	renderTexture( textureID, leftUpCornerPos, size);
}

void Render2D::renderTexture(unsigned int textureID, float leftDownCornerPos[2], float rightDownCornerPos[2], float rightUpCornerPos[2], float leftUpCornerPos[2]) //pozice v pixelech!!!
{
	this->bind();

	selectTextureUnit(0);
	glBindTexture(GL_TEXTURE_2D, textureID);

	glBegin(GL_QUADS);
		glMultiTexCoord2f( GL_TEXTURE0, 0, 0); glVertex2fv(leftDownCornerPos);
		glMultiTexCoord2f( GL_TEXTURE0, 1, 0); glVertex2fv(rightDownCornerPos);
		glMultiTexCoord2f( GL_TEXTURE0, 1, 1); glVertex2fv(rightUpCornerPos);
		glMultiTexCoord2f( GL_TEXTURE0, 0, 1); glVertex2fv(leftUpCornerPos);
	glEnd();
}

void Render2D::renderTexture(unsigned int textureID, float leftUpCornerPos[2], float size[2])	// v pixelech!!!
{
	float leftDownCornerPos[2];
	float rightDownCornerPos[2];
	float rightUpCornerPos[2];

	leftDownCornerPos[0]=leftUpCornerPos[0];
	leftDownCornerPos[1]=leftUpCornerPos[1]-size[1];

	rightDownCornerPos[0]=leftUpCornerPos[0]+size[0];
	rightDownCornerPos[1]=leftUpCornerPos[1]-size[1];

	rightUpCornerPos[0]=leftUpCornerPos[0]+size[0];
	rightUpCornerPos[1]=leftUpCornerPos[1];

	renderTexture(textureID, leftDownCornerPos, rightDownCornerPos, rightUpCornerPos, leftUpCornerPos);
}

void Render2D::renderFBO( const FBO* fbo )
{
   float start[] = {this->viewport.fromX ,this->viewport.fromY + fbo->getFrameSizeY()};
   float end[] = {fbo->getFrameSizeX(), fbo->getFrameSizeY()};
   this->renderTexture(fbo->getTextureId(), start, end);
}

void Render2D::renderTexture(GLuint id_tex)
{
	GLint _idtex, width, height;
	glGetIntegerv(GL_TEXTURE_BINDING_2D, &_idtex);
	glBindTexture(GL_TEXTURE_2D, id_tex);

	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);

	glBindTexture(GL_TEXTURE_2D, _idtex);

	float start[] = { this->viewport.fromX ,this->viewport.fromY + height };
	float end[] = { width, height };
	this->renderTexture(id_tex, start, end);
}
