Refactored osgParticle so that it natives support vertex arrays, vertex buffer objects and vertex array objects

This commit is contained in:
Robert Osfield
2016-09-01 17:14:03 +01:00
parent cd3a5f8097
commit 5a4754deef
15 changed files with 532 additions and 336 deletions

View File

@@ -63,6 +63,15 @@ namespace osgParticle
return osg::maximum(_minimumNumberOfParticlesToCreate, i);
}
virtual int getEstimatedMaxNumOfParticles(double lifeTime) const
{
int minNumParticles = static_cast<int>(_minimumNumberOfParticlesToCreate*60.0f*lifeTime);
int baseNumPartciles = static_cast<int>(_numberOfParticlesPerSecondToCreate * lifeTime);
return osg::maximum(minNumParticles, baseNumPartciles);
}
protected:
virtual ~ConstantRateCounter() {}

View File

@@ -30,8 +30,12 @@ namespace osgParticle
virtual const char* className() const { return "Counter"; }
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Counter* >(obj) != 0; }
/** get the number of particle of create for current frame.*/
virtual int numParticlesToCreate(double dt) const = 0;
/** get the esimated maximum number of particles that would be generated duration the lifetime of a particle before it expires.*/
virtual int getEstimatedMaxNumOfParticles(double lifeTime) const = 0;
protected:
~Counter() {}
Counter &operator=(const Counter &) { return *this; }

View File

@@ -42,6 +42,12 @@ namespace osgParticle
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Emitter*>(obj) != 0; }
virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }
void setParticleSystem(ParticleSystem* ps);
void setEstimatedMaxNumOfParticles(int num);
int getEstimatedMaxNumOfParticles() const { return _estimatedMaxNumOfParticles; }
/// Get the particle template.
inline const Particle& getParticleTemplate() const;
@@ -67,6 +73,8 @@ namespace osgParticle
bool _usedeftemp;
Particle _ptemp;
int _estimatedMaxNumOfParticles;
};
// INLINE FUNCTIONS

View File

@@ -23,9 +23,6 @@
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Matrix>
#include <osg/Drawable>
#include <osg/GL>
#include <osg/GLBeginEndAdapter>
namespace osgParticle
{
@@ -150,6 +147,13 @@ namespace osgParticle
/// Get the t texture coordinate of the bottom left of the particle
inline float getTTexCoord() const { return _t_coord; }
/// Get the texture coordinate width of the particle
inline float getSTexTile() const { return _s_tile; }
/// Get the texture coordinate height of the particle
inline float getTTexTile() const { return _t_tile; }
/// Get width of texture tile
inline int getTileS() const;
@@ -240,15 +244,6 @@ namespace osgParticle
*/
bool update(double dt, bool onlyTimeStamp);
/// Perform some pre-rendering tasks. Called automatically by particle systems.
inline void beginRender(osg::GLBeginEndAdapter* gl) const;
/// Render the particle. Called automatically by particle systems.
void render(osg::GLBeginEndAdapter* gl, const osg::Vec3& xpos, const osg::Vec3& px, const osg::Vec3& py, float scale = 1.0f) const;
/// Perform some post-rendering tasks. Called automatically by particle systems.
inline void endRender(osg::GLBeginEndAdapter* gl) const;
/// Get the current (interpolated) polygon size. Valid only after the first call to update().
inline float getCurrentSize() const;
@@ -560,36 +555,6 @@ namespace osgParticle
_massinv = 1 / m;
}
inline void Particle::beginRender(osg::GLBeginEndAdapter* gl) const
{
switch (_shape)
{
case POINT:
gl->Begin(GL_POINTS);
break;
case QUAD:
gl->Begin(GL_QUADS);
break;
case LINE:
gl->Begin(GL_LINES);
break;
default: ;
}
}
inline void Particle::endRender(osg::GLBeginEndAdapter* gl) const
{
switch (_shape)
{
case POINT:
case QUAD:
case LINE:
gl->End();
break;
default: ;
}
}
inline float Particle::getCurrentSize() const
{
return _current_size;

View File

@@ -71,7 +71,7 @@ namespace osgParticle
inline const ParticleSystem* getParticleSystem() const;
/// Set the destination particle system.
inline void setParticleSystem(ParticleSystem* ps);
virtual void setParticleSystem(ParticleSystem* ps);
/// Set the endless flag of this processor.
inline void setEndless(bool type);
@@ -148,7 +148,6 @@ namespace osgParticle
virtual void process(double dt) = 0;
private:
ReferenceFrame _rf;
bool _enabled;
double _t0;
@@ -211,10 +210,6 @@ namespace osgParticle
return _ps.get();
}
inline void ParticleProcessor::setParticleSystem(ParticleSystem* ps)
{
_ps = ps;
}
inline void ParticleProcessor::setEndless(bool type)
{

View File

@@ -267,6 +267,11 @@ namespace osgParticle
virtual osg::VertexArrayState* createVertexArrayState(osg::RenderInfo& renderInfo, bool usingVBOs) const;
void adjustEstimatedMaxNumOfParticles(int delta) { _estimatedMaxNumOfParticles += delta; }
void setEstimatedMaxNumOfParticles(int num) { _estimatedMaxNumOfParticles = num; }
int getEstimatedMaxNumOfParticles() const { return _estimatedMaxNumOfParticles; }
protected:
virtual ~ParticleSystem();
@@ -277,6 +282,8 @@ namespace osgParticle
void single_pass_render(osg::RenderInfo& renderInfo, const osg::Matrix& modelview) const;
void render_vertex_array(osg::RenderInfo& renderInfo) const;
void new_drawImplementation(osg::RenderInfo& renderInfo) const;
typedef std::vector<Particle> Particle_vector;
typedef std::stack<Particle*> Death_stack;
@@ -315,23 +322,38 @@ namespace osgParticle
SortMode _sortMode;
double _visibilityDistance;
mutable int _draw_count;
mutable ReadWriterMutex _readWriteMutex;
int _estimatedMaxNumOfParticles;
struct ArrayData
{
ArrayData();
void init();
void init3();
void reserve(unsigned int numVertices);
void resize(unsigned int numVertices);
void resizeGLObjectBuffers(unsigned int maxSize);
void releaseGLObjects(osg::State* state);
void clear();
void dirty();
void dispatchArrays(osg::State& state);
void dispatchPrimitives();
osg::ref_ptr<osg::BufferObject> vertexBufferObject;
osg::ref_ptr<osg::Vec3Array> vertices;
osg::ref_ptr<osg::Vec3Array> normals;
osg::ref_ptr<osg::Vec4Array> colors;
osg::ref_ptr<osg::Vec2Array> texcoords;
osg::ref_ptr<osg::Vec2Array> texcoords2;
osg::ref_ptr<osg::Vec3Array> texcoords3;
typedef std::pair<GLenum, unsigned int> ModeCount;
typedef std::vector<ModeCount> Primitives;
Primitives primitives;
};
typedef osg::buffered_object< ArrayData > BufferedArrayData;

View File

@@ -37,6 +37,8 @@ namespace osgParticle
inline void setRateRange(const rangef& r);
inline void setRateRange(float minrange, float maxrange);
virtual int getEstimatedMaxNumOfParticles(double lifeTime) const { return static_cast<int>(_rate_range.maximum * lifeTime); }
protected:
virtual ~VariableRateCounter() {}