OpenSceneGraph/include/osgUtil/CullVisitor

385 lines
15 KiB
Plaintext
Raw Normal View History

/* -*-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.
*/
2001-09-22 10:42:08 +08:00
#ifndef OSGUTIL_CULLVISITOR
#define OSGUTIL_CULLVISITOR 1
2001-09-20 05:19:47 +08:00
#include <map>
#include <vector>
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/ClearNode>
#include <osg/CameraNode>
2001-09-20 05:19:47 +08:00
#include <osg/Notify>
#include <osg/CullStack>
#include <osgUtil/StateGraph>
2001-09-20 05:19:47 +08:00
#include <osgUtil/RenderStage>
#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 is rendered in order from the furthest osg::Drawable
2001-09-20 05:19:47 +08:00
* from the eye to the one nearest the eye.
*/
class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor, public osg::CullStack
2001-09-20 05:19:47 +08:00
{
public:
typedef osg::Matrix::value_type value_type;
2001-09-20 05:19:47 +08:00
CullVisitor();
virtual ~CullVisitor();
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();
virtual osg::Vec3 getEyePoint() const { return getEyeLocal(); }
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);
virtual void apply(osg::ClipNode& node);
virtual void apply(osg::TexGenNode& node);
2001-09-20 05:19:47 +08:00
virtual void apply(osg::Group& node);
virtual void apply(osg::Transform& node);
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);
virtual void apply(osg::ClearNode& node);
virtual void apply(osg::CameraNode& node);
virtual void apply(osg::OccluderNode& node);
2001-09-20 05:19:47 +08:00
void setClearNode(const osg::ClearNode* earthSky) { _clearNode = earthSky; }
const osg::ClearNode* getClearNode() const { return _clearNode.get(); }
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)
{
_currentStateGraph = _currentStateGraph->find_or_insert(ss);
2001-09-20 05:19:47 +08:00
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 (_currentStateGraph->_stateset->useRenderBinDetails())
2001-09-20 05:19:47 +08:00
{
_currentRenderBin = _currentRenderBin->getParent();
2001-09-20 05:19:47 +08:00
}
_currentStateGraph = _currentStateGraph->_parent;
2001-09-20 05:19:47 +08:00
}
inline void setStateGraph(StateGraph* rg)
2001-09-20 05:19:47 +08:00
{
_rootStateGraph = rg;
_currentStateGraph = rg;
2001-09-20 05:19:47 +08:00
}
inline StateGraph* getRootStateGraph()
2001-09-20 05:19:47 +08:00
{
return _rootStateGraph.get();
2001-09-20 05:19:47 +08:00
}
inline StateGraph* getCurrentStateGraph()
{
return _currentStateGraph;
}
inline void setRenderStage(RenderStage* rg)
2001-09-20 05:19:47 +08:00
{
_rootRenderStage = rg;
_currentRenderBin = rg;
}
inline RenderStage* getRenderStage()
2001-09-20 05:19:47 +08:00
{
return _rootRenderStage.get();
}
inline RenderBin* getCurrentRenderBin()
{
return _currentRenderBin;
}
2002-07-10 17:04:28 +08:00
inline void setCurrentRenderBin(RenderBin* rb)
{
_currentRenderBin = rb;
}
inline value_type getCalculatedNearPlane() const { return _computed_znear; }
inline value_type getCalculatedFarPlane() const { return _computed_zfar; }
value_type computeNearestPointInFrustum(const osg::Matrix& matrix, const osg::Polytope::PlaneList& planes,const osg::Drawable& drawable);
bool updateCalculatedNearFar(const osg::Matrix& matrix,const osg::BoundingBox& bb);
bool updateCalculatedNearFar(const osg::Matrix& matrix,const osg::Drawable& drawable, bool isBillboard=false);
void updateCalculatedNearFar(const osg::Vec3& pos);
/** Add a drawable to current render graph.*/
inline void addDrawable(osg::Drawable* drawable,osg::RefMatrix* matrix);
/** Add a drawable and depth to current render graph.*/
inline void addDrawableAndDepth(osg::Drawable* drawable,osg::RefMatrix* matrix,float depth);
/** Add an attribute which is positioned relative to the modelview matrix.*/
inline void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr);
/** Add an attribute which is positioned relative to the modelview matrix.*/
inline void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr);
/** Re-implement CullStack's popProjectionMatrix() adding clamping of the projection matrix to
* the computed near and far.*/
virtual void popProjectionMatrix();
/** CullVisitor's default clamping of the projection float matrix to computed near and far values.
* Note, do not call this method directly, use clampProjectionMatrix(..) instead, unless you want to bypass the callback.*/
2004-05-26 04:35:14 +08:00
virtual bool clampProjectionMatrixImplementation(osg::Matrixf& projection, double& znear, double& zfar) const;
/** CullVisitor's default clamping of the projection double matrix to computed near and far values.
* Note, do not call this method directly, use clampProjectionMatrix(..) instead, unless you want to bypass the callback.*/
2004-05-26 04:35:14 +08:00
virtual bool clampProjectionMatrixImplementation(osg::Matrixd& projection, double& znear, double& zfar) const;
/** Clamp the projection float matrix to computed near and far values, use callback if it exists,
* otherwise use default CullVisitor implementation.*/
2004-05-26 04:35:14 +08:00
inline bool clampProjectionMatrix(osg::Matrixf& projection, value_type& znear, value_type& zfar) const
{
double zn = znear;
double zf = zfar;
bool result = false;
if (_clampProjectionMatrixCallback.valid()) result = _clampProjectionMatrixCallback->clampProjectionMatrixImplementation(projection, zn, zf);
else result = clampProjectionMatrixImplementation(projection, zn, zf);
if (result)
{
znear = zn;
zfar = zn;
return true;
}
else
return false;
}
/** Clamp the projection double matrix to computed near and far values, use callback if it exists,
* otherwise use default CullVisitor implementation.*/
inline bool clampProjectionMatrix(osg::Matrixd& projection, value_type& znear, value_type& zfar) const
{
double zn = znear;
double zf = zfar;
bool result = false;
if (_clampProjectionMatrixCallback.valid()) result = _clampProjectionMatrixCallback->clampProjectionMatrixImplementation(projection, zn, zf);
else result = clampProjectionMatrixImplementation(projection, zn, zf);
if (result)
{
znear = zn;
zfar = zn;
return true;
}
else
return false;
}
void setState(osg::State* state) { _state = state; }
osg::State* getState() { return _state.get(); }
const osg::State* getState() const { return _state.get(); }
2001-09-20 05:19:47 +08:00
protected:
/** Prevent unwanted copy operator.*/
2001-09-20 05:19:47 +08:00
CullVisitor& operator = (const CullVisitor&) { return *this; }
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);
}
osg::ref_ptr<const osg::ClearNode> _clearNode;
osg::ref_ptr<StateGraph> _rootStateGraph;
StateGraph* _currentStateGraph;
2001-09-20 05:19:47 +08:00
osg::ref_ptr<RenderStage> _rootRenderStage;
RenderBin* _currentRenderBin;
2001-09-20 05:19:47 +08:00
value_type _computed_znear;
value_type _computed_zfar;
2001-09-20 05:19:47 +08:00
typedef std::vector< osg::ref_ptr<RenderLeaf> > RenderLeafList;
RenderLeafList _reuseRenderLeafList;
unsigned int _currentReuseRenderLeafIndex;
inline RenderLeaf* createOrReuseRenderLeaf(osg::Drawable* drawable,osg::RefMatrix* projection,osg::RefMatrix* matrix, float depth=0.0f);
osg::ref_ptr<osg::State> _state;
struct MatrixPlanesDrawables
{
MatrixPlanesDrawables(const osg::Matrix& matrix, const osg::Drawable* drawable, const osg::Polytope& frustum):
_matrix(matrix),
_drawable(drawable)
{
// create a new list of planes from the active walls of the frustum.
osg::Polytope::ClippingMask result_mask = frustum.getResultMask();
osg::Polytope::ClippingMask selector_mask = 0x1;
for(osg::Polytope::PlaneList::const_iterator itr=frustum.getPlaneList().begin();
itr!=frustum.getPlaneList().end();
++itr)
{
if (result_mask&selector_mask) _planes.push_back(*itr);
selector_mask <<= 1;
}
}
MatrixPlanesDrawables(const MatrixPlanesDrawables& mpd):
_matrix(mpd._matrix),
_drawable(mpd._drawable),
_planes(mpd._planes) {}
MatrixPlanesDrawables& operator = (const MatrixPlanesDrawables& mpd)
{
_matrix = mpd._matrix;
_drawable = mpd._drawable;
_planes = mpd._planes;
return *this;
}
osg::Matrix _matrix;
const osg::Drawable* _drawable;
osg::Polytope::PlaneList _planes;
};
typedef std::multimap<value_type, MatrixPlanesDrawables> DistanceMatrixDrawableMap;
DistanceMatrixDrawableMap _nearPlaneCandidateMap;
2001-09-20 05:19:47 +08:00
};
inline void CullVisitor::addDrawable(osg::Drawable* drawable,osg::RefMatrix* matrix)
{
if (_currentStateGraph->leaves_empty())
{
// this is first leaf to be added to StateGraph
// and therefore should not already know to current render bin,
// so need to add it.
_currentRenderBin->addStateGraph(_currentStateGraph);
}
//_currentStateGraph->addLeaf(new RenderLeaf(drawable,matrix));
_currentStateGraph->addLeaf(createOrReuseRenderLeaf(drawable,_projectionStack.back().get(),matrix));
}
/** Add a drawable and depth to current render graph.*/
inline void CullVisitor::addDrawableAndDepth(osg::Drawable* drawable,osg::RefMatrix* matrix,float depth)
{
if (_currentStateGraph->leaves_empty())
{
// this is first leaf to be added to StateGraph
// and therefore should not already know to current render bin,
// so need to add it.
_currentRenderBin->addStateGraph(_currentStateGraph);
}
//_currentStateGraph->addLeaf(new RenderLeaf(drawable,matrix,depth));
_currentStateGraph->addLeaf(createOrReuseRenderLeaf(drawable,_projectionStack.back().get(),matrix,depth));
}
/** Add an attribute which is positioned relative to the modelview matrix.*/
inline void CullVisitor::addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr)
{
_currentRenderBin->getStage()->addPositionedAttribute(matrix,attr);
}
/** Add an attribute which is positioned relative to the modelview matrix.*/
inline void CullVisitor::addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr)
{
_currentRenderBin->getStage()->addPositionedTextureAttribute(textureUnit,matrix,attr);
}
inline RenderLeaf* CullVisitor::createOrReuseRenderLeaf(osg::Drawable* drawable,osg::RefMatrix* projection,osg::RefMatrix* matrix, float depth)
{
// Skips 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 then 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.
RenderLeaf* renderleaf = new RenderLeaf(drawable,projection,matrix,depth);
_reuseRenderLeafList.push_back(renderleaf);
++_currentReuseRenderLeafIndex;
return renderleaf;
}
}
2001-09-20 05:19:47 +08:00
#endif