| 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 | |
|---|
| 29 | namespace 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 | 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 | _t0 = -1; |
|---|
| 201 | _currentTime = 0; |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | inline ParticleSystem* ParticleProcessor::getParticleSystem() |
|---|
| 206 | { |
|---|
| 207 | return _ps.get(); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | inline const ParticleSystem* ParticleProcessor::getParticleSystem() const |
|---|
| 211 | { |
|---|
| 212 | return _ps.get(); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | inline void ParticleProcessor::setParticleSystem(ParticleSystem* ps) |
|---|
| 216 | { |
|---|
| 217 | _ps = ps; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | inline void ParticleProcessor::setEndless(bool type) |
|---|
| 221 | { |
|---|
| 222 | _endless = type; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | inline bool ParticleProcessor::isEndless() const |
|---|
| 226 | { |
|---|
| 227 | return _endless; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | inline void ParticleProcessor::setLifeTime(double t) |
|---|
| 231 | { |
|---|
| 232 | _lifeTime = t; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | inline double ParticleProcessor::getLifeTime() const |
|---|
| 236 | { |
|---|
| 237 | return _lifeTime; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | inline void ParticleProcessor::setStartTime(double t) |
|---|
| 241 | { |
|---|
| 242 | _startTime = t; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | inline double ParticleProcessor::getStartTime() const |
|---|
| 246 | { |
|---|
| 247 | return _startTime; |
|---|
| 248 | } |
|---|
| 249 | inline void ParticleProcessor::setCurrentTime(double t) |
|---|
| 250 | { |
|---|
| 251 | _currentTime = t; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | inline double ParticleProcessor::getCurrentTime() const |
|---|
| 255 | { |
|---|
| 256 | return _currentTime; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | inline void ParticleProcessor::setResetTime(double t) |
|---|
| 260 | { |
|---|
| 261 | _resetTime = t; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | inline double ParticleProcessor::getResetTime() const |
|---|
| 265 | { |
|---|
| 266 | return _resetTime; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | inline const osg::Matrix& ParticleProcessor::getLocalToWorldMatrix() |
|---|
| 270 | { |
|---|
| 271 | if (_need_ltw_matrix) { |
|---|
| 272 | _previous_ltw_matrix = _ltw_matrix; |
|---|
| 273 | _ltw_matrix = osg::computeLocalToWorld(_current_nodevisitor->getNodePath()); |
|---|
| 274 | if (_first_ltw_compute) |
|---|
| 275 | { |
|---|
| 276 | _previous_ltw_matrix = _ltw_matrix; |
|---|
| 277 | _first_ltw_compute = false; |
|---|
| 278 | } |
|---|
| 279 | _need_ltw_matrix = false; |
|---|
| 280 | } |
|---|
| 281 | return _ltw_matrix; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | inline const osg::Matrix& ParticleProcessor::getWorldToLocalMatrix() |
|---|
| 285 | { |
|---|
| 286 | if (_need_wtl_matrix) { |
|---|
| 287 | _previous_wtl_matrix = _wtl_matrix; |
|---|
| 288 | _wtl_matrix = osg::computeWorldToLocal(_current_nodevisitor->getNodePath()); |
|---|
| 289 | if (_first_wtl_compute) |
|---|
| 290 | { |
|---|
| 291 | _previous_wtl_matrix = _wtl_matrix; |
|---|
| 292 | _first_wtl_compute = false; |
|---|
| 293 | } |
|---|
| 294 | _need_wtl_matrix = false; |
|---|
| 295 | } |
|---|
| 296 | return _wtl_matrix; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | inline const osg::Matrix& ParticleProcessor::getPreviousLocalToWorldMatrix() |
|---|
| 300 | { |
|---|
| 301 | if (_need_ltw_matrix) getLocalToWorldMatrix(); |
|---|
| 302 | return _previous_ltw_matrix; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | inline const osg::Matrix& ParticleProcessor::getPreviousWorldToLocalMatrix() |
|---|
| 306 | { |
|---|
| 307 | if (_need_wtl_matrix) getWorldToLocalMatrix(); |
|---|
| 308 | return _previous_wtl_matrix; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | inline osg::Vec3 ParticleProcessor::transformLocalToWorld(const osg::Vec3& P) |
|---|
| 312 | { |
|---|
| 313 | return getLocalToWorldMatrix().preMult(P); |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | inline osg::Vec3 ParticleProcessor::transformWorldToLocal(const osg::Vec3& P) |
|---|
| 317 | { |
|---|
| 318 | return getWorldToLocalMatrix().preMult(P); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | inline osg::Vec3 ParticleProcessor::rotateLocalToWorld(const osg::Vec3& P) |
|---|
| 322 | { |
|---|
| 323 | return getLocalToWorldMatrix().preMult(P) - |
|---|
| 324 | getLocalToWorldMatrix().preMult(osg::Vec3(0, 0, 0)); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | inline osg::Vec3 ParticleProcessor::rotateWorldToLocal(const osg::Vec3& P) |
|---|
| 328 | { |
|---|
| 329 | return getWorldToLocalMatrix().preMult(P) - |
|---|
| 330 | getWorldToLocalMatrix().preMult(osg::Vec3(0, 0, 0)); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | inline bool ParticleProcessor::isAlive() const |
|---|
| 334 | { |
|---|
| 335 | return _currentTime < (_lifeTime + _startTime); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | #endif |
|---|