#include "skybox.h"

skybox::skybox(void)
{
}

bool skybox::init_skybox(int width, int heigth, int depth)
{

	//textures from: http://djflyer.dj.funpic.de/Map-TuT/djflyer_canyonsky01.jpg

	for (int i = 0; i<6; i++)
	{
		char file_name [50];
		sprintf_s(file_name,"pics/skybox_%i.tga",i);
		//file_name[17]= '\0';
		//printf(file_name);
	
	   if(!sky_box_tex[i].LoadTGA(file_name)) return false;
	   glGenTextures(1, &sky_box_tex[i].ID);
	   
	   glBindTexture(GL_TEXTURE_2D, sky_box_tex[i].ID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	   int type = sky_box_tex[i].type == 3 ? GL_RGB : GL_RGBA;
	   gluBuild2DMipmaps(GL_TEXTURE_2D, sky_box_tex[i].type, sky_box_tex[i].imageWidth, sky_box_tex[i].imageHeight,
						 type, GL_UNSIGNED_BYTE, sky_box_tex[i].image);
	}
	
	m_width = width;
	m_heigth = heigth;
	m_depth = depth;

	sky_box_list = glGenLists(1);
	glNewList (sky_box_list,GL_COMPILE);

		glEnable(GL_TEXTURE_2D);

		float mat_color[4] = { 0.5f, 0.5f, 1.0f, 1.0f };
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_color);

		float x = - m_width  / 2;		//center around origin
		float y = - m_heigth / 2;
		float z = - m_depth / 2;


		// front
		glBindTexture(GL_TEXTURE_2D, sky_box_tex[0].ID);
		glBegin(GL_QUADS);	
			glTexCoord2f(0.0f, 0.0f); glVertex3f(x,		  y,		z+m_depth);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(x,		  y+m_heigth, z+m_depth);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(x+m_width, y+m_heigth, z+m_depth); 
			glTexCoord2f(1.0f, 0.0f); glVertex3f(x+m_width, y,		z+m_depth);
		glEnd();
		//glDisable(GL_TEXTURE_2D);

		// back
		glBindTexture(GL_TEXTURE_2D, sky_box_tex[1].ID);
		glBegin(GL_QUADS);		
			glTexCoord2f(0.0f, 1.0f); glVertex3f(x+m_width, y,		z);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(x+m_width, y+m_heigth, z); 
			glTexCoord2f(1.0f, 0.0f); glVertex3f(x,		  y+m_heigth,	z);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(x,		  y,		z);
		glEnd();

		// left
		glBindTexture(GL_TEXTURE_2D, sky_box_tex[2].ID);
		glBegin(GL_QUADS);		
			glTexCoord2f(0.0f, 0.0f); glVertex3f(x,		  y+m_heigth,	z);	
			glTexCoord2f(1.0f, 0.0f); glVertex3f(x,		  y+m_heigth,	z+m_depth); 
			glTexCoord2f(1.0f, 1.0f); glVertex3f(x,		  y,		z+m_depth);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(x,		  y,		z);		
		glEnd();

		// right
		glBindTexture(GL_TEXTURE_2D, sky_box_tex[3].ID);
		glBegin(GL_QUADS);		
			glTexCoord2f(1.0f, 1.0f); glVertex3f(x+m_width, y,		z);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(x+m_width, y,		z+m_depth);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(x+m_width, y+m_heigth,	z+m_depth); 
			glTexCoord2f(1.0f, 0.0f); glVertex3f(x+m_width, y+m_heigth,	z);
		glEnd();

		// upper
		glBindTexture(GL_TEXTURE_2D, sky_box_tex[4].ID);
		glBegin(GL_QUADS);		
			glTexCoord2f(0.0f, 0.0f); glVertex3f(x+m_width, y+m_heigth, z);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(x+m_width, y+m_heigth, z+m_depth); 
			glTexCoord2f(1.0f, 1.0f); glVertex3f(x,		  y+m_heigth,	z+m_depth);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(x,		  y+m_heigth,	z);
		glEnd();

		// lower
		glBindTexture(GL_TEXTURE_2D, sky_box_tex[5].ID);
		glBegin(GL_QUADS);		
			glTexCoord2f(0.0f, 0.0f); glVertex3f(x,		  y,		z);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(x,		  y,		z+m_depth);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(x+m_width, y,		z+m_depth); 
			glTexCoord2f(0.0f, 1.0f); glVertex3f(x+m_width, y,		z);
		glEnd();

		glDisable(GL_TEXTURE_2D);

	glEndList();

	
	return true;

}

void skybox::render_skybox (void)
{
	
	glCallList(sky_box_list);
	

}

skybox::~skybox(void)
{
}
