openscenegraph
CullVisitor
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#ifndef OSGUTIL_CULLVISITOR
15#define OSGUTIL_CULLVISITOR 1
16
17#include <map>
18#include <vector>
19
20#include <osg/NodeVisitor>
21#include <osg/BoundingSphere>
22#include <osg/BoundingBox>
23#include <osg/Matrix>
24#include <osg/Drawable>
25#include <osg/StateSet>
26#include <osg/State>
27#include <osg/ClearNode>
28#include <osg/Camera>
29#include <osg/Notify>
30
31#include <osg/CullStack>
32
33#include <osgUtil/StateGraph>
34#include <osgUtil/RenderStage>
35
36#include <osg/Vec3>
37
38namespace osgUtil {
39
49{
50 public:
51
53
54
56
59
61
62
64 virtual osgUtil::CullVisitor* asCullVisitor() { return this; }
65
68 virtual const osgUtil::CullVisitor* asCullVisitor() const { return this; }
69
72 virtual osg::CullStack* asCullStack() { return static_cast<osg::CullStack*>(this); }
73
76 virtual const osg::CullStack* asCullStack() const { return static_cast<const osg::CullStack*>(this); }
77
78
80
82 virtual CullVisitor* clone() const { return new CullVisitor(*this); }
83
86
89
90 virtual void reset();
91
93 {
95 virtual ~Identifier() {}
96 };
97
98 void setIdentifier(Identifier* identifier) { _identifier = identifier; }
99 Identifier* getIdentifier() { return _identifier.get(); }
100 const Identifier* getIdentifier() const { return _identifier.get(); }
101
102 virtual osg::Vec3 getEyePoint() const { return getEyeLocal(); }
103 virtual osg::Vec3 getViewPoint() const { return getViewPointLocal(); }
104
105 virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
106 virtual float getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScale) const;
107
108 virtual float getDistanceToViewPoint(const osg::Vec3& pos, bool withLODScale) const;
109
110 virtual void apply(osg::Node&);
111 virtual void apply(osg::Geode& node);
112 virtual void apply(osg::Drawable& drawable);
113 virtual void apply(osg::Billboard& node);
114 virtual void apply(osg::LightSource& node);
115 virtual void apply(osg::ClipNode& node);
116 virtual void apply(osg::TexGenNode& node);
117
118 virtual void apply(osg::Group& node);
119 virtual void apply(osg::Transform& node);
120 virtual void apply(osg::Projection& node);
121 virtual void apply(osg::Switch& node);
122 virtual void apply(osg::LOD& node);
123 virtual void apply(osg::ClearNode& node);
124 virtual void apply(osg::Camera& node);
125 virtual void apply(osg::OccluderNode& node);
126 virtual void apply(osg::OcclusionQueryNode& node);
127
135 inline void pushStateSet(const osg::StateSet* ss)
136 {
137 _currentStateGraph = _currentStateGraph->find_or_insert(ss);
138
139 bool useRenderBinDetails = (ss->useRenderBinDetails() && !ss->getBinName().empty()) &&
140 (_numberOfEncloseOverrideRenderBinDetails==0 || (ss->getRenderBinMode()&osg::StateSet::PROTECTED_RENDERBIN_DETAILS)!=0);
141
142 if (useRenderBinDetails)
143 {
144 _renderBinStack.push_back(_currentRenderBin);
145
146 _currentRenderBin = ss->getNestRenderBins() ?
147 _currentRenderBin->find_or_insert(ss->getBinNumber(),ss->getBinName()) :
148 _currentRenderBin->getStage()->find_or_insert(ss->getBinNumber(),ss->getBinName());
149 }
150
152 {
153 ++_numberOfEncloseOverrideRenderBinDetails;
154 }
155 }
156
161 inline void popStateSet()
162 {
163 const osg::StateSet* ss = _currentStateGraph->getStateSet();
165 {
166 --_numberOfEncloseOverrideRenderBinDetails;
167 }
168
169 bool useRenderBinDetails = (ss->useRenderBinDetails() && !ss->getBinName().empty()) &&
170 (_numberOfEncloseOverrideRenderBinDetails==0 || (ss->getRenderBinMode()&osg::StateSet::PROTECTED_RENDERBIN_DETAILS)!=0);
171
172 if (useRenderBinDetails)
173 {
174 if (_renderBinStack.empty())
175 {
176 _currentRenderBin = _currentRenderBin->getStage();
177 }
178 else
179 {
180 _currentRenderBin = _renderBinStack.back();
181 _renderBinStack.pop_back();
182 }
183 }
184 _currentStateGraph = _currentStateGraph->_parent;
185 }
186
187 inline void setStateGraph(StateGraph* rg)
188 {
189 _rootStateGraph = rg;
190 _currentStateGraph = rg;
191 }
192
194 {
195 return _rootStateGraph.get();
196 }
197
199 {
200 return _currentStateGraph;
201 }
202
204 {
205 _rootRenderStage = rg;
206 _currentRenderBin = rg;
207 }
208
210 {
211 return _rootRenderStage.get();
212 }
213
215 {
216 return _currentRenderBin->getStage();
217 }
218
220 {
221 return getCurrentRenderStage()->getCamera();
222 }
223
225 {
226 return _currentRenderBin;
227 }
228
230 {
231 _currentRenderBin = rb;
232 }
233
234 void setCalculatedNearPlane(value_type value) { _computed_znear = value; }
235 inline value_type getCalculatedNearPlane() const { return _computed_znear; }
236
237 void setCalculatedFarPlane(value_type value) { _computed_zfar = value; }
238 inline value_type getCalculatedFarPlane() const { return _computed_zfar; }
239
242
244
245 bool updateCalculatedNearFar(const osg::Matrix& matrix,const osg::Drawable& drawable, bool isBillboard=false);
246
248
250 inline void addDrawable(osg::Drawable* drawable,osg::RefMatrix* matrix);
251
253 inline void addDrawableAndDepth(osg::Drawable* drawable,osg::RefMatrix* matrix,float depth);
254
256 inline void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr);
257
259 inline void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr);
260
261
265
268 virtual void popProjectionMatrix();
269
270
273 virtual bool clampProjectionMatrixImplementation(osg::Matrixf& projection, double& znear, double& zfar) const;
274
277 virtual bool clampProjectionMatrixImplementation(osg::Matrixd& projection, double& znear, double& zfar) const;
278
281 inline bool clampProjectionMatrix(osg::Matrixf& projection, value_type& znear, value_type& zfar) const
282 {
283 double zn = znear;
284 double zf = zfar;
285 bool result = false;
286 if (_clampProjectionMatrixCallback.valid()) result = _clampProjectionMatrixCallback->clampProjectionMatrixImplementation(projection, zn, zf);
287 else result = clampProjectionMatrixImplementation(projection, zn, zf);
288
289 if (result)
290 {
291 znear = zn;
292 zfar = zf;
293 return true;
294 }
295 else
296 return false;
297 }
298
301 inline bool clampProjectionMatrix(osg::Matrixd& projection, value_type& znear, value_type& zfar) const
302 {
303 double zn = znear;
304 double zf = zfar;
305 bool result = false;
306
307 if (_clampProjectionMatrixCallback.valid()) result = _clampProjectionMatrixCallback->clampProjectionMatrixImplementation(projection, zn, zf);
308 else result = clampProjectionMatrixImplementation(projection, zn, zf);
309
310 if (result)
311 {
312 znear = zn;
313 zfar = zf;
314 return true;
315 }
316 else
317 return false;
318 }
319
320
321 void setState(osg::State* state) { _renderInfo.setState(state); }
322 osg::State* getState() { return _renderInfo.getState(); }
323 const osg::State* getState() const { return _renderInfo.getState(); }
324
325 void setRenderInfo(osg::RenderInfo& renderInfo) { _renderInfo = renderInfo; }
326 osg::RenderInfo& getRenderInfo() { return _renderInfo; }
327 const osg::RenderInfo& getRenderInfo() const { return _renderInfo; }
328
329 protected:
330
331 virtual ~CullVisitor();
332
334 CullVisitor& operator = (const CullVisitor&) { return *this; }
335
337 {
338 osg::Callback* callback = node.getCullCallback();
339 if (callback) callback->run(&node,this);
340 else traverse(node);
341 }
342
344 {
345 osg::Callback* callback = node.getCullCallback();
346 if (callback) callback->run(&node,this);
347 else acceptNode->accept(*this);
348 }
349
352
355 std::vector<RenderBin*> _renderBinStack;
356
359
361
362
363 typedef std::vector< osg::ref_ptr<RenderLeaf> > RenderLeafList;
366
367 inline RenderLeaf* createOrReuseRenderLeaf(osg::Drawable* drawable,osg::RefMatrix* projection,osg::RefMatrix* matrix, float depth=0.0f);
368
370
372
373
375 {
377 _drawable(0)
378 {
379 }
380
381 void set(const osg::Matrix& matrix, const osg::Drawable* drawable, const osg::Polytope& frustum)
382 {
383 _matrix = matrix;
384 _drawable = drawable;
385 if (!_planes.empty()) _planes.clear();
386
387 // create a new list of planes from the active walls of the frustum.
388 osg::Polytope::ClippingMask result_mask = frustum.getResultMask();
389 osg::Polytope::ClippingMask selector_mask = 0x1;
390 for(osg::Polytope::PlaneList::const_iterator itr=frustum.getPlaneList().begin();
391 itr!=frustum.getPlaneList().end();
392 ++itr)
393 {
394 if (result_mask&selector_mask) _planes.push_back(*itr);
395 selector_mask <<= 1;
396 }
397 }
398
400 _matrix(mpd._matrix),
401 _drawable(mpd._drawable),
402 _planes(mpd._planes) {}
403
405 {
406 _matrix = mpd._matrix;
407 _drawable = mpd._drawable;
408 _planes = mpd._planes;
409 return *this;
410 }
411
415 };
416
417 typedef std::multimap<value_type, MatrixPlanesDrawables> DistanceMatrixDrawableMap;
420
422};
423
425{
427 {
428 // this is first leaf to be added to StateGraph
429 // and therefore should not already know to current render bin,
430 // so need to add it.
432 }
433 //_currentStateGraph->addLeaf(new RenderLeaf(drawable,matrix));
435}
436
438inline void CullVisitor::addDrawableAndDepth(osg::Drawable* drawable,osg::RefMatrix* matrix,float depth)
439{
441 {
442 // this is first leaf to be added to StateGraph
443 // and therefore should not already know to current render bin,
444 // so need to add it.
446 }
447 //_currentStateGraph->addLeaf(new RenderLeaf(drawable,matrix,depth));
449}
450
456
458inline void CullVisitor::addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr)
459{
460 _currentRenderBin->getStage()->addPositionedTextureAttribute(textureUnit,matrix,attr);
461}
462
464{
465 // Skips any already reused renderleaf.
468 {
469 osg::notify(osg::INFO)<<"CullVisitor:createOrReuseRenderLeaf() skipping multiply referenced entry. _reuseRenderLeafList.size()="<< _reuseRenderLeafList.size()<<" _reuseRenderLeafList["<<_currentReuseRenderLeafIndex<<"]->referenceCount()="<<_reuseRenderLeafList[_currentReuseRenderLeafIndex]->referenceCount()<<std::endl;
471 }
472
473 // If still within list, element must be singularly referenced then return it to be reused.
475 {
477 renderleaf->set(drawable,projection,matrix,depth,_traversalOrderNumber++);
478 return renderleaf;
479 }
480
481
482 // Otherwise need to create new renderleaf.
483 RenderLeaf* renderleaf = new RenderLeaf(drawable,projection,matrix,depth,_traversalOrderNumber++);
484 _reuseRenderLeafList.push_back(renderleaf);
485
487 return renderleaf;
488}
489
490}
491
492#endif
Definition CullVisitor:49
virtual void apply(osg::Camera &node)
void setStateGraph(StateGraph *rg)
Definition CullVisitor:187
RenderLeaf * createOrReuseRenderLeaf(osg::Drawable *drawable, osg::RefMatrix *projection, osg::RefMatrix *matrix, float depth=0.0f)
Definition CullVisitor:463
unsigned int _traversalOrderNumber
Definition CullVisitor:360
Identifier * getIdentifier()
Definition CullVisitor:99
osg::Camera * getCurrentCamera()
Definition CullVisitor:219
virtual void apply(osg::Projection &node)
RenderStage * getCurrentRenderStage()
Definition CullVisitor:214
value_type getCalculatedFarPlane() const
Definition CullVisitor:238
virtual void apply(osg::ClearNode &node)
virtual void apply(osg::Geode &node)
bool updateCalculatedNearFar(const osg::Matrix &matrix, const osg::Drawable &drawable, bool isBillboard=false)
value_type _computed_zfar
Definition CullVisitor:358
static CullVisitor * create()
value_type getCalculatedNearPlane() const
Definition CullVisitor:235
DistanceMatrixDrawableMap _nearPlaneCandidateMap
Definition CullVisitor:418
virtual const osgUtil::CullVisitor * asCullVisitor() const
Definition CullVisitor:68
osg::ref_ptr< StateGraph > _rootStateGraph
Definition CullVisitor:350
virtual bool clampProjectionMatrixImplementation(osg::Matrixf &projection, double &znear, double &zfar) const
virtual void apply(osg::ClipNode &node)
void addDrawable(osg::Drawable *drawable, osg::RefMatrix *matrix)
Definition CullVisitor:424
void setState(osg::State *state)
Definition CullVisitor:321
virtual void apply(osg::LightSource &node)
virtual void apply(osg::LOD &node)
unsigned int _numberOfEncloseOverrideRenderBinDetails
Definition CullVisitor:369
virtual void apply(osg::TexGenNode &node)
void setCalculatedNearPlane(value_type value)
Definition CullVisitor:234
static osg::ref_ptr< CullVisitor > & prototype()
const osg::RenderInfo & getRenderInfo() const
Definition CullVisitor:327
void handle_cull_callbacks_and_traverse(osg::Node &node)
Definition CullVisitor:336
void setRenderInfo(osg::RenderInfo &renderInfo)
Definition CullVisitor:325
osg::RenderInfo _renderInfo
Definition CullVisitor:371
const osg::State * getState() const
Definition CullVisitor:323
virtual void apply(osg::Transform &node)
void setIdentifier(Identifier *identifier)
Definition CullVisitor:98
const Identifier * getIdentifier() const
Definition CullVisitor:100
StateGraph * getCurrentStateGraph()
Definition CullVisitor:198
RenderLeafList _reuseRenderLeafList
Definition CullVisitor:364
void pushStateSet(const osg::StateSet *ss)
Definition CullVisitor:135
StateGraph * _currentStateGraph
Definition CullVisitor:351
virtual osg::CullStack * asCullStack()
Definition CullVisitor:72
value_type computeNearestPointInFrustum(const osg::Matrix &matrix, const osg::Polytope::PlaneList &planes, const osg::Drawable &drawable)
RenderStage * getRenderStage()
Definition CullVisitor:209
RenderBin * _currentRenderBin
Definition CullVisitor:354
StateGraph * getRootStateGraph()
Definition CullVisitor:193
void addDrawableAndDepth(osg::Drawable *drawable, osg::RefMatrix *matrix, float depth)
Definition CullVisitor:438
bool clampProjectionMatrix(osg::Matrixf &projection, value_type &znear, value_type &zfar) const
Definition CullVisitor:281
virtual osg::Vec3 getViewPoint() const
Definition CullVisitor:103
virtual void apply(osg::Drawable &drawable)
virtual void apply(osg::Billboard &node)
void setCurrentRenderBin(RenderBin *rb)
Definition CullVisitor:229
bool clampProjectionMatrix(osg::Matrixd &projection, value_type &znear, value_type &zfar) const
Definition CullVisitor:301
virtual void apply(osg::Group &node)
bool updateCalculatedNearFar(const osg::Matrix &matrix, const osg::BoundingBox &bb)
void updateCalculatedNearFar(const osg::Vec3 &pos)
virtual void apply(osg::OccluderNode &node)
virtual float getDistanceToEyePoint(const osg::Vec3 &pos, bool withLODScale) const
void setRenderStage(RenderStage *rg)
Definition CullVisitor:203
virtual void reset()
virtual bool clampProjectionMatrixImplementation(osg::Matrixd &projection, double &znear, double &zfar) const
virtual float getDistanceToViewPoint(const osg::Vec3 &pos, bool withLODScale) const
osg::State * getState()
Definition CullVisitor:322
virtual void apply(osg::OcclusionQueryNode &node)
virtual const osg::CullStack * asCullStack() const
Definition CullVisitor:76
osg::Matrix::value_type value_type
Definition CullVisitor:52
void popStateSet()
Definition CullVisitor:161
virtual osg::Vec3 getEyePoint() const
Definition CullVisitor:102
void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition CullVisitor:458
value_type computeFurthestPointInFrustum(const osg::Matrix &matrix, const osg::Polytope::PlaneList &planes, const osg::Drawable &drawable)
unsigned int _currentReuseRenderLeafIndex
Definition CullVisitor:365
RenderBin * getCurrentRenderBin()
Definition CullVisitor:224
void handle_cull_callbacks_and_accept(osg::Node &node, osg::Node *acceptNode)
Definition CullVisitor:343
void addPositionedAttribute(osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition CullVisitor:452
virtual CullVisitor * clone() const
Definition CullVisitor:82
CullVisitor(const CullVisitor &)
Copy constructor that does a shallow copy.
std::vector< osg::ref_ptr< RenderLeaf > > RenderLeafList
Definition CullVisitor:363
osg::ref_ptr< RenderStage > _rootRenderStage
Definition CullVisitor:353
virtual void popProjectionMatrix()
value_type _computed_znear
Definition CullVisitor:357
std::vector< RenderBin * > _renderBinStack
Definition CullVisitor:355
void setCalculatedFarPlane(value_type value)
Definition CullVisitor:237
DistanceMatrixDrawableMap _farPlaneCandidateMap
Definition CullVisitor:419
osg::ref_ptr< Identifier > _identifier
Definition CullVisitor:421
std::multimap< value_type, MatrixPlanesDrawables > DistanceMatrixDrawableMap
Definition CullVisitor:417
virtual void apply(osg::Node &)
META_NodeVisitor(osgUtil, CullVisitor) virtual osgUtil
Definition CullVisitor:60
virtual float getDistanceFromEyePoint(const osg::Vec3 &pos, bool withLODScale) const
virtual void apply(osg::Switch &node)
osg::RenderInfo & getRenderInfo()
Definition CullVisitor:326
Definition RenderBin:34
RenderStage * getStage()
Definition RenderBin:87
void addStateGraph(StateGraph *rg)
Definition RenderBin:104
Definition RenderLeaf:34
void set(osg::Drawable *drawable, osg::RefMatrix *projection, osg::RefMatrix *modelview, float depth=0.0f, unsigned int traversalOrderNumber=0)
Definition RenderLeaf:51
Definition RenderStage:38
virtual void addPositionedAttribute(osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition RenderStage:210
virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition RenderStage:215
Definition StateGraph:42
bool leaves_empty() const
Definition StateGraph:122
void addLeaf(RenderLeaf *leaf)
Definition StateGraph:229
Definition Billboard:27
Definition BoundingBox:34
Definition Callback:34
virtual bool run(osg::Object *object, osg::Object *data)
Definition Callback:80
Definition Camera:45
Definition ClearNode:31
Definition ClipNode:24
Definition CullStack:28
MatrixStack _projectionStack
Definition CullStack:194
Definition Drawable:89
Definition Geode:29
Definition Group:29
Definition LOD:36
Definition LightSource:25
Definition Matrixd:27
double value_type
Definition Matrixd:30
Definition Matrixf:27
Definition NodeVisitor:82
Definition Node:72
Callback * getCullCallback()
Definition Node:293
virtual void accept(NodeVisitor &nv)
virtual Object * clone(const CopyOp &) const =0
Definition OccluderNode:27
Definition OcclusionQueryNode:110
Definition Polytope:26
ClippingMask getResultMask() const
Definition Polytope:178
PlaneList & getPlaneList()
Definition Polytope:150
unsigned int ClippingMask
Definition Polytope:30
std::vector< Plane > PlaneList
Definition Polytope:31
Definition Projection:25
Definition Matrixd:412
Definition Referenced:44
int referenceCount() const
Definition Referenced:97
Definition RenderInfo:28
Definition StateAttribute:77
Definition StateSet:46
bool getNestRenderBins() const
Definition StateSet:442
const std::string & getBinName() const
Definition StateSet:430
@ PROTECTED_RENDERBIN_DETAILS
Definition StateSet:401
@ OVERRIDE_RENDERBIN_DETAILS
Definition StateSet:400
bool useRenderBinDetails() const
Definition StateSet:412
int getBinNumber() const
Definition StateSet:424
RenderBinMode getRenderBinMode() const
Definition StateSet:418
Definition State:80
Definition Switch:27
Definition TexGenNode:24
Definition Transform:75
Definition Vec3f:29
T & back()
Definition fast_back_stack:54
Definition ref_ptr:32
T * get() const
Definition ref_ptr:117
Shader generator framework.
Definition NodeVisitor:25
@ INFO
Definition Notify:35
std::ostream & notify(void)
Definition Notify:80
#define OSGUTIL_EXPORT
Definition osgUtil/Export:40
Definition CullVisitor:93
virtual ~Identifier()
Definition CullVisitor:95
Identifier()
Definition CullVisitor:94
void set(const osg::Matrix &matrix, const osg::Drawable *drawable, const osg::Polytope &frustum)
Definition CullVisitor:381
osg::Polytope::PlaneList _planes
Definition CullVisitor:414
MatrixPlanesDrawables()
Definition CullVisitor:376
osg::Matrix _matrix
Definition CullVisitor:412
const osg::Drawable * _drawable
Definition CullVisitor:413
MatrixPlanesDrawables(const MatrixPlanesDrawables &mpd)
Definition CullVisitor:399