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

Revision 5855, 10.9 kB (checked in by robert, 7 years ago)

Added an osgDB::ReadWriteMutex? to help manage the ability to have serialize
write to objects but allow multiple threads to read at once in a read only way.

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