OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OcclusionQueryNode.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007 Skew Matrix Software LLC (http://www.skew-matrix.com)
3 //
4 // This library is open source and may be redistributed and/or modified under
5 // the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 // (at your option) any later version. The full license is in LICENSE file
7 // included with this distribution, and on the openscenegraph.org website.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // OpenSceneGraph Public License for more details.
13 //
14 
15 #ifndef OSG_OCCLUSION_QUERY_NODE
16 #define OSG_OCCLUSION_QUERY_NODE 1
17 
18 #include <osg/Export>
19 #include <osg/CopyOp>
20 #include <osg/Group>
21 #include <osg/Geometry>
22 
23 
24 namespace osg {
25 
26 // Create and return a StateSet appropriate for performing an occlusion
27 // query test (disable lighting, texture mapping, etc). Probably some
28 // room for improvement here. Could disable shaders, for example.
30 
31 // Create and return a StateSet for rendering a debug representation of query geometry.
33 
34 // TestResult -- stores (per context) results of an occlusion query
35 // test performed by QueryGeometry. An OcclusionQueryNode has a
36 // Geode owning a single QueryGeometry that
37 // draws the occlusion query geometry. QueryGeometry keeps a
38 // TestResult per context to store the result/status of each query.
39 // Accessed during the cull and draw traversals.
41 {
42 public:
43  TestResult() : _init( false ), _id( 0 ), _contextID( 0 ), _active( false ), _numPixels( 0 ) {}
45 
46  bool _init;
47 
48  // Query ID for this context.
49  GLuint _id;
50  // Context ID owning this query ID.
51  unsigned int _contextID;
52 
53  // Set to true when a query gets issued and set to
54  // false when the result is retrieved.
55  mutable bool _active;
56 
57  // Result of last query.
58  GLint _numPixels;
59 };
60 
61 // QueryGeometry -- A Drawable that performs an occlusion query,
62 // using its geometric data as the query geometry.
64 {
65 public:
66  QueryGeometry( const std::string& oqnName=std::string("") );
67  ~QueryGeometry();
68 
69  void reset();
70 
71  // TBD implement copy constructor
72 
73  virtual void drawImplementation( osg::RenderInfo& renderInfo ) const;
74 
75  unsigned int getNumPixels( const osg::Camera* cam );
76 
77  virtual void releaseGLObjects( osg::State* state = 0 ) const;
78 
79  static void deleteQueryObject( unsigned int contextID, GLuint handle );
80  static void flushDeletedQueryObjects( unsigned int contextID, double currentTime, double& availableTime );
81  static void discardDeletedQueryObjects( unsigned int contextID );
82 
83 protected:
84  typedef std::map< const osg::Camera*, TestResult > ResultMap;
85  mutable ResultMap _results;
87 
88  // Needed for debug only
89  std::string _oqnName;
90 };
91 
92 // This Node performs occlusion query testing on its children.
93 // You can use it directly to occlusion query test a portion
94 // of your scene graph, or you can use it implicitly with an
95 // OcclusionQueryRoot, which places OcclusionQueryNodes where
96 // needed and acts as a master control.
98 {
99 public:
101 
102  // Copy constructor using CopyOp to manage deep vs shallow copy.
104 
106 
107  virtual osg::BoundingSphere computeBound() const;
108 
109  virtual void releaseGLObjects( osg::State* state = 0 ) const;
110 
111 
112  // When disabled, OQN doesn't perform occlusion queries, and simply
113  // renders its children.
114  void setQueriesEnabled( bool enable=true );
115  bool getQueriesEnabled() const { return _enabled; }
116 
117 
118  // Sets/gets the visibility threshold. If the test indicates that
119  // the number of visible pixels is less than the specified
120  // threshold, don't draw the actual geometry.
121  void setVisibilityThreshold( unsigned int pixels ) { _visThreshold = pixels; }
122  unsigned int getVisibilityThreshold() const { return _visThreshold; }
123 
124  // Specifies how many frames to wait before issuing another query.
125  void setQueryFrameCount( unsigned int frames ) { _queryFrameCount = frames; }
126  unsigned int getQueryFrameCount() const { return _queryFrameCount; }
127 
128  // Indicate whether or not the bounding box used in the occlusion query test
129  // should be rendered. Handy for debugging and development.
130  // Should only be called outside of cull/draw. No thread issues.
131  void setDebugDisplay( bool enable );
132  bool getDebugDisplay() const;
133 
134 
135  // Set and get the StateSet used by the OcclusionQueryNode
136  // when rendering the query geometry. OQN creates its own by
137  // default, but if you use many OQNs you might want to use
138  // this method to set all OQNs to use the same StateSet
139  // for more efficient processing.
140  void setQueryStateSet( osg::StateSet* ss );
141  osg::StateSet* getQueryStateSet();
142  const osg::StateSet* getQueryStateSet() const;
143 
144  // Get the QueryGeometry object used for occlusion query. Returns 0 if no QueryGeometry is created.
145  osg::QueryGeometry* getQueryGeometry();
146  const osg::QueryGeometry* getQueryGeometry() const;
147 
148  // Set and get the StateSet used by the OcclusionQueryNode
149  // when rendering the debug query geometry (see setDebugDisplay).
150  void setDebugStateSet( osg::StateSet* ss );
151  osg::StateSet* getDebugStateSet();
152  const osg::StateSet* getDebugStateSet() const;
153 
154  // For statistics gathering, e.g., by a NodeVisitor.
155  bool getPassed() const;
156 
157 
158  // These methods are public so that osgUtil::CullVisitor can access them.
159  // Not intended for application use.
160  virtual bool getPassed( const osg::Camera* camera, osg::NodeVisitor& nv );
161  void traverseQuery( const osg::Camera* camera, osg::NodeVisitor& nv );
162  void traverseDebug( osg::NodeVisitor& nv );
163 
164 
165  // Delete unused query IDs for this contextID.
166  static void flushDeletedQueryObjects( unsigned int contextID, double currentTime, double& availableTime );
167 
168  // discard all the cached query objects which need to be deleted
169  // in the OpenGL context related to contextID.
170  // Note, unlike flush no OpenGL calls are made, instead the handles are all removed.
171  // this call is useful for when an OpenGL context has been destroyed.
172  static void discardDeletedQueryObjects( unsigned int contextID );
173 
174 protected:
175  virtual ~OcclusionQueryNode();
176 
177  virtual void createSupportNodes();
178 
181 
182  bool _enabled;
183 
184  // Tracks the last frame number that we performed a query.
185  // User can set how many times (See setQueryFrameCount).
186  typedef std::map< const osg::Camera*, unsigned int > FrameCountMap;
187  FrameCountMap _frameCountMap;
189 
190  // For statistics gathering
191  bool _passed;
192 
193  // User-settable variables
194  unsigned int _visThreshold;
195  unsigned int _queryFrameCount;
196  bool _debugBB;
197 
198 
199  // Required to ensure that computeBound() is thread-safe.
201 };
202 
203 }
204 
205 
206 #endif
OpenThreads::Mutex _mapMutex
std::map< const osg::Camera *, TestResult > ResultMap
#define OSG_EXPORT
Definition: Export.h:43
osg::ref_ptr< osg::Geode > _debugGeode
osg::StateSet * initOQDebugState()
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
std::map< const osg::Camera *, unsigned int > FrameCountMap
OpenThreads::Mutex _frameCountMutex
osg::StateSet * initOQState()
#define META_Node(library, name)
Definition: Node.h:59
void setVisibilityThreshold(unsigned int pixels)
void setQueryFrameCount(unsigned int frames)
osg::ref_ptr< osg::Geode > _queryGeode
OpenThreads::Mutex _computeBoundMutex
unsigned int getVisibilityThreshold() const
unsigned int getQueryFrameCount() const
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: GLU.h:71
Definition: AlphaFunc.h:19
unsigned int _contextID