00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00021
00022
00023 #ifndef H_SPK_PARTICLE
00024 #define H_SPK_PARTICLE
00025
00026 #include "Core/SPK_DEF.h"
00027 #include "Core/SPK_Vector3D.h"
00028 #include "Core/SPK_Pool.h"
00029 #include "Core/SPK_Model.h"
00030
00031
00032 namespace SPK
00033 {
00034 class Group;
00035
00047 class SPK_PREFIX Particle
00048 {
00049 friend bool isFurtherToCamera(const Particle&,const Particle&);
00050 friend void swapParticles(Particle& a,Particle& b);
00051 friend class Group;
00052 friend class Pool<Particle>;
00053
00054
00055 public :
00056
00067 bool setParamCurrentValue(ModelParam type,float value);
00068
00080 bool setParamFinalValue(ModelParam type,float value);
00081
00093 bool changeParamCurrentValue(ModelParam type,float delta);
00094
00106 bool changeParamFinalValue(ModelParam type,float delta);
00107
00115 void setLifeLeft(float life);
00116
00117
00119
00121
00127 Vector3D& position();
00128
00134 Vector3D& velocity();
00135
00141 Vector3D& oldPosition();
00142
00151 const Vector3D& position() const;
00152
00161 const Vector3D& velocity() const;
00162
00171 const Vector3D& oldPosition() const;
00172
00181 float getParamCurrentValue(ModelParam type) const;
00182
00192 float getParamFinalValue(ModelParam type) const;
00193
00198 Model* getModel() const;
00199
00205 Group* getGroup() const;
00206
00212 size_t getIndex() const;
00213
00222 float getLifeLeft() const;
00223
00233 float getAge() const;
00234
00243 float getDistanceFromCamera() const;
00244
00254 float getSqrDistanceFromCamera() const;
00255
00264 bool isNewBorn() const;
00265
00274 bool isAlive() const;
00275
00277
00279
00285 void init();
00286
00294 void kill();
00295
00296
00297 float getR() const { return currentParams[PARAM_RED]; }
00298 float getG() const { return currentParams[PARAM_GREEN]; }
00299 float getB() const { return currentParams[PARAM_BLUE]; }
00300
00301 private :
00302
00303 struct ParticleData
00304 {
00305 Vector3D oldPosition;
00306 Vector3D position;
00307 Vector3D velocity;
00308 float age;
00309 float life;
00310 float sqrDist;
00311 };
00312
00313 Group* group;
00314 size_t index;
00315
00316 ParticleData* data;
00317 float* currentParams;
00318 float* extendedParams;
00319
00320 Particle(Group* group,size_t index);
00321
00322 bool update(float timeDelta);
00323 void computeSqrDist();
00324
00325 void interpolateParameters();
00326 };
00327
00328
00329 inline Group* Particle::getGroup() const
00330 {
00331 return group;
00332 }
00333
00334 inline size_t Particle::getIndex() const
00335 {
00336 return index;
00337 }
00338
00339 inline void Particle::setLifeLeft(float life)
00340 {
00341 data->life = life;
00342 }
00343
00344 inline Vector3D& Particle::position()
00345 {
00346 return data->position;
00347 }
00348
00349 inline Vector3D& Particle::velocity()
00350 {
00351 return data->velocity;
00352 }
00353
00354 inline Vector3D& Particle::oldPosition()
00355 {
00356 return data->oldPosition;
00357 }
00358
00359 inline const Vector3D& Particle::position() const
00360 {
00361 return data->position;
00362 }
00363
00364 inline const Vector3D& Particle::velocity() const
00365 {
00366 return data->velocity;
00367 }
00368
00369 inline const Vector3D& Particle::oldPosition() const
00370 {
00371 return data->oldPosition;
00372 }
00373
00374 inline float Particle::getLifeLeft() const
00375 {
00376 return data->life;
00377 }
00378
00379 inline float Particle::getAge() const
00380 {
00381 return data->age;
00382 }
00383
00384 inline float Particle::getDistanceFromCamera() const
00385 {
00386 return std::sqrt(data->sqrDist);
00387 }
00388
00389 inline float Particle::getSqrDistanceFromCamera() const
00390 {
00391 return data->sqrDist;
00392 }
00393
00394 inline bool Particle::isNewBorn() const
00395 {
00396 return data->age == 0.0f;
00397 }
00398
00399 inline bool Particle::isAlive() const
00400 {
00401 return data->life > 0.0f;
00402 }
00403
00404 inline void Particle::kill()
00405 {
00406 data->life = 0.0f;
00407 }
00408
00409
00410 template<>
00411 inline void Pool<Particle>::swapElements(Particle& a,Particle& b)
00412 {
00413 swapParticles(a,b);
00414 }
00415
00416
00418
00420
00421 inline bool isFurtherToCamera(const Particle& a, const Particle& b)
00422 {
00423 return a.getSqrDistanceFromCamera() > b.getSqrDistanceFromCamera();
00424 }
00425
00426
00427 extern void swapParticles(Particle& a,Particle& b);
00428 }
00429
00430 #endif