#include "SpaceSky.h"


void SpaceSkyPlanet::simulate( float dt )
{

	//-------------------
	// color intensity change
	this->colorMultiplier += colorChangeSpeed * dt;

	if ( colorChangeSpeed>0 && this->colorMultiplier > this->colorMultiplierLimits[1] )
	{
		this->colorMultiplier = this->colorMultiplierLimits[1];
		colorChangeSpeed = -colorChangeSpeed; //change direction
	}
	else if ( colorChangeSpeed<=0 && this->colorMultiplier < this->colorMultiplierLimits[0] )
	{
		this->colorMultiplier = this->colorMultiplierLimits[0];
		colorChangeSpeed = -colorChangeSpeed; //change direction
	}
	//-------------------



}
void SpaceSkyPlanet::render( Render &render )
{
	CTriCol tmpColor;
	tmpColor = this->color * this->colorMultiplier;

	clamp<float>( 0.0, 1.0, tmpColor.r );
	clamp<float>( 0.0, 1.0, tmpColor.g );
	clamp<float>( 0.0, 1.0, tmpColor.b );

	glColor3f( tmpColor.r, tmpColor.g, tmpColor.b );
	glPointSize( this->size );

	glBegin( GL_POINTS );
		glVertex2f( this->pos[0], this->pos[1] );
	glEnd();
}

void SpaceSkyBox::simulate( float dt )
{
	for ( int i=0; i<6; ++i)
	{
		for ( int n=0; n<this->skyPlane[i].size(); ++n)
		{
			this->skyPlane[i][n]->simulate( dt );
		}
	}
}
void SpaceSkyBox::render( Render &render )
{
	render.bind();

	GLboolean useDepthTest;
	glGetBooleanv( GL_DEPTH_TEST, &useDepthTest );
	glDisable( GL_DEPTH_TEST );

	glDisable(GL_TEXTURE_2D);
	

	//BACK SIDE
	glPushMatrix();
	
	glTranslatef( 0,0, -200 );
	glScalef( 200,200,200);
	for ( int n=0; n<this->skyPlane[BACK].size(); ++n)
	{
		this->skyPlane[ BACK ][n]->render( render );
	}
	glPopMatrix();

	//FRONT SIDE
	glPushMatrix();
	glTranslatef( 0,0, 200 );
	glScalef( 200,200,200);
	
	for ( int n=0; n<this->skyPlane[FRONT].size(); ++n)
	{
		this->skyPlane[ FRONT ][n]->render( render );
	}
	glPopMatrix();

	//LEFT SIDE
	glPushMatrix();
	glRotatef( 90,0,1,0);
	glTranslatef( 0,0, -200 );
	glScalef( 200,200,200);
	
	for ( int n=0; n<this->skyPlane[LEFT].size(); ++n)
	{
		this->skyPlane[ LEFT ][n]->render( render );
	}
	glPopMatrix();

	//RIGHT SIDE
	glPushMatrix();
	glRotatef( -90,0,1,0);
	glTranslatef( 0,0, -200 );
	glScalef( 200,200,200);
	
	for ( int n=0; n<this->skyPlane[RIGHT].size(); ++n)
	{
		this->skyPlane[ RIGHT ][n]->render( render );
	}
	glPopMatrix();

	//TOP SIDE
	glPushMatrix();
	glRotatef( -90,1,0,0);
	glTranslatef( 0,0, -200 );
	glScalef( 200,200,200);
	
	for ( int n=0; n<this->skyPlane[TOP].size(); ++n)
	{
		this->skyPlane[ TOP ][n]->render( render );
	}
	glPopMatrix();

	//BOTTOM SIDE
	glPushMatrix();
	glRotatef( 90,1,0,0);
	glTranslatef( 0,0, -200 );
	glScalef( 200,200,200);
	
	for ( int n=0; n<this->skyPlane[BOTTOM].size(); ++n)
	{
		this->skyPlane[ BOTTOM ][n]->render( render );
	}
	glPopMatrix();

	if ( useDepthTest ) glEnable( GL_DEPTH_TEST ); else glDisable( GL_DEPTH_TEST );
}


SpaceSkyBox::~SpaceSkyBox(void)
{
	for ( int i=0; i<6; ++i)
	{
		for ( int n=0; n<this->skyPlane[i].size(); ++n)
		{
			delete this->skyPlane[i][n];
		}
		this->skyPlane[i].clear();
	}

}


void SpaceSkyPlane::simulate( float dt )
{
	for ( int i=0; i<this->skyPlane.size(); ++i)
	{
		this->skyPlane[i]->simulate( dt );
	}
}
void SpaceSkyPlane::render( Render &render )
{
	render.bind();

	//GLboolean useDepthTest;
	//glGetBooleanv( GL_DEPTH_TEST, &useDepthTest );
	//glDisable( GL_DEPTH_TEST );


	glDisable(GL_TEXTURE_2D);

	glPushMatrix();
	glLoadIdentity();
	glTranslatef( 0,0,  -render.getProjection().zFar * ( 1-EPSILON)  );
	glScalef( -render.getProjection().zFar * ( 1-EPSILON) , -render.getProjection().zFar * ( 1-EPSILON) , -render.getProjection().zFar * ( 1-EPSILON) );

	for ( int i=0; i<this->skyPlane.size(); ++i )
	{
		this->skyPlane[i]->render( render );
	}

	glPopMatrix();

	//if ( useDepthTest ) glEnable( GL_DEPTH_TEST ); else glDisable( GL_DEPTH_TEST );
}

SpaceSkyPlane::~SpaceSkyPlane(void)
{
	for ( int i=0; i<this->skyPlane.size(); ++i )
	{
		delete this->skyPlane[i];
	}
	this->skyPlane.clear();
}


void generateSpaceSkyPlanets( unsigned int planetCount, vector<SpaceSkyPlanet*> &dstContainer)
{
	for ( int i=0; i< planetCount; ++i)
	{
		SpaceSkyPlanet *planet = new SpaceSkyPlanet();
		planet->color = CTriCol( 0.5 + 0.4 * getRandomNumberFrom0To1(), 0.5 + 0.4 * getRandomNumberFrom0To1(), 0.5 + 0.6 * getRandomNumberFrom0To1() );
		planet->colorChangeSpeed = 0.01 * getRandomNumberFrom0To1();
		planet->colorMultiplier = planet->colorChangeSpeed ;

		planet->colorMultiplierLimits[0] = 0.0 + 0.5 * getRandomNumberFrom0To1();
		planet->colorMultiplierLimits[1] = 0.5 + 5.0 * getRandomNumberFrom0To1();

		planet->pos[0] = -1.0 + 2.0 * getRandomNumberFrom0To1();
		planet->pos[1] = -1.0 + 2.0 * getRandomNumberFrom0To1();

		planet->size = 3.0 * getRandomNumberFrom0To1();

		dstContainer.push_back( planet );
	}
}

