ParticleProcessor 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/
//osgParticle - Copyright (C) 2002 Marco Jez

#ifndef OSGPARTICLE_PARTICLEPROCESSOR
#define OSGPARTICLE_PARTICLEPROCESSOR 1

#include <osgParticle/Export>
#include <osgParticle/ParticleSystem>

#include <osg/ref_ptr>
#include <osg/Object>
#include <osg/Transform>
#include <osg/NodeVisitor>
#include <osg/CopyOp>
#include <osg/Vec3>
#include <osg/Matrix>

namespace osgParticle
{

    /** A common base interface for those classes which need to do something on particles. Such classes
      * are, for example, Emitter (particle generation) and Program (particle animation).
      * This class holds some properties, like a <I>reference frame</I> and a reference to a ParticleSystem;
      * descendant classes should process the particles taking into account the reference frame, computing the right
      * transformations when needed.
    */
    class OSGPARTICLE_EXPORT ParticleProcessor: public osg::Node {
    public:
    
        enum ReferenceFrame {
            RELATIVE_RF,
            ABSOLUTE_RF
        };
        
        ParticleProcessor();
        ParticleProcessor(const ParticleProcessor& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);

        virtual const char* libraryName() const { return "osgParticle"; }
        virtual const char* className() const { return "ParticleProcessor"; }
        virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ParticleProcessor*>(obj) != 0; }
        virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }        
        
        /// Get the reference frame.
        inline ReferenceFrame getReferenceFrame() const;
        
        /// Set the reference frame.
        inline void setReferenceFrame(ReferenceFrame rf);
        
        /// Get whether this processor is enabled or not.
        bool getEnabled() const { return _enabled; }
        inline bool isEnabled() const;
        
        /// Set whether this processor is enabled or not.
        inline void setEnabled(bool v);
        
        /// Get a pointer to the destination particle system.
        inline ParticleSystem* getParticleSystem();
        
        /// Get a const pointer to the destination particle system.
        inline const ParticleSystem* getParticleSystem() const;
        
        /// Set the destination particle system.
        inline void setParticleSystem(ParticleSystem* ps);
        
        /// Set the endless flag of this processor.
        inline void setEndless(bool type);
        
        /// Check whether this processor is endless.
        bool getEndless() const { return _endless; }
        inline bool isEndless() const;
        
        /// Set the lifetime of this processor.
        inline void setLifeTime(double t);
        
        /// Get the lifetime of this processor.
        inline double getLifeTime() const;
        
        /// Set the start time of this processor.
        inline void setStartTime(double t);
        
        /// Get the start time of this processor.
        inline double getStartTime() const;

        /// Set the current time of this processor.
        inline void setCurrentTime(double t);
        
        /// Get the current time of this processor.
        inline double getCurrentTime() const;
        
        /// Set the reset time of this processor. A value of 0 disables reset.
        inline void setResetTime(double t);
        
        /// Get the reset time of this processor.
        inline double getResetTime() const;
        
        /**
          Check whether the processor is alive with respect to start time and
          life duration. Note that this method may return true even if the 
          processor has been disabled by calling setEnabled(false). To test
          whether the processor is actually processing particles or not, you
          should evaluate (isEnabled() && isAlive()).
         */
        inline bool isAlive() const;
        
        void traverse(osg::NodeVisitor& nv);

        /// Get the current local-to-world transformation matrix (valid only during cull traversal).
        inline const osg::Matrix& getLocalToWorldMatrix();
        
        /// Get the current world-to-local transformation matrix (valid only during cull traversal).
        inline const osg::Matrix& getWorldToLocalMatrix();
        
        /// Get the previous local-to-world transformation matrix (valid only during cull traversal).
        inline const osg::Matrix& getPreviousLocalToWorldMatrix();
        
        /// Get the previous world-to-local transformation matrix (valid only during cull traversal).
        inline const osg::Matrix& getPreviousWorldToLocalMatrix();


        /// Transform a point from local to world coordinates (valid only during cull traversal).
        inline osg::Vec3 transformLocalToWorld(const osg::Vec3& P);
        
        /// Transform a vector from local to world coordinates, discarding translation (valid only during cull traversal).
        inline osg::Vec3 rotateLocalToWorld(const osg::Vec3& P);
        
        /// Transform a point from world to local coordinates (valid only during cull traversal).
        inline osg::Vec3 transformWorldToLocal(const osg::Vec3& P);
        
        /// Transform a vector from world to local coordinates, discarding translation (valid only during cull traversal).
        inline osg::Vec3 rotateWorldToLocal(const osg::Vec3& P);

        virtual osg::BoundingSphere computeBound() const;    

    protected:
        virtual ~ParticleProcessor() {}
        ParticleProcessor& operator=(const ParticleProcessor&) { return *this; }
        
        virtual void process(double dt) = 0;
        
    private:
        ReferenceFrame _rf;
        bool _enabled;
        double _t0;
        osg::ref_ptr<ParticleSystem> _ps;
        bool _first_ltw_compute;
        bool _need_ltw_matrix;
        bool _first_wtl_compute;
        bool _need_wtl_matrix;
        osg::Matrix _ltw_matrix;
        osg::Matrix _wtl_matrix;
        osg::Matrix _previous_ltw_matrix;
        osg::Matrix _previous_wtl_matrix;
        osg::NodeVisitor* _current_nodevisitor;
        
        bool _endless;
        
        double _lifeTime;
        double _startTime;
        double _currentTime;
        double _resetTime;
        
        //added- 1/17/06- bgandere@nps.edu 
        //a var to keep from doing multiple updates
        unsigned int _frameNumber;
    };
    
    // INLINE FUNCTIONS
    
    inline ParticleProcessor::ReferenceFrame ParticleProcessor::getReferenceFrame() const
    {
        return _rf;
    }
    
    inline void ParticleProcessor::setReferenceFrame(ReferenceFrame rf)
    {
        _rf = rf;
    }
    
    inline bool ParticleProcessor::isEnabled() const
    {
        return _enabled;
    }
    
    inline void ParticleProcessor::setEnabled(bool v)
    {
        _enabled = v;
        if (_enabled)
        {
            _currentTime = 0;
        }
    }
    
    inline ParticleSystem* ParticleProcessor::getParticleSystem()
    {
        return _ps.get();
    }

    inline const ParticleSystem* ParticleProcessor::getParticleSystem() const
    {
        return _ps.get();
    }
    
    inline void ParticleProcessor::setParticleSystem(ParticleSystem* ps)
    {
        _ps = ps;
    }
   
    inline void ParticleProcessor::setEndless(bool type)
    {
        _endless = type;
    }
        
    inline bool ParticleProcessor::isEndless() const
    {
        return _endless;
    }

    inline void ParticleProcessor::setLifeTime(double t)
    {
        _lifeTime = t;
    }
        
    inline double ParticleProcessor::getLifeTime() const
    {
        return _lifeTime;
    }
       
    inline void ParticleProcessor::setStartTime(double t)
    {
        _startTime = t;
    }
        
    inline double ParticleProcessor::getStartTime() const
    {
        return _startTime;
    }
    inline void ParticleProcessor::setCurrentTime(double t)
    {
        _currentTime = t;
    }
        
    inline double ParticleProcessor::getCurrentTime() const
    {
        return _currentTime;
    }
    
    inline void ParticleProcessor::setResetTime(double t)
    {
        _resetTime = t;
    }
    
    inline double ParticleProcessor::getResetTime() const
    {
        return _resetTime;
    }

    inline const osg::Matrix& ParticleProcessor::getLocalToWorldMatrix()    
    {
        if (_need_ltw_matrix) {
            _previous_ltw_matrix = _ltw_matrix;
            _ltw_matrix = osg::computeLocalToWorld(_current_nodevisitor->getNodePath());
            if (_first_ltw_compute)
            {
                _previous_ltw_matrix = _ltw_matrix;
                _first_ltw_compute = false;
            }
            _need_ltw_matrix = false;
        }
        return _ltw_matrix;
    }

    inline const osg::Matrix& ParticleProcessor::getWorldToLocalMatrix()    
    {
        if (_need_wtl_matrix) {
            _previous_wtl_matrix = _wtl_matrix;
            _wtl_matrix = osg::computeWorldToLocal(_current_nodevisitor->getNodePath());
            if (_first_wtl_compute)
            {
                _previous_wtl_matrix = _wtl_matrix;
                _first_wtl_compute = false;
            }
            _need_wtl_matrix = false;
        }
        return _wtl_matrix;
    }
    
    inline const osg::Matrix& ParticleProcessor::getPreviousLocalToWorldMatrix()
    {
        if (_need_ltw_matrix) getLocalToWorldMatrix();
        return _previous_ltw_matrix;
    }

    inline const osg::Matrix& ParticleProcessor::getPreviousWorldToLocalMatrix()
    {
        if (_need_wtl_matrix) getWorldToLocalMatrix();
        return _previous_wtl_matrix;
    }

    inline osg::Vec3 ParticleProcessor::transformLocalToWorld(const osg::Vec3& P)
    {
        return getLocalToWorldMatrix().preMult(P);
    }
    
    inline osg::Vec3 ParticleProcessor::transformWorldToLocal(const osg::Vec3& P)
    {
        return getWorldToLocalMatrix().preMult(P);
    }

    inline osg::Vec3 ParticleProcessor::rotateLocalToWorld(const osg::Vec3& P)
    {
        return getLocalToWorldMatrix().preMult(P) - 
            getLocalToWorldMatrix().preMult(osg::Vec3(0, 0, 0));
    }
    
    inline osg::Vec3 ParticleProcessor::rotateWorldToLocal(const osg::Vec3& P)
    {
        return getWorldToLocalMatrix().preMult(P) -
            getWorldToLocalMatrix().preMult(osg::Vec3(0, 0, 0));
    }
    
    inline bool ParticleProcessor::isAlive() const
    {
        return _currentTime < (_lifeTime + _startTime);
    }

}


#endif