//sky.cpp
//draws the background skybox

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h> 
#include "vectors.h"
#include "textures.h"
#include "fileio.h"
#include "renderframe.h"
#include "screen.h"
#include "environment.h"
#include "gameinitexit.h"
#include "entities.h"
#include "gameframe.h"
#include "random.h"
#include "config.h"

#define kSkyDistance 7000.0
#define kBackgroundDistance 5000.0
#define kMapScale 1500.0

int gLightning=false;

//draws the background skybox
void SkyRender()
{
	glPushAttrib(GL_FOG_BIT+GL_LIGHTING_BIT+GL_DEPTH_BUFFER_BIT+GL_COLOR_BUFFER_BIT+GL_TRANSFORM_BIT);
	glDisable(GL_FOG);		
	glDisable(GL_LIGHTING);						
	glDepthFunc(GL_ALWAYS);		

	SetupWorldTranslation();
	glMatrixMode(GL_PROJECTION);
	
	glPushMatrix();
	glLoadIdentity();							// Reset The Projection Matrix

	float aspect=(float)gConfig->screenXSize/(float)gConfig->screenYSize;
	gluPerspective(gFOVY*(360.0/(2*PI)),aspect,0.1f,15000.0f);	// Calculate The Aspect Ratio Of The Window
				
				
	//==========draw skybox===============	
	if(gMapEnv)
		if(gMapEnv->sky0)
			TexturesSelectTex(gMapEnv->sky0);
		else		
			TexturesSelectTex(gEnvironment->sky0);
	else		
		TexturesSelectTex(gEnvironment->sky0);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(-kSkyDistance,kSkyDistance,kSkyDistance);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(kSkyDistance,kSkyDistance,kSkyDistance);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(kSkyDistance,-kSkyDistance,kSkyDistance);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(-kSkyDistance,-kSkyDistance,kSkyDistance);
	glEnd();
	
	if(gMapEnv)
		if(gMapEnv->sky270)
			TexturesSelectTex(gMapEnv->sky270);
		else		
			TexturesSelectTex(gEnvironment->sky270);
	else		
		TexturesSelectTex(gEnvironment->sky270);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(-kSkyDistance,kSkyDistance,-kSkyDistance);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(-kSkyDistance,kSkyDistance,kSkyDistance);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(-kSkyDistance,-kSkyDistance,kSkyDistance);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(-kSkyDistance,-kSkyDistance,-kSkyDistance);
	glEnd();
	
	if(gMapEnv)
		if(gMapEnv->sky180)
			TexturesSelectTex(gMapEnv->sky180);
		else		
			TexturesSelectTex(gEnvironment->sky180);
	else		
		TexturesSelectTex(gEnvironment->sky180);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(kSkyDistance,kSkyDistance,-kSkyDistance);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(-kSkyDistance,kSkyDistance,-kSkyDistance);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(-kSkyDistance,-kSkyDistance,-kSkyDistance);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(kSkyDistance,-kSkyDistance,-kSkyDistance);
	glEnd();
	
	if(gMapEnv)
		if(gMapEnv->sky90)
			TexturesSelectTex(gMapEnv->sky90);
		else		
			TexturesSelectTex(gEnvironment->sky90);
	else		
		TexturesSelectTex(gEnvironment->sky90);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(kSkyDistance,kSkyDistance,kSkyDistance);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(kSkyDistance,kSkyDistance,-kSkyDistance);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(kSkyDistance,-kSkyDistance,-kSkyDistance);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(kSkyDistance,-kSkyDistance,kSkyDistance);
	glEnd();
		
	if(gMapEnv)
		if(gMapEnv->skytop)
			TexturesSelectTex(gMapEnv->skytop);
		else		
			TexturesSelectTex(gEnvironment->skytop);
	else		
		TexturesSelectTex(gEnvironment->skytop);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(-kSkyDistance,kSkyDistance,-kSkyDistance);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(kSkyDistance,kSkyDistance,-kSkyDistance);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(kSkyDistance,kSkyDistance,kSkyDistance);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(-kSkyDistance,kSkyDistance,kSkyDistance);
	glEnd();

	if(gMapEnv)
		if(gMapEnv->skybot)
			TexturesSelectTex(gMapEnv->skybot);
		else		
			TexturesSelectTex(gEnvironment->skybot);
	else		
		TexturesSelectTex(gEnvironment->skybot);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(-kSkyDistance,-kSkyDistance,kSkyDistance);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(kSkyDistance,-kSkyDistance,kSkyDistance);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(kSkyDistance,-kSkyDistance,-kSkyDistance);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(-kSkyDistance,-kSkyDistance,-kSkyDistance);
	glEnd();

	glPopMatrix();


	//=========draw lightning flashes=======
	
	//is there lighning in this environment?
	if(gEnvironment->flashIntensity>0)
		//and is there a flash at the moment?
		if(gLightning)
		{
			glPushAttrib(GL_DEPTH_BUFFER_BIT+GL_FOG_BIT+GL_TEXTURE_BIT+GL_CURRENT_BIT+GL_TRANSFORM_BIT);

			gEnvironment->flashColor;
			
			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();
			glLoadIdentity();

			glDisable(GL_TEXTURE_2D);
			glDisable(GL_DEPTH_TEST);

			glEnable(GL_BLEND);
//			glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
			glBlendFunc(GL_DST_ALPHA,GL_ONE);

			//glEnable(GL_FOG);
			//just draw a covering plane
			glBegin(GL_TRIANGLE_STRIP);
				glColor3fv(&(RandomFl(0,1)*gEnvironment->flashColor).x);
				glVertex3f(-1.0f, 1.0f,-1.00f);
				glColor3fv(&(RandomFl(0,1)*gEnvironment->flashColor).x);
				glVertex3f(-1.0f,-1.0f,-1.00f);
				glColor3fv(&(RandomFl(0,1)*gEnvironment->flashColor).x);
				glVertex3f( 1.0f, 1.0f,-1.00f);
				glColor3fv(&(RandomFl(0,1)*gEnvironment->flashColor).x);
				glVertex3f( 1.0f,-1.0f,-1.00f);
			glEnd();
			glPopMatrix();
			glPopAttrib();
		}
	glPopAttrib();
	
/*	glPushMatrix();
	glLoadIdentity();							// Reset The Projection Matrix
	gluPerspective(gFOVY*(360.0/(2*PI)),aspect,0.1f,15000.0f);	// Calculate The Aspect Ratio Of The Window

	glEnable(GL_LIGHTING);						
	glNormal3f(0,1,0);
	

	//==========draw background===============		
	//the background (mountains, for example) is as another box inside the skybox,
	//which moves relative to the skybox as the camera moves, similar to paralax scrolling in 2d games.
	
/*	tVector3 backgroundShift;
	backgroundShift.x=-(gCameraEntity->pos.x/kMapScale);
	backgroundShift.y=-(gCameraEntity->pos.y/kMapScale);
	backgroundShift.z=-(gCameraEntity->pos.z/kMapScale);
	if(fabs(backgroundShift.x)>1)backgroundShift.x=sign(backgroundShift.x);
	if(fabs(backgroundShift.y)>1)backgroundShift.y=sign(backgroundShift.y);
	if(fabs(backgroundShift.z)>1)backgroundShift.z=sign(backgroundShift.z);	
	backgroundShift=backgroundShift*(kSkyDistance-kBackgroundDistance);

	TexturesSelectTex(gMapInfo->background0);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glEnd();
	
	TexturesSelectTex(gMapInfo->background270);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glEnd();
	
	TexturesSelectTex(gMapInfo->background180);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(-kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glEnd();
	
	TexturesSelectTex(gMapInfo->background90);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0f,0.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,0.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(1.0f,1.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glTexCoord2f(0.0f,1.0f);	
	glVertex3f(kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glEnd();

	glBegin(GL_QUADS);
	glVertex3f(-kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glVertex3f(kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,kBackgroundDistance+backgroundShift.z);
	glVertex3f(kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glVertex3f(-kBackgroundDistance+backgroundShift.x,-kBackgroundDistance+backgroundShift.y,-kBackgroundDistance+backgroundShift.z);
	glEnd();
		
	glPopMatrix();	

	glPopAttrib();*/


	#ifdef __POLYCOUNT
	gPolyCount+=20;
	#endif
}