[4597] | 1 | /*! |
---|
[5039] | 2 | * @file particle_system.h |
---|
[3329] | 3 | |
---|
[3245] | 4 | */ |
---|
[1853] | 5 | |
---|
[3925] | 6 | #ifndef _PARTICLE_SYSTEM_H |
---|
| 7 | #define _PARTICLE_SYSTEM_H |
---|
[1853] | 8 | |
---|
[4493] | 9 | #include "world_entity.h" |
---|
[4377] | 10 | #include "physics_interface.h" |
---|
| 11 | |
---|
[4381] | 12 | #include "glincl.h" |
---|
[3925] | 13 | #include "vector.h" |
---|
[6612] | 14 | #include <list> |
---|
[3930] | 15 | |
---|
[4421] | 16 | #include "quick_animation.h" |
---|
| 17 | |
---|
[4602] | 18 | // Forward Declaration |
---|
| 19 | class TiXmlElement; |
---|
| 20 | |
---|
[4478] | 21 | #define PARTICLE_DOT_MASK 0x000001 //!< A Mask if the Particles should be displayed as DOTs |
---|
| 22 | #define PARTICLE_SPARK_MASK 0x000010 //!< A Mask if the Particles should be displayed as SPARKs |
---|
| 23 | #define PARTICLE_SPRITE_MASK 0x000100 //!< A Mask if the Particles should be displayed as SPRITESs |
---|
| 24 | #define PARTICLE_MODEL_MASK 0x001000 //!< A Mask if the Particles should be displayed as MODELSs |
---|
| 25 | #define PARTICLE_WORDL_ENTITY_MASK 0x010000 //!< A Mask if the Particles should be displayed as WORLD_ENTITIEs |
---|
| 26 | #define PARTICLE_MULTI_MASK 0x100000 //!< A Mask if they are Multi-partilces |
---|
[3956] | 27 | |
---|
[4478] | 28 | #define PARTICLE_DEFAULT_MAX_COUNT 200 //!< A default count of particles in the system. |
---|
[3930] | 29 | |
---|
[5405] | 30 | // FORWARD DECLARATION |
---|
[3926] | 31 | class Material; |
---|
[3930] | 32 | class ParticleEmitter; |
---|
[4338] | 33 | class Field; |
---|
[3543] | 34 | |
---|
[3925] | 35 | //! A struct for one Particle |
---|
| 36 | typedef struct Particle |
---|
| 37 | { |
---|
[4493] | 38 | float lifeTime; //!< The time this particle has to live. |
---|
| 39 | float lifeCycle; //!< The fraction of time passed. (in percentage of its lifeTime) |
---|
[4338] | 40 | |
---|
[4493] | 41 | Vector position; //!< The current position of this particle. |
---|
[4687] | 42 | Vector velocity; //!< The current velocity of this Particle. |
---|
[4493] | 43 | Vector extForce; //!< The external Force that influences this Particle. |
---|
[4687] | 44 | Quaternion orientation; //!< The current orientation of this Particle. |
---|
| 45 | Quaternion momentum; //!< The current angular momentum (spin) of this Particle. |
---|
| 46 | float mass; //!< The mass of this Particle. |
---|
[4493] | 47 | float massRand; //!< A random mass |
---|
[4687] | 48 | float radius; //!< The current size of this Particle. |
---|
[4493] | 49 | float radiusRand; //!< a random Radius |
---|
| 50 | GLfloat color [4]; //!< A Color for the particles. |
---|
[3930] | 51 | |
---|
[4493] | 52 | Particle* next; //!< pointer to the next particle in the List. (NULL if no preceding one) |
---|
[3925] | 53 | }; |
---|
[3543] | 54 | |
---|
[4394] | 55 | //! A class to handle ParticleSystems |
---|
[4493] | 56 | class ParticleSystem : public WorldEntity, public PhysicsInterface { |
---|
[4597] | 57 | |
---|
[1904] | 58 | public: |
---|
[6621] | 59 | ParticleSystem(unsigned int maxCount = PARTICLE_DEFAULT_MAX_COUNT); |
---|
[3925] | 60 | virtual ~ParticleSystem(); |
---|
[1853] | 61 | |
---|
[4746] | 62 | void init(); |
---|
[6512] | 63 | virtual void loadParams(const TiXmlElement* root); |
---|
[6623] | 64 | void loadEmitters(const TiXmlElement* root); |
---|
[4602] | 65 | |
---|
[3931] | 66 | void setLifeSpan(float lifeSpan, float randomLifeSpan = 0.0); |
---|
[3932] | 67 | void setConserve(float conserve); |
---|
[4727] | 68 | void setMaxCount(int maxCount); |
---|
[3245] | 69 | |
---|
[4430] | 70 | /* Per-Particle-Attributes */ |
---|
| 71 | void setRadius(float lifeCycleTime, float radius, float randRadius = 0.0); |
---|
| 72 | void setMass(float lifeCycleTime, float mass, float randMass = 0.0); |
---|
[4725] | 73 | void setColor(float lifeCycleTime, float red, float green, float blue, float alpha); |
---|
[4338] | 74 | |
---|
[4836] | 75 | /** @returns the lifespan of the particles */ |
---|
[4746] | 76 | inline float getLifeSpan() const { return this->lifeSpan; }; |
---|
[4836] | 77 | /** @returns the starting-radius of the particles */ |
---|
[4746] | 78 | inline float getStartRadius() { return this->radiusAnim.getValue(0.0); }; |
---|
[4836] | 79 | /** @returns the end-radius of the particles */ |
---|
[4746] | 80 | inline float getEndRadius() { return this->radiusAnim.getValue(1.0); }; |
---|
[4836] | 81 | /** @returns the conserve-factor of the particles */ |
---|
[4746] | 82 | inline float getConserve() const { return this->conserve; }; |
---|
[4836] | 83 | /** @returns the initial mass of the particles */ |
---|
[4746] | 84 | inline float getMass() const { return this->initialMass; }; |
---|
[4338] | 85 | |
---|
[7027] | 86 | /** @returns the count of particles in this System */ |
---|
| 87 | inline unsigned int getCount() const { return this->count; }; |
---|
| 88 | /** @returns the maximum count of particles that can be contained by this System */ |
---|
| 89 | inline unsigned int getMaxCount() const { return this->maxCount; }; |
---|
| 90 | |
---|
[4746] | 91 | virtual unsigned int getFaceCount() const; |
---|
[4677] | 92 | |
---|
[6612] | 93 | void addEmitter(ParticleEmitter* emitter); |
---|
| 94 | void removeEmitter(ParticleEmitter* emitter); |
---|
[4677] | 95 | |
---|
[4395] | 96 | virtual void applyField(Field* field); |
---|
[6621] | 97 | /** @brief this is an empty function, because the Physics are implemented in tick @param dt: useless here */ |
---|
[4396] | 98 | virtual void tickPhys(float dt) {}; |
---|
[4338] | 99 | |
---|
[4690] | 100 | void addParticle(const Vector& position, const Vector& velocity, const Quaternion& orientation, const Quaternion& momentum, unsigned int data = 0); |
---|
[4394] | 101 | |
---|
[6629] | 102 | void precache(unsigned int seconds, unsigned int ticksPerSecond = 25); |
---|
| 103 | |
---|
[4493] | 104 | virtual void tick(float dt); |
---|
[6621] | 105 | virtual void draw() const = 0; |
---|
[3931] | 106 | |
---|
[4746] | 107 | void debug() const; |
---|
[3944] | 108 | |
---|
[6621] | 109 | protected: |
---|
[4478] | 110 | float conserve; //!< How much energy gets conserved to the next Tick. |
---|
| 111 | float lifeSpan; //!< Initial lifetime of a Particle. |
---|
| 112 | float randomLifeSpan; //!< A random value for the Lifespan (around the initial lifetime) |
---|
| 113 | float initialMass; //!< The initial Mass of the Particle |
---|
| 114 | float randomInitialMass; //!< The random initial Mass of the Particle |
---|
[3938] | 115 | |
---|
[4478] | 116 | int maxCount; //!< The maximum count of Particles. |
---|
| 117 | int count; //!< The current count of Particles. |
---|
| 118 | Particle* particles; //!< A list of particles of this System. |
---|
| 119 | Particle* deadList; //!< A list of dead Particles in the System. |
---|
[3932] | 120 | |
---|
[6612] | 121 | std::list<ParticleEmitter*> emitters; //!< The Emitters that do emit into this System. |
---|
| 122 | |
---|
[4431] | 123 | // per particle attributes |
---|
[4493] | 124 | QuickAnimation radiusAnim; //!< Animation of the radius |
---|
| 125 | QuickAnimation randRadiusAnim; //!< Animation of the random Value of the radius |
---|
| 126 | QuickAnimation massAnim; //!< Animation of the mass |
---|
| 127 | QuickAnimation randMassAnim; //!< Animation of the random Mass |
---|
| 128 | QuickAnimation colorAnim[4]; //!< Animation of the 4 colors (r,g,b,a) |
---|
[1853] | 129 | }; |
---|
| 130 | |
---|
[3925] | 131 | #endif /* _PARTICLE_SYSTEM_H */ |
---|