Moved osg::Impostor to osgSim::Impostor, as Impostor isn't a core feature.
This commit is contained in:
118
include/osgSim/Impostor
Normal file
118
include/osgSim/Impostor
Normal file
@@ -0,0 +1,118 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 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.
|
||||
*/
|
||||
|
||||
#ifndef OSGSIM_IMPOSTOR
|
||||
#define OSGSIM_IMPOSTOR 1
|
||||
|
||||
#include <osg/LOD>
|
||||
#include <osg/buffered_value>
|
||||
|
||||
#include <osgSim/ImpostorSprite>
|
||||
|
||||
namespace osgSim {
|
||||
|
||||
/** Impostor - is a form of Level Of Detail group node which allows both switching
|
||||
* between children depending on distance from eye point and image caching.
|
||||
*
|
||||
* The principle behind Imposters is that they cache an image of real geometry and then the image is drawn
|
||||
* in subsequent frames instead of the real geometry. It's a bit like a
|
||||
* Billboard *but* is updated at runtime and w.r.t view point. By drawing
|
||||
* just the texture mapped quad you can cut down scene complexity and
|
||||
* improve performance.
|
||||
*
|
||||
* For more details have a look at:
|
||||
*
|
||||
* http://grail.cs.washington.edu/projects/hic/
|
||||
*
|
||||
* The OSG doesn't implement exactly the same technique as above, but its
|
||||
* should be a good starting place. The OSG's impostors are much less
|
||||
* intrusive since you don't need to restructure your whole scene to use
|
||||
* them.
|
||||
*
|
||||
* All you need to do to use Impostors is to set up the visible
|
||||
* range values for each LOD child of the Impostor, as per osg::LOD,
|
||||
* and set an Impostor threshold to tell the renderer at what distance
|
||||
* the Impostor's image caching should cut in. The osg::CullVisitor
|
||||
* automatically handles all the setting of pre-rendering stages to
|
||||
* calculate the required ImpostorSprites (which encapsulates the image
|
||||
* cache and quad), and updates them as the view point changes. If you
|
||||
* use osg::SceneView/CullVisitor all the complexity of supporting
|
||||
* Impostor will be nicely hidden away.
|
||||
*
|
||||
* TODO:
|
||||
* Various improvements are planned for the Impostor-
|
||||
* 1) Estimation of how many frames an ImpostorSprite will be reused, if
|
||||
* it won't be used more often than a minimum threshold then do not create
|
||||
* ImpostorSprite - use the real geometry.
|
||||
* 2) Sharing of texture memory between ImpostorSprites.
|
||||
* 3) Simple 3D geometry for ImpostorSprite's rather than Billboarding.
|
||||
* 4) Shrinking of the ImpostorSprite size to more closely fit the underlying
|
||||
* geometry.
|
||||
*/
|
||||
class OSGSIM_EXPORT Impostor : public osg::LOD
|
||||
{
|
||||
public :
|
||||
Impostor();
|
||||
|
||||
Impostor(const Impostor& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
|
||||
osg::LOD(es,copyop),
|
||||
_impostorThreshold(es._impostorThreshold) {}
|
||||
|
||||
META_Node(osgSim, Impostor);
|
||||
|
||||
virtual void traverse(osg::NodeVisitor& nv);
|
||||
|
||||
typedef std::vector< osg::ref_ptr<ImpostorSprite> > ImpostorSpriteList;
|
||||
|
||||
/** Set the Impostor threshold distance.
|
||||
* For eye points further than this threshold the Imposter is used if appropriate,
|
||||
* otherwise the LOD children as chosen as per a standard LOD node.
|
||||
*/
|
||||
inline void setImpostorThreshold(float distance) { _impostorThreshold = distance; }
|
||||
|
||||
/* Get the Impostor threshold distance. */
|
||||
inline float getImpostorThreshold() const { return _impostorThreshold; }
|
||||
|
||||
/** Set the Impostor threshold distance relative to the node's bounding
|
||||
* sphere's radius.
|
||||
*/
|
||||
inline void setImpostorThresholdToBound(float ratio=1.0f) { _impostorThreshold = getBound().radius()*ratio; }
|
||||
|
||||
/** Find the ImposterSprite which fits the current eye point best. */
|
||||
ImpostorSprite* findBestImpostorSprite(unsigned int contextID, const osg::Vec3& currLocalEyePoint) const;
|
||||
|
||||
/** Add an ImpostorSprite to the Impostor. */
|
||||
void addImpostorSprite(unsigned int contextID, ImpostorSprite* is);
|
||||
|
||||
/** Get the list of ImpostorSprites attached to this Impostor. */
|
||||
inline ImpostorSpriteList& getImpostorSpriteList(unsigned int contexID) { return _impostorSpriteListBuffer[contexID]; }
|
||||
|
||||
/** Get a const list of ImpostorSprites attached to this const Impostor. */
|
||||
inline const ImpostorSpriteList& getImpostorSpriteList(unsigned int contexID) const { return _impostorSpriteListBuffer[contexID]; }
|
||||
|
||||
protected :
|
||||
|
||||
virtual ~Impostor() {}
|
||||
|
||||
virtual bool computeBound() const;
|
||||
|
||||
mutable osg::buffered_object<ImpostorSpriteList> _impostorSpriteListBuffer;
|
||||
|
||||
|
||||
float _impostorThreshold;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
228
include/osgSim/ImpostorSprite
Normal file
228
include/osgSim/ImpostorSprite
Normal file
@@ -0,0 +1,228 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 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.
|
||||
*/
|
||||
|
||||
#ifndef OSG_ImpostorSprite
|
||||
#define OSG_ImpostorSprite 1
|
||||
|
||||
#include <osg/Vec2>
|
||||
#include <osg/BoundingSphere>
|
||||
#include <osg/Drawable>
|
||||
#include <osg/AlphaFunc>
|
||||
#include <osg/TexEnv>
|
||||
#include <osg/Texture2D>
|
||||
|
||||
#include <osgSim/Export>
|
||||
|
||||
namespace osgSim {
|
||||
|
||||
class Impostor;
|
||||
class ImpostorSpriteManager;
|
||||
|
||||
/** An ImposterSprite is a textured quad which is rendered in place of
|
||||
* 3D geometry. The ImposterSprite is generated by rendering the original
|
||||
* 3D geometry to a texture as an image cache. The ImpostorSprite is
|
||||
* automatically generated by the osgUtil::CullVisitor so it not
|
||||
* necessary to deal with it directly.
|
||||
*/
|
||||
class OSGSIM_EXPORT ImpostorSprite : public osg::Drawable
|
||||
{
|
||||
public:
|
||||
|
||||
ImpostorSprite();
|
||||
|
||||
/** Clone an object of the same type as an ImpostorSprite. */
|
||||
virtual osg::Object* cloneType() const { return new ImpostorSprite(); }
|
||||
|
||||
/** Clone on ImpostorSprite just returns a clone of type,
|
||||
* since it is not appropriate to share data of an ImpostorSprite.
|
||||
*/
|
||||
virtual osg::Object* clone(const osg::CopyOp&) const { return new ImpostorSprite(); }
|
||||
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ImpostorSprite*>(obj)!=NULL; }
|
||||
virtual const char* libraryName() const { return "osgSim"; }
|
||||
virtual const char* className() const { return "ImpostorSprite"; }
|
||||
|
||||
/** Set the parent, which must be an Impostor.
|
||||
* Unlike conventional Drawables, ImpostorSprites can only ever have
|
||||
* one parent.
|
||||
*/
|
||||
void setParent(Impostor* parent) { _parent = parent; }
|
||||
|
||||
/** Get the parent, which is an Impostor. */
|
||||
Impostor* getParent() { return _parent; }
|
||||
|
||||
/** Get the const parent, which is an Impostor. */
|
||||
const Impostor* getParent() const { return _parent; }
|
||||
|
||||
/** Set the eye point for when the ImpostorSprite was snapped. */
|
||||
inline void setStoredLocalEyePoint(const osg::Vec3& v) { _storedLocalEyePoint=v; }
|
||||
|
||||
/** Get the eye point for when the ImpostorSprite was snapped. */
|
||||
inline const osg::Vec3& getStoredLocalEyePoint() const { return _storedLocalEyePoint; }
|
||||
|
||||
/** Set the frame number for when the ImpostorSprite was last used in rendering. */
|
||||
inline void setLastFrameUsed(int frameNumber) { _lastFrameUsed = frameNumber; }
|
||||
|
||||
/** Get the frame number for when the ImpostorSprite was last used in rendering. */
|
||||
inline int getLastFrameUsed() const { return _lastFrameUsed; }
|
||||
|
||||
|
||||
/** Get the coordinates of the corners of the quad.
|
||||
* Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
|
||||
*/
|
||||
inline osg::Vec3* getCoords() { return _coords; }
|
||||
|
||||
/** Get the const coordinates of the corners of the quad. */
|
||||
inline const osg::Vec3* getCoords() const { return _coords; }
|
||||
|
||||
|
||||
|
||||
/** Get the texture coordinates of the corners of the quad.
|
||||
* Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
|
||||
*/
|
||||
inline osg::Vec2* getTexCoords() { return _texcoords; }
|
||||
|
||||
/** Get the const texture coordinates of the corners of the quad. */
|
||||
inline const osg::Vec2* getTexCoords() const { return _texcoords; }
|
||||
|
||||
/** Get the control coordinates of the corners of the quad.
|
||||
* The control coordinates are the corners of the quad projected
|
||||
* out onto the front face of bounding box which enclosed the impostor
|
||||
* geometry when it was pre-rendered into the impostor sprite's texture.
|
||||
* At the point of creation/or update of the impostor sprite the control
|
||||
* coords will lie on top of the corners of the quad in screen space - with a pixel error
|
||||
* of zero. Once the camera moves relative to the impostor sprite the
|
||||
* control coords will no longer lie on top of the corners of the quad in
|
||||
* screen space - a pixel error will have accumulated. This pixel error
|
||||
* can then be used to determine whether the impostor needs to be updated.
|
||||
* Stored in the order, [0] - top_left, [1] - bottom_left, [2] - bottom_right, [3] - top_left.
|
||||
*/
|
||||
inline osg::Vec3* getControlCoords() { return _controlcoords; }
|
||||
|
||||
/** Get the const control coordinates of the corners of the quad. */
|
||||
inline const osg::Vec3* getControlCoords() const { return _controlcoords; }
|
||||
|
||||
|
||||
/** Calculate the pixel error value for passing in the ModelViewProjectionWindow transform,
|
||||
* which transform local coords into screen space.
|
||||
*/
|
||||
float calcPixelError(const osg::Matrix& MVPW) const;
|
||||
|
||||
void setTexture(osg::Texture2D* tex,int s,int t);
|
||||
osg::Texture2D* getTexture() { return _texture; }
|
||||
const osg::Texture2D* getTexture() const { return _texture; }
|
||||
|
||||
int s() const { return _s; }
|
||||
int t() const { return _t; }
|
||||
|
||||
/** Draw ImpostorSprite directly. */
|
||||
virtual void drawImplementation(osg::State& state) const;
|
||||
|
||||
/** Return true, osg::ImpostorSprite does support accept(AttributeFunctor&). */
|
||||
virtual bool supports(const AttributeFunctor&) const { return true; }
|
||||
|
||||
/** Accept an AttributeFunctor and call its methods to tell it about the interal attributes that this Drawable has. */
|
||||
virtual void accept(AttributeFunctor& af);
|
||||
|
||||
/** Return true, osg::ImpostorSprite does support accept(ConstAttributeFunctor&). */
|
||||
virtual bool supports(const ConstAttributeFunctor&) const { return true; }
|
||||
|
||||
/** Accept a ConstAttributeFunctor and call its methods to tell it about the interal attributes that this Drawable has. */
|
||||
virtual void accept(ConstAttributeFunctor& af) const;
|
||||
|
||||
/** Return true, osg::ImpostorSprite does support accept(PrimitiveFunctor&). */
|
||||
virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
|
||||
|
||||
/** Accept a PrimtiveFunctor and call its methods to tell it about the interal primtives that this Drawable has. */
|
||||
virtual void accept(osg::PrimitiveFunctor& pf) const;
|
||||
|
||||
// for debugging purposes.
|
||||
osg::Vec4 _color;
|
||||
|
||||
protected:
|
||||
|
||||
ImpostorSprite(const ImpostorSprite&):Drawable() {}
|
||||
ImpostorSprite& operator = (const ImpostorSprite&) { return *this;}
|
||||
|
||||
virtual ~ImpostorSprite();
|
||||
|
||||
virtual bool computeBound() const;
|
||||
|
||||
Impostor* _parent;
|
||||
|
||||
friend class osgSim::ImpostorSpriteManager;
|
||||
|
||||
// support for a double linked list managed by the
|
||||
// ImposotorSpriteManager.
|
||||
ImpostorSpriteManager* _ism;
|
||||
ImpostorSprite* _previous;
|
||||
ImpostorSprite* _next;
|
||||
|
||||
int _lastFrameUsed;
|
||||
|
||||
osg::Vec3 _storedLocalEyePoint;
|
||||
|
||||
osg::Vec3 _coords[4];
|
||||
osg::Vec2 _texcoords[4];
|
||||
osg::Vec3 _controlcoords[4];
|
||||
|
||||
osg::Texture2D* _texture;
|
||||
int _s;
|
||||
int _t;
|
||||
|
||||
|
||||
};
|
||||
|
||||
/** Helper class for managing the reuse of ImpostorSprite resources. */
|
||||
class OSGSIM_EXPORT ImpostorSpriteManager : public osg::Referenced
|
||||
{
|
||||
public:
|
||||
|
||||
ImpostorSpriteManager();
|
||||
|
||||
bool empty() const { return _first==0; }
|
||||
|
||||
ImpostorSprite* first() { return _first; }
|
||||
|
||||
ImpostorSprite* last() { return _last; }
|
||||
|
||||
void push_back(ImpostorSprite* is);
|
||||
|
||||
void remove(ImpostorSprite* is);
|
||||
|
||||
ImpostorSprite* createOrReuseImpostorSprite(int s,int t,int frameNumber);
|
||||
|
||||
osg::StateSet* createOrReuseStateSet();
|
||||
|
||||
void reset();
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
~ImpostorSpriteManager();
|
||||
|
||||
osg::ref_ptr<osg::TexEnv> _texenv;
|
||||
osg::ref_ptr<osg::AlphaFunc> _alphafunc;
|
||||
|
||||
ImpostorSprite* _first;
|
||||
ImpostorSprite* _last;
|
||||
|
||||
typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSetList;
|
||||
StateSetList _stateSetList;
|
||||
unsigned int _reuseStateSetIndex;
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
66
include/osgSim/InsertImpostorsVisitor
Normal file
66
include/osgSim/InsertImpostorsVisitor
Normal file
@@ -0,0 +1,66 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 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.
|
||||
*/
|
||||
|
||||
#ifndef OSGSIM_INSERTIMPOSTORSVISITOR
|
||||
#define OSGSIM_INSERTIMPOSTORSVISITOR
|
||||
|
||||
#include <osg/NodeVisitor>
|
||||
#include <osgSim/Impostor>
|
||||
|
||||
namespace osgSim {
|
||||
|
||||
/** Insert impostor nodes into scene graph.
|
||||
* For example of usage see examples/osgimpostor.
|
||||
*/
|
||||
class OSGSIM_EXPORT InsertImpostorsVisitor : public osg::NodeVisitor
|
||||
{
|
||||
public:
|
||||
|
||||
/** Default to traversing all children. */
|
||||
InsertImpostorsVisitor();
|
||||
|
||||
void setImpostorThresholdRatio(float ratio) { _impostorThresholdRatio = ratio; }
|
||||
float getImpostorThresholdRatio() const { return _impostorThresholdRatio; }
|
||||
|
||||
void setMaximumNumberOfNestedImpostors(unsigned int num) { _maximumNumNestedImpostors = num; }
|
||||
unsigned int getMaximumNumberOfNestedImpostors() const { return _maximumNumNestedImpostors; }
|
||||
|
||||
/** Empty visitor, make it ready for next traversal. */
|
||||
void reset();
|
||||
|
||||
virtual void apply(osg::Node& node);
|
||||
|
||||
virtual void apply(osg::Group& node);
|
||||
|
||||
virtual void apply(osg::LOD& node);
|
||||
|
||||
/* Insert the required impostors into the scene graph. */
|
||||
void insertImpostors();
|
||||
|
||||
protected:
|
||||
|
||||
typedef std::vector< osg::Group* > GroupList;
|
||||
typedef std::vector< osg::LOD* > LODList;
|
||||
|
||||
GroupList _groupList;
|
||||
LODList _lodList;
|
||||
|
||||
float _impostorThresholdRatio;
|
||||
unsigned int _maximumNumNestedImpostors;
|
||||
unsigned int _numNestedImpostors;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user