Pyrogenesis  trunk
ParticleEmitter.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_PARTICLEEMITTER
19 #define INCLUDED_PARTICLEEMITTER
20 
21 #include "graphics/ModelAbstract.h"
23 #include "graphics/Texture.h"
24 #include "maths/Quaternion.h"
25 #include "renderer/VertexArray.h"
26 
27 #include <map>
28 
29 /**
30  * Simulation state for a single particle.
31  */
32 struct SParticle
33 {
36  float angle;
37  float angleSpeed;
38  float size;
41  float age;
42  float maxAge;
43 };
44 
45 typedef shared_ptr<CParticleEmitter> CParticleEmitterPtr;
46 
47 /**
48  * Particle emitter.
49  *
50  * Emitters store particle data in two forms:
51  * * m_Particles contains the raw data used for the CPU particle simulation.
52  * * m_VertexArray contains the data required for rendering.
53  * Particles are rendered as billboard quads, so the vertex array contains four vertices
54  * per particle with different UV coordinates. The billboard position computation is
55  * performed by a vertex shader.
56  *
57  * The number of particles is a constant for the entire life of the emitter,
58  * to simplify the updating and rendering.
59  * m_Particles acts like a ring buffer, so we don't have to worry about dynamically
60  * allocating particles. If particles have variable lifetimes, they'll exist in the
61  * array with alpha=0 until they're overwritten by a new particle after the maximum
62  * lifetime.
63  *
64  * (It's quite likely this could be made more efficient, if the overhead of any added
65  * complexity is not high.)
66  */
68 {
69 public:
71 
72  /**
73  * Set the position to be used for emission of new particles.
74  */
75  void SetPosition(const CVector3D& pos)
76  {
77  m_Pos = pos;
78  }
79 
81  {
82  return m_Pos;
83  }
84 
85  /**
86  * Set the rotation to be used for emission of new particles (note: depends on particles).
87  */
88  void SetRotation(const CQuaternion& rot)
89  {
90  m_Rot = rot;
91  }
92 
94  {
95  return m_Rot;
96  }
97 
98  /**
99  * Get the bounding box of the center points of particles at their current positions.
100  */
101  CBoundingBoxAligned GetParticleBounds() { return m_ParticleBounds; }
102 
103  /**
104  * Push a new particle onto the ring buffer. (May overwrite an old particle.)
105  */
106  void AddParticle(const SParticle& particle);
107 
108  /**
109  * Update particle and vertex array data. Must be called before RenderArray.
110  *
111  * If frameNumber is the same as the previous call to UpdateArrayData,
112  * then the function will do no work and return immediately.
113  */
114  void UpdateArrayData(int frameNumber);
115 
116  /**
117  * Make the vertex data available for subsequent binding and rendering.
118  */
119  void PrepareForRendering();
120 
121  /**
122  * Bind rendering state (textures and blend modes).
123  */
124  void Bind(const CShaderProgramPtr& shader);
125 
126  /**
127  * Draw the vertex array.
128  */
129  void RenderArray(const CShaderProgramPtr& shader);
130 
131  /**
132  * Stop this emitter emitting new particles, and pass responsibility for rendering
133  * to the CParticleManager. This should be called before dropping the last shared_ptr
134  * to this object so that it will carry on rendering (until all particles have dissipated)
135  * even when it's no longer attached to a model.
136  * @param self the shared_ptr you're about to drop
137  */
138  void Unattach(const CParticleEmitterPtr& self);
139 
140  void SetEntityVariable(const std::string& name, float value);
141 
143 
144  /// Whether this emitter is still emitting new particles
145  bool m_Active;
146 
149 
150  std::map<std::string, float> m_EntityVariables;
151 
152  std::vector<SParticle> m_Particles;
154 
157 
158 private:
159  /// Bounding box of the current particle center points
161 
163 
169 
171 };
172 
173 /**
174  * Particle emitter model, for attaching emitters as props on other models.
175  */
177 {
178 public:
181 
182  /// Dynamic cast
184  {
185  return this;
186  }
187 
188  virtual CModelAbstract* Clone() const;
189 
190  virtual void SetDirtyRec(int dirtyflags)
191  {
192  SetDirty(dirtyflags);
193  }
194 
196  {
197  }
198 
199  virtual void SetEntityVariable(const std::string& name, float value);
200 
201  virtual void CalcBounds();
202  virtual void ValidatePosition();
203  virtual void InvalidatePosition();
204  virtual void SetTransform(const CMatrix3D& transform);
205 
208 };
209 
210 #endif // INCLUDED_PARTICLEEMITTER
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
float maxAge
Definition: ParticleEmitter.h:42
CParticleEmitterPtr m_Emitter
Definition: ParticleEmitter.h:207
bool m_Active
Whether this emitter is still emitting new particles.
Definition: ParticleEmitter.h:145
Particle emitter.
Definition: ParticleEmitter.h:67
shared_ptr< CParticleEmitterType > CParticleEmitterTypePtr
Definition: ParticleEmitterType.h:115
float size
Definition: ParticleEmitter.h:38
void SetPosition(const CVector3D &pos)
Set the position to be used for emission of new particles.
Definition: ParticleEmitter.h:75
Definition: Vector3D.h:28
std::vector< SParticle > m_Particles
Definition: ParticleEmitter.h:152
float sizeGrowthRate
Definition: ParticleEmitter.h:39
CQuaternion GetRotation() const
Definition: ParticleEmitter.h:93
shared_ptr< CParticleEmitter > CParticleEmitterPtr
Definition: ParticleEmitter.h:45
CVector3D pos
Definition: ParticleEmitter.h:34
A VertexArray that is specialised to handle 16-bit array indices.
Definition: VertexArray.h:214
Definition: Matrix3D.h:33
float m_EmissionRoundingError
Definition: ParticleEmitter.h:156
Definition: VertexArray.h:135
VertexArray m_VertexArray
Definition: ParticleEmitter.h:164
virtual CModelParticleEmitter * ToCModelParticleEmitter()
Dynamic cast.
Definition: ParticleEmitter.h:183
CVector3D velocity
Definition: ParticleEmitter.h:35
VertexArray::Attribute m_AttributeAxis
Definition: ParticleEmitter.h:166
Definition: SColor.h:30
VertexArray::Attribute m_AttributeUV
Definition: ParticleEmitter.h:167
VertexArray::Attribute m_AttributeColor
Definition: ParticleEmitter.h:168
Definition: VertexArray.h:132
CBoundingBoxAligned GetParticleBounds()
Get the bounding box of the center points of particles at their current positions.
Definition: ParticleEmitter.h:101
float angle
Definition: ParticleEmitter.h:36
intptr_t ssize_t
Definition: wposix_types.h:82
VertexIndexArray m_IndexArray
Definition: ParticleEmitter.h:162
void SetRotation(const CQuaternion &rot)
Set the rotation to be used for emission of new particles (note: depends on particles).
Definition: ParticleEmitter.h:88
Definition: Quaternion.h:24
Abstract base class for graphical objects that are used by units, or as props attached to other CMode...
Definition: ModelAbstract.h:36
Definition: BoundingBoxAligned.h:35
virtual void SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
Called when terrain has changed in the given inclusive bounds.
Definition: ParticleEmitter.h:195
virtual void SetDirtyRec(int dirtyflags)
Calls SetDirty on this model and all child objects.
Definition: ParticleEmitter.h:190
float m_LastUpdateTime
Definition: ParticleEmitter.h:155
CVector3D m_Pos
Definition: ParticleEmitter.h:147
CBoundingBoxAligned m_ParticleBounds
Bounding box of the current particle center points.
Definition: ParticleEmitter.h:160
Particle emitter model, for attaching emitters as props on other models.
Definition: ParticleEmitter.h:176
SColor4ub color
Definition: ParticleEmitter.h:40
int m_LastFrameNumber
Definition: ParticleEmitter.h:170
std::map< std::string, float > m_EntityVariables
Definition: ParticleEmitter.h:150
float age
Definition: ParticleEmitter.h:41
CVector3D GetPosition() const
Definition: ParticleEmitter.h:80
CQuaternion m_Rot
Definition: ParticleEmitter.h:148
VertexArray::Attribute m_AttributePos
Definition: ParticleEmitter.h:165
size_t m_NextParticleIdx
Definition: ParticleEmitter.h:153
Simulation state for a single particle.
Definition: ParticleEmitter.h:32
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:25
float angleSpeed
Definition: ParticleEmitter.h:37
CParticleEmitterTypePtr m_Type
Definition: ParticleEmitter.h:206
CParticleEmitterTypePtr m_Type
Definition: ParticleEmitter.h:142