1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
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.
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.
13// Written by Wang Rui, (C) 2010
15#ifndef OSGPARTICLE_COMPOSITEPLACER
16#define OSGPARTICLE_COMPOSITEPLACER
18#include <osgParticle/Placer>
19#include <osgParticle/Particle>
25/** A composite particle placer which allows particles to be generated from a union of placers. */
26class CompositePlacer : public Placer
29 CompositePlacer() : Placer() {}
31 CompositePlacer( const CompositePlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
32 : Placer(copy, copyop), _placers(copy._placers) {}
34 META_Object( osgParticle, CompositePlacer );
36 // Set a child placer at specific index
37 void setPlacer( unsigned int i, Placer* p )
39 if (i<_placers.size()) _placers[i] = p;
43 /// Add a child placer
44 void addPlacer( Placer* p ) { _placers.push_back(p); }
46 /// Remove a child placer
47 void removePlacer( unsigned int i )
48 { if (i<_placers.size()) _placers.erase(_placers.begin()+i); }
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(); }
54 /// Get number of placers
55 unsigned int getNumPlacers() const { return _placers.size(); }
57 /// Place a particle. Do not call it manually.
58 inline void place( Particle* P ) const;
60 /// return the volume of the box
61 inline float volume() const;
63 /// return the control position
64 inline osg::Vec3 getControlPosition() const;
67 virtual ~CompositePlacer() {}
68 CompositePlacer& operator=( const CompositePlacer& ) { return *this; }
70 typedef std::vector< osg::ref_ptr<Placer> > PlacerList;
76inline void CompositePlacer::place( Particle* P ) const
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 )
82 current += (*itr)->volume();
83 if ( selected<=current ) (*itr)->place( P );
87inline float CompositePlacer::volume() const
89 float total_size = 0.0f;
90 for ( PlacerList::const_iterator itr=_placers.begin(); itr!=_placers.end(); ++itr )
91 total_size += (*itr)->volume();
95inline osg::Vec3 CompositePlacer::getControlPosition() const
97 if ( !_placers.size() ) return osg::Vec3();
98 return _placers.front()->getControlPosition();