CompositePlacer 3.1 KB

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