
#include "stdafx.h"
#include "CParticleSys.h"

CParticleEmitor::CParticleEmitor()
{
	_textureID = 0;
	_startVel = W_GetVel(0.5f, 0.5f);
	_stop = 0;
	_emPos = CVector3(0,0,0);
	_blend = 1;
	_alpha = 0;
	_blendSFactor = GL_SRC_ALPHA;
	_blendDFactor = GL_ONE;
}

void CParticleEmitor::_RenderParticle( CParticle &p , float uhelx, float uhely )
{
	TRGBf color;
	register float _AS1 = 1.0f/p.max_live;
	color.r =  (1-(_AS1*p.live))*_endColor.r +(_AS1*p.live)*_startColor.r;
	color.g =  (1-(_AS1*p.live))*_endColor.g +(_AS1*p.live)*_startColor.g;
	color.b =  (1-(_AS1*p.live))*_endColor.b +(_AS1*p.live)*_startColor.b;
	glPushMatrix();
	
	CVector3 v = p.pos + _emPos;
	glColor4f(color.r, color.g, color.b, p.live);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 1); glVertex3f(v.x			, v.y			, v.z);
		glTexCoord2f(1, 1); glVertex3f(v.x+p.width	, v.y			, v.z);
		glTexCoord2f(1, 0); glVertex3f(v.x+p.width	, v.y+p.height	, v.z);
		glTexCoord2f(0, 0); glVertex3f(v.x			, v.y+p.height	, v.z);
	glEnd();
/*	CVector3 v = p.pos + _emPos;
	float velPulX = p.width/2.0f;
	float velPulY = p.height/2.0f;
	glTranslatef( _startPos.x+velPulX+_emPos.x, _startPos.y+velPulY+_emPos.y, _startPos.z+_emPos.z );
	glRotatef( -uhelx, 1.0f, 0.0f, 0.0f );
	glRotatef( -uhely, 0.0f, 1.0f, 0.0f );
	glTranslatef( p.pos.x, p.pos.y, p.pos.z );
	
	glColor4f(color.r, color.g, color.b, p.live);
	glBegin(GL_QUADS);
		glTexCoord2f(0, 1); glVertex3f(-velPulX	, -velPulY	, 0);
		glTexCoord2f(1, 1); glVertex3f(velPulX	, -velPulY	, 0);
		glTexCoord2f(1, 0); glVertex3f(velPulX	, velPulY	, 0);
		glTexCoord2f(0, 0); glVertex3f(-velPulX	, velPulY	, 0);
	glEnd();*/
	glPopMatrix();
}

void CParticleEmitor::Render( float uhelx, float uhely )
{
	std::vector<CParticle>::iterator iter = _vParticle.begin();
	
	if( !_textureID ) glDisable( GL_TEXTURE_2D );
	else
	{
		glEnable( GL_TEXTURE_2D );
		glBindTexture( GL_TEXTURE_2D, _textureID );
		if(_blend)
		{
			glEnable(GL_BLEND);
			glBlendFunc(_blendSFactor, _blendDFactor);
		}
		if(_alpha)
		{
			glEnable( GL_ALPHA_TEST );
			glAlphaFunc(GL_NOTEQUAL, 0.0f);
		}
	}
	char bFog = glIsEnabled(GL_FOG); //new
	char bLight = glIsEnabled(GL_LIGHTING); //new
	if(bFog) glDisable(GL_FOG);
	if(bLight) glDisable( GL_LIGHTING );
	// smycka
	for(; iter != _vParticle.end(); iter++)
	{
		if( iter->show ) this->_RenderParticle( *iter, uhelx, uhely );
	}
	
	glDisable( GL_BLEND );
	glDisable( GL_ALPHA_TEST );
	if(bFog) glEnable(GL_FOG);
	if(bLight) glEnable( GL_LIGHTING );
}

