2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
#ifndef OSGUTIL_CULLVISITOR
|
|
|
|
#define OSGUTIL_CULLVISITOR 1
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/BoundingSphere>
|
|
|
|
#include <osg/BoundingBox>
|
|
|
|
#include <osg/Matrix>
|
|
|
|
#include <osg/Drawable>
|
|
|
|
#include <osg/StateSet>
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/Impostor>
|
2002-08-19 19:42:37 +08:00
|
|
|
#include <osg/ClearNode>
|
2001-09-20 05:19:47 +08:00
|
|
|
#include <osg/Notify>
|
2002-06-03 23:39:41 +08:00
|
|
|
#include <osg/Notify>
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
#include <osg/CullStack>
|
2002-05-28 18:24:43 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
#include <osgUtil/RenderGraph>
|
|
|
|
#include <osgUtil/RenderStage>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2001-12-17 06:20:26 +08:00
|
|
|
#include <osg/Vec3>
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
namespace osgUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic NodeVisitor implementation for rendering a scene.
|
|
|
|
* This visitor traverses the scene graph, collecting transparent and
|
|
|
|
* opaque osg::Drawables into a depth sorted transparent bin and a state
|
|
|
|
* sorted opaque bin. The opaque bin is rendered first, and then the
|
|
|
|
* transparent bin in rendered in order from the furthest osg::Drawable
|
|
|
|
* from the eye to the one nearest the eye.
|
|
|
|
*/
|
2002-06-10 19:21:21 +08:00
|
|
|
class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor, public osg::CullStack
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CullVisitor();
|
|
|
|
virtual ~CullVisitor();
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
virtual CullVisitor* cloneType() const { return new CullVisitor(); }
|
2002-02-19 07:01:09 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
virtual void reset();
|
2002-10-07 04:33:13 +08:00
|
|
|
|
2002-12-17 23:41:05 +08:00
|
|
|
virtual osg::Vec3 getEyePoint() const { return getEyeLocal(); }
|
2002-11-12 18:22:38 +08:00
|
|
|
virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
|
|
|
|
virtual float getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScale) const;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
virtual void apply(osg::Node&);
|
|
|
|
virtual void apply(osg::Geode& node);
|
|
|
|
virtual void apply(osg::Billboard& node);
|
|
|
|
virtual void apply(osg::LightSource& node);
|
2002-05-02 08:14:40 +08:00
|
|
|
virtual void apply(osg::ClipNode& node);
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
virtual void apply(osg::Group& node);
|
|
|
|
virtual void apply(osg::Transform& node);
|
2002-04-01 00:40:44 +08:00
|
|
|
virtual void apply(osg::Projection& node);
|
2001-09-20 05:19:47 +08:00
|
|
|
virtual void apply(osg::Switch& node);
|
|
|
|
virtual void apply(osg::LOD& node);
|
2002-08-19 19:42:37 +08:00
|
|
|
virtual void apply(osg::ClearNode& node);
|
2002-06-05 17:39:04 +08:00
|
|
|
virtual void apply(osg::OccluderNode& node);
|
2001-09-20 05:19:47 +08:00
|
|
|
virtual void apply(osg::Impostor& node);
|
|
|
|
|
2002-08-19 19:42:37 +08:00
|
|
|
void setClearNode(const osg::ClearNode* earthSky) { _clearNode = earthSky; }
|
|
|
|
const osg::ClearNode* getClearNode() const { return _clearNode.get(); }
|
2001-10-02 19:36:14 +08:00
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
/** Switch the creation of Impostors on or off.
|
|
|
|
* Setting active to false forces the CullVisitor to use the Impostor
|
|
|
|
* LOD children for rendering. Setting active to true forces the
|
|
|
|
* CullVisitor to create the appropriate pre-rendering stages which
|
|
|
|
* render to the ImpostorSprite's texture.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void setImpostorsActive(bool active) { _impostorActive = active; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Get whether impostors are active or not. */
|
2002-09-02 20:31:35 +08:00
|
|
|
bool getImpostorsActive() const { return _impostorActive; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Set the impostor error threshold.
|
|
|
|
* Used in calculation of whether impostors remain valid.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void setImpostorPixelErrorThreshold(float numPixels) { _impostorPixelErrorThreshold=numPixels; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Get the impostor error threshold.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
float getImpostorPixelErrorThreshold() const { return _impostorPixelErrorThreshold; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Set whether ImpsotorSprite's should be placed in a depth sorted bin for rendering.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void setDepthSortImpostorSprites(bool doDepthSort) { _depthSortImpostorSprites = doDepthSort; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Get whether ImpsotorSprite's are depth sorted bin for rendering.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
bool setDepthSortImpostorSprites() const { return _depthSortImpostorSprites; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Set the number of frames that an ImpsotorSprite's is kept whilst not being beyond,
|
|
|
|
* before being recycled.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void setNumberOfFrameToKeepImpostorSprites(int numFrames) { _numFramesToKeepImpostorSprites = numFrames; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** Get the number of frames that an ImpsotorSprite's is kept whilst not being beyond,
|
|
|
|
* before being recycled.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
int getNumberOfFrameToKeepImpostorSprites() const { return _numFramesToKeepImpostorSprites; }
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-05-15 19:27:47 +08:00
|
|
|
enum ComputeNearFarMode
|
|
|
|
{
|
|
|
|
DO_NOT_COMPUTE_NEAR_FAR = 0,
|
|
|
|
COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES,
|
|
|
|
COMPUTE_NEAR_FAR_USING_PRIMITIVES
|
|
|
|
};
|
|
|
|
|
|
|
|
void setComputeNearFarMode(ComputeNearFarMode cnfm) { _computeNearFar=cnfm; }
|
|
|
|
ComputeNearFarMode getComputeNearFarMode() const { return _computeNearFar;}
|
|
|
|
|
2003-05-02 03:56:02 +08:00
|
|
|
void setNearFarRatio(float ratio) { _nearFarRatio = ratio; }
|
|
|
|
float getNearFarRatio() const { return _nearFarRatio; }
|
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
/** Push state set on the current state group.
|
|
|
|
* If the state exists in a child state group of the current
|
|
|
|
* state group then move the current state group to that child.
|
|
|
|
* Otherwise, create a new state group for the state set, add
|
|
|
|
* it to the current state group then move the current state
|
|
|
|
* group pointer to the new state group.
|
|
|
|
*/
|
|
|
|
inline void pushStateSet(const osg::StateSet* ss)
|
|
|
|
{
|
|
|
|
_currentRenderGraph = _currentRenderGraph->find_or_insert(ss);
|
|
|
|
if (ss->useRenderBinDetails())
|
|
|
|
{
|
|
|
|
_currentRenderBin = _currentRenderBin->find_or_insert(ss->getBinNumber(),ss->getBinName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Pop the top state set and hence associated state group.
|
|
|
|
* Move the current state group to the parent of the popped
|
|
|
|
* state group.
|
|
|
|
*/
|
|
|
|
inline void popStateSet()
|
|
|
|
{
|
|
|
|
if (_currentRenderGraph->_stateset->useRenderBinDetails())
|
|
|
|
{
|
|
|
|
_currentRenderBin = _currentRenderBin->_parent;
|
|
|
|
}
|
|
|
|
_currentRenderGraph = _currentRenderGraph->_parent;
|
|
|
|
}
|
|
|
|
|
2002-05-15 19:27:47 +08:00
|
|
|
inline void setRenderGraph(RenderGraph* rg)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
_rootRenderGraph = rg;
|
|
|
|
_currentRenderGraph = rg;
|
|
|
|
}
|
|
|
|
|
2002-05-22 20:01:12 +08:00
|
|
|
inline RenderGraph* getRootRenderGraph()
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
return _rootRenderGraph.get();
|
|
|
|
}
|
|
|
|
|
2002-05-22 20:01:12 +08:00
|
|
|
inline RenderGraph* getCurrentRenderGraph()
|
|
|
|
{
|
|
|
|
return _currentRenderGraph;
|
|
|
|
}
|
|
|
|
|
2002-05-15 19:27:47 +08:00
|
|
|
inline void setRenderStage(RenderStage* rg)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
_rootRenderStage = rg;
|
|
|
|
_currentRenderBin = rg;
|
|
|
|
}
|
|
|
|
|
2002-05-15 19:27:47 +08:00
|
|
|
inline RenderStage* getRenderStage()
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
return _rootRenderStage.get();
|
|
|
|
}
|
|
|
|
|
2002-05-15 19:27:47 +08:00
|
|
|
inline RenderBin* getCurrentRenderBin()
|
|
|
|
{
|
|
|
|
return _currentRenderBin;
|
|
|
|
}
|
|
|
|
|
2002-07-10 17:04:28 +08:00
|
|
|
inline void setCurrentRenderBin(RenderBin* rb)
|
|
|
|
{
|
|
|
|
_currentRenderBin = rb;
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline float getCalculatedNearPlane() const { return _computed_znear; }
|
2002-05-15 19:27:47 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline float getCalculatedFarPlane() const { return _computed_zfar; }
|
2002-05-10 02:59:19 +08:00
|
|
|
|
2002-05-18 16:39:42 +08:00
|
|
|
void updateCalculatedNearFar(const osg::Matrix& matrix,const osg::Drawable& drawable) { updateCalculatedNearFar(matrix,drawable.getBound()); }
|
|
|
|
void updateCalculatedNearFar(const osg::Matrix& matrix,const osg::BoundingBox& bb);
|
2002-05-14 18:20:55 +08:00
|
|
|
void updateCalculatedNearFar(const osg::Vec3& pos);
|
|
|
|
|
|
|
|
/** Add a drawable to current render graph.*/
|
2003-01-10 17:25:42 +08:00
|
|
|
inline void addDrawable(osg::Drawable* drawable,osg::RefMatrix* matrix);
|
2002-05-14 18:20:55 +08:00
|
|
|
|
|
|
|
/** Add a drawable and depth to current render graph.*/
|
2003-01-10 17:25:42 +08:00
|
|
|
inline void addDrawableAndDepth(osg::Drawable* drawable,osg::RefMatrix* matrix,float depth);
|
2002-05-14 18:20:55 +08:00
|
|
|
|
|
|
|
/** Add an attribute which is positioned related to the modelview matrix.*/
|
2003-01-10 17:25:42 +08:00
|
|
|
inline void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr);
|
2002-05-10 02:59:19 +08:00
|
|
|
|
2002-06-10 19:21:21 +08:00
|
|
|
/** reimplement CullStack's popProjectionMatrix() adding clamping of the projection matrix to the computed near and far.*/
|
2003-01-10 17:25:42 +08:00
|
|
|
virtual void popProjectionMatrix();
|
2002-06-10 19:21:21 +08:00
|
|
|
|
2002-07-02 14:22:28 +08:00
|
|
|
void setState(osg::State* state) { _state = state; }
|
|
|
|
osg::State* getState() { return _state.get(); }
|
|
|
|
const osg::State* getState() const { return _state.get(); }
|
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
|
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
protected:
|
|
|
|
|
2003-06-24 23:40:09 +08:00
|
|
|
// /** prevent unwanted copy construction.*/
|
|
|
|
// CullVisitor(const CullVisitor&): osg::NodeVisitor(), osg::CullStack() {}
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
/** prevent unwanted copy operator.*/
|
|
|
|
CullVisitor& operator = (const CullVisitor&) { return *this; }
|
|
|
|
|
2002-04-15 21:15:48 +08:00
|
|
|
inline void handle_cull_callbacks_and_traverse(osg::Node& node)
|
|
|
|
{
|
|
|
|
osg::NodeCallback* callback = node.getCullCallback();
|
|
|
|
if (callback) (*callback)(&node,this);
|
|
|
|
else traverse(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void handle_cull_callbacks_and_accept(osg::Node& node,osg::Node* acceptNode)
|
|
|
|
{
|
|
|
|
osg::NodeCallback* callback = node.getCullCallback();
|
|
|
|
if (callback) (*callback)(&node,this);
|
|
|
|
else acceptNode->accept(*this);
|
|
|
|
}
|
|
|
|
|
2002-05-02 08:14:40 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
/** create an impostor sprite by setting up a pre-rendering stage
|
|
|
|
* to generate the impostor texture. */
|
|
|
|
osg::ImpostorSprite* createImpostorSprite(osg::Impostor& node);
|
|
|
|
|
2002-05-28 18:24:43 +08:00
|
|
|
osg::ref_ptr<RenderGraph> _rootRenderGraph;
|
|
|
|
RenderGraph* _currentRenderGraph;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-05-28 18:24:43 +08:00
|
|
|
osg::ref_ptr<RenderStage> _rootRenderStage;
|
|
|
|
RenderBin* _currentRenderBin;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-04-22 22:54:39 +08:00
|
|
|
ComputeNearFarMode _computeNearFar;
|
2003-05-02 03:56:02 +08:00
|
|
|
float _nearFarRatio;
|
2002-04-22 22:54:39 +08:00
|
|
|
float _computed_znear;
|
|
|
|
float _computed_zfar;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-08-19 19:42:37 +08:00
|
|
|
osg::ref_ptr<const osg::ClearNode> _clearNode;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
bool _impostorActive;
|
|
|
|
bool _depthSortImpostorSprites;
|
|
|
|
float _impostorPixelErrorThreshold;
|
|
|
|
int _numFramesToKeepImpostorSprites;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2002-05-15 19:27:47 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<RenderLeaf> > RenderLeafList;
|
|
|
|
RenderLeafList _reuseRenderLeafList;
|
|
|
|
unsigned int _currentReuseRenderLeafIndex;
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
inline RenderLeaf* createOrReuseRenderLeaf(osg::Drawable* drawable,osg::RefMatrix* projection,osg::RefMatrix* matrix, float depth=0.0f);
|
2002-05-15 19:27:47 +08:00
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
osg::ref_ptr<osg::ImpostorSpriteManager> _impostorSpriteManager;
|
2002-07-02 14:22:28 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::State> _state;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
inline void CullVisitor::addDrawable(osg::Drawable* drawable,osg::RefMatrix* matrix)
|
2002-05-15 19:27:47 +08:00
|
|
|
{
|
|
|
|
if (_currentRenderGraph->leaves_empty())
|
|
|
|
{
|
|
|
|
// this is first leaf to be added to RenderGraph
|
|
|
|
// and therefore should not already know to current render bin,
|
|
|
|
// so need to add it.
|
|
|
|
_currentRenderBin->addRenderGraph(_currentRenderGraph);
|
|
|
|
}
|
|
|
|
//_currentRenderGraph->addLeaf(new RenderLeaf(drawable,matrix));
|
|
|
|
_currentRenderGraph->addLeaf(createOrReuseRenderLeaf(drawable,_projectionStack.back().get(),matrix));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add a drawable and depth to current render graph.*/
|
2003-01-10 17:25:42 +08:00
|
|
|
inline void CullVisitor::addDrawableAndDepth(osg::Drawable* drawable,osg::RefMatrix* matrix,float depth)
|
2002-05-15 19:27:47 +08:00
|
|
|
{
|
|
|
|
if (_currentRenderGraph->leaves_empty())
|
|
|
|
{
|
|
|
|
// this is first leaf to be added to RenderGraph
|
|
|
|
// and therefore should not already know to current render bin,
|
|
|
|
// so need to add it.
|
|
|
|
_currentRenderBin->addRenderGraph(_currentRenderGraph);
|
|
|
|
}
|
|
|
|
//_currentRenderGraph->addLeaf(new RenderLeaf(drawable,matrix,depth));
|
|
|
|
_currentRenderGraph->addLeaf(createOrReuseRenderLeaf(drawable,_projectionStack.back().get(),matrix,depth));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Add an attribute which is positioned related to the modelview matrix.*/
|
2003-01-10 17:25:42 +08:00
|
|
|
inline void CullVisitor::addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr)
|
2002-05-15 19:27:47 +08:00
|
|
|
{
|
|
|
|
_currentRenderBin->_stage->addPositionedAttribute(matrix,attr);
|
|
|
|
}
|
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
inline RenderLeaf* CullVisitor::createOrReuseRenderLeaf(osg::Drawable* drawable,osg::RefMatrix* projection,osg::RefMatrix* matrix, float depth)
|
2002-05-15 19:27:47 +08:00
|
|
|
{
|
|
|
|
// skip of any already reused renderleaf.
|
|
|
|
while (_currentReuseRenderLeafIndex<_reuseRenderLeafList.size() &&
|
|
|
|
_reuseRenderLeafList[_currentReuseRenderLeafIndex]->referenceCount()>1)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Warning:createOrReuseRenderLeaf() skipping multiply refrenced entry."<< std::endl;
|
|
|
|
++_currentReuseRenderLeafIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if still within list, element must be singularly referenced
|
|
|
|
// there return it to be reused.
|
|
|
|
if (_currentReuseRenderLeafIndex<_reuseRenderLeafList.size())
|
|
|
|
{
|
|
|
|
RenderLeaf* renderleaf = _reuseRenderLeafList[_currentReuseRenderLeafIndex++].get();
|
|
|
|
renderleaf->set(drawable,projection,matrix,depth);
|
|
|
|
return renderleaf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise need to create new renderleaf.
|
2002-12-16 21:40:58 +08:00
|
|
|
RenderLeaf* renderleaf = new RenderLeaf(drawable,projection,matrix,depth);
|
2002-05-15 19:27:47 +08:00
|
|
|
_reuseRenderLeafList.push_back(renderleaf);
|
|
|
|
++_currentReuseRenderLeafIndex;
|
|
|
|
return renderleaf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|