b4789863ac
type is supported at present. The attached osgparticleshader.cpp will show how it works. It can also be placed in the examples folder. But I just wonder how this example co-exists with another two (osgparticle and osgparticleeffect)? Member variables in Particle, including _alive, _current_size and _current_alpha, are now merged into one Vec3 variable. Then we can make use of the set...Pointer() methods to treat them as vertex attribtues in GLSL. User interfaces are not changed. Additional methods of ParticleSystem are introduced, including setDefaultAttributesUsingShaders(), setSortMode() and setVisibilityDistance(). You can see how they work in osgparticleshader.cpp. Additional user-defined particle type is introduced. Set the particle type to USER and attach a drawable to the template. Be careful because of possible huge memory consumption. It is highly suggested to use display lists here. The ParticleSystemUpdater can accepts ParticleSystem objects as child drawables now. I myself think it is a little simpler in structure, than creating a new geode for each particle system. Of course, the latter is still compatible, and can be used to transform entire particles in the world. New particle operators: bounce, sink, damping, orbit and explosion. The bounce and sink opeartors both use a concept of domains, and can simulate a very basic collision of particles and objects. New composite placer. It contains a set of placers and emit particles from them randomly. The added virtual method size() of each placer will help determine the probability of generating. New virtual method operateParticles() for the Operator class. It actually calls operate() for each particle, but can be overrode to use speedup techniques like SSE, or even shaders in the future. Partly fix a floating error of 'delta time' in emitter, program and updaters. Previously they keep the _t0 variable seperately and compute different copies of dt by themseleves, which makes some operators, especially the BounceOperator, work incorrectly (because the dt in operators and updaters are slightly different). Now a getDeltaTime() method is maintained in ParticleSystem, and will return the unique dt value (passing by reference) for use. This makes thing better, but still very few unexpected behavours at present... All dotosg and serialzier wrappers for functionalities above are provided. ... According to some simple tests, the new shader support is slightly efficient than ordinary glBegin()/end(). That means, I haven't got a big improvement at present. I think the bottlenack here seems to be the cull traversal time. Because operators go through the particle list again and again (for example, the fountain in the shader example requires 4 operators working all the time). A really ideal solution here is to implement the particle operators in shaders, too, and copy the results back to particle attributes. The concept of GPGPU is good for implementing this. But in my opinion, the Camera class seems to be too heavy for realizing such functionality in a particle system. Myabe a light-weight ComputeDrawable class is enough for receiving data as textures and outputting the results to the FBO render buffer. What do you think then? The floating error of emitters (http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/2009-May/028435.html) is not solved this time. But what I think is worth testing is that we could directly compute the node path from the emitter to the particle system rather than multiplying the worldToLocal and LocalToWorld matrices. I'll try this idea later. "
236 lines
7.8 KiB
C++
236 lines
7.8 KiB
C++
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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.
|
|
*/
|
|
// Written by Wang Rui, (C) 2010
|
|
|
|
#ifndef OSGPARTICLE_DOMAINOPERATOR
|
|
#define OSGPARTICLE_DOMAINOPERATOR
|
|
|
|
#include <osg/Plane>
|
|
#include <osgParticle/Operator>
|
|
#include <osgParticle/Particle>
|
|
|
|
namespace osgParticle
|
|
{
|
|
|
|
|
|
/** A domain operator which accepts different domain shapes as the parameter.
|
|
It can be derived to implement operators that require particles interacting with domains.
|
|
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
|
*/
|
|
class OSGPARTICLE_EXPORT DomainOperator : public Operator
|
|
{
|
|
public:
|
|
struct Domain
|
|
{
|
|
enum Type
|
|
{
|
|
UNDEFINED_DOMAIN = 0,
|
|
POINT_DOMAIN,
|
|
LINE_DOMAIN,
|
|
TRI_DOMAIN,
|
|
RECT_DOMAIN,
|
|
PLANE_DOMAIN,
|
|
SPHERE_DOMAIN,
|
|
BOX_DOMAIN,
|
|
DISK_DOMAIN
|
|
};
|
|
|
|
Domain( Type t ) : r1(0.0f), r2(0.0f), type(t) {}
|
|
osg::Plane plane;
|
|
osg::Vec3 v1;
|
|
osg::Vec3 v2;
|
|
osg::Vec3 v3;
|
|
osg::Vec3 s1;
|
|
osg::Vec3 s2;
|
|
float r1;
|
|
float r2;
|
|
Type type;
|
|
};
|
|
|
|
DomainOperator()
|
|
: Operator()
|
|
{}
|
|
|
|
DomainOperator( const DomainOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
|
: Operator(copy, copyop), _domains(copy._domains), _backupDomains(copy._backupDomains)
|
|
{}
|
|
|
|
META_Object( osgParticle, DomainOperator );
|
|
|
|
/// Add a point domain
|
|
inline void addPointDomain( const osg::Vec3& p );
|
|
|
|
/// Add a line segment domain
|
|
inline void addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 );
|
|
|
|
/// Add a triangle domain
|
|
inline void addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 );
|
|
|
|
/// Add a rectangle domain
|
|
inline void addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h );
|
|
|
|
/// Add a plane domain
|
|
inline void addPlaneDomain( const osg::Plane& plane );
|
|
|
|
/// Add a sphere domain
|
|
inline void addSphereDomain( const osg::Vec3& c, float r );
|
|
|
|
/// Add a box domain
|
|
inline void addBoxDomain( const osg::Vec3& min, const osg::Vec3& max );
|
|
|
|
/// Add a disk domain
|
|
inline void addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2=0.0f );
|
|
|
|
/// Add a domain object directly, used by the .osg wrappers and serializers.
|
|
void addDomain( const Domain& domain ) { _domains.push_back(domain); }
|
|
|
|
/// Get a domain object directly, used by the .osg wrappers and serializers.
|
|
const Domain& getDomain( unsigned int i ) const { return _domains[i]; }
|
|
|
|
/// Remove a domain at specific index
|
|
void removeDomain( unsigned int i )
|
|
{ if (i<_domains.size()) _domains.erase(_domains.begin() + i); }
|
|
|
|
/// Remove all existing domains
|
|
void removeAllDomains() { _domains.clear(); }
|
|
|
|
/// Get number of domains
|
|
unsigned int getNumDomains() const { return _domains.size(); }
|
|
|
|
/// Apply the acceleration to a particle. Do not call this method manually.
|
|
void operate( Particle* P, double dt );
|
|
|
|
/// Perform some initializations. Do not call this method manually.
|
|
void beginOperate( Program* prg );
|
|
|
|
/// Perform some post-operations. Do not call this method manually.
|
|
void endOperate();
|
|
|
|
protected:
|
|
virtual ~DomainOperator() {}
|
|
DomainOperator& operator=( const DomainOperator& ) { return *this; }
|
|
|
|
virtual void handlePoint( const Domain& domain, Particle* P, double dt ) { ignore("Point"); }
|
|
virtual void handleLineSegment( const Domain& domain, Particle* P, double dt ) { ignore("LineSegment"); }
|
|
virtual void handleTriangle( const Domain& domain, Particle* P, double dt ) { ignore("Triangle"); }
|
|
virtual void handleRectangle( const Domain& domain, Particle* P, double dt ) { ignore("Rectangle"); }
|
|
virtual void handlePlane( const Domain& domain, Particle* P, double dt ) { ignore("Plane"); }
|
|
virtual void handleSphere( const Domain& domain, Particle* P, double dt ) { ignore("Sphere"); }
|
|
virtual void handleBox( const Domain& domain, Particle* P, double dt ) { ignore("Box"); }
|
|
virtual void handleDisk( const Domain& domain, Particle* P, double dt ) { ignore("Disk"); }
|
|
|
|
inline void computeNewBasis( const osg::Vec3&, const osg::Vec3&, osg::Vec3&, osg::Vec3& );
|
|
inline void ignore( const std::string& func );
|
|
|
|
std::vector<Domain> _domains;
|
|
std::vector<Domain> _backupDomains;
|
|
};
|
|
|
|
// INLINE METHODS
|
|
|
|
inline void DomainOperator::addPointDomain( const osg::Vec3& p )
|
|
{
|
|
Domain domain( Domain::POINT_DOMAIN );
|
|
domain.v1 = p;
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 )
|
|
{
|
|
Domain domain( Domain::LINE_DOMAIN );
|
|
domain.v1 = v1;
|
|
domain.v2 = v2;
|
|
domain.r1 = (v2 - v1).length();
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 )
|
|
{
|
|
Domain domain( Domain::TRI_DOMAIN );
|
|
domain.v1 = v1;
|
|
domain.v2 = v2;
|
|
domain.v3 = v3;
|
|
domain.plane.set(v1, v2, v3);
|
|
computeNewBasis( v2-v1, v3-v1, domain.s1, domain.s2 );
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h )
|
|
{
|
|
Domain domain( Domain::RECT_DOMAIN );
|
|
domain.v1 = corner;
|
|
domain.v2 = w;
|
|
domain.v3 = h;
|
|
domain.plane.set(corner, corner+w, corner+h);
|
|
computeNewBasis( w, h, domain.s1, domain.s2 );
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::addPlaneDomain( const osg::Plane& plane )
|
|
{
|
|
Domain domain( Domain::PLANE_DOMAIN );
|
|
domain.plane.set(plane);
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::addSphereDomain( const osg::Vec3& c, float r )
|
|
{
|
|
Domain domain( Domain::SPHERE_DOMAIN );
|
|
domain.v1 = c;
|
|
domain.r1 = r;
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::addBoxDomain( const osg::Vec3& min, const osg::Vec3& max )
|
|
{
|
|
Domain domain( Domain::BOX_DOMAIN );
|
|
domain.v1 = min;
|
|
domain.v2 = max;
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2 )
|
|
{
|
|
Domain domain( Domain::DISK_DOMAIN );
|
|
domain.v1 = c;
|
|
domain.v2 = n;
|
|
domain.r1 = r1;
|
|
domain.r2 = r2;
|
|
domain.plane.set(n, c);
|
|
_domains.push_back( domain );
|
|
}
|
|
|
|
inline void DomainOperator::computeNewBasis( const osg::Vec3& u, const osg::Vec3& v, osg::Vec3& s1, osg::Vec3& s2 )
|
|
{
|
|
// Copied from David McAllister's Particle System API (http://www.particlesystems.org), pDomain.h
|
|
osg::Vec3 w = u ^ v;
|
|
float det = w.z()*u.x()*v.y() - w.z()*u.y()*v.x() - u.z()*w.x()*v.y() -
|
|
u.x()*v.z()*w.y() + v.z()*w.x()*u.y() + u.z()*v.x()*w.y();
|
|
det = 1.0f / det;
|
|
|
|
s1.set( v.y()*w.z() - v.z()*w.y(), v.z()*w.x() - v.x()*w.z(), v.x()*w.y() - v.y()*w.x() );
|
|
s1 = s1 * det;
|
|
s2.set( u.y()*w.z() - u.z()*w.y(), u.z()*w.x() - u.x()*w.z(), u.x()*w.y() - u.y()*w.x() );
|
|
s2 = s2 * (-det);
|
|
}
|
|
|
|
inline void DomainOperator::ignore( const std::string& func )
|
|
{
|
|
OSG_NOTICE << className() << ": " << func << " domain not yet implemented. " << std::endl;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
#endif
|