OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
particle.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_COMPONENTS_NIFOSG_PARTICLE_H
2 #define OPENMW_COMPONENTS_NIFOSG_PARTICLE_H
3 
4 #include <osgParticle/Particle>
5 #include <osgParticle/Shooter>
6 #include <osgParticle/Operator>
7 #include <osgParticle/Emitter>
8 #include <osgParticle/Placer>
9 #include <osgParticle/Counter>
10 
11 #include <osg/NodeCallback>
12 
13 #include "controller.hpp" // ValueInterpolator
14 
15 namespace Nif
16 {
17  class NiGravity;
18  class NiPlanarCollider;
19  class NiSphericalCollider;
20  class NiColorData;
21 }
22 
23 namespace NifOsg
24 {
25 
26  // Subclass ParticleSystem to support a limit on the number of active particles.
27  class ParticleSystem : public osgParticle::ParticleSystem
28  {
29  public:
31  ParticleSystem(const ParticleSystem& copy, const osg::CopyOp& copyop);
32 
33  META_Object(NifOsg, ParticleSystem)
34 
35  virtual osgParticle::Particle* createParticle(const osgParticle::Particle *ptemplate);
36 
37  void setQuota(int quota);
38 
39  private:
40  int mQuota;
41  };
42 
43  // HACK: Particle doesn't allow setting the initial age, but we need this for loading the particle system state
44  class ParticleAgeSetter : public osgParticle::Particle
45  {
46  public:
47  ParticleAgeSetter(float age)
48  : Particle()
49  {
50  _t0 = age;
51  }
52  };
53 
54  // Node callback used to set the inverse of the parent's world matrix on the MatrixTransform
55  // that the callback is attached to. Used for certain particle systems,
56  // so that the particles do not move with the node they are attached to.
57  class InverseWorldMatrix : public osg::NodeCallback
58  {
59  public:
61  {
62  }
63  InverseWorldMatrix(const InverseWorldMatrix& copy, const osg::CopyOp& op)
64  : osg::Object(), osg::NodeCallback()
65  {
66  }
67 
68  META_Object(NifOsg, InverseWorldMatrix)
69 
70  void operator()(osg::Node* node, osg::NodeVisitor* nv);
71  };
72 
73  class ParticleShooter : public osgParticle::Shooter
74  {
75  public:
76  ParticleShooter(float minSpeed, float maxSpeed, float horizontalDir, float horizontalAngle, float verticalDir, float verticalAngle,
77  float lifetime, float lifetimeRandom);
79  ParticleShooter(const ParticleShooter& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
80 
81  META_Object(NifOsg, ParticleShooter)
82 
83  virtual void shoot(osgParticle::Particle* particle) const;
84 
85  private:
86  float mMinSpeed;
87  float mMaxSpeed;
88  float mHorizontalDir;
89  float mHorizontalAngle;
90  float mVerticalDir;
91  float mVerticalAngle;
92  float mLifetime;
93  float mLifetimeRandom;
94  };
95 
96  class PlanarCollider : public osgParticle::Operator
97  {
98  public:
99  PlanarCollider(const Nif::NiPlanarCollider* collider);
100  PlanarCollider();
101  PlanarCollider(const PlanarCollider& copy, const osg::CopyOp& copyop);
102 
103  META_Object(NifOsg, PlanarCollider)
104 
105  virtual void beginOperate(osgParticle::Program* program);
106  virtual void operate(osgParticle::Particle* particle, double dt);
107 
108  private:
110  osg::Plane mPlane;
112  };
113 
114  class SphericalCollider : public osgParticle::Operator
115  {
116  public:
119  SphericalCollider(const SphericalCollider& copy, const osg::CopyOp& copyop);
120 
121  META_Object(NifOsg, SphericalCollider)
122 
123  virtual void beginOperate(osgParticle::Program* program);
124  virtual void operate(osgParticle::Particle* particle, double dt);
125  private:
127  osg::BoundingSphere mSphere;
128  osg::BoundingSphere mSphereInParticleSpace;
129  };
130 
131  class GrowFadeAffector : public osgParticle::Operator
132  {
133  public:
134  GrowFadeAffector(float growTime, float fadeTime);
136  GrowFadeAffector(const GrowFadeAffector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
137 
138  META_Object(NifOsg, GrowFadeAffector)
139 
140  virtual void beginOperate(osgParticle::Program* program);
141  virtual void operate(osgParticle::Particle* particle, double dt);
142 
143  private:
144  float mGrowTime;
145  float mFadeTime;
146 
148  };
149 
151  class ParticleColorAffector : public osgParticle::Operator
152  {
153  public:
154  ParticleColorAffector(const Nif::NiColorData* clrdata);
156  ParticleColorAffector(const ParticleColorAffector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
157 
158  META_Object(NifOsg, ParticleColorAffector)
159 
160  virtual void operate(osgParticle::Particle* particle, double dt);
161 
162  private:
163  Vec4Interpolator mData;
164  };
165 
166  class GravityAffector : public osgParticle::Operator
167  {
168  public:
169  GravityAffector(const Nif::NiGravity* gravity);
170  GravityAffector();
171  GravityAffector(const GravityAffector& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
172 
173  META_Object(NifOsg, GravityAffector)
174 
175  virtual void operate(osgParticle::Particle* particle, double dt);
176  virtual void beginOperate(osgParticle::Program *);
177 
178  private:
179  float mForce;
180  enum ForceType {
182  Type_Point
183  };
185  osg::Vec3f mPosition;
186  osg::Vec3f mDirection;
187  float mDecay;
190  };
191 
192  // NodeVisitor to find a Group node with the given record index, stored in the node's user data container.
193  // Alternatively, returns the node's parent Group if that node is not a Group (i.e. a leaf node).
194  class FindGroupByRecIndex : public osg::NodeVisitor
195  {
196  public:
197  FindGroupByRecIndex(int recIndex);
198 
199  virtual void apply(osg::Node &searchNode);
200 
201  osg::Group* mFound;
202  osg::NodePath mFoundPath;
203  private:
205  };
206 
207  // Subclass emitter to support randomly choosing one of the child node's transforms for the emit position of new particles.
208  class Emitter : public osgParticle::Emitter
209  {
210  public:
211  Emitter(const std::vector<int>& targets);
212  Emitter();
213  Emitter(const Emitter& copy, const osg::CopyOp& copyop);
214 
215  META_Object(NifOsg, Emitter)
216 
217  virtual void emitParticles(double dt);
218 
219  void setShooter(osgParticle::Shooter* shooter);
220  void setPlacer(osgParticle::Placer* placer);
221  void setCounter(osgParticle::Counter* counter);
222 
223  private:
224  // NIF Record indices
225  std::vector<int> mTargets;
226 
227  osg::ref_ptr<osgParticle::Placer> mPlacer;
228  osg::ref_ptr<osgParticle::Shooter> mShooter;
229  osg::ref_ptr<osgParticle::Counter> mCounter;
230  };
231 
232 }
233 
234 #endif
osg::Vec3f mPosition
Definition: particle.hpp:185
std::vector< int > mTargets
Definition: particle.hpp:225
int mRecIndex
Definition: particle.hpp:204
osg::BoundingSphere mSphere
Definition: particle.hpp:127
Definition: particle.hpp:57
ForceType
Definition: particle.hpp:180
float mDecay
Definition: particle.hpp:187
osg::ref_ptr< osgParticle::Counter > mCounter
Definition: particle.hpp:229
Definition: particle.hpp:181
osg::Plane mPlaneInParticleSpace
Definition: particle.hpp:111
Definition: particle.hpp:194
Definition: particle.hpp:73
float mGrowTime
Definition: particle.hpp:144
float mCachedDefaultSize
Definition: particle.hpp:147
Definition: controlled.hpp:103
InverseWorldMatrix(const InverseWorldMatrix &copy, const osg::CopyOp &op)
Definition: particle.hpp:63
osg::Vec3f mDirection
Definition: particle.hpp:186
Definition: particle.hpp:114
osg::NodePath mFoundPath
Definition: particle.hpp:202
Definition: particle.hpp:151
int mQuota
Definition: particle.hpp:40
float mFadeTime
Definition: particle.hpp:145
ForceType mType
Definition: particle.hpp:184
osg::BoundingSphere mSphereInParticleSpace
Definition: particle.hpp:128
Definition: particle.hpp:44
float mBounceFactor
Definition: particle.hpp:126
osg::Plane mPlane
Definition: particle.hpp:110
osg::ref_ptr< osgParticle::Shooter > mShooter
Definition: particle.hpp:228
ParticleSystem()
Definition: particle.cpp:16
osg::Vec3f mCachedWorldDirection
Definition: particle.hpp:189
float mBounceFactor
Definition: particle.hpp:109
Definition: particle.hpp:96
ParticleAgeSetter(float age)
Definition: particle.hpp:47
META_Object(NifOsg, ParticleSystem) virtual osgParticle void setQuota(int quota)
Definition: particle.cpp:31
Definition: data.hpp:135
InverseWorldMatrix()
Definition: particle.hpp:60
osg::Vec3f mCachedWorldPosition
Definition: particle.hpp:188
float mForce
Definition: particle.hpp:179
Definition: controlled.hpp:114
osg::Group * mFound
Definition: particle.hpp:201
Definition: particle.hpp:166
Definition: particle.hpp:27
Definition: controlled.hpp:87
Definition: particle.hpp:131
ValueInterpolator< Nif::Vector4KeyMap, LerpFunc > Vec4Interpolator
Definition: particle.hpp:150
Definition: particle.hpp:208
osg::ref_ptr< osgParticle::Placer > mPlacer
Definition: particle.hpp:227