void CParticleEmitor::_UpdateParticle( CParticle &p, float time)
{
	p.pos = p.pos + p.smer*((p.speed*60.0f)/time);
	p.live -= (p.speed*60.0f)/time;
	if(p.live <= 0) 
	{
		p.show = 0;
		_NewParticle(p);
	}
}

void CParticleEmitor::Update(float timeFrame)
{
	std::vector<CParticle>::iterator iter = _vParticle.begin();
	for(; iter != _vParticle.end(); iter++)
	{
		if(iter->show) this->_UpdateParticle( *iter, timeFrame );
	}
}

void CParticleEmitor::_NewParticle(CParticle &p)
{
	if( _stop ) return;
	p.show = 1;
	p.pos = _startPos;
	p.smer = _smer;
	p.width = _startVel.width;
	p.height = _startVel.height;
	p.speed = (float)(rand()%(int)((_speed/2)*100000))/100000.0f + _speed/2; // pro
	p.live = _live;
	p.max_live = _live;

	float alpha = _pocUhel;
	float maxuhel = alpha + (_rozptil/2);
	float minuhel = alpha - (_rozptil/2);
	int nah = W_Spec2Rand(maxuhel, minuhel);
	p.smer.x = (float) sin( DegToRad( nah ) )*2;
	p.smer.y = (float) cos( DegToRad( nah ) )*2;
}

void CParticleEmitor::Create(uint quantity)
{
	if(_stop) return;
	//_vParticle.e
	for(int i=0; i<quantity; i++)
	{
		CParticle temp;
		_NewParticle(temp);
		_vParticle.push_back(temp);
	}
}

void CParticleEmitor::DeleteParticle()
{
	_vParticle.clear();
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

CPartKruh::CPartKruh()
{
	
}

void CPartKruh::Add( uint texID, CVector3 pos, float vel, float speed_vel, float speed  )
{
	TParticleKruh t;
	t.show = 1;
	t.texture_id = texID;
	t.pos = pos;
	t.vel = vel;
	t.speed_vel = speed_vel;
	t.speed = speed;
	t.live = 0.3f;
	_vParticle.push_back( t );
}
	
void CPartKruh::Draw()
{
	std::list<TParticleKruh>::iterator i = _vParticle.begin();
	for(; i != _vParticle.end(); i++)
	{
		CVector3 *p = &i->pos;
		float akt_vel = i->vel;
		float w = akt_vel/2;
		float h = akt_vel/2;
		glPushAttrib( GL_ENABLE_BIT );
		glColor4f(0,0,1, i->live);
		glEnable( GL_TEXTURE_2D );
		glBindTexture( GL_TEXTURE_2D, i->texture_id );
		glEnable( GL_BLEND );
		glBlendFunc( GL_SRC_ALPHA, GL_ONE ); 
		glDisable( GL_ALPHA_TEST );
		glDisable( GL_LIGHTING );
		glDisable( GL_FOG );
		glBegin( GL_QUADS );
			glTexCoord2f( 0, 0 ); glVertex3f( p->x-w, p->y, p->z-h );
			glTexCoord2f( 0, 1 ); glVertex3f( p->x-w, p->y, p->z+h );
			glTexCoord2f( 1, 1 ); glVertex3f( p->x+w, p->y, p->z+h );
			glTexCoord2f( 1, 0 ); glVertex3f( p->x+w, p->y, p->z-h );
		glEnd();
		/*glEnable( GL_ALPHA_TEST );
		glDisable( GL_BLEND );
		glDisable( GL_TEXTURE_2D );*/
		glPopAttrib( );
	}
}
	
void CPartKruh::Move( float time )
{
	std::list<TParticleKruh>::iterator i = _vParticle.begin();
	for(; i != _vParticle.end(); i++)
	{
		i->live -= time/60 * i->speed;
		i->vel += i->speed_vel;
		if( i->live < 0.0f ) i = _vParticle.erase( i );
	}
}