Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "renderer/VertexArray.h"
25 
26 #include <map>
27 
28 /**
29  * Simulation state for a single particle.
30  */
31 struct SParticle
32 {
35  float angle;
36  float angleSpeed;
37  float size;
39  float age;
40  float maxAge;
41 };
42 
43 typedef shared_ptr<CParticleEmitter> CParticleEmitterPtr;
44 
45 /**
46  * Particle emitter.
47  *
48  * Emitters store particle data in two forms:
49  * * m_Particles contains the raw data used for the CPU particle simulation.
50  * * m_VertexArray contains the data required for rendering.
51  * Particles are rendered as billboard quads, so the vertex array contains four vertices
52  * per particle with different UV coordinates. The billboard position computation is
53  * performed by a vertex shader.
54  *
55  * The number of particles is a constant for the entire life of the emitter,
56  * to simplify the updating and rendering.
57  * m_Particles acts like a ring buffer, so we don't have to worry about dynamically
58  * allocating particles. If particles have variable lifetimes, they'll exist in the
59  * array with alpha=0 until they're overwritten by a new particle after the maximum
60  * lifetime.
61  *
62  * (It's quite likely this could be made more efficient, if the overhead of any added
63  * complexity is not high.)
64  */
66 {
67 public:
69 
70  /**
71  * Set the position to be used for emission of new particles.
72  */
73  void SetPosition(const CVector3D& pos)
74  {
75  m_Pos = pos;
76  }
77 
79  {
80  return m_Pos;
81  }
82 
83  /**
84  * Get the bounding box of the center points of particles at their current positions.
85  */
87 
88  /**
89  * Push a new particle onto the ring buffer. (May overwrite an old particle.)
90  */
91  void AddParticle(const SParticle& particle);
92 
93  /**
94  * Update particle and vertex array data. Must be called before RenderArray.
95  */
96  void UpdateArrayData();
97 
98  /**
99  * Bind rendering state (textures and blend modes).
100  */
101  void Bind(const CShaderProgramPtr& shader);
102 
103  /**
104  * Draw the vertex array.
105  */
106  void RenderArray(const CShaderProgramPtr& shader);
107 
108  /**
109  * Stop this emitter emitting new particles, and pass responsibility for rendering
110  * to the CParticleManager. This should be called before dropping the last shared_ptr
111  * to this object so that it will carry on rendering (until all particles have dissipated)
112  * even when it's no longer attached to a model.
113  * @param self the shared_ptr you're about to drop
114  */
115  void Unattach(const CParticleEmitterPtr& self);
116 
117  void SetEntityVariable(const std::string& name, float value);
118 
120 
121  /// Whether this emitter is still emitting new particles
122  bool m_Active;
123 
125 
126  std::map<std::string, float> m_EntityVariables;
127 
128  std::vector<SParticle> m_Particles;
130 
133 
134 private:
135  /// Bounding box of the current particle center points
137 
139 
145 };
146 
147 /**
148  * Particle emitter model, for attaching emitters as props on other models.
149  */
151 {
152 public:
155 
156  /// Dynamic cast
158  {
159  return this;
160  }
161 
162  virtual CModelAbstract* Clone() const;
163 
164  virtual void SetDirtyRec(int dirtyflags)
165  {
166  SetDirty(dirtyflags);
167  }
168 
170  {
171  }
172 
173  virtual void SetEntityVariable(const std::string& name, float value);
174 
175  virtual void CalcBounds();
176  virtual void ValidatePosition();
177  virtual void InvalidatePosition();
178  virtual void SetTransform(const CMatrix3D& transform);
179 
182 };
183 
184 #endif // INCLUDED_PARTICLEEMITTER
void Unattach(const CParticleEmitterPtr &self)
Stop this emitter emitting new particles, and pass responsibility for rendering to the CParticleManag...
virtual void SetTransform(const CMatrix3D &transform)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
CParticleEmitterPtr m_Emitter
bool m_Active
Whether this emitter is still emitting new particles.
Particle emitter.
void UpdateArrayData()
Update particle and vertex array data.
shared_ptr< CParticleEmitterType > CParticleEmitterTypePtr
CModelParticleEmitter(const CParticleEmitterTypePtr &type)
void SetPosition(const CVector3D &pos)
Set the position to be used for emission of new particles.
void AddParticle(const SParticle &particle)
Push a new particle onto the ring buffer.
virtual CModelAbstract * Clone() const
std::vector< SParticle > m_Particles
void RenderArray(const CShaderProgramPtr &shader)
Draw the vertex array.
shared_ptr< CParticleEmitter > CParticleEmitterPtr
CVector3D pos
A VertexArray that is specialised to handle 16-bit array indices.
Definition: VertexArray.h:212
virtual void InvalidatePosition()
Mark this model&#39;s position and bone matrices, and all props&#39; positions as invalid.
void Bind(const CShaderProgramPtr &shader)
Bind rendering state (textures and blend modes).
VertexArray m_VertexArray
virtual CModelParticleEmitter * ToCModelParticleEmitter()
Dynamic cast.
CVector3D velocity
VertexArray::Attribute m_AttributeAxis
VertexArray::Attribute m_AttributeUV
VertexArray::Attribute m_AttributeColor
void SetDirty(u32 dirtyflags)
CBoundingBoxAligned GetParticleBounds()
Get the bounding box of the center points of particles at their current positions.
virtual void SetEntityVariable(const std::string &name, float value)
Called when the entity tries to set some variable to affect the display of this model and/or its chil...
intptr_t ssize_t
Definition: wposix_types.h:82
VertexIndexArray m_IndexArray
virtual void ValidatePosition()
Ensure that both the transformation and the bone matrices are correct for this model and all its prop...
Abstract base class for graphical objects that are used by units, or as props attached to other CMode...
Definition: ModelAbstract.h:36
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.
virtual void SetDirtyRec(int dirtyflags)
Calls SetDirty on this model and all child objects.
CBoundingBoxAligned m_ParticleBounds
Bounding box of the current particle center points.
Particle emitter model, for attaching emitters as props on other models.
SColor4ub color
std::map< std::string, float > m_EntityVariables
virtual void CalcBounds()
(Re)calculates and stores any bounds or bound-dependent data for this object.
CVector3D GetPosition() const
shared_ptr< CShaderProgram > CShaderProgramPtr
VertexArray::Attribute m_AttributePos
Simulation state for a single particle.
CParticleEmitter(const CParticleEmitterTypePtr &type)
float angleSpeed
CParticleEmitterTypePtr m_Type
CParticleEmitterTypePtr m_Type
void SetEntityVariable(const std::string &name, float value)