OpenFrames
DistanceAccumulator.hpp
Go to the documentation of this file.
1 /***********************************
2  Copyright 2018 Ravishankar Mathur
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  ***********************************/
16 
21 #ifndef _OF_DISTANCEACCUMULATOR_
22 #define _OF_DISTANCEACCUMULATOR_
23 
24 #include <OpenFrames/Export.h>
25 #include <osg/Group>
26 #include <osg/NodeVisitor>
27 #include <osg/Polytope>
28 #include <osg/fast_back_stack>
29 
30 namespace OpenFrames
31 {
41  class OF_EXPORT DistanceAccumulator : public osg::NodeVisitor
42  {
43  public:
44  typedef std::pair<double, double> DistancePair;
45  typedef std::vector<DistancePair> PairList;
46 
48 
49  // DistanceAccumulator considers these types of nodes
50  virtual void apply(osg::Node &node);
51  virtual void apply(osg::Projection &proj);
52  virtual void apply(osg::Transform &transform);
53  virtual void apply(osg::Geode &geode);
54 
55  // Specify the modelview & projection matrices
56  void setMatrices(const osg::Matrix &modelview,
57  const osg::Matrix &projection);
58 
59  // Reset visitor before a new traversal
60  virtual void reset();
61 
62  // Create a (near,far) distance pair for each camera of the specified
63  // distance pair list and distance limits.
64  void computeCameraPairs();
65 
66  // Get info on the cameras that should be used for scene rendering
67  PairList& getCameraPairs() { return _cameraPairs; }
68 
69  // Get info on the computed distance pairs
70  PairList& getDistancePairs() { return _distancePairs; }
71 
72  // Get info on the computed nearest/farthest distances
73  DistancePair& getLimits() { return _limits; }
74 
75  // Set/get the desired near/far ratio
76  void setNearFarRatio(double ratio);
77  inline double getNearFarRatio() const { return _nearFarRatio; }
78 
79  // Set the max traversal depth into the scene graph
80  inline void setMaxDepth(unsigned int depth) { _maxDepth = depth; }
81  inline unsigned int getMaxDepth() const { return _maxDepth; }
82 
83  // Set the minimum allowable near plane. Zero means no minimum.
84  inline void setMinZNear(double minZNear) { if(minZNear >= 0.0) _minZNear = minZNear; }
85  inline double getMinZNear() { return _minZNear; }
86 
87  protected:
88  virtual ~DistanceAccumulator();
89 
90  void pushLocalFrustum();
91  void pushDistancePair(double zNear, double zFar);
92  bool shouldContinueTraversal(osg::Node &node);
93 
94  // Stack of matrices accumulated during traversal
95  osg::fast_back_stack<osg::Matrix> _viewMatrices;
96  osg::fast_back_stack<osg::Matrix> _projectionMatrices;
97 
98  // Main modelview/projection matrices
99  osg::Matrix _modelview, _projection;
100 
101  // The view frusta in local coordinate space
102  osg::fast_back_stack<osg::Polytope> _localFrusta;
103 
104  // Bounding box corners that should be used for cull computation
105  typedef std::pair<unsigned int, unsigned int> bbCornerPair;
106  osg::fast_back_stack<bbCornerPair> _bbCorners;
107 
108  // Nar/far planes that should be used for each camera
109  PairList _cameraPairs;
110 
111  // Accumulated pairs of min/max distances
112  PairList _distancePairs;
113 
114  // The closest & farthest distances found while traversing
115  DistancePair _limits;
116 
117  // Ratio of nearest/farthest clip plane for each section of the scene
118  double _nearFarRatio;
119 
120  // Maximum depth to traverse to
121  unsigned int _maxDepth, _currentDepth;
122 
123  // Minimum zNear value
124  double _minZNear;
125  };
126 
127 }
128 
129 #endif
This class computes distances to drawables and splits the scene if necessary.
Definition: DistanceAccumulator.hpp:41
Definition: CoordinateAxes.hpp:29