/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/drivers/common/opengl/ogl2dprimitives.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/graphics/graphics.hpp"
#include "drivers/common/opengl/opengl.hpp"

void 
COpenGLEngine::drawLine2D(const tINT32 fromX, const tINT32 fromY, const tINT32 toX, const tINT32 toY) {
	CSGXGraphicsEngine::drawLine2D(fromX, fromY, toX, toY);

	GLenum mode = getOGLMode();
	sgxUV uv;


	const sgxColorRGBA &col = m_PrimitiveColor.getState();
	glPushAttrib(GL_ENABLE_BIT);

	glEnable(GL_BLEND);

	COpenGLEngine::m_StateBlendFunc.apply(oglBlend(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

	// TODO: Support vertex colors on these rects?
	glColor4f(col.r, col.g, col.b, col.a);
	glBegin(mode);
		glTexCoord2f(uv.u1, uv.v1);
		glVertex2i(fromX, fromY);

		glTexCoord2f(uv.u2, uv.v1);
		glVertex2i(toX, fromY);
	glEnd();

	glPopAttrib();
}

void 
COpenGLEngine::drawLine2D(const sgxPoint2i &from, const sgxPoint2i &to) {
	drawLine2D(from.x, from.y, to.x, to.y);
}

void COpenGLEngine::drawRect2D(const sgxPoint2i &from, const sgxPoint2i &to)
{
	sgxPoint2i pt2(to.x,from.y);
	sgxPoint2i pt4(from.x,to.y);

	drawRect2D(from.x,from.y, pt2.x,pt2.y, to.x,to.y, pt4.x,pt4.y);
}

void COpenGLEngine::drawRect2D(const tPOINT x1, const tPOINT y1, const tPOINT x2, const tPOINT y2, const tPOINT x3, const tPOINT y3, const tPOINT x4, const tPOINT y4)
{
	COpenGLEngine::drawRect2D((tREAL32)x1,(tREAL32)y1, (tREAL32)x2,(tREAL32)y2, (tREAL32)x3,(tREAL32)y3, (tREAL32)x4,(tREAL32)y4);
}

void COpenGLEngine::drawRect2D(const tREAL32 x1, const tREAL32 y1, const tREAL32 x2, const tREAL32 y2, const tREAL32 x3, const tREAL32 y3, const tREAL32 x4, const tREAL32 y4)
{
	CSGXGraphicsEngine::drawRect2D(x1,y1, x2,y2, x3,y3, x4,y4);

	const sgxColorRGBA &col = m_PrimitiveColor.getState();

	if (isAlphaSortable(col)) {
		// TODO: ADD TO ALPHA SORTER
		//return;
	}

	GLenum mode = getOGLMode();

	// TODO: REMOVE THIS WHEN 2D RECTS in ALPHA SORTER
	// TODO: BUT ALL TRANSPARENCY STUFF SHOULD BE PUT IN THE ALPHA SORTER
	// TODO: Check this is the correct attrib to push
	glPushAttrib(GL_ENABLE_BIT);

	//
	sgxMatrix43 transform;
	getTransform(transform);
	if (!transform.isIdentity()) {
	}
		oglMatrix mtx(transform);
	
	glColor4f(col.r, col.g, col.b, col.a);

	glDisable(GL_COLOR_MATERIAL);
	glDisable(GL_LIGHTING);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// TODO: NEED TO PUT THIS INTO A STATE
	CSGXTexture *pTexture = CSGXTextureManager::get()->getCurrentTexture();
	if (pTexture) {
		sgxUV uv;
		pTexture->set();
		CSGXTextureManager::get()->getCurrentUV(uv);
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glEnable(GL_COLOR);

		glBegin(mode);
			glTexCoord2f(uv.u1, uv.v1);
			glVertex3f(x1, y1,0);
	
			glTexCoord2f(uv.u2, uv.v1);
			glVertex3f(x2, y2,0);
			
			glTexCoord2f(uv.u2, uv.v2);
			glVertex3f(x3, y3,0);

			glTexCoord2f(uv.u1, uv.v2);
			glVertex3f(x4, y4,0);
		glEnd();

	} else {	// vertex colour only
		glEnable(GL_TEXTURE_2D);

		glBegin(mode);
			glVertex3f(x1, y1,0);
			glVertex3f(x2, y2,0);
			glVertex3f(x3, y3,0);
			glVertex3f(x4, y4,0);
		glEnd();
	}

	glPopAttrib();
}


// TODO: Use sgxPolyVtx somehow ?!?!?
void 
COpenGLEngine::drawPolygon2D(const sgxVector<sgxPoint2i> &ptList) {
	CSGXGraphicsEngine::drawPolygon2D(ptList);

	const sgxColorRGBA &col = m_PrimitiveColor.getState();

	if (isAlphaSortable(col)) {
		// TODO: ADD TO ALPHA SORTER
		//return;
	}

	GLenum mode = getOGLMode();
	sgxUV uv;

	// TODO: NEED TO PUT THIS INTO A STATE
	CSGXTextureManager::get()->getCurrentTexture()->set();
	CSGXTextureManager::get()->getCurrentUV(uv);


	// TODO: REMOVE THIS WHEN 2D RECTS in ALPHA SORTER
	// TODO: BUT ALL TRANSPARENCY STUFF SHOULD BE PUT IN THE ALPHA SORTER
	// TODO: Check this is the correct attrib to push
	glPushAttrib(GL_ENABLE_BIT);

	glEnable(GL_BLEND);

	COpenGLEngine::m_StateBlendFunc.apply(oglBlend(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));

	// TODO: Support vertex colors on these rects?
	glColor4f(col.r, col.g, col.b, col.a);
	glBegin(mode);
	
		sgxVector<sgxPoint2i>::const_iterator cit = ptList.begin();
		for(;cit!=ptList.end();++cit) {
			glVertex2i((*cit).x, (*cit).y);
		}
	glEnd();

	glPopAttrib();
}
