| 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 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | namespace osg { |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | // This Node performs occlusion query testing on its children. |
|---|
| 28 | // You can use it directly to occlusion query test a portion |
|---|
| 29 | // of your scene graph, or you can use it implicitly with an |
|---|
| 30 | // OcclusionQueryRoot, which places OcclusionQueryNodes where |
|---|
| 31 | // needed and acts as a master control. |
|---|
| 32 | class OSG_EXPORT OcclusionQueryNode : public osg::Group |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | OcclusionQueryNode(); |
|---|
| 36 | |
|---|
| 37 | // Copy constructor using CopyOp to manage deep vs shallow copy. |
|---|
| 38 | OcclusionQueryNode( const OcclusionQueryNode& oqn, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY ); |
|---|
| 39 | |
|---|
| 40 | META_Node( osg, OcclusionQueryNode ); |
|---|
| 41 | |
|---|
| 42 | virtual osg::BoundingSphere computeBound() const; |
|---|
| 43 | |
|---|
| 44 | virtual void releaseGLObjects( osg::State* state = 0 ) const; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | // When disabled, OQN doesn't perform occlusion queries, and simply |
|---|
| 48 | // renders its children. |
|---|
| 49 | void setQueriesEnabled( bool enable=true ); |
|---|
| 50 | bool getQueriesEnabled() const { return _enabled; } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | // Sets/gets the visibility threshold. If the test indicates that |
|---|
| 54 | // the number of visible pixels is less than the specified |
|---|
| 55 | // threshold, don't draw the actual geometry. |
|---|
| 56 | void setVisibilityThreshold( unsigned int pixels ) { _visThreshold = pixels; } |
|---|
| 57 | unsigned int getVisibilityThreshold() const { return _visThreshold; } |
|---|
| 58 | |
|---|
| 59 | // Specifies how many frames to wait before issuing another query. |
|---|
| 60 | void setQueryFrameCount( int frames ) { _queryFrameCount = frames; } |
|---|
| 61 | int getQueryFrameCount() const { return _queryFrameCount; } |
|---|
| 62 | |
|---|
| 63 | // Indicate whether or not the bounding box used in the occlusion query test |
|---|
| 64 | // should be rendered. Handy for debugging and development. |
|---|
| 65 | // Should only be called outside of cull/draw. No thread issues. |
|---|
| 66 | void setDebugDisplay( bool enable ); |
|---|
| 67 | bool getDebugDisplay() const; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | // Set and get the StateSet used by the OcclusionQueryNode |
|---|
| 71 | // when rendering the query geometry. OQN creates its own by |
|---|
| 72 | // default, but if you use many OQNs you might want to use |
|---|
| 73 | // this method to set all OQNs to use the same StateSet |
|---|
| 74 | // for more efficient processing. |
|---|
| 75 | void setQueryStateSet( osg::StateSet* ss ); |
|---|
| 76 | osg::StateSet* getQueryStateSet(); |
|---|
| 77 | const osg::StateSet* getQueryStateSet() const; |
|---|
| 78 | |
|---|
| 79 | // Set and get the StateSet used by the OcclusionQueryNode |
|---|
| 80 | // when rendering the debug query geometry (see setDebugDisplay). |
|---|
| 81 | void setDebugStateSet( osg::StateSet* ss ); |
|---|
| 82 | osg::StateSet* getDebugStateSet(); |
|---|
| 83 | const osg::StateSet* getDebugStateSet() const; |
|---|
| 84 | |
|---|
| 85 | // For statistics gathering, e.g., by a NodeVisitor. |
|---|
| 86 | bool getPassed() const; |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | // These methods are public so that osgUtil::CullVisitor can access them. |
|---|
| 90 | // Not intended for application use. |
|---|
| 91 | bool getPassed( const osg::Camera* camera, osg::NodeVisitor& nv ); |
|---|
| 92 | void traverseQuery( const osg::Camera* camera, osg::NodeVisitor& nv ); |
|---|
| 93 | void traverseDebug( osg::NodeVisitor& nv ); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | // Delete unused query IDs for this contextID. |
|---|
| 97 | static void flushDeletedQueryObjects( unsigned int contextID, double currentTime, double& availableTime ); |
|---|
| 98 | |
|---|
| 99 | // discard all the cached query objects which need to be deleted |
|---|
| 100 | // in the OpenGL context related to contextID. |
|---|
| 101 | // Note, unlike flush no OpenGL calls are made, instead the handles are all removed. |
|---|
| 102 | // this call is useful for when an OpenGL context has been destroyed. |
|---|
| 103 | static void discardDeletedQueryObjects( unsigned int contextID ); |
|---|
| 104 | |
|---|
| 105 | protected: |
|---|
| 106 | virtual ~OcclusionQueryNode(); |
|---|
| 107 | |
|---|
| 108 | void createSupportNodes(); |
|---|
| 109 | |
|---|
| 110 | osg::ref_ptr< osg::Geode > _queryGeode; |
|---|
| 111 | osg::ref_ptr< osg::Geode > _debugGeode; |
|---|
| 112 | |
|---|
| 113 | bool _enabled; |
|---|
| 114 | |
|---|
| 115 | // Tracks the last frame number that we performed a query. |
|---|
| 116 | // User can set how many times (See setQueryFrameCount). |
|---|
| 117 | typedef std::map< const osg::Camera*, int > FrameCountMap; |
|---|
| 118 | FrameCountMap _frameCountMap; |
|---|
| 119 | mutable OpenThreads::Mutex _frameCountMutex; |
|---|
| 120 | |
|---|
| 121 | // For statistics gathering |
|---|
| 122 | bool _passed; |
|---|
| 123 | |
|---|
| 124 | // User-settable variables |
|---|
| 125 | unsigned int _visThreshold; |
|---|
| 126 | int _queryFrameCount; |
|---|
| 127 | bool _debugBB; |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | // Required to ensure that computeBound() is thread-safe. |
|---|
| 131 | mutable OpenThreads::Mutex _computeBoundMutex; |
|---|
| 132 | }; |
|---|
| 133 | |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | #endif |
|---|