#include ".\particlesystem.h" #include "includes.h" #include #include int CompareInc( const void *arg1, const void *arg2 ) { Particle* particle1=(Particle*) arg1; Particle* particle2=(Particle*) arg2; if(*particle1>*particle2) return -1; else return 1; } int CompareDec( const void *arg1, const void *arg2 ) { Particle* particle1=(Particle*) arg1; Particle* particle2=(Particle*) arg2; if(*particle1>*particle2) return 1; else return -1; } void ParticleSystem::SortParticles(bool increment) { if(increment) qsort( m_ParticleArray, (size_t)m_Count, sizeof(Particle), CompareInc ); else qsort( m_ParticleArray, (size_t)m_Count, sizeof(Particle), CompareDec ); } ParticleSystem::ParticleSystem(void) { VertexBuffer=NULL; TexcoordBuffer=NULL; ColorBuffer=NULL; m_LastParticle=NULL; m_ParticleList=new Particle; m_ParticleArray=NULL; m_Count=0; m_Quota=100; m_Type=0; m_UpVector=Vector(0,1,0); m_RightVector=Vector(1,0,0); m_refreshed=false; m_refreshonce=false; m_LittleSystemRadius=1.0; } ParticleSystem::~ParticleSystem(void) { } void ParticleSystem::RefreshParticles(unsigned int Dt,unsigned int TimefromSecond) { if(!m_refreshed) { //Refresh particle ages, kill dead particles; refresh particle positions Particle* thisParticle=m_ParticleList->m_NextParticle; Particle* parentParticle=m_ParticleList; while(thisParticle) { if(thisParticle->LiveMore(Dt)) { //refresh particle thisParticle->Refresh(Dt); //thisParticle->m_Distance=(thisParticle->getPosition()-m_CameraPosition).Length(); m_LastParticle=thisParticle; parentParticle=thisParticle; thisParticle=thisParticle->m_NextParticle; } else { //kill particle parentParticle->m_NextParticle=thisParticle->m_NextParticle; delete thisParticle; thisParticle=parentParticle->m_NextParticle; m_LastParticle=parentParticle; m_Count--; } } //Ask emitter to add new particles Particle* Addto=m_LastParticle; if(Addto==NULL)Addto=m_ParticleList; unsigned int AddedCount=m_Emitter.Emit(TimefromSecond,m_Quota-m_Count,Addto,m_CameraPosition); m_Count+=AddedCount; //fill array ;update bounding radius delete[] m_ParticleArray; m_ParticleArray=new Particle[m_Count]; //RefrBRadius_Dist(); } } void ParticleSystem::RefrBRadius_Dist(bool refresh_rad) { Particle* thisParticle=m_ParticleList->m_NextParticle; if(refresh_rad||!m_refreshed)m_BoundingRadius=0; double dist; for(unsigned int i=0;im_Distance=(thisParticle->getPosition()-m_CameraPosition).Length(); thisParticle->m_ID=i; double oldSize; if(refresh_rad||!m_refreshed) { m_refreshed=true; oldSize=thisParticle->getSize(); thisParticle->setSize(oldSize*m_LittleSystemRadius); dist=thisParticle->getPosition().Length()+thisParticle->getSize(); if(dist>m_BoundingRadius) m_BoundingRadius=dist; } m_ParticleArray[i]=*thisParticle; if(refresh_rad||!m_refreshed)thisParticle->setSize(oldSize); thisParticle=thisParticle->m_NextParticle; } } void ParticleSystem::RefreshBuffer(bool billboard) { /* if(VertexBuffer!=NULL)delete VertexBuffer; if(TexcoordBuffer!=NULL)delete TexcoordBuffer; if(ColorBuffer!=NULL)delete ColorBuffer; */ if(billboard) { double x,y,z; Vector LeftDown=m_UpVector*-1-m_RightVector; Vector RightDown=m_UpVector*-1+m_RightVector; Vector RightUp=m_UpVector+m_RightVector; Vector LeftUp=m_UpVector-m_RightVector; VertexBuffer=new float[12*m_Count]; ColorBuffer=new float[16*m_Count]; TexcoordBuffer=new float[16*m_Count]; for(unsigned int i=0;i