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