Particle 19.3 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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
/* -*-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_PARTICLE
#define OSGPARTICLE_PARTICLE 1

#include <osgParticle/Export>
#include <osgParticle/Interpolator>
#include <osgParticle/range>

#include <osg/ref_ptr>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Matrix>
#include <osg/Drawable>
#include <osg/GL>
#include <osg/GLBeginEndAdapter>

namespace osgParticle
{

    // forward declare so we can reference it
    class ParticleSystem;

    /**    Implementation of a <B>particle</B>.
        Objects of this class are particles, they have some graphical properties
        and some physical properties. Particles are created by emitters and then placed
        into Particle Systems, where they live and get updated at each frame.
        Particles can either live forever (lifeTime < 0), or die after a specified
        time (lifeTime >= 0). For each property which is defined as a range of values, a
        "current" value will be evaluated at each frame by interpolating the <I>min</I>
        and <I>max</I> values so that <I>curr_value = min</I> when <I>t == 0</I>, and 
        <I>curr_value = max</I> when <I>t == lifeTime</I>.
        You may customize the interpolator objects to achieve any kind of transition.
        If you want the particle to live forever, set its lifetime to any value <= 0;
        in that case, no interpolation is done to compute real-time properties, and only
        minimum values are used.
    */
    class OSGPARTICLE_EXPORT Particle {
        friend class ParticleSystem;
    public:
        
        enum
        {
            INVALID_INDEX = -1
        };

        /**
         Shape of particles.
         NOTE: the LINE shape should be used in conjunction with FIXED alignment mode (see ParticleSystem).
        */
        enum Shape {
            POINT,            // uses GL_POINTS as primitive
            QUAD,            // uses GL_QUADS as primitive
            QUAD_TRIANGLESTRIP,    // uses GL_TRI_angleSTRIP as primitive, but each particle needs a glBegin/glEnd pair
            HEXAGON,            // may save some filling time, but uses more triangles
            LINE,               // uses GL_LINES to draw line segments that point to the direction of motion   
            USER               // uses a user-defined drawable as primitive
        };
        
        Particle();

        /// Get the shape of the particle.
        inline Shape getShape() const;
        
        /// Set the shape of the particle.
        inline void setShape(Shape s);

        /// Get whether the particle is still alive.
        inline bool isAlive() const;
        
        /// Get the life time of the particle (in seconds).
        inline double getLifeTime() const;
        
        /// Get the age of the particle (in seconds).
        inline double getAge() const;
        
        /// Get the minimum and maximum values for polygon size.
        inline const rangef& getSizeRange() const;
        
        /// Get the minimum and maximum values for alpha.
        inline const rangef& getAlphaRange() const;
        
        /// Get the minimum and maximum values for color.
        inline const rangev4& getColorRange() const;
        
        /// Get the interpolator for computing the size of polygons.
        inline const Interpolator* getSizeInterpolator() const;
        
        /// Get the interpolator for computing alpha values.
        inline const Interpolator* getAlphaInterpolator() const;
        
        /// Get the interpolator for computing color values.
        inline const Interpolator* getColorInterpolator() const;

        /** Get the physical radius of the particle.
            For built-in operators to work correctly, lengths must be expressed in meters.
        */
        inline float getRadius() const;
        
        /** Get the mass of the particle.
            For built-in operators to work correctly, remember that the mass is expressed in kg.
        */
        inline float getMass() const;
        
        /// Get <CODE>1 / getMass()</CODE>.
        inline float getMassInv() const;
        
        /// Get the position vector.
        inline const osg::Vec3& getPosition() const;
        
        /**    Get the velocity vector.
            For built-in operators to work correctly, remember that velocity components are expressed
            in meters per second.
        */
        inline const osg::Vec3& getVelocity() const;        
        
        /// Get the previous position (the position before last update).
        inline const osg::Vec3& getPreviousPosition() const;

        /// Get the angle vector.
        inline const osg::Vec3& getAngle() const;
        
        /// Get the rotational velocity vector.
        inline const osg::Vec3& getAngularVelocity() const;
        
        /// Get the previous angle vector.
        inline const osg::Vec3& getPreviousAngle() const;
        
        /// Get the current color
        inline const osg::Vec4& getCurrentColor() const { return _current_color; }

        /// Get the current alpha
        inline float getCurrentAlpha() const { return _current_alpha; }
        
        /// Get the s texture coordinate of the bottom left of the particle
        inline float getSTexCoord() const { return _s_coord; }

        /// Get the t texture coordinate of the bottom left of the particle
        inline float getTTexCoord() const { return _t_coord; }

        /// Get width of texture tile
        inline int getTileS() const;
        
        /// Get height of texture tile
        inline int getTileT() const;
        
        /// Get number of texture tiles
        inline int getNumTiles() const { return _end_tile - _start_tile + 1; }
        
        /** Kill the particle on next update
            NOTE: after calling this function, the <CODE>isAlive()</CODE> method will still 
            return true until the particle is updated again.
        */
        inline void kill();
        
        /// Set the life time of the particle.
        inline void setLifeTime(double t);
        
        /// Set the minimum and maximum values for polygon size.
        inline void setSizeRange(const rangef& r);
        
        /// Set the minimum and maximum values for alpha.
        inline void setAlphaRange(const rangef& r);
        
        /// Set the minimum and maximum values for color.
        inline void setColorRange(const rangev4& r);
        
        /// Set the interpolator for computing size values.
        inline void setSizeInterpolator(Interpolator* ri);
        
        /// Set the interpolator for computing alpha values.        
        inline void setAlphaInterpolator(Interpolator* ai);
        
        /// Set the interpolator for computing color values.
        inline void setColorInterpolator(Interpolator* ci);

        /** Set the physical radius of the particle.
            For built-in operators to work correctly, lengths must be expressed in meters.
        */
        inline void setRadius(float r);
        
        /** Set the mass of the particle.
            For built-in operators to work correctly, remember that the mass is expressed in kg.
        */
        inline void setMass(float m);
        
        /// Set the position vector.        
        inline void setPosition(const osg::Vec3& p);
        
        /**    Set the velocity vector.
            For built-in operators to work correctly, remember that velocity components are expressed
            in meters per second.
        */
        inline void setVelocity(const osg::Vec3& v);
        
        /// Add a vector to the velocity vector.
        inline void addVelocity(const osg::Vec3& dv);
        
        /// Transform position and velocity vectors by a matrix.
        inline void transformPositionVelocity(const osg::Matrix& xform);

        /// Transform position and velocity vectors by a combination of two matrices
        void transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r);

        /// Set the angle vector.
        inline void setAngle(const osg::Vec3& a);
        
        /**
          Set the angular velocity vector. 
          Components x, y and z are angles of rotation around the respective axis (in radians).
        */
        inline void setAngularVelocity(const osg::Vec3& v);
        
        /// Add a vector to the angular velocity vector.
        inline void addAngularVelocity(const osg::Vec3& dv);
        
        /// Transform angle and angularVelocity vectors by a matrix.
        inline void transformAngleVelocity(const osg::Matrix& xform);
        
        /** Update the particle (don't call this method manually).
            This method is called automatically by <CODE>ParticleSystem::update()</CODE>; it
            updates the graphical properties of the particle for the current time,
            checks whether the particle is still alive, and then updates its position
            by computing <I>P = P + V * dt</I> (where <I>P</I> is the position and <I>V</I> is the velocity).
        */
        bool update(double dt, bool onlyTimeStamp);

        /// Perform some pre-rendering tasks. Called automatically by particle systems.
        inline void beginRender(osg::GLBeginEndAdapter* gl) const;
        
        /// Render the particle. Called automatically by particle systems.
        void render(osg::GLBeginEndAdapter* gl, const osg::Vec3& xpos, const osg::Vec3& px, const osg::Vec3& py, float scale = 1.0f) const;
        
        /// Render the particle with user-defined drawable
        void render(osg::RenderInfo& renderInfo, const osg::Vec3& xpos, const osg::Vec3& xrot) const;
        
        /// Perform some post-rendering tasks. Called automatically by particle systems.
        inline void endRender(osg::GLBeginEndAdapter* gl) const;
        
        /// Get the current (interpolated) polygon size. Valid only after the first call to update().
        inline float getCurrentSize() const;
        
        /// Specify how the particle texture is tiled.
        /// All tiles in the given range are sequentially displayed during the lifetime
        /// of the particle. When no range is given, all tiles are displayed during the lifetime.
        inline void setTextureTileRange(int sTile, int tTile, int startTile, int endTile);

        /// Same as above, range starts at 0 and ends at end
        inline void setTextureTile(int sTile, int tTile, int end = -1);

        /// Set the previous particle
        inline void setPreviousParticle(int previous) { _previousParticle = previous; }

        /// Get the previous particle
        inline int getPreviousParticle() const { return _previousParticle; }

        /// Set the next particle
        inline void setNextParticle(int next) { _nextParticle = next; }

        /// Get the const next particle
        inline int getNextParticle() const { return _nextParticle; }
        
        /// Set the depth of the particle
        inline void setDepth(double d) { _depth = d; }
        
        /// Get the depth of the particle
        inline double getDepth() const { return _depth; }
        
        /// Set the user-defined particle drawable
        inline void setDrawable(osg::Drawable* d) { _drawable = d; }
        
        /// Get the user-defined particle drawable
        inline osg::Drawable* getDrawable() const { return _drawable.get(); }
        
        /// Sorting operator
        bool operator<(const Particle &P) const { return _depth < P._depth; }

        /// Method for initializing a particles texture coords as part of a connected particle system.
        void setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem* ps);

    protected:
    
        Shape _shape;

        rangef _sr;
        rangef _ar;
        rangev4 _cr;

        osg::ref_ptr<Interpolator> _si;
        osg::ref_ptr<Interpolator> _ai;
        osg::ref_ptr<Interpolator> _ci;

        bool _mustdie;
        double _lifeTime;

        float _radius;
        float _mass;
        float _massinv;
        osg::Vec3 _prev_pos;
        osg::Vec3 _position;
        osg::Vec3 _velocity;

        osg::Vec3 _prev_angle;
        osg::Vec3 _angle;
        osg::Vec3 _angul_arvel;

        double _t0;        

        float _alive;
        float _current_size;
        float _current_alpha;
        osg::Vec3 _base_prop;  // [0] _alive [1] _current_size [2] _current_alpha
        osg::Vec4 _current_color;
        
        float _s_tile;
        float _t_tile;
        int _start_tile;
        int _end_tile;
        int _cur_tile;
        float _s_coord;
        float _t_coord;
        
        // previous and next Particles are only used in ConnectedParticleSystems
        int _previousParticle;
        int _nextParticle;
        
        // the depth of the particle is used only when sorting is enabled
        double _depth;
        
        // the particle drawable is used only when USER shape is enabled
        osg::ref_ptr<osg::Drawable> _drawable;
    };

    // INLINE FUNCTIONS

    inline Particle::Shape Particle::getShape() const
    {
        return _shape;
    }

    inline void Particle::setShape(Shape s)
    {
        _shape = s;
    }

    inline bool Particle::isAlive() const
    {
        return _alive>0.0f;
    }

    inline double Particle::getLifeTime() const
    {
        return _lifeTime;
    }
    
    inline double Particle::getAge() const
    {
        return _t0;
    }
    
    inline float Particle::getRadius() const
    {
        return _radius;
    }
    
    inline void Particle::setRadius(float r)
    {
        _radius = r;
    }

    inline const rangef& Particle::getSizeRange() const
    {
        return _sr;
    }

    inline const rangef& Particle::getAlphaRange() const
    {
        return _ar;
    }

    inline const rangev4& Particle::getColorRange() const
    {
        return _cr;
    }

    inline const Interpolator* Particle::getSizeInterpolator() const
    {
        return _si.get();
    }

    inline const Interpolator* Particle::getAlphaInterpolator() const
    {
        return _ai.get();
    }

    inline const Interpolator* Particle::getColorInterpolator() const
    {
        return _ci.get();
    }

    inline const osg::Vec3& Particle::getPosition() const
    {
        return _position;
    }

    inline const osg::Vec3& Particle::getVelocity() const
    {
        return _velocity;
    }
    
    inline const osg::Vec3& Particle::getPreviousPosition() const
    {
        return _prev_pos;
    }

    inline const osg::Vec3& Particle::getAngle() const
    {
        return _angle;
    }
    
    inline const osg::Vec3& Particle::getAngularVelocity() const
    {
        return _angul_arvel;
    }
    
    inline const osg::Vec3& Particle::getPreviousAngle() const
    {
        return _prev_angle;
    }
    
    inline int Particle::getTileS() const
    {
        return (_s_tile>0.0f) ? static_cast<int>(1.0f / _s_tile) : 1;
    }

    inline int Particle::getTileT() const
    {
        return (_t_tile>0.0f) ? static_cast<int>(1.0f / _t_tile) : 1;
    }
    
    inline void Particle::kill()
    {
        _mustdie = true;
    }

    inline void Particle::setLifeTime(double t)
    {
        _lifeTime = t;
    }

    inline void Particle::setSizeRange(const rangef& r)
    {
        _sr = r;
    }

    inline void Particle::setAlphaRange(const rangef& r)
    {
        _ar = r;
    }

    inline void Particle::setColorRange(const rangev4& r)
    {
        _cr = r;
    }

    inline void Particle::setSizeInterpolator(Interpolator* ri)
    {
        _si = ri;
    }

    inline void Particle::setAlphaInterpolator(Interpolator* ai)
    {
        _ai = ai;
    }

    inline void Particle::setColorInterpolator(Interpolator* ci)
    {
        _ci = ci;
    }

    inline void Particle::setPosition(const osg::Vec3& p)
    {
        _position = p;
    }

    inline void Particle::setVelocity(const osg::Vec3& v)
    {
        _velocity = v;
    }

    inline void Particle::addVelocity(const osg::Vec3& dv)
    {
        _velocity += dv;
    }

    inline void Particle::transformPositionVelocity(const osg::Matrix& xform)
    {
        _position = xform.preMult(_position);
        _velocity = osg::Matrix::transform3x3(_velocity, xform);
    }
    
    inline void Particle::transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r)
    {
        osg::Vec3 position1 = xform1.preMult(_position);
        osg::Vec3 velocity1 = osg::Matrix::transform3x3(_velocity, xform1);
        osg::Vec3 position2 = xform2.preMult(_position);
        osg::Vec3 velocity2 = osg::Matrix::transform3x3(_velocity, xform2);
        float one_minus_r = 1.0f-r;
        _position = position1*r + position2*one_minus_r;
        _velocity = velocity1*r + velocity2*one_minus_r;
    }
    
    inline void Particle::setAngle(const osg::Vec3& a)
    {
        _angle = a;
    }
    
    inline void Particle::setAngularVelocity(const osg::Vec3& v)
    {
        _angul_arvel = v;
    }
    
    inline void Particle::addAngularVelocity(const osg::Vec3& dv)
    {
        _angul_arvel += dv;
    }
    
    inline void Particle::transformAngleVelocity(const osg::Matrix& xform)
    {
        // this should be optimized!
        
        osg::Vec3 a1 = _angle + _angul_arvel;
        
        _angle = xform.preMult(_angle);
        a1 = xform.preMult(a1);
        
        _angul_arvel = a1 - _angle;
    }
            
    inline float Particle::getMass() const
    {
        return _mass;
    }
    
    inline float Particle::getMassInv() const
    {
        return _massinv;
    }
    
    inline void Particle::setMass(float m)
    {
        _mass = m;
        _massinv = 1 / m;
    }
    
    inline void Particle::beginRender(osg::GLBeginEndAdapter* gl) const
    {
        switch (_shape)
        {
        case POINT:
            gl->Begin(GL_POINTS);
            break;
        case QUAD:
            gl->Begin(GL_QUADS);
            break;
        case LINE:
            gl->Begin(GL_LINES);
            break;
        default: ;
        }
    }

    inline void Particle::endRender(osg::GLBeginEndAdapter* gl) const
    {
        switch (_shape)
        {
        case POINT:
        case QUAD:
        case LINE:
            gl->End();
            break;
        default: ;
        }
    }

    inline float Particle::getCurrentSize() const
    {
        return _current_size;
    }


    inline void Particle::setTextureTile(int sTile, int tTile, int end)
    {
        setTextureTileRange(sTile, tTile, -1, end);
    }

    inline void Particle::setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
    {
       _s_tile = (sTile>0) ? 1.0f / static_cast<float>(sTile) : 1.0f;
       _t_tile = (tTile>0) ? 1.0f / static_cast<float>(tTile) : 1.0f;
       
       if(startTile == -1)
       {
          _start_tile = 0;
       }
       else
       {
          _start_tile = startTile;
       }

       if(endTile == -1)
       {
          _end_tile = sTile * tTile;
       }
       else
       {
          _end_tile = endTile;
       }
    }

}

#endif