Form Wang Rui, "An initial GLSL shader support of rendering particles. Only the POINT

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.
"
This commit is contained in:
Robert Osfield
2010-09-14 15:47:29 +00:00
parent 551d2b6479
commit b4789863ac
50 changed files with 3001 additions and 130 deletions

View File

@@ -28,7 +28,7 @@ osgParticle::Particle::Particle()
_si(new LinearInterpolator),
_ai(new LinearInterpolator),
_ci(new LinearInterpolator),
_alive(true),
//_alive(true),
_mustdie(false),
_lifeTime(2),
_radius(0.2f),
@@ -41,8 +41,8 @@ osgParticle::Particle::Particle()
_angle(0, 0, 0),
_angul_arvel(0, 0, 0),
_t0(0),
_current_size(0),
_current_alpha(0),
//_current_size(0),
//_current_alpha(0),
_s_tile(1.0f),
_t_tile(1.0f),
_start_tile(0),
@@ -51,16 +51,18 @@ osgParticle::Particle::Particle()
_s_coord(0.0f),
_t_coord(0.0f),
_previousParticle(INVALID_INDEX),
_nextParticle(INVALID_INDEX)
_nextParticle(INVALID_INDEX),
_depth(0.0)
{
_base_prop.set(1.0f, 0.0f, 0.0f);
}
bool osgParticle::Particle::update(double dt)
bool osgParticle::Particle::update(double dt, bool onlyTimeStamp)
{
// this method should return false when the particle dies;
// so, if we were instructed to die, do it now and return.
if (_mustdie) {
_alive = false;
_base_prop.x() = -1.0;
return false;
}
@@ -75,9 +77,30 @@ bool osgParticle::Particle::update(double dt)
// if our age is over the lifetime limit, then die and return.
if (x > 1) {
_alive = false;
_base_prop.x() = -1.0;
return false;
}
// compute the current values for size, alpha and color.
if (_lifeTime <= 0) {
if (dt == _t0) {
_base_prop.y() = _sr.get_random();
_base_prop.z() = _ar.get_random();
_current_color = _cr.get_random();
}
} else {
_base_prop.y() = _si.get()->interpolate(x, _sr);
_base_prop.z() = _ai.get()->interpolate(x, _ar);
_current_color = _ci.get()->interpolate(x, _cr);
}
// update position
_prev_pos = _position;
_position += _velocity * dt;
// return now if we indicate that only time stamp should be updated
// the shader will handle remain properties in this case
if (onlyTimeStamp) return true;
//Compute the current texture tile based on our normalized age
int currentTile = _start_tile + static_cast<int>(x * getNumTiles());
@@ -93,23 +116,6 @@ bool osgParticle::Particle::update(double dt)
// OSG_NOTICE<<this<<" setting tex coords "<<_s_coord<<" "<<_t_coord<<std::endl;
}
// compute the current values for size, alpha and color.
if (_lifeTime <= 0) {
if (dt == _t0) {
_current_size = _sr.get_random();
_current_alpha = _ar.get_random();
_current_color = _cr.get_random();
}
} else {
_current_size = _si.get()->interpolate(x, _sr);
_current_alpha = _ai.get()->interpolate(x, _ar);
_current_color = _ci.get()->interpolate(x, _cr);
}
// update position
_prev_pos = _position;
_position += _velocity * dt;
// update angle
_prev_angle = _angle;
_angle += _angul_arvel * dt;
@@ -129,10 +135,10 @@ void osgParticle::Particle::render(osg::GLBeginEndAdapter* gl, const osg::Vec3&
gl->Color4f( _current_color.x(),
_current_color.y(),
_current_color.z(),
_current_color.w() * _current_alpha);
_current_color.w() * _base_prop.z());
osg::Vec3 p1(px * _current_size * scale);
osg::Vec3 p2(py * _current_size * scale);
osg::Vec3 p1(px * _base_prop.y() * scale);
osg::Vec3 p2(py * _base_prop.y() * scale);
switch (_shape)
{
@@ -199,7 +205,7 @@ void osgParticle::Particle::render(osg::GLBeginEndAdapter* gl, const osg::Vec3&
// calculation of one of the linesegment endpoints.
float vl = _velocity.length();
if (vl != 0) {
osg::Vec3 v = _velocity * _current_size * scale / vl;
osg::Vec3 v = _velocity * _base_prop.y() * scale / vl;
gl->TexCoord1f(0);
gl->Vertex3f(xpos.x(), xpos.y(), xpos.z());
@@ -214,11 +220,36 @@ void osgParticle::Particle::render(osg::GLBeginEndAdapter* gl, const osg::Vec3&
}
}
void osgParticle::Particle::render(osg::RenderInfo& renderInfo, const osg::Vec3& xpos, const osg::Vec3& xrot) const
{
#if defined(OSG_GL_MATRICES_AVAILABLE)
if (_drawable.valid())
{
bool requiresRotation = (xrot.x()!=0.0f || xrot.y()!=0.0f || xrot.z()!=0.0f);
glColor4f(_current_color.x(),
_current_color.y(),
_current_color.z(),
_current_color.w() * _base_prop.z());
glPushMatrix();
glTranslatef(xpos.x(), xpos.y(), xpos.z());
if (requiresRotation)
{
osg::Quat rotation(xrot.x(), osg::X_AXIS, xrot.y(), osg::Y_AXIS, xrot.z(), osg::Z_AXIS);
glMultMatrixd(osg::Matrixd(rotation).ptr());
}
_drawable->draw(renderInfo);
glPopMatrix();
}
#else
OSG_NOTICE<<"Warning: Particle::render(..) not supported for user-defined shape."<<std::endl;
#endif
}
void osgParticle::Particle::setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem* ps)
{
if (getPreviousParticle()!=Particle::INVALID_INDEX)
{
update(0.0);
update(0.0, false);
Particle* previousParticle = ps->getParticle(getPreviousParticle());
const osg::Vec3& previousPosition = previousParticle->getPosition();