1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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.
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.
13//osgParticle - Copyright (C) 2002 Marco Jez
15#ifndef OSGPARTICLE_PARTICLEPROCESSOR
16#define OSGPARTICLE_PARTICLEPROCESSOR 1
18#include <osgParticle/Export>
19#include <osgParticle/ParticleSystem>
23#include <osg/Transform>
24#include <osg/NodeVisitor>
32 /** A common base interface for those classes which need to do something on particles. Such classes
33 * are, for example, Emitter (particle generation) and Program (particle animation).
34 * This class holds some properties, like a <I>reference frame</I> and a reference to a ParticleSystem;
35 * descendant classes should process the particles taking into account the reference frame, computing the right
36 * transformations when needed.
38 class OSGPARTICLE_EXPORT ParticleProcessor: public osg::Node {
47 ParticleProcessor(const ParticleProcessor& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
49 virtual const char* libraryName() const { return "osgParticle"; }
50 virtual const char* className() const { return "ParticleProcessor"; }
51 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ParticleProcessor*>(obj) != 0; }
52 virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }
54 /// Get the reference frame.
55 inline ReferenceFrame getReferenceFrame() const;
57 /// Set the reference frame.
58 inline void setReferenceFrame(ReferenceFrame rf);
60 /// Get whether this processor is enabled or not.
61 bool getEnabled() const { return _enabled; }
62 inline bool isEnabled() const;
64 /// Set whether this processor is enabled or not.
65 inline void setEnabled(bool v);
67 /// Get a pointer to the destination particle system.
68 inline ParticleSystem* getParticleSystem();
70 /// Get a const pointer to the destination particle system.
71 inline const ParticleSystem* getParticleSystem() const;
73 /// Set the destination particle system.
74 virtual void setParticleSystem(ParticleSystem* ps);
76 /// Set the endless flag of this processor.
77 inline void setEndless(bool type);
79 /// Check whether this processor is endless.
80 bool getEndless() const { return _endless; }
81 inline bool isEndless() const;
83 /// Set the lifetime of this processor.
84 inline void setLifeTime(double t);
86 /// Get the lifetime of this processor.
87 inline double getLifeTime() const;
89 /// Set the start time of this processor.
90 inline void setStartTime(double t);
92 /// Get the start time of this processor.
93 inline double getStartTime() const;
95 /// Set the current time of this processor.
96 inline void setCurrentTime(double t);
98 /// Get the current time of this processor.
99 inline double getCurrentTime() const;
101 /// Set the reset time of this processor. A value of 0 disables reset.
102 inline void setResetTime(double t);
104 /// Get the reset time of this processor.
105 inline double getResetTime() const;
108 Check whether the processor is alive with respect to start time and
109 life duration. Note that this method may return true even if the
110 processor has been disabled by calling setEnabled(false). To test
111 whether the processor is actually processing particles or not, you
112 should evaluate (isEnabled() && isAlive()).
114 inline bool isAlive() const;
116 void traverse(osg::NodeVisitor& nv);
118 /// Get the current local-to-world transformation matrix (valid only during cull traversal).
119 inline const osg::Matrix& getLocalToWorldMatrix();
121 /// Get the current world-to-local transformation matrix (valid only during cull traversal).
122 inline const osg::Matrix& getWorldToLocalMatrix();
124 /// Get the previous local-to-world transformation matrix (valid only during cull traversal).
125 inline const osg::Matrix& getPreviousLocalToWorldMatrix();
127 /// Get the previous world-to-local transformation matrix (valid only during cull traversal).
128 inline const osg::Matrix& getPreviousWorldToLocalMatrix();
131 /// Transform a point from local to world coordinates (valid only during cull traversal).
132 inline osg::Vec3 transformLocalToWorld(const osg::Vec3& P);
134 /// Transform a vector from local to world coordinates, discarding translation (valid only during cull traversal).
135 inline osg::Vec3 rotateLocalToWorld(const osg::Vec3& P);
137 /// Transform a point from world to local coordinates (valid only during cull traversal).
138 inline osg::Vec3 transformWorldToLocal(const osg::Vec3& P);
140 /// Transform a vector from world to local coordinates, discarding translation (valid only during cull traversal).
141 inline osg::Vec3 rotateWorldToLocal(const osg::Vec3& P);
143 virtual osg::BoundingSphere computeBound() const;
146 virtual ~ParticleProcessor() {}
147 ParticleProcessor& operator=(const ParticleProcessor&) { return *this; }
149 virtual void process(double dt) = 0;
154 osg::ref_ptr<ParticleSystem> _ps;
155 bool _first_ltw_compute;
156 bool _need_ltw_matrix;
157 bool _first_wtl_compute;
158 bool _need_wtl_matrix;
159 osg::Matrix _ltw_matrix;
160 osg::Matrix _wtl_matrix;
161 osg::Matrix _previous_ltw_matrix;
162 osg::Matrix _previous_wtl_matrix;
163 osg::NodeVisitor* _current_nodevisitor;
172 //added- 1/17/06- bgandere@nps.edu
173 //a var to keep from doing multiple updates
174 unsigned int _frameNumber;
179 inline ParticleProcessor::ReferenceFrame ParticleProcessor::getReferenceFrame() const
184 inline void ParticleProcessor::setReferenceFrame(ReferenceFrame rf)
189 inline bool ParticleProcessor::isEnabled() const
194 inline void ParticleProcessor::setEnabled(bool v)
203 inline ParticleSystem* ParticleProcessor::getParticleSystem()
208 inline const ParticleSystem* ParticleProcessor::getParticleSystem() const
214 inline void ParticleProcessor::setEndless(bool type)
219 inline bool ParticleProcessor::isEndless() const
224 inline void ParticleProcessor::setLifeTime(double t)
229 inline double ParticleProcessor::getLifeTime() const
234 inline void ParticleProcessor::setStartTime(double t)
239 inline double ParticleProcessor::getStartTime() const
243 inline void ParticleProcessor::setCurrentTime(double t)
248 inline double ParticleProcessor::getCurrentTime() const
253 inline void ParticleProcessor::setResetTime(double t)
258 inline double ParticleProcessor::getResetTime() const
263 inline const osg::Matrix& ParticleProcessor::getLocalToWorldMatrix()
265 if (_need_ltw_matrix) {
266 _previous_ltw_matrix = _ltw_matrix;
267 _ltw_matrix = osg::computeLocalToWorld(_current_nodevisitor->getNodePath());
268 if (_first_ltw_compute)
270 _previous_ltw_matrix = _ltw_matrix;
271 _first_ltw_compute = false;
273 _need_ltw_matrix = false;
278 inline const osg::Matrix& ParticleProcessor::getWorldToLocalMatrix()
280 if (_need_wtl_matrix) {
281 _previous_wtl_matrix = _wtl_matrix;
282 _wtl_matrix = osg::computeWorldToLocal(_current_nodevisitor->getNodePath());
283 if (_first_wtl_compute)
285 _previous_wtl_matrix = _wtl_matrix;
286 _first_wtl_compute = false;
288 _need_wtl_matrix = false;
293 inline const osg::Matrix& ParticleProcessor::getPreviousLocalToWorldMatrix()
295 if (_need_ltw_matrix) getLocalToWorldMatrix();
296 return _previous_ltw_matrix;
299 inline const osg::Matrix& ParticleProcessor::getPreviousWorldToLocalMatrix()
301 if (_need_wtl_matrix) getWorldToLocalMatrix();
302 return _previous_wtl_matrix;
305 inline osg::Vec3 ParticleProcessor::transformLocalToWorld(const osg::Vec3& P)
307 return getLocalToWorldMatrix().preMult(P);
310 inline osg::Vec3 ParticleProcessor::transformWorldToLocal(const osg::Vec3& P)
312 return getWorldToLocalMatrix().preMult(P);
315 inline osg::Vec3 ParticleProcessor::rotateLocalToWorld(const osg::Vec3& P)
317 return getLocalToWorldMatrix().preMult(P) -
318 getLocalToWorldMatrix().preMult(osg::Vec3(0, 0, 0));
321 inline osg::Vec3 ParticleProcessor::rotateWorldToLocal(const osg::Vec3& P)
323 return getWorldToLocalMatrix().preMult(P) -
324 getWorldToLocalMatrix().preMult(osg::Vec3(0, 0, 0));
327 inline bool ParticleProcessor::isAlive() const
329 return _currentTime < (_lifeTime + _startTime);