#define dDOUBLE

#include <allegro.h>
#include <alleggl.h>

#include "skybox.h"

void CSkybox::init(char *file0, char *file1, char *file2, char *file3, char *file4, char *file5)
{
	BITMAP *bmp;
	
	bmp = load_bmp(file0, NULL);
	if(bmp==NULL)
	{
		allegro_message("Can't find %s", file0);
		exit(-1);
	}
	tex[0]=allegro_gl_make_texture(bmp);
	destroy_bitmap(bmp);
	bmp = load_bmp(file1, NULL);
	if(bmp==NULL)
	{
		allegro_message("Can't find %s", file1);
		exit(-1);
	}
	tex[1]=allegro_gl_make_texture(bmp);
	destroy_bitmap(bmp);
	bmp = load_bmp(file2, NULL);
	if(bmp==NULL)
	{
		allegro_message("Can't find %s", file2);
		exit(-1);
	}
	tex[2]=allegro_gl_make_texture(bmp);
	destroy_bitmap(bmp);
	bmp = load_bmp(file3, NULL);
	if(bmp==NULL)
	{
		allegro_message("Can't find %s", file3);
		exit(-1);
	}
	tex[3]=allegro_gl_make_texture(bmp);
	destroy_bitmap(bmp);
	bmp = load_bmp(file4, NULL);
	if(bmp==NULL)
	{
		allegro_message("Can't find %s", file4);
		exit(-1);
	}
	tex[4]=allegro_gl_make_texture(bmp);
	destroy_bitmap(bmp);
	bmp = load_bmp(file5, NULL);
	if(bmp==NULL)
	{
		allegro_message("Can't find %s", file5);
		exit(-1);
	}
	tex[5]=allegro_gl_make_texture(bmp);
	destroy_bitmap(bmp);
}

void CSkybox::draw(CVector camera, double roll)
{
	glDisable(GL_LIGHTING);
	glEnable(GL_TEXTURE_2D);
	glDisable(GL_DEPTH_TEST);
	glColor3f(1, 1, 1);
	
	glBindTexture(GL_TEXTURE_2D, tex[0]);
	glBegin(GL_QUADS);
	glTexCoord2f(1, 1);
	glVertex3f(1, 1, 1);
	glTexCoord2f(0, 1);
	glVertex3f(-1, 1, 1);
	glTexCoord2f(0, 0);
	glVertex3f(-1, 1, -1);
	glTexCoord2f(1, 0);
	glVertex3f(1, 1, -1);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, tex[1]);
	glBegin(GL_QUADS);
	glTexCoord2f(1, 1);
	glVertex3f(-1, -1, 1);
	glTexCoord2f(0, 1);
	glVertex3f(1, -1, 1);
	glTexCoord2f(0, 0);
	glVertex3f(1, -1, -1);
	glTexCoord2f(1, 0);
	glVertex3f(-1, -1, -1);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, tex[2]);
	glBegin(GL_QUADS);
	glTexCoord2f(1, 1);
	glVertex3f(-1, 1, 1);
	glTexCoord2f(0, 1);
	glVertex3f(-1, -1, 1);
	glTexCoord2f(0, 0);
	glVertex3f(-1, -1, -1);
	glTexCoord2f(1, 0);
	glVertex3f(-1, 1, -1);
	glEnd();

	glBindTexture(GL_TEXTURE_2D, tex[3]);
	glBegin(GL_QUADS);
	glTexCoord2f(1, 1);
	glVertex3f(1, -1, 1);
	glTexCoord2f(0, 1);
	glVertex3f(1, 1, 1);
	glTexCoord2f(0, 0);
	glVertex3f(1, 1, -1);
	glTexCoord2f(1, 0);
	glVertex3f(1, -1, -1);
	glEnd();
	
	glBindTexture(GL_TEXTURE_2D, tex[4]);
	glBegin(GL_QUADS);
	glTexCoord2f(1, 1);
	glVertex3f(1, -1, 1);
	glTexCoord2f(0, 1);
	glVertex3f(-1, -1, 1);
	glTexCoord2f(0, 0);
	glVertex3f(-1, 1, 1);
	glTexCoord2f(1, 0);
	glVertex3f(1, 1, 1);
	glEnd();
	
	glBindTexture(GL_TEXTURE_2D, tex[5]);
	glBegin(GL_QUADS);
	glTexCoord2f(1, 1);
	glVertex3f(1, 1, -1);
	glTexCoord2f(0, 1);
	glVertex3f(-1, 1, -1);
	glTexCoord2f(0, 0);
	glVertex3f(-1, -1, -1);
	glTexCoord2f(1, 0);
	glVertex3f(1, -1, -1);
	glEnd();
	
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
}

