Operator 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_OPERATOR
  15. #define OSGPARTICLE_OPERATOR 1
  16. #include <osgParticle/Program>
  17. #include <osg/CopyOp>
  18. #include <osg/Object>
  19. #include <osg/Matrix>
  20. namespace osgParticle
  21. {
  22. // forward declaration to avoid including the whole header file
  23. class Particle;
  24. /** An abstract base class used by <CODE>ModularProgram</CODE> to perform operations on particles before they are updated.
  25. To implement a new operator, derive from this class and override the <CODE>operate()</CODE> method.
  26. You should also override the <CODE>beginOperate()</CODE> method to query the calling program for the reference frame
  27. used, and initialize the right transformations if needed.
  28. */
  29. class Operator: public osg::Object {
  30. public:
  31. inline Operator();
  32. inline Operator(const Operator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  33. virtual const char* libraryName() const { return "osgParticle"; }
  34. virtual const char* className() const { return "Operator"; }
  35. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Operator* >(obj) != 0; }
  36. /// Get whether this operator is enabled.
  37. inline bool isEnabled() const;
  38. /// Enable or disable this operator.
  39. inline void setEnabled(bool v);
  40. /** Do something on all emitted particles.
  41. This method is called by <CODE>ModularProgram</CODE> objects to perform some operations
  42. on the particles. By default, it will call the <CODE>operate()</CODE> method for each particle.
  43. You must override it in descendant classes.
  44. */
  45. virtual void operateParticles(ParticleSystem* ps, double dt)
  46. {
  47. int n = ps->numParticles();
  48. for (int i=0; i<n; ++i)
  49. {
  50. Particle* P = ps->getParticle(i);
  51. if (P->isAlive() && isEnabled()) operate(P, dt);
  52. }
  53. }
  54. /** Do something on a particle.
  55. You must override it in descendant classes. Common operations
  56. consist of modifying the particle's velocity vector. The <CODE>dt</CODE> parameter is
  57. the time elapsed from last operation.
  58. */
  59. virtual void operate(Particle* P, double dt) = 0;
  60. /** Do something before processing particles via the <CODE>operate()</CODE> method.
  61. Overriding this method could be necessary to query the calling <CODE>Program</CODE> object
  62. for the current reference frame. If the reference frame is RELATIVE_RF, then your
  63. class should prepare itself to do all operations in local coordinates.
  64. */
  65. virtual void beginOperate(Program *) {}
  66. /// Do something after all particles have been processed.
  67. virtual void endOperate() {}
  68. protected:
  69. virtual ~Operator() {}
  70. Operator &operator=(const Operator &) { return *this; }
  71. private:
  72. bool _enabled;
  73. };
  74. // INLINE FUNCTIONS
  75. inline Operator::Operator()
  76. : osg::Object(), _enabled(true)
  77. {
  78. }
  79. inline Operator::Operator(const Operator& copy, const osg::CopyOp& copyop)
  80. : osg::Object(copy, copyop), _enabled(copy._enabled)
  81. {
  82. }
  83. inline bool Operator::isEnabled() const
  84. {
  85. return _enabled;
  86. }
  87. inline void Operator::setEnabled(bool v)
  88. {
  89. _enabled = v;
  90. }
  91. }
  92. #endif