ParticleProcessor 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. //osgParticle - Copyright (C) 2002 Marco Jez
  14. #ifndef OSGPARTICLE_PARTICLEPROCESSOR
  15. #define OSGPARTICLE_PARTICLEPROCESSOR 1
  16. #include <osgParticle/Export>
  17. #include <osgParticle/ParticleSystem>
  18. #include <osg/ref_ptr>
  19. #include <osg/Object>
  20. #include <osg/Transform>
  21. #include <osg/NodeVisitor>
  22. #include <osg/CopyOp>
  23. #include <osg/Vec3>
  24. #include <osg/Matrix>
  25. namespace osgParticle
  26. {
  27. /** A common base interface for those classes which need to do something on particles. Such classes
  28. * are, for example, Emitter (particle generation) and Program (particle animation).
  29. * This class holds some properties, like a <I>reference frame</I> and a reference to a ParticleSystem;
  30. * descendant classes should process the particles taking into account the reference frame, computing the right
  31. * transformations when needed.
  32. */
  33. class OSGPARTICLE_EXPORT ParticleProcessor: public osg::Node {
  34. public:
  35. enum ReferenceFrame {
  36. RELATIVE_RF,
  37. ABSOLUTE_RF
  38. };
  39. ParticleProcessor();
  40. ParticleProcessor(const ParticleProcessor& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  41. virtual const char* libraryName() const { return "osgParticle"; }
  42. virtual const char* className() const { return "ParticleProcessor"; }
  43. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ParticleProcessor*>(obj) != 0; }
  44. virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }
  45. /// Get the reference frame.
  46. inline ReferenceFrame getReferenceFrame() const;
  47. /// Set the reference frame.
  48. inline void setReferenceFrame(ReferenceFrame rf);
  49. /// Get whether this processor is enabled or not.
  50. bool getEnabled() const { return _enabled; }
  51. inline bool isEnabled() const;
  52. /// Set whether this processor is enabled or not.
  53. inline void setEnabled(bool v);
  54. /// Get a pointer to the destination particle system.
  55. inline ParticleSystem* getParticleSystem();
  56. /// Get a const pointer to the destination particle system.
  57. inline const ParticleSystem* getParticleSystem() const;
  58. /// Set the destination particle system.
  59. virtual void setParticleSystem(ParticleSystem* ps);
  60. /// Set the endless flag of this processor.
  61. inline void setEndless(bool type);
  62. /// Check whether this processor is endless.
  63. bool getEndless() const { return _endless; }
  64. inline bool isEndless() const;
  65. /// Set the lifetime of this processor.
  66. inline void setLifeTime(double t);
  67. /// Get the lifetime of this processor.
  68. inline double getLifeTime() const;
  69. /// Set the start time of this processor.
  70. inline void setStartTime(double t);
  71. /// Get the start time of this processor.
  72. inline double getStartTime() const;
  73. /// Set the current time of this processor.
  74. inline void setCurrentTime(double t);
  75. /// Get the current time of this processor.
  76. inline double getCurrentTime() const;
  77. /// Set the reset time of this processor. A value of 0 disables reset.
  78. inline void setResetTime(double t);
  79. /// Get the reset time of this processor.
  80. inline double getResetTime() const;
  81. /**
  82. Check whether the processor is alive with respect to start time and
  83. life duration. Note that this method may return true even if the
  84. processor has been disabled by calling setEnabled(false). To test
  85. whether the processor is actually processing particles or not, you
  86. should evaluate (isEnabled() && isAlive()).
  87. */
  88. inline bool isAlive() const;
  89. void traverse(osg::NodeVisitor& nv);
  90. /// Get the current local-to-world transformation matrix (valid only during cull traversal).
  91. inline const osg::Matrix& getLocalToWorldMatrix();
  92. /// Get the current world-to-local transformation matrix (valid only during cull traversal).
  93. inline const osg::Matrix& getWorldToLocalMatrix();
  94. /// Get the previous local-to-world transformation matrix (valid only during cull traversal).
  95. inline const osg::Matrix& getPreviousLocalToWorldMatrix();
  96. /// Get the previous world-to-local transformation matrix (valid only during cull traversal).
  97. inline const osg::Matrix& getPreviousWorldToLocalMatrix();
  98. /// Transform a point from local to world coordinates (valid only during cull traversal).
  99. inline osg::Vec3 transformLocalToWorld(const osg::Vec3& P);
  100. /// Transform a vector from local to world coordinates, discarding translation (valid only during cull traversal).
  101. inline osg::Vec3 rotateLocalToWorld(const osg::Vec3& P);
  102. /// Transform a point from world to local coordinates (valid only during cull traversal).
  103. inline osg::Vec3 transformWorldToLocal(const osg::Vec3& P);
  104. /// Transform a vector from world to local coordinates, discarding translation (valid only during cull traversal).
  105. inline osg::Vec3 rotateWorldToLocal(const osg::Vec3& P);
  106. virtual osg::BoundingSphere computeBound() const;
  107. protected:
  108. virtual ~ParticleProcessor() {}
  109. ParticleProcessor& operator=(const ParticleProcessor&) { return *this; }
  110. virtual void process(double dt) = 0;
  111. ReferenceFrame _rf;
  112. bool _enabled;
  113. double _t0;
  114. osg::ref_ptr<ParticleSystem> _ps;
  115. bool _first_ltw_compute;
  116. bool _need_ltw_matrix;
  117. bool _first_wtl_compute;
  118. bool _need_wtl_matrix;
  119. osg::Matrix _ltw_matrix;
  120. osg::Matrix _wtl_matrix;
  121. osg::Matrix _previous_ltw_matrix;
  122. osg::Matrix _previous_wtl_matrix;
  123. osg::NodeVisitor* _current_nodevisitor;
  124. bool _endless;
  125. double _lifeTime;
  126. double _startTime;
  127. double _currentTime;
  128. double _resetTime;
  129. //added- 1/17/06- bgandere@nps.edu
  130. //a var to keep from doing multiple updates
  131. unsigned int _frameNumber;
  132. };
  133. // INLINE FUNCTIONS
  134. inline ParticleProcessor::ReferenceFrame ParticleProcessor::getReferenceFrame() const
  135. {
  136. return _rf;
  137. }
  138. inline void ParticleProcessor::setReferenceFrame(ReferenceFrame rf)
  139. {
  140. _rf = rf;
  141. }
  142. inline bool ParticleProcessor::isEnabled() const
  143. {
  144. return _enabled;
  145. }
  146. inline void ParticleProcessor::setEnabled(bool v)
  147. {
  148. _enabled = v;
  149. if (_enabled)
  150. {
  151. _currentTime = 0;
  152. }
  153. }
  154. inline ParticleSystem* ParticleProcessor::getParticleSystem()
  155. {
  156. return _ps.get();
  157. }
  158. inline const ParticleSystem* ParticleProcessor::getParticleSystem() const
  159. {
  160. return _ps.get();
  161. }
  162. inline void ParticleProcessor::setEndless(bool type)
  163. {
  164. _endless = type;
  165. }
  166. inline bool ParticleProcessor::isEndless() const
  167. {
  168. return _endless;
  169. }
  170. inline void ParticleProcessor::setLifeTime(double t)
  171. {
  172. _lifeTime = t;
  173. }
  174. inline double ParticleProcessor::getLifeTime() const
  175. {
  176. return _lifeTime;
  177. }
  178. inline void ParticleProcessor::setStartTime(double t)
  179. {
  180. _startTime = t;
  181. }
  182. inline double ParticleProcessor::getStartTime() const
  183. {
  184. return _startTime;
  185. }
  186. inline void ParticleProcessor::setCurrentTime(double t)
  187. {
  188. _currentTime = t;
  189. }
  190. inline double ParticleProcessor::getCurrentTime() const
  191. {
  192. return _currentTime;
  193. }
  194. inline void ParticleProcessor::setResetTime(double t)
  195. {
  196. _resetTime = t;
  197. }
  198. inline double ParticleProcessor::getResetTime() const
  199. {
  200. return _resetTime;
  201. }
  202. inline const osg::Matrix& ParticleProcessor::getLocalToWorldMatrix()
  203. {
  204. if (_need_ltw_matrix) {
  205. _previous_ltw_matrix = _ltw_matrix;
  206. _ltw_matrix = osg::computeLocalToWorld(_current_nodevisitor->getNodePath());
  207. if (_first_ltw_compute)
  208. {
  209. _previous_ltw_matrix = _ltw_matrix;
  210. _first_ltw_compute = false;
  211. }
  212. _need_ltw_matrix = false;
  213. }
  214. return _ltw_matrix;
  215. }
  216. inline const osg::Matrix& ParticleProcessor::getWorldToLocalMatrix()
  217. {
  218. if (_need_wtl_matrix) {
  219. _previous_wtl_matrix = _wtl_matrix;
  220. _wtl_matrix = osg::computeWorldToLocal(_current_nodevisitor->getNodePath());
  221. if (_first_wtl_compute)
  222. {
  223. _previous_wtl_matrix = _wtl_matrix;
  224. _first_wtl_compute = false;
  225. }
  226. _need_wtl_matrix = false;
  227. }
  228. return _wtl_matrix;
  229. }
  230. inline const osg::Matrix& ParticleProcessor::getPreviousLocalToWorldMatrix()
  231. {
  232. if (_need_ltw_matrix) getLocalToWorldMatrix();
  233. return _previous_ltw_matrix;
  234. }
  235. inline const osg::Matrix& ParticleProcessor::getPreviousWorldToLocalMatrix()
  236. {
  237. if (_need_wtl_matrix) getWorldToLocalMatrix();
  238. return _previous_wtl_matrix;
  239. }
  240. inline osg::Vec3 ParticleProcessor::transformLocalToWorld(const osg::Vec3& P)
  241. {
  242. return getLocalToWorldMatrix().preMult(P);
  243. }
  244. inline osg::Vec3 ParticleProcessor::transformWorldToLocal(const osg::Vec3& P)
  245. {
  246. return getWorldToLocalMatrix().preMult(P);
  247. }
  248. inline osg::Vec3 ParticleProcessor::rotateLocalToWorld(const osg::Vec3& P)
  249. {
  250. return getLocalToWorldMatrix().preMult(P) -
  251. getLocalToWorldMatrix().preMult(osg::Vec3(0, 0, 0));
  252. }
  253. inline osg::Vec3 ParticleProcessor::rotateWorldToLocal(const osg::Vec3& P)
  254. {
  255. return getWorldToLocalMatrix().preMult(P) -
  256. getWorldToLocalMatrix().preMult(osg::Vec3(0, 0, 0));
  257. }
  258. inline bool ParticleProcessor::isAlive() const
  259. {
  260. return _currentTime < (_lifeTime + _startTime);
  261. }
  262. }
  263. #endif