OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CullStack.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 OSG_CULLSTACK
15 #define OSG_CULLSTACK 1
16 
17 #include <osg/CullingSet>
18 #include <osg/CullSettings>
19 #include <osg/Viewport>
20 #include <osg/fast_back_stack>
21 #include <osg/Transform>
22 
23 namespace osg {
24 
28 {
29 
30  public:
31 
32 
33  CullStack();
34  CullStack(const CullStack& cs);
35 
36  ~CullStack();
37 
38  typedef std::vector<ShadowVolumeOccluder> OccluderList;
39 
40  void reset();
41 
42  void pushCullingSet();
43  void popCullingSet();
44 
45  void setOccluderList(const ShadowVolumeOccluderList& svol) { _occluderList = svol; }
46  ShadowVolumeOccluderList& getOccluderList() { return _occluderList; }
47  const ShadowVolumeOccluderList& getOccluderList() const { return _occluderList; }
48 
49  void pushViewport(osg::Viewport* viewport);
50  void popViewport();
51 
52  void pushProjectionMatrix(osg::RefMatrix* matrix);
53  void popProjectionMatrix();
54 
55  void pushModelViewMatrix(osg::RefMatrix* matrix, Transform::ReferenceFrame referenceFrame);
56  void popModelViewMatrix();
57 
58  inline float getFrustumVolume() { if (_frustumVolume<0.0f) computeFrustumVolume(); return _frustumVolume; }
59 
60 
62  float pixelSize(const Vec3& v,float radius) const
63  {
64  return getCurrentCullingSet().pixelSize(v,radius);
65  }
66 
68  float pixelSize(const BoundingSphere& bs) const
69  {
70  return pixelSize(bs.center(),bs.radius());
71  }
72 
74  float clampedPixelSize(const Vec3& v,float radius) const
75  {
76  return getCurrentCullingSet().clampedPixelSize(v,radius);
77  }
78 
80  float clampedPixelSize(const BoundingSphere& bs) const
81  {
82  return clampedPixelSize(bs.center(),bs.radius());
83  }
84 
86  {
87  getCurrentCullingSet().disableAndPushOccludersCurrentMask(nodePath);
88  }
89 
90  inline void popOccludersCurrentMask(NodePath& nodePath)
91  {
92  getCurrentCullingSet().popOccludersCurrentMask(nodePath);
93  }
94 
95  inline bool isCulled(const std::vector<Vec3>& vertices)
96  {
97  return getCurrentCullingSet().isCulled(vertices);
98  }
99 
100  inline bool isCulled(const BoundingBox& bb)
101  {
102  return bb.valid() && getCurrentCullingSet().isCulled(bb);
103  }
104 
105  inline bool isCulled(const BoundingSphere& bs)
106  {
107  return getCurrentCullingSet().isCulled(bs);
108  }
109 
110  inline bool isCulled(const osg::Node& node)
111  {
112  if (node.isCullingActive())
113  {
114  return getCurrentCullingSet().isCulled(node.getBound());
115  }
116  else
117  {
118  getCurrentCullingSet().resetCullingMask();
119  return false;
120  }
121  }
122 
123  inline void pushCurrentMask()
124  {
125  getCurrentCullingSet().pushCurrentMask();
126  }
127 
128  inline void popCurrentMask()
129  {
130  getCurrentCullingSet().popCurrentMask();
131  }
132 
133 
134  typedef std::vector< CullingSet > CullingStack;
135 
136  inline CullingStack& getClipSpaceCullingStack() { return _clipspaceCullingStack; }
137 
138  inline CullingStack& getProjectionCullingStack() { return _projectionCullingStack; }
139 
140  inline CullingStack& getModelViewCullingStack() { return _modelviewCullingStack; }
141 
142  inline CullingSet& getCurrentCullingSet() { return *_back_modelviewCullingStack; }
143  inline const CullingSet& getCurrentCullingSet() const { return *_back_modelviewCullingStack; }
144 
145  inline osg::Viewport* getViewport();
146  inline osg::RefMatrix* getModelViewMatrix();
147  inline osg::RefMatrix* getProjectionMatrix();
148  inline osg::Matrix getWindowMatrix();
149  inline const osg::RefMatrix* getMVPW();
150 
151  inline const osg::Vec3& getReferenceViewPoint() const { return _referenceViewPoints.back(); }
152  inline void pushReferenceViewPoint(const osg::Vec3& viewPoint) { _referenceViewPoints.push_back(viewPoint); }
153  inline void popReferenceViewPoint() { _referenceViewPoints.pop_back(); }
154 
155  inline const osg::Vec3& getEyeLocal() const { return _eyePointStack.back(); }
156 
157  inline const osg::Vec3& getViewPointLocal() const { return _viewPointStack.back(); }
158 
159  inline const osg::Vec3 getUpLocal() const
160  {
161  const osg::Matrix& matrix = *_modelviewStack.back();
162  return osg::Vec3(matrix(0,1),matrix(1,1),matrix(2,1));
163  }
164 
165  inline const osg::Vec3 getLookVectorLocal() const
166  {
167  const osg::Matrix& matrix = *_modelviewStack.back();
168  return osg::Vec3(-matrix(0,2),-matrix(1,2),-matrix(2,2));
169  }
170 
171 
172  protected:
173 
174  // base set of shadow volume occluder to use in culling.
176 
178 
179  MatrixStack _projectionStack;
180 
181  MatrixStack _modelviewStack;
182  MatrixStack _MVPW_Stack;
183 
185  ViewportStack _viewportStack;
186 
188  EyePointStack _referenceViewPoints;
189  EyePointStack _eyePointStack;
190  EyePointStack _viewPointStack;
191 
194 
198 
199  void computeFrustumVolume();
201 
202  unsigned int _bbCornerNear;
203  unsigned int _bbCornerFar;
204 
206 
207  typedef std::vector< osg::ref_ptr<osg::RefMatrix> > MatrixList;
208  MatrixList _reuseMatrixList;
210 
211  inline osg::RefMatrix* createOrReuseMatrix(const osg::Matrix& value);
212 
213 
214 };
215 
217 {
218  if (!_viewportStack.empty())
219  {
220  return _viewportStack.back().get();
221  }
222  else
223  {
224  return 0L;
225  }
226 }
227 
229 {
230  if (!_modelviewStack.empty())
231  {
232  return _modelviewStack.back().get();
233  }
234  else
235  {
236  return _identity.get();
237  }
238 }
239 
241 {
242  if (!_projectionStack.empty())
243  {
244  return _projectionStack.back().get();
245  }
246  else
247  {
248  return _identity.get();
249  }
250 }
251 
253 {
254  if (!_viewportStack.empty())
255  {
256  osg::Viewport* viewport = _viewportStack.back().get();
257  return viewport->computeWindowMatrix();
258  }
259  else
260  {
261  return *_identity;
262  }
263 }
264 
266 {
267  if (!_MVPW_Stack.empty())
268  {
269  if (!_MVPW_Stack.back())
270  {
272  (*_MVPW_Stack.back()) *= *(getProjectionMatrix());
273  (*_MVPW_Stack.back()) *= getWindowMatrix();
274  }
275  return _MVPW_Stack.back().get();
276  }
277  else
278  {
279  return _identity.get();
280  }
281 }
282 
284 {
285  // skip of any already reused matrix.
287  _reuseMatrixList[_currentReuseMatrixIndex]->referenceCount()>1)
288  {
290  }
291 
292  // if still within list, element must be singularly referenced
293  // there return it to be reused.
295  {
297  matrix->set(value);
298  return matrix;
299  }
300 
301  // otherwise need to create new matrix.
302  osg::RefMatrix* matrix = new RefMatrix(value);
303  _reuseMatrixList.push_back(matrix);
305  return matrix;
306 }
307 
308 } // end of namespace
309 
310 #endif
bool valid() const
Definition: BoundingBox.h:86
CullingStack & getProjectionCullingStack()
Definition: CullStack.h:138
bool isCulled(const BoundingSphere &bs)
Definition: CullStack.h:105
RefMatrixd RefMatrix
Definition: Matrix.h:28
#define OSG_EXPORT
Definition: Export.h:43
float getFrustumVolume()
Definition: CullStack.h:58
std::vector< ShadowVolumeOccluder > ShadowVolumeOccluderList
std::vector< CullingSet > CullingStack
Definition: CullStack.h:134
const osg::Vec3 & getEyeLocal() const
Definition: CullStack.h:155
unsigned int _bbCornerFar
Definition: CullStack.h:203
CullingStack & getClipSpaceCullingStack()
Definition: CullStack.h:136
const osg::Matrix computeWindowMatrix() const
Definition: Viewport.h:129
void popReferenceViewPoint()
Definition: CullStack.h:153
void disableAndPushOccludersCurrentMask(NodePath &nodePath)
Definition: CullStack.h:85
float clampedPixelSize(const Vec3 &v, float radius) const
Definition: CullStack.h:74
CullingStack _clipspaceCullingStack
Definition: CullStack.h:192
const BoundingSphere & getBound() const
Definition: Node.h:412
EyePointStack _eyePointStack
Definition: CullStack.h:189
fast_back_stack< ref_ptr< RefMatrix > > MatrixStack
Definition: CullStack.h:177
CullingSet & getCurrentCullingSet()
Definition: CullStack.h:142
float clampedPixelSize(const BoundingSphere &bs) const
Definition: CullStack.h:80
const osg::Vec3 & getViewPointLocal() const
Definition: CullStack.h:157
void popCurrentMask()
Definition: CullStack.h:128
const osg::RefMatrix * getMVPW()
Definition: CullStack.h:265
fast_back_stack< ref_ptr< Viewport > > ViewportStack
Definition: CullStack.h:184
void setOccluderList(const ShadowVolumeOccluderList &svol)
Definition: CullStack.h:45
CullingStack & getModelViewCullingStack()
Definition: CullStack.h:140
MatrixStack _projectionStack
Definition: CullStack.h:179
float _frustumVolume
Definition: CullStack.h:200
ref_ptr< osg::RefMatrix > _identity
Definition: CullStack.h:205
const ShadowVolumeOccluderList & getOccluderList() const
Definition: CullStack.h:47
CullingStack _projectionCullingStack
Definition: CullStack.h:193
osg::Matrix getWindowMatrix()
Definition: CullStack.h:252
Vec3f Vec3
Definition: Vec3.h:21
bool isCulled(const std::vector< Vec3 > &vertices)
Definition: CullStack.h:95
unsigned int _index_modelviewCullingStack
Definition: CullStack.h:196
T * get() const
Definition: ref_ptr.h:92
unsigned int _bbCornerNear
Definition: CullStack.h:202
void pushCurrentMask()
Definition: CullStack.h:123
bool isCulled(const BoundingBox &bb)
Definition: CullStack.h:100
EyePointStack _viewPointStack
Definition: CullStack.h:190
float pixelSize(const Vec3 &v, float radius) const
Definition: CullStack.h:62
fast_back_stack< Vec3 > EyePointStack
Definition: CullStack.h:187
std::vector< ShadowVolumeOccluder > OccluderList
Definition: CullStack.h:38
const osg::Vec3 getUpLocal() const
Definition: CullStack.h:159
void pushReferenceViewPoint(const osg::Vec3 &viewPoint)
Definition: CullStack.h:152
ShadowVolumeOccluderList _occluderList
Definition: CullStack.h:175
const CullingSet & getCurrentCullingSet() const
Definition: CullStack.h:143
Definition: Node.h:71
Definition: AlphaFunc.h:19
bool isCulled(const osg::Node &node)
Definition: CullStack.h:110
bool isCullingActive() const
Definition: Node.h:326
CullingStack _modelviewCullingStack
Definition: CullStack.h:195
MatrixList _reuseMatrixList
Definition: CullStack.h:208
unsigned int _currentReuseMatrixIndex
Definition: CullStack.h:209
void set(const Matrixd &rhs)
Definition: Matrixd.h:71
ViewportStack _viewportStack
Definition: CullStack.h:185
EyePointStack _referenceViewPoints
Definition: CullStack.h:188
osg::Viewport * getViewport()
Definition: CullStack.h:216
osg::RefMatrix * getProjectionMatrix()
Definition: CullStack.h:240
CullingSet * _back_modelviewCullingStack
Definition: CullStack.h:197
const osg::Vec3 & getReferenceViewPoint() const
Definition: CullStack.h:151
MatrixStack _MVPW_Stack
Definition: CullStack.h:182
void popOccludersCurrentMask(NodePath &nodePath)
Definition: CullStack.h:90
MatrixStack _modelviewStack
Definition: CullStack.h:181
const osg::Vec3 getLookVectorLocal() const
Definition: CullStack.h:165
osg::RefMatrix * createOrReuseMatrix(const osg::Matrix &value)
Definition: CullStack.h:283
float pixelSize(const BoundingSphere &bs) const
Definition: CullStack.h:68
std::vector< osg::ref_ptr< osg::RefMatrix > > MatrixList
Definition: CullStack.h:207
osg::RefMatrix * getModelViewMatrix()
Definition: CullStack.h:228
std::vector< Node * > NodePath
Definition: Node.h:44
ShadowVolumeOccluderList & getOccluderList()
Definition: CullStack.h:46