#include "particle.h"

BasicParticle::BasicParticle( void )
{
	this->acceleration = 0;
	this->lifeTime = -1;
}
BasicParticle::BasicParticle( const Point_3<float> &position, Vector_3<float> &velocity, float acceleration, int lifeTime ): position(position), velocity(velocity), acceleration(acceleration), lifeTime(lifeTime)
{
}
void BasicParticle::simulate( float dt )
{
	//this->velocity += this->velocity * this->acceleration * dt; //BUG FIXED 24.8.
	this->velocity += this->acceleration * dt;
	this->position += this->velocity * dt;
	this->lifeTime -= dt;
}
bool BasicParticle::isAlive( void )
{
	return this->lifeTime > 0;
}
void BasicParticle::setPosition( const Point_3<float> &position )
{
	this->position = position;
}
void BasicParticle::setVelocity( const Vector_3<float> &velocity )
{
	this->velocity = velocity;
}
void BasicParticle::setAcceleration( float acceleration )
{
	this->acceleration = acceleration;
}
void BasicParticle::setLifeTime( float lifeTime )
{
	this->lifeTime = lifeTime;
}
GraphicParticle::GraphicParticle(void)
{
	for ( int i=0; i<4; ++i) 
	{
		this->colorOffset[i]= 0;
		this->color[i]= 1.0;
	}

	this->size = 0;
	this->textureID = 0;
}
GraphicParticle::GraphicParticle( const GraphicParticle& copyParticle )
{
	*this = copyParticle; //use user defined operator= 
}
GraphicParticle& GraphicParticle::operator=( const GraphicParticle& copyParticle)
{
	BasicParticle::operator=(copyParticle);

	for ( int i=0; i<4; ++i)
	{
		this->colorOffset[i] = copyParticle.colorOffset[i];
		this->color[i] = copyParticle.color[i];
	}

	this->sizeOffset = copyParticle.sizeOffset;
	this->size = copyParticle.size;
	this->textureID = copyParticle.textureID;

	return *this;
}

void GraphicParticle::setSizeOffset( float sizeOffset )
{
	this->sizeOffset = sizeOffset;
}
void GraphicParticle::setColorOffset( float redOffset, float greenOffset, float blueOffset, float alphaOffset )
{
	this->colorOffset[0] = redOffset;
	this->colorOffset[1] = greenOffset;
	this->colorOffset[2] = blueOffset;
	this->colorOffset[3] = alphaOffset;
}

void GraphicParticle::simulate( float dt )
{
	BasicParticle::simulate( dt );

	for ( int i=0; i<4; ++i) 
	{
		this->color[i] += this->colorOffset[i] * dt;
		clamp<float>( 0.0, 1.0, this->color[i] );
	}

	this->size += this->sizeOffset * dt;
}


void GraphicParticle::render(  const Vector_3<float> &normRightVec, const Vector_3<float> &normUpVec )
{
	const Point_3<float>  zeroPoint(0,0,0);
	glBindTexture( GL_TEXTURE_2D, this->textureID );
	glColor4fv( this->color );
	
	glPushMatrix();

	//TODO, PERFORMANCE, FIXME : very ineficient to call translate and begin for each particle!!!
	glTranslatef( this->position.x(), this->position.y(), this->position.z() );
	glBegin( GL_QUADS );
		glTexCoord2f(1,1); glVertex3fv( ( zeroPoint + normRightVec * this->size + normUpVec * this->size).getArrPtr() );
		glTexCoord2f(0,1); glVertex3fv( ( zeroPoint - normRightVec * this->size + normUpVec * this->size).getArrPtr() );
		glTexCoord2f(0,0); glVertex3fv( ( zeroPoint - normRightVec * this->size - normUpVec * this->size).getArrPtr() );
		glTexCoord2f(1,0); glVertex3fv( ( zeroPoint + normRightVec * this->size - normUpVec * this->size).getArrPtr() );
	glEnd();

	glPopMatrix();
}