| 1 | /*! |
|---|
| 2 | * @file particle_system.h |
|---|
| 3 | |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _PARTICLE_SYSTEM_H |
|---|
| 7 | #define _PARTICLE_SYSTEM_H |
|---|
| 8 | |
|---|
| 9 | #include "world_entity.h" |
|---|
| 10 | #include "physics_interface.h" |
|---|
| 11 | |
|---|
| 12 | #include "glincl.h" |
|---|
| 13 | #include "vector.h" |
|---|
| 14 | #include <list> |
|---|
| 15 | |
|---|
| 16 | #include "quick_animation.h" |
|---|
| 17 | |
|---|
| 18 | // Forward Declaration |
|---|
| 19 | class TiXmlElement; |
|---|
| 20 | |
|---|
| 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 |
|---|
| 27 | |
|---|
| 28 | //! An enumerator for the different types of particles. |
|---|
| 29 | typedef enum PARTICLE_TYPE |
|---|
| 30 | { |
|---|
| 31 | PARTICLE_DOT = PARTICLE_DOT_MASK, |
|---|
| 32 | PARTICLE_SPARK = PARTICLE_SPARK_MASK, |
|---|
| 33 | PARTICLE_SPRITE = PARTICLE_SPRITE_MASK, |
|---|
| 34 | PARTICLE_MULTI_SPRITE = PARTICLE_SPRITE_MASK | PARTICLE_MULTI_MASK, |
|---|
| 35 | PARTICLE_MODEL = PARTICLE_MODEL_MASK, |
|---|
| 36 | PARTICLE_MULTI_MODE = PARTICLE_MODEL_MASK | PARTICLE_MULTI_MASK |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | #define PARTICLE_DEFAULT_MAX_COUNT 200 //!< A default count of particles in the system. |
|---|
| 40 | #define PARTICLE_DEFAULT_TYPE PARTICLE_SPRITE //!< A default type of the system. |
|---|
| 41 | |
|---|
| 42 | // FORWARD DECLARATION |
|---|
| 43 | class Material; |
|---|
| 44 | class ParticleEmitter; |
|---|
| 45 | class Field; |
|---|
| 46 | |
|---|
| 47 | //! A struct for one Particle |
|---|
| 48 | typedef struct Particle |
|---|
| 49 | { |
|---|
| 50 | float lifeTime; //!< The time this particle has to live. |
|---|
| 51 | float lifeCycle; //!< The fraction of time passed. (in percentage of its lifeTime) |
|---|
| 52 | |
|---|
| 53 | Vector position; //!< The current position of this particle. |
|---|
| 54 | Vector velocity; //!< The current velocity of this Particle. |
|---|
| 55 | Vector extForce; //!< The external Force that influences this Particle. |
|---|
| 56 | Quaternion orientation; //!< The current orientation of this Particle. |
|---|
| 57 | Quaternion momentum; //!< The current angular momentum (spin) of this Particle. |
|---|
| 58 | float mass; //!< The mass of this Particle. |
|---|
| 59 | float massRand; //!< A random mass |
|---|
| 60 | float radius; //!< The current size of this Particle. |
|---|
| 61 | float radiusRand; //!< a random Radius |
|---|
| 62 | GLfloat color [4]; //!< A Color for the particles. |
|---|
| 63 | |
|---|
| 64 | Particle* next; //!< pointer to the next particle in the List. (NULL if no preceding one) |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | //! A class to handle ParticleSystems |
|---|
| 68 | class ParticleSystem : public WorldEntity, public PhysicsInterface { |
|---|
| 69 | |
|---|
| 70 | public: |
|---|
| 71 | ParticleSystem(unsigned int maxCount = PARTICLE_DEFAULT_MAX_COUNT, |
|---|
| 72 | PARTICLE_TYPE type = PARTICLE_DEFAULT_TYPE); |
|---|
| 73 | ParticleSystem(const TiXmlElement* root); |
|---|
| 74 | virtual ~ParticleSystem(); |
|---|
| 75 | |
|---|
| 76 | void init(); |
|---|
| 77 | virtual void loadParams(const TiXmlElement* root); |
|---|
| 78 | |
|---|
| 79 | void setType(const char* particleType); |
|---|
| 80 | void setType(PARTICLE_TYPE particleType, int count = 0); |
|---|
| 81 | void setMaterial(Material* material); |
|---|
| 82 | void setMaterialTexture(const char* textureFile); |
|---|
| 83 | void setModel(const char* modelName = NULL); |
|---|
| 84 | void setLifeSpan(float lifeSpan, float randomLifeSpan = 0.0); |
|---|
| 85 | void setConserve(float conserve); |
|---|
| 86 | void setMaxCount(int maxCount); |
|---|
| 87 | |
|---|
| 88 | /* Per-Particle-Attributes */ |
|---|
| 89 | void setRadius(float lifeCycleTime, float radius, float randRadius = 0.0); |
|---|
| 90 | void setMass(float lifeCycleTime, float mass, float randMass = 0.0); |
|---|
| 91 | void setColor(float lifeCycleTime, float red, float green, float blue, float alpha); |
|---|
| 92 | |
|---|
| 93 | /** @returns the Type of the particles */ |
|---|
| 94 | inline PARTICLE_TYPE getType() const { return this->particleType; }; |
|---|
| 95 | /** @returns the Material that lies on this particles */ |
|---|
| 96 | inline const Material* getMaterial() const { return this->material; }; |
|---|
| 97 | /** @returns the lifespan of the particles */ |
|---|
| 98 | inline float getLifeSpan() const { return this->lifeSpan; }; |
|---|
| 99 | /** @returns the starting-radius of the particles */ |
|---|
| 100 | inline float getStartRadius() { return this->radiusAnim.getValue(0.0); }; |
|---|
| 101 | /** @returns the end-radius of the particles */ |
|---|
| 102 | inline float getEndRadius() { return this->radiusAnim.getValue(1.0); }; |
|---|
| 103 | /** @returns the conserve-factor of the particles */ |
|---|
| 104 | inline float getConserve() const { return this->conserve; }; |
|---|
| 105 | /** @returns the initial mass of the particles */ |
|---|
| 106 | inline float getMass() const { return this->initialMass; }; |
|---|
| 107 | |
|---|
| 108 | virtual unsigned int getFaceCount() const; |
|---|
| 109 | |
|---|
| 110 | void addEmitter(ParticleEmitter* emitter); |
|---|
| 111 | void addEmitterXML(const TiXmlElement* emitterRoot); |
|---|
| 112 | void removeEmitter(ParticleEmitter* emitter); |
|---|
| 113 | |
|---|
| 114 | virtual void applyField(Field* field); |
|---|
| 115 | /** \brief this is an empty function, because the Physics are implemented in tick @param dt: useless here */ |
|---|
| 116 | virtual void tickPhys(float dt) {}; |
|---|
| 117 | |
|---|
| 118 | void addParticle(const Vector& position, const Vector& velocity, const Quaternion& orientation, const Quaternion& momentum, unsigned int data = 0); |
|---|
| 119 | |
|---|
| 120 | virtual void tick(float dt); |
|---|
| 121 | virtual void draw() const; |
|---|
| 122 | |
|---|
| 123 | void debug() const; |
|---|
| 124 | |
|---|
| 125 | private: |
|---|
| 126 | float conserve; //!< How much energy gets conserved to the next Tick. |
|---|
| 127 | float lifeSpan; //!< Initial lifetime of a Particle. |
|---|
| 128 | float randomLifeSpan; //!< A random value for the Lifespan (around the initial lifetime) |
|---|
| 129 | float initialMass; //!< The initial Mass of the Particle |
|---|
| 130 | float randomInitialMass; //!< The random initial Mass of the Particle |
|---|
| 131 | |
|---|
| 132 | int maxCount; //!< The maximum count of Particles. |
|---|
| 133 | int count; //!< The current count of Particles. |
|---|
| 134 | PARTICLE_TYPE particleType; //!< A type for all the Particles |
|---|
| 135 | Material* material; //!< A Material for all the Particles. |
|---|
| 136 | Particle* particles; //!< A list of particles of this System. |
|---|
| 137 | Particle* deadList; //!< A list of dead Particles in the System. |
|---|
| 138 | |
|---|
| 139 | GLuint* glID; //!< A List of different gl-List-ID's |
|---|
| 140 | GLuint dialectCount; //!< How many different types of particles are there in the Particle System |
|---|
| 141 | |
|---|
| 142 | std::list<ParticleEmitter*> emitters; //!< The Emitters that do emit into this System. |
|---|
| 143 | |
|---|
| 144 | // per particle attributes |
|---|
| 145 | QuickAnimation radiusAnim; //!< Animation of the radius |
|---|
| 146 | QuickAnimation randRadiusAnim; //!< Animation of the random Value of the radius |
|---|
| 147 | QuickAnimation massAnim; //!< Animation of the mass |
|---|
| 148 | QuickAnimation randMassAnim; //!< Animation of the random Mass |
|---|
| 149 | QuickAnimation colorAnim[4]; //!< Animation of the 4 colors (r,g,b,a) |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | #endif /* _PARTICLE_SYSTEM_H */ |
|---|