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:
@@ -110,6 +110,26 @@ namespace osgParticle
|
||||
*/
|
||||
inline void setDefaultBoundingBox(const osg::BoundingBox& bbox);
|
||||
|
||||
/// Return true if we use vertex arrays for rendering particles.
|
||||
bool getUseVertexArray() const { return _useVertexArray; }
|
||||
|
||||
/** Set to use vertex arrays for rendering particles.
|
||||
Lots of variables will be omitted: particles' shape, alive or not, visibility distance, and so on,
|
||||
so the rendering result is not as good as we wish (although it's fast than using glBegin/glEnd).
|
||||
We had better use this for GLSL shaders, in which particle parameters will be kept as uniforms.
|
||||
This method is called automatically by <CODE>setDefaultAttributesUsingShaders()</CODE>.
|
||||
*/
|
||||
void setUseVertexArray(bool v) { _useVertexArray = v; }
|
||||
|
||||
/// Return true if shaders are required.
|
||||
bool getUseShaders() const { return _useShaders; }
|
||||
|
||||
/** Set to use GLSL shaders for rendering particles.
|
||||
Particles' parameters will be used as shader attribute arrays, and necessary variables, including
|
||||
the visibility distance, texture, etc, will be used and updated as uniforms.
|
||||
*/
|
||||
void setUseShaders(bool v) { _useShaders = v; _dirty_uniforms = true; }
|
||||
|
||||
/// Get the double pass rendering flag.
|
||||
inline bool getDoublePassRendering() const;
|
||||
|
||||
@@ -157,6 +177,9 @@ namespace osgParticle
|
||||
|
||||
/// Get the last frame number.
|
||||
inline int getLastFrameNumber() const;
|
||||
|
||||
/// Get the unique delta time for emitters and updaters to use
|
||||
inline double& getDeltaTime( double currentTime );
|
||||
|
||||
/// Get a reference to the default particle template.
|
||||
inline Particle& getDefaultParticleTemplate();
|
||||
@@ -178,6 +201,12 @@ namespace osgParticle
|
||||
*/
|
||||
void setDefaultAttributes(const std::string& texturefile = "", bool emissive_particles = true, bool lighting = false, int texture_unit = 0);
|
||||
|
||||
/** A useful method to set the most common <CODE>StateAttribute</CODE> and use GLSL shaders to draw particles.
|
||||
At present, when enabling shaders in the particle system, user-defined shapes will not be usable.
|
||||
If <CODE>texturefile</CODE> is empty, then texturing is turned off.
|
||||
*/
|
||||
void setDefaultAttributesUsingShaders(const std::string& texturefile = "", bool emissive_particles = true, int texture_unit = 0);
|
||||
|
||||
/// (<B>EXPERIMENTAL</B>) Get the level of detail.
|
||||
inline int getLevelOfDetail() const;
|
||||
|
||||
@@ -185,9 +214,32 @@ namespace osgParticle
|
||||
get the actual number of particles to be drawn. This value must be greater than zero.
|
||||
*/
|
||||
inline void setLevelOfDetail(int v);
|
||||
|
||||
enum SortMode
|
||||
{
|
||||
NO_SORT,
|
||||
SORT_FRONT_TO_BACK,
|
||||
SORT_BACK_TO_FRONT
|
||||
};
|
||||
|
||||
/// Get the sort mode.
|
||||
inline SortMode getSortMode() const;
|
||||
|
||||
/** Set the sort mode. It will force resorting the particle list by the Z direction of the view coordinates.
|
||||
This can be used for the purpose of transparent rendering or <CODE>setVisibilityDistance()</CODE>.
|
||||
*/
|
||||
inline void setSortMode(SortMode mode);
|
||||
|
||||
/// Get the visibility distance.
|
||||
inline double getVisibilityDistance() const;
|
||||
|
||||
/** Set the visibility distance which allows the particles to be rendered only when depth is inside the distance.
|
||||
When using shaders, it can work well directly; otherwise the sort mode should also be set to pre-compute depth.
|
||||
*/
|
||||
inline void setVisibilityDistance(double distance);
|
||||
|
||||
/// Update the particles. Don't call this directly, use a <CODE>ParticleSystemUpdater</CODE> instead.
|
||||
virtual void update(double dt);
|
||||
virtual void update(double dt, osg::NodeVisitor& nv);
|
||||
|
||||
virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
|
||||
|
||||
@@ -212,7 +264,8 @@ namespace osgParticle
|
||||
ParticleSystem& operator=(const ParticleSystem&) { return *this; }
|
||||
|
||||
inline void update_bounds(const osg::Vec3& p, float r);
|
||||
void single_pass_render(osg::State& state, const osg::Matrix& modelview) const;
|
||||
void single_pass_render(osg::RenderInfo& renderInfo, const osg::Matrix& modelview) const;
|
||||
void render_vertex_array(osg::RenderInfo& renderInfo) const;
|
||||
|
||||
typedef std::vector<Particle> Particle_vector;
|
||||
typedef std::stack<Particle*> Death_stack;
|
||||
@@ -227,6 +280,10 @@ namespace osgParticle
|
||||
osg::Vec3 _align_Y_axis;
|
||||
ParticleScaleReferenceFrame _particleScaleReferenceFrame;
|
||||
|
||||
bool _useVertexArray;
|
||||
bool _useShaders;
|
||||
bool _dirty_uniforms;
|
||||
|
||||
bool _doublepass;
|
||||
bool _frozen;
|
||||
|
||||
@@ -238,9 +295,16 @@ namespace osgParticle
|
||||
|
||||
Particle _def_ptemp;
|
||||
mutable int _last_frame;
|
||||
mutable bool _dirty_dt;
|
||||
bool _freeze_on_cull;
|
||||
|
||||
double _t0;
|
||||
double _dt;
|
||||
|
||||
int _detail;
|
||||
SortMode _sortMode;
|
||||
double _visibilityDistance;
|
||||
|
||||
mutable int _draw_count;
|
||||
|
||||
mutable ReadWriterMutex _readWriteMutex;
|
||||
@@ -343,6 +407,19 @@ namespace osgParticle
|
||||
{
|
||||
return _last_frame;
|
||||
}
|
||||
|
||||
inline double& ParticleSystem::getDeltaTime( double currentTime )
|
||||
{
|
||||
if ( _dirty_dt )
|
||||
{
|
||||
_dt = currentTime - _t0;
|
||||
if ( _dt<0.0 ) _dt = 0.0;
|
||||
|
||||
_t0 = currentTime;
|
||||
_dirty_dt = false;
|
||||
}
|
||||
return _dt;
|
||||
}
|
||||
|
||||
inline void ParticleSystem::update_bounds(const osg::Vec3& p, float r)
|
||||
{
|
||||
@@ -398,6 +475,27 @@ namespace osgParticle
|
||||
_detail = v;
|
||||
}
|
||||
|
||||
inline ParticleSystem::SortMode ParticleSystem::getSortMode() const
|
||||
{
|
||||
return _sortMode;
|
||||
}
|
||||
|
||||
inline void ParticleSystem::setSortMode(SortMode mode)
|
||||
{
|
||||
_sortMode = mode;
|
||||
}
|
||||
|
||||
inline double ParticleSystem::getVisibilityDistance() const
|
||||
{
|
||||
return _visibilityDistance;
|
||||
}
|
||||
|
||||
inline void ParticleSystem::setVisibilityDistance(double distance)
|
||||
{
|
||||
_visibilityDistance = distance;
|
||||
if (_useShaders) _dirty_uniforms = true;
|
||||
}
|
||||
|
||||
// I'm not sure this function should be inlined...
|
||||
|
||||
inline Particle* ParticleSystem::createParticle(const Particle* ptemplate)
|
||||
|
||||
Reference in New Issue
Block a user