| 1 | /*! |
|---|
| 2 | \file particle_system.h |
|---|
| 3 | |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _PARTICLE_SYSTEM_H |
|---|
| 7 | #define _PARTICLE_SYSTEM_H |
|---|
| 8 | |
|---|
| 9 | #include "base_object.h" |
|---|
| 10 | #include "vector.h" |
|---|
| 11 | |
|---|
| 12 | #define PARTICLE_DOT_MASK 0x000001 |
|---|
| 13 | #define PARTICLE_SPARK_MASK 0x000010 |
|---|
| 14 | #define PARTICLE_SPRITE_MASK 0x000100 |
|---|
| 15 | #define PARTICLE_MODEL_MASK 0x001000 |
|---|
| 16 | #define PARTICLE_WORDL_ENTITY_MASK 0x010000 |
|---|
| 17 | #define PARTICLE_MULTI_MASK 0x100000 |
|---|
| 18 | |
|---|
| 19 | //! An enumerator for the different types of particles. |
|---|
| 20 | typedef enum PARTICLE_TYPE {PARTICLE_DOT = PARTICLE_DOT_MASK, |
|---|
| 21 | PARTICLE_SPARK = PARTICLE_SPARK_MASK, |
|---|
| 22 | PARTICLE_SPRITE = PARTICLE_SPRITE_MASK, |
|---|
| 23 | PARTICLE_MULTI_SPRITE = PARTICLE_SPRITE_MASK | PARTICLE_MULTI_MASK, |
|---|
| 24 | PARTICLE_MODEL = PARTICLE_MODEL_MASK, |
|---|
| 25 | PARTICLE_MULTI_MODE = PARTICLE_MODEL_MASK | PARTICLE_MULTI_MASK}; |
|---|
| 26 | |
|---|
| 27 | #define PARTICLE_DEFAULT_MAX_COUNT 200 //!< a default count of particles in the system. |
|---|
| 28 | #define PARTICLE_DEFAULT_TYPE PARTICLE_SPRITE //!< A default type of the system. |
|---|
| 29 | |
|---|
| 30 | // FORWARD DEFINITION |
|---|
| 31 | class Material; |
|---|
| 32 | class ParticleEmitter; |
|---|
| 33 | class Field; |
|---|
| 34 | |
|---|
| 35 | //! A struct for one Particle |
|---|
| 36 | typedef struct Particle |
|---|
| 37 | { |
|---|
| 38 | float lifeTime; //!< The time this particle has to live. |
|---|
| 39 | float lifeCycle; //!< The fraction of time passed. (in percentage of its lifeTime) |
|---|
| 40 | |
|---|
| 41 | Vector position; //!< The current position of this particle. |
|---|
| 42 | Vector velocity; //!< The current velocity of this particle. |
|---|
| 43 | Vector extForce; //!< The external Force that influences this Particle. |
|---|
| 44 | Quaternion rotation; //!< The current rotation of this particle. |
|---|
| 45 | float mass; //!< The mass of this particle. |
|---|
| 46 | float radius; //!< The current size of this particle. |
|---|
| 47 | float radiusIt; //!< The difference of the Size per second. |
|---|
| 48 | |
|---|
| 49 | GLfloat color [4]; //!< A Color for the particles. |
|---|
| 50 | |
|---|
| 51 | PARTICLE_TYPE type; |
|---|
| 52 | |
|---|
| 53 | Particle* next; //!< pointer to the next particle in the List. (NULL if no preceding one) |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | //! A class to handle particle Systems |
|---|
| 57 | class ParticleSystem : public BaseObject { |
|---|
| 58 | friend class ParticleEmitter; |
|---|
| 59 | |
|---|
| 60 | public: |
|---|
| 61 | ParticleSystem(unsigned int maxCount = PARTICLE_DEFAULT_MAX_COUNT, |
|---|
| 62 | PARTICLE_TYPE type = PARTICLE_DEFAULT_TYPE); |
|---|
| 63 | virtual ~ParticleSystem(); |
|---|
| 64 | void setName(const char* name); |
|---|
| 65 | const char* getName(void) const; |
|---|
| 66 | |
|---|
| 67 | void setType(PARTICLE_TYPE particleType, int count = 0); |
|---|
| 68 | void setMaterial(Material* material); |
|---|
| 69 | void setInheritSpeed(float value); |
|---|
| 70 | void setLifeSpan(float lifeSpan, float randomLifeSpan = 0.0); |
|---|
| 71 | void setRadius(float startRadius, float endRadius, |
|---|
| 72 | float randomStartRadius = 0.0, float randomEndRadius = 0.0); |
|---|
| 73 | void setConserve(float conserve); |
|---|
| 74 | void setMass(float mass, float randomMass); |
|---|
| 75 | |
|---|
| 76 | void setColor(GLfloat br, GLfloat bg, GLfloat bb, GLfloat ba, |
|---|
| 77 | GLfloat mr, GLfloat mg, GLfloat mb, GLfloat ma, |
|---|
| 78 | GLfloat er, GLfloat eg, GLfloat eb, GLfloat ea); |
|---|
| 79 | |
|---|
| 80 | /** \returns the Type of the particles */ |
|---|
| 81 | inline PARTICLE_TYPE getType(void) const { return this->particleType; }; |
|---|
| 82 | /** \returns the Material that lies on this particles */ |
|---|
| 83 | inline const Material* getMaterial(void) const { return this->material; }; |
|---|
| 84 | /** \returns the inherit-speed-factor */ |
|---|
| 85 | inline float getInheritSpeed(void) const { return this->inheritSpeed; }; |
|---|
| 86 | /** \returns the lifespan of the particles */ |
|---|
| 87 | inline float getLifeSpan(void) const { return this->lifeSpan; }; |
|---|
| 88 | /** \returns the starting-radius of the particles */ |
|---|
| 89 | inline float getStartRadius(void) const { return this->startRadius; }; |
|---|
| 90 | /** \returns the end-radius of the particles */ |
|---|
| 91 | inline float getEndRadius(void) const { return this->endRadius; }; |
|---|
| 92 | /** \returns the conserve-factor of the particles */ |
|---|
| 93 | inline float getConserve(void) const { return this->conserve; }; |
|---|
| 94 | /** \returns the initial mass of the particles */ |
|---|
| 95 | inline float getMass(void) const { return this->initialMass; }; |
|---|
| 96 | |
|---|
| 97 | void applyField(float dt, Field* field); |
|---|
| 98 | |
|---|
| 99 | void tick(float dt); |
|---|
| 100 | void draw(void) const; |
|---|
| 101 | |
|---|
| 102 | void debug(void); |
|---|
| 103 | |
|---|
| 104 | private: |
|---|
| 105 | char* name; // the Name of the Particle System |
|---|
| 106 | |
|---|
| 107 | float conserve; //!< How much energy gets conserved to the next Tick. |
|---|
| 108 | float lifeSpan; //!< Initial lifetime of a Particle. |
|---|
| 109 | float randomLifeSpan; //!< A random value for the Lifespan (around the initial lifetime) |
|---|
| 110 | float startRadius; //!< The beginning Radius of the Particle |
|---|
| 111 | float endRadius; //!< The end Radius of the Particle |
|---|
| 112 | float randomStartRadius; //!< The Random start Radius (begin + rand*randomValue) |
|---|
| 113 | float randomEndRadius; //!< Random end value |
|---|
| 114 | float initialMass; //!< The initial Mass of the Particle |
|---|
| 115 | float randomInitialMass; //!< The random initial Mass of the Particle |
|---|
| 116 | float inheritSpeed; //!< How much speed the particle inherits from the Emitters speed \todo move this to the emitter |
|---|
| 117 | |
|---|
| 118 | GLfloat startColor[4]; //!< Color of the Particle at the beginning |
|---|
| 119 | GLfloat midColor[4]; //!< Color of the Particle at the middle of its lifeSpan |
|---|
| 120 | GLfloat endColor[4]; //!< Color of the Particle at the end of its lifeSpan |
|---|
| 121 | |
|---|
| 122 | // particles |
|---|
| 123 | int maxCount; //!< The maximum count of Particles. |
|---|
| 124 | int count; //!< The current count of Particles. |
|---|
| 125 | PARTICLE_TYPE particleType;//!< A type for all the Particles |
|---|
| 126 | Material* material; //!< A Material for all the Particles. |
|---|
| 127 | Particle* particles; //!< A list of particles of this System. |
|---|
| 128 | Particle* deadList; //!< A list of dead Particles in the System. |
|---|
| 129 | |
|---|
| 130 | GLuint* glID; //!< A List of different gl-List-ID's |
|---|
| 131 | GLuint dialectCount; //!< How many different types of particles are there in the Particle System |
|---|
| 132 | |
|---|
| 133 | void addParticle(const Vector& position, const Vector& velocity, unsigned int data = 0); |
|---|
| 134 | |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | #endif /* _PARTICLE_SYSTEM_H */ |
|---|