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