/** 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/oglskybox.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/graphics/graphics.hpp"

#include "drivers/common/opengl/opengl.hpp"

void COpenGLEngine::drawSkyBox(const CSGXTexture **pTextures,const sgxMatrix43 &transform, const sgxVector3 &minpos, const sgxVector3 &maxpos)
{
oglMatrix mtx(transform);

	CSGXGraphicsEngine::drawSkyBox(pTextures,transform,minpos,maxpos);

	GLenum mode=getOGLMode();

	// TODO: Build this data into vertex arrays
	// Back
	CSGXTextureManager::get()->applyTexture(pTextures[2]);
	glBegin(mode);		
		glTexCoord2f(0.0f,0.0f);glVertex3f(minpos.x,maxpos.y,minpos.z);
		glTexCoord2f(1.0f,0.0f);glVertex3f(maxpos.x,maxpos.y,minpos.z);
		glTexCoord2f(1.0f,1.0f);glVertex3f(maxpos.x,minpos.y,minpos.z);
		glTexCoord2f(0.0f,1.0f);glVertex3f(minpos.x,minpos.y,minpos.z);
	glEnd();

	// Bottom
	CSGXTextureManager::get()->applyTexture(*pTextures[1]);
	glBegin(mode);	
		glTexCoord2f(0.0f,0.0f);glVertex3f(maxpos.x,minpos.y,maxpos.z);
		glTexCoord2f(1.0f,0.0f);glVertex3f(minpos.x,minpos.y,maxpos.z);
		glTexCoord2f(1.0f,1.0f);glVertex3f(minpos.x,minpos.y,minpos.z);
		glTexCoord2f(0.0f,1.0f);glVertex3f(maxpos.x,minpos.y,minpos.z);
	glEnd();

	//Front
	CSGXTextureManager::get()->applyTexture(*pTextures[3]);
	glBegin(mode);		
		glTexCoord2f(0.0f,0.0f);glVertex3f(maxpos.x,maxpos.y,maxpos.z);
		glTexCoord2f(1.0f,0.0f);glVertex3f(minpos.x,maxpos.y,maxpos.z);
		glTexCoord2f(1.0f,1.0f);glVertex3f(minpos.x,minpos.y,maxpos.z);
		glTexCoord2f(0.0f,1.0f);glVertex3f(maxpos.x,minpos.y,maxpos.z);
	glEnd();

	//TOP
	CSGXTextureManager::get()->applyTexture(*pTextures[0]);
	
	glBegin(mode);		
		glTexCoord2f(0.0f,0.0f);glVertex3f(maxpos.x,maxpos.y,minpos.z);
		glTexCoord2f(1.0f,0.0f);glVertex3f(minpos.x,maxpos.y,minpos.z);
		glTexCoord2f(1.0f,1.0f);glVertex3f(minpos.x,maxpos.y,maxpos.z);
		glTexCoord2f(0.0f,1.0f);glVertex3f(maxpos.x,maxpos.y,maxpos.z);
	glEnd();

	//LEFT
	CSGXTextureManager::get()->applyTexture(*pTextures[4]);
	
	glBegin(mode);		
		glTexCoord2f(0.0f,0.0f);glVertex3f(minpos.x,maxpos.y,maxpos.z);		
		glTexCoord2f(1.0f,0.0f);glVertex3f(minpos.x,maxpos.y,minpos.z);
		glTexCoord2f(1.0f,1.0f);glVertex3f(minpos.x,minpos.y,minpos.z);
		glTexCoord2f(0.0f,1.0f);glVertex3f(minpos.x,minpos.y,maxpos.z);	
		
	glEnd();

	//RIGHT
	CSGXTextureManager::get()->applyTexture(*pTextures[5]);

	glBegin(mode);		
		glTexCoord2f(0.0f,0.0f);glVertex3f(maxpos.x,maxpos.y,minpos.z);
		glTexCoord2f(1.0f,0.0f);glVertex3f(maxpos.x,maxpos.y,maxpos.z);
		glTexCoord2f(1.0f,1.0f);glVertex3f(maxpos.x,minpos.y,maxpos.z);
		glTexCoord2f(0.0f,1.0f);glVertex3f(maxpos.x,minpos.y,minpos.z);
	glEnd();
}
