#include "CubeMap.h"

CCubeMap::CCubeMap(void)
{
}

CCubeMap::~CCubeMap(void)
{
}

void CCubeMap::initTexture()
{
	glEnable(GL_TEXTURE_2D);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

	glGenTextures(6,m_nTex);

	AUX_RGBImageRec* tex = auxDIBImageLoad(L"data\\p_z.bmp");
	glBindTexture(GL_TEXTURE_2D,m_nTex[0]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tex->sizeX,tex->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,tex->data);
	delete [] tex->data;

	tex = auxDIBImageLoad(L"data\\n_x.bmp");
	glBindTexture(GL_TEXTURE_2D,m_nTex[1]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tex->sizeX,tex->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,tex->data);
	delete [] tex->data;

	tex = auxDIBImageLoad(L"data\\p_x.bmp");
	glBindTexture(GL_TEXTURE_2D,m_nTex[2]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tex->sizeX,tex->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,tex->data);
	delete [] tex->data;

	tex = auxDIBImageLoad(L"data\\n_z.bmp");
	glBindTexture(GL_TEXTURE_2D,m_nTex[3]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tex->sizeX,tex->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,tex->data);
	delete [] tex->data;

	tex = auxDIBImageLoad(L"data\\p_y.bmp");
	glBindTexture(GL_TEXTURE_2D,m_nTex[4]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tex->sizeX,tex->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,tex->data);
	delete [] tex->data;

	tex = auxDIBImageLoad(L"data\\n_y.bmp");
	glBindTexture(GL_TEXTURE_2D,m_nTex[5]);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tex->sizeX,tex->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,tex->data);
	delete [] tex->data;

	tex->data = NULL;
	tex = NULL;


}
void CCubeMap::drawCube(float size, Point3f objectPos)
{
	double h = size/2.0;
	//6 textures

	glBindTexture(GL_TEXTURE_2D,m_nTex[0]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.f,0.f);		glVertex3d( h,-0, h);			
	glTexCoord2f(1.f,0.f);		glVertex3d(-h,-0, h);			
	glTexCoord2f(1.f,1.f);		glVertex3d(-h, size, h);			
	glTexCoord2f(0.f,1.f);		glVertex3d( h, size, h);			
	glEnd();

	//Left(-X)
	glBindTexture(GL_TEXTURE_2D,m_nTex[1]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.f,0.f);		glVertex3d( h,0,-h);			
	glTexCoord2f(1.f,0.f);		glVertex3d( h,0, h);				
	glTexCoord2f(1.f,1.f);		glVertex3d( h, size, h);				
	glTexCoord2f(0.f,1.f);		glVertex3d( h, size,-h);				
	glEnd();

	//Right(X)
	glBindTexture(GL_TEXTURE_2D,m_nTex[2]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.f,0.f);		glVertex3d(-h,0, h);				
	glTexCoord2f(1.f,0.f);		glVertex3d(-h,0,-h);				
	glTexCoord2f(1.f,1.f);		glVertex3d(-h, size,-h);				
	glTexCoord2f(0.f,1.f);		glVertex3d(-h, size, h);				
	glEnd();

	//Back(Z)
	glBindTexture(GL_TEXTURE_2D,m_nTex[3]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.f,0.f);		glVertex3d(-h,0,-h);				
	glTexCoord2f(1.f,0.f);		glVertex3d( h,0,-h);			
	glTexCoord2f(1.f,1.f);		glVertex3d( h, size,-h);			
	glTexCoord2f(0.f,1.f);		glVertex3d(-h, size,-h);				
	glEnd();

	//Top(-Y)
	glBindTexture(GL_TEXTURE_2D,m_nTex[4]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.f,0.f);		glVertex3d( h, size, h);			
	glTexCoord2f(1.f,0.f);		glVertex3d(-h, size, h);				
	glTexCoord2f(1.f,1.f);		glVertex3d(-h, size,-h);				
	glTexCoord2f(0.f,1.f);		glVertex3d( h, size,-h);				
	glEnd();

	//Bottom(Y)
	glBindTexture(GL_TEXTURE_2D,m_nTex[5]);
	glBegin(GL_QUADS);
	glTexCoord2f(0.f,0.f);		glVertex3d( h,0,-h);			
	glTexCoord2f(1.f,0.f);		glVertex3d(-h,0,-h);				
	glTexCoord2f(1.f,1.f);		glVertex3d(-h,0, h);				
	glTexCoord2f(0.f,1.f);		glVertex3d( h,0, h);				
	glEnd();
}