openscenegraph
ParticleProcessor
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_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 virtual 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 ReferenceFrame _rf;
152 bool _enabled;
153 double _t0;
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;
164
165 bool _endless;
166
167 double _lifeTime;
168 double _startTime;
169 double _currentTime;
170 double _resetTime;
171
172 //added- 1/17/06- bgandere@nps.edu
173 //a var to keep from doing multiple updates
174 unsigned int _frameNumber;
175 };
176
177 // INLINE FUNCTIONS
178
179 inline ParticleProcessor::ReferenceFrame ParticleProcessor::getReferenceFrame() const
180 {
181 return _rf;
182 }
183
184 inline void ParticleProcessor::setReferenceFrame(ReferenceFrame rf)
185 {
186 _rf = rf;
187 }
188
189 inline bool ParticleProcessor::isEnabled() const
190 {
191 return _enabled;
192 }
193
194 inline void ParticleProcessor::setEnabled(bool v)
195 {
196 _enabled = v;
197 if (_enabled)
198 {
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
214 inline void ParticleProcessor::setEndless(bool type)
215 {
216 _endless = type;
217 }
218
219 inline bool ParticleProcessor::isEndless() const
220 {
221 return _endless;
222 }
223
224 inline void ParticleProcessor::setLifeTime(double t)
225 {
226 _lifeTime = t;
227 }
228
229 inline double ParticleProcessor::getLifeTime() const
230 {
231 return _lifeTime;
232 }
233
234 inline void ParticleProcessor::setStartTime(double t)
235 {
236 _startTime = t;
237 }
238
239 inline double ParticleProcessor::getStartTime() const
240 {
241 return _startTime;
242 }
243 inline void ParticleProcessor::setCurrentTime(double t)
244 {
245 _currentTime = t;
246 }
247
248 inline double ParticleProcessor::getCurrentTime() const
249 {
250 return _currentTime;
251 }
252
253 inline void ParticleProcessor::setResetTime(double t)
254 {
255 _resetTime = t;
256 }
257
258 inline double ParticleProcessor::getResetTime() const
259 {
260 return _resetTime;
261 }
262
263 inline const osg::Matrix& ParticleProcessor::getLocalToWorldMatrix()
264 {
265 if (_need_ltw_matrix) {
266 _previous_ltw_matrix = _ltw_matrix;
267 _ltw_matrix = osg::computeLocalToWorld(_current_nodevisitor->getNodePath());
268 if (_first_ltw_compute)
269 {
270 _previous_ltw_matrix = _ltw_matrix;
271 _first_ltw_compute = false;
272 }
273 _need_ltw_matrix = false;
274 }
275 return _ltw_matrix;
276 }
277
278 inline const osg::Matrix& ParticleProcessor::getWorldToLocalMatrix()
279 {
280 if (_need_wtl_matrix) {
281 _previous_wtl_matrix = _wtl_matrix;
282 _wtl_matrix = osg::computeWorldToLocal(_current_nodevisitor->getNodePath());
283 if (_first_wtl_compute)
284 {
285 _previous_wtl_matrix = _wtl_matrix;
286 _first_wtl_compute = false;
287 }
288 _need_wtl_matrix = false;
289 }
290 return _wtl_matrix;
291 }
292
293 inline const osg::Matrix& ParticleProcessor::getPreviousLocalToWorldMatrix()
294 {
295 if (_need_ltw_matrix) getLocalToWorldMatrix();
296 return _previous_ltw_matrix;
297 }
298
299 inline const osg::Matrix& ParticleProcessor::getPreviousWorldToLocalMatrix()
300 {
301 if (_need_wtl_matrix) getWorldToLocalMatrix();
302 return _previous_wtl_matrix;
303 }
304
305 inline osg::Vec3 ParticleProcessor::transformLocalToWorld(const osg::Vec3& P)
306 {
307 return getLocalToWorldMatrix().preMult(P);
308 }
309
310 inline osg::Vec3 ParticleProcessor::transformWorldToLocal(const osg::Vec3& P)
311 {
312 return getWorldToLocalMatrix().preMult(P);
313 }
314
315 inline osg::Vec3 ParticleProcessor::rotateLocalToWorld(const osg::Vec3& P)
316 {
317 return getLocalToWorldMatrix().preMult(P) -
318 getLocalToWorldMatrix().preMult(osg::Vec3(0, 0, 0));
319 }
320
321 inline osg::Vec3 ParticleProcessor::rotateWorldToLocal(const osg::Vec3& P)
322 {
323 return getWorldToLocalMatrix().preMult(P) -
324 getWorldToLocalMatrix().preMult(osg::Vec3(0, 0, 0));
325 }
326
327 inline bool ParticleProcessor::isAlive() const
328 {
329 return _currentTime < (_lifeTime + _startTime);
330 }
331
332}
333
334
335#endif