root/OpenSceneGraph/trunk/include/osgParticle/ParticleProcessor @ 13041

Revision 13041, 10.6 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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_PARTICLEPROCESSOR
16#define OSGPARTICLE_PARTICLEPROCESSOR 1
17
18#include <osgParticle/Export>
19#include <osgParticle/ParticleSystem>
20
21#include <osg/ref_ptr>
22#include <osg/Object>
23#include <osg/Transform>
24#include <osg/NodeVisitor>
25#include <osg/CopyOp>
26#include <osg/Vec3>
27#include <osg/Matrix>
28
29namespace osgParticle
30{
31
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.
37    */
38    class OSGPARTICLE_EXPORT ParticleProcessor: public osg::Node {
39    public:
40
41        enum ReferenceFrame {
42            RELATIVE_RF,
43            ABSOLUTE_RF
44        };
45
46        ParticleProcessor();
47        ParticleProcessor(const ParticleProcessor& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
48
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(); } }
53
54        /// Get the reference frame.
55        inline ReferenceFrame getReferenceFrame() const;
56
57        /// Set the reference frame.
58        inline void setReferenceFrame(ReferenceFrame rf);
59
60        /// Get whether this processor is enabled or not.
61        bool getEnabled() const { return _enabled; }
62        inline bool isEnabled() const;
63
64        /// Set whether this processor is enabled or not.
65        inline void setEnabled(bool v);
66
67        /// Get a pointer to the destination particle system.
68        inline ParticleSystem* getParticleSystem();
69
70        /// Get a const pointer to the destination particle system.
71        inline const ParticleSystem* getParticleSystem() const;
72
73        /// Set the destination particle system.
74        inline void setParticleSystem(ParticleSystem* ps);
75
76        /// Set the endless flag of this processor.
77        inline void setEndless(bool type);
78
79        /// Check whether this processor is endless.
80        bool getEndless() const { return _endless; }
81        inline bool isEndless() const;
82
83        /// Set the lifetime of this processor.
84        inline void setLifeTime(double t);
85
86        /// Get the lifetime of this processor.
87        inline double getLifeTime() const;
88
89        /// Set the start time of this processor.
90        inline void setStartTime(double t);
91
92        /// Get the start time of this processor.
93        inline double getStartTime() const;
94
95        /// Set the current time of this processor.
96        inline void setCurrentTime(double t);
97
98        /// Get the current time of this processor.
99        inline double getCurrentTime() const;
100
101        /// Set the reset time of this processor. A value of 0 disables reset.
102        inline void setResetTime(double t);
103
104        /// Get the reset time of this processor.
105        inline double getResetTime() const;
106
107        /**
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()).
113         */
114        inline bool isAlive() const;
115
116        void traverse(osg::NodeVisitor& nv);
117
118        /// Get the current local-to-world transformation matrix (valid only during cull traversal).
119        inline const osg::Matrix& getLocalToWorldMatrix();
120
121        /// Get the current world-to-local transformation matrix (valid only during cull traversal).
122        inline const osg::Matrix& getWorldToLocalMatrix();
123
124        /// Get the previous local-to-world transformation matrix (valid only during cull traversal).
125        inline const osg::Matrix& getPreviousLocalToWorldMatrix();
126
127        /// Get the previous world-to-local transformation matrix (valid only during cull traversal).
128        inline const osg::Matrix& getPreviousWorldToLocalMatrix();
129
130
131        /// Transform a point from local to world coordinates (valid only during cull traversal).
132        inline osg::Vec3 transformLocalToWorld(const osg::Vec3& P);
133
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);
136
137        /// Transform a point from world to local coordinates (valid only during cull traversal).
138        inline osg::Vec3 transformWorldToLocal(const osg::Vec3& P);
139
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);
142
143        virtual osg::BoundingSphere computeBound() const;
144
145    protected:
146        virtual ~ParticleProcessor() {}
147        ParticleProcessor& operator=(const ParticleProcessor&) { return *this; }
148
149        virtual void process(double dt) = 0;
150
151    private:
152        ReferenceFrame _rf;
153        bool _enabled;
154        double _t0;
155        osg::ref_ptr<ParticleSystem> _ps;
156        bool _first_ltw_compute;
157        bool _need_ltw_matrix;
158        bool _first_wtl_compute;
159        bool _need_wtl_matrix;
160        osg::Matrix _ltw_matrix;
161        osg::Matrix _wtl_matrix;
162        osg::Matrix _previous_ltw_matrix;
163        osg::Matrix _previous_wtl_matrix;
164        osg::NodeVisitor* _current_nodevisitor;
165
166        bool _endless;
167
168        double _lifeTime;
169        double _startTime;
170        double _currentTime;
171        double _resetTime;
172
173        //added- 1/17/06- bgandere@nps.edu
174        //a var to keep from doing multiple updates
175        unsigned int _frameNumber;
176    };
177
178    // INLINE FUNCTIONS
179
180    inline ParticleProcessor::ReferenceFrame ParticleProcessor::getReferenceFrame() const
181    {
182        return _rf;
183    }
184
185    inline void ParticleProcessor::setReferenceFrame(ReferenceFrame rf)
186    {
187        _rf = rf;
188    }
189
190    inline bool ParticleProcessor::isEnabled() const
191    {
192        return _enabled;
193    }
194
195    inline void ParticleProcessor::setEnabled(bool v)
196    {
197        _enabled = v;
198        if (_enabled)
199        {
200            _currentTime = 0;
201        }
202    }
203
204    inline ParticleSystem* ParticleProcessor::getParticleSystem()
205    {
206        return _ps.get();
207    }
208
209    inline const ParticleSystem* ParticleProcessor::getParticleSystem() const
210    {
211        return _ps.get();
212    }
213
214    inline void ParticleProcessor::setParticleSystem(ParticleSystem* ps)
215    {
216        _ps = ps;
217    }
218
219    inline void ParticleProcessor::setEndless(bool type)
220    {
221        _endless = type;
222    }
223
224    inline bool ParticleProcessor::isEndless() const
225    {
226        return _endless;
227    }
228
229    inline void ParticleProcessor::setLifeTime(double t)
230    {
231        _lifeTime = t;
232    }
233
234    inline double ParticleProcessor::getLifeTime() const
235    {
236        return _lifeTime;
237    }
238
239    inline void ParticleProcessor::setStartTime(double t)
240    {
241        _startTime = t;
242    }
243
244    inline double ParticleProcessor::getStartTime() const
245    {
246        return _startTime;
247    }
248    inline void ParticleProcessor::setCurrentTime(double t)
249    {
250        _currentTime = t;
251    }
252
253    inline double ParticleProcessor::getCurrentTime() const
254    {
255        return _currentTime;
256    }
257
258    inline void ParticleProcessor::setResetTime(double t)
259    {
260        _resetTime = t;
261    }
262
263    inline double ParticleProcessor::getResetTime() const
264    {
265        return _resetTime;
266    }
267
268    inline const osg::Matrix& ParticleProcessor::getLocalToWorldMatrix()
269    {
270        if (_need_ltw_matrix) {
271            _previous_ltw_matrix = _ltw_matrix;
272            _ltw_matrix = osg::computeLocalToWorld(_current_nodevisitor->getNodePath());
273            if (_first_ltw_compute)
274            {
275                _previous_ltw_matrix = _ltw_matrix;
276                _first_ltw_compute = false;
277            }
278            _need_ltw_matrix = false;
279        }
280        return _ltw_matrix;
281    }
282
283    inline const osg::Matrix& ParticleProcessor::getWorldToLocalMatrix()
284    {
285        if (_need_wtl_matrix) {
286            _previous_wtl_matrix = _wtl_matrix;
287            _wtl_matrix = osg::computeWorldToLocal(_current_nodevisitor->getNodePath());
288            if (_first_wtl_compute)
289            {
290                _previous_wtl_matrix = _wtl_matrix;
291                _first_wtl_compute = false;
292            }
293            _need_wtl_matrix = false;
294        }
295        return _wtl_matrix;
296    }
297
298    inline const osg::Matrix& ParticleProcessor::getPreviousLocalToWorldMatrix()
299    {
300        if (_need_ltw_matrix) getLocalToWorldMatrix();
301        return _previous_ltw_matrix;
302    }
303
304    inline const osg::Matrix& ParticleProcessor::getPreviousWorldToLocalMatrix()
305    {
306        if (_need_wtl_matrix) getWorldToLocalMatrix();
307        return _previous_wtl_matrix;
308    }
309
310    inline osg::Vec3 ParticleProcessor::transformLocalToWorld(const osg::Vec3& P)
311    {
312        return getLocalToWorldMatrix().preMult(P);
313    }
314
315    inline osg::Vec3 ParticleProcessor::transformWorldToLocal(const osg::Vec3& P)
316    {
317        return getWorldToLocalMatrix().preMult(P);
318    }
319
320    inline osg::Vec3 ParticleProcessor::rotateLocalToWorld(const osg::Vec3& P)
321    {
322        return getLocalToWorldMatrix().preMult(P) -
323            getLocalToWorldMatrix().preMult(osg::Vec3(0, 0, 0));
324    }
325
326    inline osg::Vec3 ParticleProcessor::rotateWorldToLocal(const osg::Vec3& P)
327    {
328        return getWorldToLocalMatrix().preMult(P) -
329            getWorldToLocalMatrix().preMult(osg::Vec3(0, 0, 0));
330    }
331
332    inline bool ParticleProcessor::isAlive() const
333    {
334        return _currentTime < (_lifeTime + _startTime);
335    }
336
337}
338
339
340#endif
Note: See TracBrowser for help on using the browser.