| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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 | // Written by Wang Rui, (C) 2010 |
|---|
| 14 | |
|---|
| 15 | #ifndef OSGPARTICLE_COMPOSITEPLACER |
|---|
| 16 | #define OSGPARTICLE_COMPOSITEPLACER |
|---|
| 17 | |
|---|
| 18 | #include <osgParticle/Placer> |
|---|
| 19 | #include <osgParticle/Particle> |
|---|
| 20 | |
|---|
| 21 | namespace osgParticle |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /** A composite particle placer which allows particles to be generated from a union of placers. */ |
|---|
| 26 | class CompositePlacer : public Placer |
|---|
| 27 | { |
|---|
| 28 | public: |
|---|
| 29 | CompositePlacer() : Placer() {} |
|---|
| 30 | |
|---|
| 31 | CompositePlacer( const CompositePlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY ) |
|---|
| 32 | : Placer(copy, copyop), _placers(copy._placers) {} |
|---|
| 33 | |
|---|
| 34 | META_Object( osgParticle, CompositePlacer ); |
|---|
| 35 | |
|---|
| 36 | // Set a child placer at specific index |
|---|
| 37 | void setPlacer( unsigned int i, Placer* p ) |
|---|
| 38 | { |
|---|
| 39 | if (i<_placers.size()) _placers[i] = p; |
|---|
| 40 | else addPlacer(p); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /// Add a child placer |
|---|
| 44 | void addPlacer( Placer* p ) { _placers.push_back(p); } |
|---|
| 45 | |
|---|
| 46 | /// Remove a child placer |
|---|
| 47 | void removePlacer( unsigned int i ) |
|---|
| 48 | { if (i<_placers.size()) _placers.erase(_placers.begin()+i); } |
|---|
| 49 | |
|---|
| 50 | /// Get a child placer |
|---|
| 51 | Placer* getPlacer( unsigned int i ) { return _placers[i].get(); } |
|---|
| 52 | const Placer* getPlacer( unsigned int i ) const { return _placers[i].get(); } |
|---|
| 53 | |
|---|
| 54 | /// Get number of placers |
|---|
| 55 | unsigned int getNumPlacers() const { return _placers.size(); } |
|---|
| 56 | |
|---|
| 57 | /// Place a particle. Do not call it manually. |
|---|
| 58 | inline void place( Particle* P ) const; |
|---|
| 59 | |
|---|
| 60 | /// return the volume of the box |
|---|
| 61 | inline float volume() const; |
|---|
| 62 | |
|---|
| 63 | /// return the control position |
|---|
| 64 | inline osg::Vec3 getControlPosition() const; |
|---|
| 65 | |
|---|
| 66 | protected: |
|---|
| 67 | virtual ~CompositePlacer() {} |
|---|
| 68 | CompositePlacer& operator=( const CompositePlacer& ) { return *this; } |
|---|
| 69 | |
|---|
| 70 | typedef std::vector< osg::ref_ptr<Placer> > PlacerList; |
|---|
| 71 | PlacerList _placers; |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | // INLINE METHODS |
|---|
| 75 | |
|---|
| 76 | inline void CompositePlacer::place( Particle* P ) const |
|---|
| 77 | { |
|---|
| 78 | rangef sizeRange( 0.0f, volume() ); |
|---|
| 79 | float current = 0.0f, selected = sizeRange.get_random(); |
|---|
| 80 | for ( PlacerList::const_iterator itr=_placers.begin(); itr!=_placers.end(); ++itr ) |
|---|
| 81 | { |
|---|
| 82 | current += (*itr)->volume(); |
|---|
| 83 | if ( selected<=current ) (*itr)->place( P ); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | inline float CompositePlacer::volume() const |
|---|
| 88 | { |
|---|
| 89 | float total_size = 0.0f; |
|---|
| 90 | for ( PlacerList::const_iterator itr=_placers.begin(); itr!=_placers.end(); ++itr ) |
|---|
| 91 | total_size += (*itr)->volume(); |
|---|
| 92 | return total_size; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | inline osg::Vec3 CompositePlacer::getControlPosition() const |
|---|
| 96 | { |
|---|
| 97 | if ( !_placers.size() ) return osg::Vec3(); |
|---|
| 98 | return _placers.front()->getControlPosition(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #endif |
|---|