OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CullVisitor.h
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 
38 namespace osgUtil {
39 
49 {
50  public:
51 
53 
54 
55  CullVisitor();
56 
58  CullVisitor(const CullVisitor&);
59 
61 
62 
63  virtual CullVisitor* clone() const { return new CullVisitor(*this); }
64 
66  static osg::ref_ptr<CullVisitor>& prototype();
67 
69  static CullVisitor* create();
70 
71  virtual void reset();
72 
73  struct Identifier : public osg::Referenced
74  {
76  virtual ~Identifier() {}
77  };
78 
79  void setIdentifier(Identifier* identifier) { _identifier = identifier; }
80  Identifier* getIdentifier() { return _identifier.get(); }
81  const Identifier* getIdentifier() const { return _identifier.get(); }
82 
83  virtual osg::Vec3 getEyePoint() const { return getEyeLocal(); }
84  virtual osg::Vec3 getViewPoint() const { return getViewPointLocal(); }
85 
86  virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
87  virtual float getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScale) const;
88 
89  virtual float getDistanceToViewPoint(const osg::Vec3& pos, bool withLODScale) const;
90 
91  virtual void apply(osg::Node&);
92  virtual void apply(osg::Geode& node);
93  virtual void apply(osg::Drawable& drawable);
94  virtual void apply(osg::Billboard& node);
95  virtual void apply(osg::LightSource& node);
96  virtual void apply(osg::ClipNode& node);
97  virtual void apply(osg::TexGenNode& node);
98 
99  virtual void apply(osg::Group& node);
100  virtual void apply(osg::Transform& node);
101  virtual void apply(osg::Projection& node);
102  virtual void apply(osg::Switch& node);
103  virtual void apply(osg::LOD& node);
104  virtual void apply(osg::ClearNode& node);
105  virtual void apply(osg::Camera& node);
106  virtual void apply(osg::OccluderNode& node);
107  virtual void apply(osg::OcclusionQueryNode& node);
108 
116  inline void pushStateSet(const osg::StateSet* ss)
117  {
118  _currentStateGraph = _currentStateGraph->find_or_insert(ss);
119 
120  bool useRenderBinDetails = (ss->useRenderBinDetails() && !ss->getBinName().empty()) &&
121  (_numberOfEncloseOverrideRenderBinDetails==0 || (ss->getRenderBinMode()&osg::StateSet::PROTECTED_RENDERBIN_DETAILS)!=0);
122 
123  if (useRenderBinDetails)
124  {
125  _renderBinStack.push_back(_currentRenderBin);
126 
127  _currentRenderBin = ss->getNestRenderBins() ?
128  _currentRenderBin->find_or_insert(ss->getBinNumber(),ss->getBinName()) :
129  _currentRenderBin->getStage()->find_or_insert(ss->getBinNumber(),ss->getBinName());
130  }
131 
133  {
134  ++_numberOfEncloseOverrideRenderBinDetails;
135  }
136  }
137 
142  inline void popStateSet()
143  {
144  const osg::StateSet* ss = _currentStateGraph->getStateSet();
146  {
147  --_numberOfEncloseOverrideRenderBinDetails;
148  }
149 
150  bool useRenderBinDetails = (ss->useRenderBinDetails() && !ss->getBinName().empty()) &&
151  (_numberOfEncloseOverrideRenderBinDetails==0 || (ss->getRenderBinMode()&osg::StateSet::PROTECTED_RENDERBIN_DETAILS)!=0);
152 
153  if (useRenderBinDetails)
154  {
155  if (_renderBinStack.empty())
156  {
157  _currentRenderBin = _currentRenderBin->getStage();
158  }
159  else
160  {
161  _currentRenderBin = _renderBinStack.back();
162  _renderBinStack.pop_back();
163  }
164  }
165  _currentStateGraph = _currentStateGraph->_parent;
166  }
167 
168  inline void setStateGraph(StateGraph* rg)
169  {
170  _rootStateGraph = rg;
171  _currentStateGraph = rg;
172  }
173 
175  {
176  return _rootStateGraph.get();
177  }
178 
180  {
181  return _currentStateGraph;
182  }
183 
184  inline void setRenderStage(RenderStage* rg)
185  {
186  _rootRenderStage = rg;
187  _currentRenderBin = rg;
188  }
189 
191  {
192  return _rootRenderStage.get();
193  }
194 
196  {
197  return _currentRenderBin->getStage();
198  }
199 
201  {
202  return getCurrentRenderStage()->getCamera();
203  }
204 
206  {
207  return _currentRenderBin;
208  }
209 
211  {
212  _currentRenderBin = rb;
213  }
214 
215  void setCalculatedNearPlane(value_type value) { _computed_znear = value; }
216  inline value_type getCalculatedNearPlane() const { return _computed_znear; }
217 
218  void setCalculatedFarPlane(value_type value) { _computed_zfar = value; }
219  inline value_type getCalculatedFarPlane() const { return _computed_zfar; }
220 
221  value_type computeNearestPointInFrustum(const osg::Matrix& matrix, const osg::Polytope::PlaneList& planes,const osg::Drawable& drawable);
222  value_type computeFurthestPointInFrustum(const osg::Matrix& matrix, const osg::Polytope::PlaneList& planes,const osg::Drawable& drawable);
223 
224  bool updateCalculatedNearFar(const osg::Matrix& matrix,const osg::BoundingBox& bb);
225 
226  bool updateCalculatedNearFar(const osg::Matrix& matrix,const osg::Drawable& drawable, bool isBillboard=false);
227 
228  void updateCalculatedNearFar(const osg::Vec3& pos);
229 
231  inline void addDrawable(osg::Drawable* drawable,osg::RefMatrix* matrix);
232 
234  inline void addDrawableAndDepth(osg::Drawable* drawable,osg::RefMatrix* matrix,float depth);
235 
237  inline void addPositionedAttribute(osg::RefMatrix* matrix,const osg::StateAttribute* attr);
238 
240  inline void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr);
241 
242 
245  void computeNearPlane();
246 
249  virtual void popProjectionMatrix();
250 
251 
254  virtual bool clampProjectionMatrixImplementation(osg::Matrixf& projection, double& znear, double& zfar) const;
255 
258  virtual bool clampProjectionMatrixImplementation(osg::Matrixd& projection, double& znear, double& zfar) const;
259 
262  inline bool clampProjectionMatrix(osg::Matrixf& projection, value_type& znear, value_type& zfar) const
263  {
264  double zn = znear;
265  double zf = zfar;
266  bool result = false;
267  if (_clampProjectionMatrixCallback.valid()) result = _clampProjectionMatrixCallback->clampProjectionMatrixImplementation(projection, zn, zf);
268  else result = clampProjectionMatrixImplementation(projection, zn, zf);
269 
270  if (result)
271  {
272  znear = zn;
273  zfar = zf;
274  return true;
275  }
276  else
277  return false;
278  }
279 
282  inline bool clampProjectionMatrix(osg::Matrixd& projection, value_type& znear, value_type& zfar) const
283  {
284  double zn = znear;
285  double zf = zfar;
286  bool result = false;
287 
288  if (_clampProjectionMatrixCallback.valid()) result = _clampProjectionMatrixCallback->clampProjectionMatrixImplementation(projection, zn, zf);
289  else result = clampProjectionMatrixImplementation(projection, zn, zf);
290 
291  if (result)
292  {
293  znear = zn;
294  zfar = zf;
295  return true;
296  }
297  else
298  return false;
299  }
300 
301 
302  void setState(osg::State* state) { _renderInfo.setState(state); }
303  osg::State* getState() { return _renderInfo.getState(); }
304  const osg::State* getState() const { return _renderInfo.getState(); }
305 
306  void setRenderInfo(osg::RenderInfo& renderInfo) { _renderInfo = renderInfo; }
307  osg::RenderInfo& getRenderInfo() { return _renderInfo; }
308  const osg::RenderInfo& getRenderInfo() const { return _renderInfo; }
309 
310  protected:
311 
312  virtual ~CullVisitor();
313 
315  CullVisitor& operator = (const CullVisitor&) { return *this; }
316 
318  {
319  osg::Callback* callback = node.getCullCallback();
320  if (callback) callback->run(&node,this);
321  else traverse(node);
322  }
323 
324  inline void handle_cull_callbacks_and_accept(osg::Node& node,osg::Node* acceptNode)
325  {
326  osg::Callback* callback = node.getCullCallback();
327  if (callback) callback->run(&node,this);
328  else acceptNode->accept(*this);
329  }
330 
333 
336  std::vector<RenderBin*> _renderBinStack;
337 
338  unsigned int _traversalNumber;
339 
340  value_type _computed_znear;
341  value_type _computed_zfar;
342 
343 
344  typedef std::vector< osg::ref_ptr<RenderLeaf> > RenderLeafList;
345  RenderLeafList _reuseRenderLeafList;
347 
348  inline RenderLeaf* createOrReuseRenderLeaf(osg::Drawable* drawable,osg::RefMatrix* projection,osg::RefMatrix* matrix, float depth=0.0f);
349 
351 
353 
354 
356  {
358  _drawable(0)
359  {
360  }
361 
362  void set(const osg::Matrix& matrix, const osg::Drawable* drawable, const osg::Polytope& frustum)
363  {
364  _matrix = matrix;
365  _drawable = drawable;
366  if (!_planes.empty()) _planes.clear();
367 
368  // create a new list of planes from the active walls of the frustum.
369  osg::Polytope::ClippingMask result_mask = frustum.getResultMask();
370  osg::Polytope::ClippingMask selector_mask = 0x1;
371  for(osg::Polytope::PlaneList::const_iterator itr=frustum.getPlaneList().begin();
372  itr!=frustum.getPlaneList().end();
373  ++itr)
374  {
375  if (result_mask&selector_mask) _planes.push_back(*itr);
376  selector_mask <<= 1;
377  }
378  }
379 
381  _matrix(mpd._matrix),
382  _drawable(mpd._drawable),
383  _planes(mpd._planes) {}
384 
386  {
387  _matrix = mpd._matrix;
388  _drawable = mpd._drawable;
389  _planes = mpd._planes;
390  return *this;
391  }
392 
396  };
397 
398  typedef std::multimap<value_type, MatrixPlanesDrawables> DistanceMatrixDrawableMap;
399  DistanceMatrixDrawableMap _nearPlaneCandidateMap;
400  DistanceMatrixDrawableMap _farPlaneCandidateMap;
401 
403 };
404 
406 {
408  {
409  // this is first leaf to be added to StateGraph
410  // and therefore should not already know to current render bin,
411  // so need to add it.
413  }
414  //_currentStateGraph->addLeaf(new RenderLeaf(drawable,matrix));
416 }
417 
420 {
422  {
423  // this is first leaf to be added to StateGraph
424  // and therefore should not already know to current render bin,
425  // so need to add it.
427  }
428  //_currentStateGraph->addLeaf(new RenderLeaf(drawable,matrix,depth));
430 }
431 
434 {
436 }
437 
439 inline void CullVisitor::addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix* matrix,const osg::StateAttribute* attr)
440 {
441  _currentRenderBin->getStage()->addPositionedTextureAttribute(textureUnit,matrix,attr);
442 }
443 
445 {
446  // Skips any already reused renderleaf.
449  {
450  osg::notify(osg::NOTICE)<<"Warning:createOrReuseRenderLeaf() skipping multiply refrenced entry."<< std::endl;
452  }
453 
454  // If still within list, element must be singularly referenced then return it to be reused.
456  {
458  renderleaf->set(drawable,projection,matrix,depth,_traversalNumber++);
459  return renderleaf;
460  }
461 
462  // Otherwise need to create new renderleaf.
463  RenderLeaf* renderleaf = new RenderLeaf(drawable,projection,matrix,depth,_traversalNumber++);
464  _reuseRenderLeafList.push_back(renderleaf);
466  return renderleaf;
467 }
468 
469 
470 }
471 
472 #endif
473 
void set(osg::Drawable *drawable, osg::RefMatrix *projection, osg::RefMatrix *modelview, float depth=0.0f, unsigned int traversalNumber=0)
Definition: RenderLeaf.h:51
void setCalculatedNearPlane(value_type value)
Definition: CullVisitor.h:215
std::vector< osg::ref_ptr< RenderLeaf > > RenderLeafList
Definition: CullVisitor.h:344
StateGraph * getRootStateGraph()
Definition: CullVisitor.h:174
osg::Matrix::value_type value_type
Definition: CullVisitor.h:52
MatrixPlanesDrawables(const MatrixPlanesDrawables &mpd)
Definition: CullVisitor.h:380
META_NodeVisitor(osgUtil, CullVisitor) virtual CullVisitor *clone() const
Definition: CullVisitor.h:60
const osg::RenderInfo & getRenderInfo() const
Definition: CullVisitor.h:308
virtual osg::Vec3 getEyePoint() const
Definition: CullVisitor.h:83
RenderBinMode getRenderBinMode() const
Definition: StateSet.h:385
value_type _computed_zfar
Definition: CullVisitor.h:341
std::vector< RenderBin * > _renderBinStack
Definition: CullVisitor.h:336
unsigned int ClippingMask
Definition: Polytope.h:30
PlaneList & getPlaneList()
Definition: Polytope.h:150
void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition: CullVisitor.h:439
GLint GLenum GLsizei GLsizei GLsizei depth
Definition: GLU.h:71
void setRenderInfo(osg::RenderInfo &renderInfo)
Definition: CullVisitor.h:306
void setCalculatedFarPlane(value_type value)
Definition: CullVisitor.h:218
void setStateGraph(StateGraph *rg)
Definition: CullVisitor.h:168
virtual osg::Vec3 getViewPoint() const
Definition: CullVisitor.h:84
Identifier * getIdentifier()
Definition: CullVisitor.h:80
ClippingMask getResultMask() const
Definition: Polytope.h:178
RenderLeafList _reuseRenderLeafList
Definition: CullVisitor.h:345
void addPositionedAttribute(osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition: CullVisitor.h:433
DistanceMatrixDrawableMap _farPlaneCandidateMap
Definition: CullVisitor.h:400
Callback * getCullCallback()
Definition: Node.h:286
void set(const osg::Matrix &matrix, const osg::Drawable *drawable, const osg::Polytope &frustum)
Definition: CullVisitor.h:362
osg::ref_ptr< StateGraph > _rootStateGraph
Definition: CullVisitor.h:331
unsigned int _traversalNumber
Definition: CullVisitor.h:338
RenderStage * getRenderStage()
Definition: CullVisitor.h:190
osg::Camera * getCurrentCamera()
Definition: CullVisitor.h:200
T * clone(const T *t, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
Definition: Object.h:242
double value_type
Definition: Matrixd.h:30
bool clampProjectionMatrix(osg::Matrixf &projection, value_type &znear, value_type &zfar) const
Definition: CullVisitor.h:262
std::multimap< value_type, MatrixPlanesDrawables > DistanceMatrixDrawableMap
Definition: CullVisitor.h:398
value_type getCalculatedFarPlane() const
Definition: CullVisitor.h:219
MatrixStack _projectionStack
Definition: CullStack.h:179
void handle_cull_callbacks_and_traverse(osg::Node &node)
Definition: CullVisitor.h:317
bool clampProjectionMatrix(osg::Matrixd &projection, value_type &znear, value_type &zfar) const
Definition: CullVisitor.h:282
void pushStateSet(const osg::StateSet *ss)
Definition: CullVisitor.h:116
Definition: LOD.h:35
osg::State * getState()
Definition: CullVisitor.h:303
virtual bool run(osg::Object *object, osg::Object *data)
Definition: Callback.h:42
const std::string & getBinName() const
Definition: StateSet.h:397
void addDrawableAndDepth(osg::Drawable *drawable, osg::RefMatrix *matrix, float depth)
Definition: CullVisitor.h:419
StateGraph * getCurrentStateGraph()
Definition: CullVisitor.h:179
value_type _computed_znear
Definition: CullVisitor.h:340
const osg::State * getState() const
Definition: CullVisitor.h:304
virtual void accept(NodeVisitor &nv)
bool getNestRenderBins() const
Definition: StateSet.h:409
bool useRenderBinDetails() const
Definition: StateSet.h:379
void addStateGraph(StateGraph *rg)
Definition: RenderBin.h:101
RenderBin * _currentRenderBin
Definition: CullVisitor.h:335
T * get() const
Definition: ref_ptr.h:92
DistanceMatrixDrawableMap _nearPlaneCandidateMap
Definition: CullVisitor.h:399
virtual void addPositionedTextureAttribute(unsigned int textureUnit, osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition: RenderStage.h:213
unsigned int _currentReuseRenderLeafIndex
Definition: CullVisitor.h:346
void addDrawable(osg::Drawable *drawable, osg::RefMatrix *matrix)
Definition: CullVisitor.h:405
void setIdentifier(Identifier *identifier)
Definition: CullVisitor.h:79
int getBinNumber() const
Definition: StateSet.h:391
void setState(osg::State *state)
Definition: CullVisitor.h:302
RenderBin * getCurrentRenderBin()
Definition: CullVisitor.h:205
Definition: Node.h:71
osg::ref_ptr< RenderStage > _rootRenderStage
Definition: CullVisitor.h:334
#define OSGUTIL_EXPORT
Definition: Export.h:40
StateGraph * _currentStateGraph
Definition: CullVisitor.h:332
void addLeaf(RenderLeaf *leaf)
Definition: StateGraph.h:189
bool leaves_empty() const
Definition: StateGraph.h:118
OSG_EXPORT std::ostream & notify(const NotifySeverity severity)
unsigned int _numberOfEncloseOverrideRenderBinDetails
Definition: CullVisitor.h:350
const Identifier * getIdentifier() const
Definition: CullVisitor.h:81
std::vector< Plane > PlaneList
Definition: Polytope.h:31
value_type getCalculatedNearPlane() const
Definition: CullVisitor.h:216
RenderStage * getStage()
Definition: RenderBin.h:84
virtual void addPositionedAttribute(osg::RefMatrix *matrix, const osg::StateAttribute *attr)
Definition: RenderStage.h:208
void handle_cull_callbacks_and_accept(osg::Node &node, osg::Node *acceptNode)
Definition: CullVisitor.h:324
osg::RenderInfo _renderInfo
Definition: CullVisitor.h:352
RenderStage * getCurrentRenderStage()
Definition: CullVisitor.h:195
osg::RenderInfo & getRenderInfo()
Definition: CullVisitor.h:307
void setRenderStage(RenderStage *rg)
Definition: CullVisitor.h:184
void setCurrentRenderBin(RenderBin *rb)
Definition: CullVisitor.h:210
osg::ref_ptr< Identifier > _identifier
Definition: CullVisitor.h:402
RenderLeaf * createOrReuseRenderLeaf(osg::Drawable *drawable, osg::RefMatrix *projection, osg::RefMatrix *matrix, float depth=0.0f)
Definition: CullVisitor.h:444
Shader generator framework.
Definition: RenderInfo.h:20