[32] | 1 | #ifndef _OcclusionCullingSceneTraverser_H__
|
---|
| 2 | #define _OcclusionCullingSceneTraverser_H__
|
---|
| 3 |
|
---|
| 4 | #include "OgreSceneNode.h"
|
---|
| 5 | #include "OgreSceneManager.h"
|
---|
| 6 | #include "OgrePrerequisites.h"
|
---|
| 7 | #include "OgreSolidHalfBoundingBox.h"
|
---|
| 8 | #include "OgreCamera.h"
|
---|
| 9 | #include "OgreRenderSystem.h"
|
---|
| 10 | #include <queue>
|
---|
| 11 |
|
---|
| 12 | using namespace std; |
---|
| 13 | |
---|
| 14 | namespace Ogre { |
---|
| 15 | /** |
---|
| 16 | This class implements the compare operator for the priority queue. |
---|
| 17 | a lower distance has a higher value in the queue |
---|
| 18 | */ |
---|
| 19 | template <typename T> class myless
|
---|
| 20 | {
|
---|
| 21 | public:
|
---|
| 22 | myless(Camera *cam) { mCamera = cam; } |
---|
| 23 | //bool operator() (HierarchyNode *v1, HierarchyNode *v2) const
|
---|
| 24 | bool operator() (T v1, T v2) const
|
---|
| 25 | {
|
---|
| 26 | return v1->getSquaredViewDepth(mCamera) > v2->getSquaredViewDepth(mCamera);
|
---|
| 27 | }
|
---|
| 28 | private:
|
---|
| 29 | Camera *mCamera;
|
---|
| 30 | };
|
---|
| 31 |
|
---|
[39] | 32 | typedef pair<SceneNode *, HardwareOcclusionQuery *> QueryPair;
|
---|
[32] | 33 | typedef priority_queue<SceneNode *, vector<SceneNode *>, myless<vector<SceneNode *>::value_type> > PriorityQueue;
|
---|
[39] | 34 | typedef queue<QueryPair> QueryQueue;
|
---|
[32] | 35 | /**
|
---|
| 36 | Class which implements a scene mangager which uses occlusion queries for culling occluded objects
|
---|
| 37 | */
|
---|
| 38 | class OcclusionCullingSceneTraverser
|
---|
| 39 | {
|
---|
| 40 | public:
|
---|
[33] | 41 | /** Construction taking the current scene manager and the current rendersystem as argument
|
---|
| 42 | @param sm current scene manager
|
---|
| 43 | @param rsys current render system
|
---|
| 44 | */
|
---|
| 45 | OcclusionCullingSceneTraverser(SceneManager *sm, RenderSystem *rsys);
|
---|
[32] | 46 | ~OcclusionCullingSceneTraverser();
|
---|
| 47 |
|
---|
| 48 | enum {RENDER_CULL_FRUSTUM, RENDER_STOP_AND_WAIT, RENDER_COHERENT, NUM_RENDERMODES};
|
---|
| 49 |
|
---|
[33] | 50 | /** Renders the scene with the specified algorithm |
---|
| 51 | @comment |
---|
| 52 | The algorithm type can be set with the parameter "Algorithm" and setOption. |
---|
| 53 | |
---|
| 54 | The algorithm is one of: |
---|
[32] | 55 | RENDER_CULL_FRUSTUM: renders the scene with view frustum culling only |
---|
| 56 | RENDER_STOP_AND_WAIT: renders the scene with the hierarchical stop and wait algorithm |
---|
| 57 | RENDER_COHERENT: renders the scene with the coherent hierarchical algorithm |
---|
[33] | 58 | |
---|
| 59 | @param cam current camera
|
---|
[32] | 60 | */
|
---|
[42] | 61 | void renderScene( Camera *cam );
|
---|
[33] | 62 |
|
---|
| 63 | /** Sets the given option for the scene traverser.
|
---|
[32] | 64 | @remarks
|
---|
| 65 | Options are:
|
---|
| 66 | "Algorithm", int *;
|
---|
| 67 | */
|
---|
| 68 | bool setOption( const String &, const void * );
|
---|
[33] | 69 | /** Gets the given option for the scene traverser.
|
---|
[32] | 70 | @remarks
|
---|
| 71 | See setOption
|
---|
| 72 | */
|
---|
| 73 | bool getOption( const String &, void * );
|
---|
[33] | 74 | bool getOptionKeys( StringVector &refKeys );
|
---|
| 75 |
|
---|
| 76 | /** Sets pointer to the current scene manager.
|
---|
| 77 | @param the scene manager */
|
---|
[32] | 78 | void setSceneManager( SceneManager *sm );
|
---|
[33] | 79 |
|
---|
| 80 | /** Sets pointer to the current render system
|
---|
| 81 | @param the rendersystem */
|
---|
[32] | 82 | void setRenderSystem( RenderSystem *rsys );
|
---|
[33] | 83 |
|
---|
| 84 | /** Sets the current number of scene nodes in the scene.
|
---|
| 85 | @param num number of scene nodes
|
---|
| 86 | */
|
---|
| 87 | void setNumSceneNodes(int num );
|
---|
| 88 |
|
---|
[42] | 89 | /** Sets the root of the scene hierarchy. */
|
---|
| 90 | void setSceneRoot(SceneNode *root);
|
---|
| 91 |
|
---|
[41] | 92 | /** Sets the required number of occlusion queries.
|
---|
| 93 | @param num number occlusion queries
|
---|
| 94 | */
|
---|
[42] | 95 | //void setNumQueries( int num );
|
---|
[41] | 96 |
|
---|
[42] | 97 | /** Doing some necessary preprocessing.
|
---|
| 98 | @comment e.g., initialises occlusion queries */
|
---|
| 99 | //void preprocess( void );
|
---|
| 100 |
|
---|
[32] | 101 | protected:
|
---|
[33] | 102 | /** query mode (= without color) or RENDER_MODE */
|
---|
[32] | 103 | enum {MODE_QUERY, MODE_RENDER};
|
---|
| 104 | /** returns true if node is leaf of the hierarchy */
|
---|
| 105 | bool isLeaf( SceneNode *node );
|
---|
| 106 | //HACK
|
---|
| 107 | //unsigned int countSceneNodes(SceneNode *node);
|
---|
[39] | 108 | void traverseNode( Camera *cam, SceneNode *node );
|
---|
[32] | 109 | /** Renders current scene node
|
---|
| 110 | @param cam current camera
|
---|
| 111 | @param node current scene node to be rendered
|
---|
| 112 | */
|
---|
| 113 | void renderSceneNode( Camera *cam, SceneNode *node);
|
---|
| 114 | /** Sets rendering mode, e.g. query mode or render mode*/
|
---|
| 115 | void setRenderingMode( int mode );
|
---|
[42] | 116 | |
---|
| 117 | |
---|
[32] | 118 | /** Renders the scene with view frustum culling only. */ |
---|
[42] | 119 | virtual void renderCullFrustum( Camera *cam ); |
---|
[32] | 120 | /** Renders the scene with the hierarchical stop and wait algorithm. */ |
---|
[42] | 121 | virtual void renderStopAndWait( Camera *cam ); |
---|
[32] | 122 | /** Renders the scene with the coherent hierarchical algorithm and the query queye. */ |
---|
[42] | 123 | virtual void renderCoherentWithQueue( Camera *cam );
|
---|
| 124 |
|
---|
[41] | 125 | /** Issue a occlusion query for this node.
|
---|
| 126 | @param box the axis aligned bounding box of the node
|
---|
| 127 | @wasVisible if the node was visible in previous frame
|
---|
| 128 | */
|
---|
| 129 | HardwareOcclusionQuery *issueOcclusionQuery( AxisAlignedBox *box, bool wasVisible );
|
---|
[42] | 130 | /** Returns next available occlusion query or creates new one.
|
---|
| 131 | @return the next occlusion query
|
---|
| 132 | */
|
---|
| 133 | HardwareOcclusionQuery *getNextOcclusionQuery(void);
|
---|
[52] | 134 | /** Pulls up the visibility from the child node.
|
---|
| 135 | @param the child node
|
---|
| 136 | */
|
---|
| 137 | void pullUpVisibility(SceneNode *node);
|
---|
[32] | 138 | /** delete all previously defined occlusion queries */
|
---|
| 139 | void deleteQueries();
|
---|
| 140 | /** Renders bounding box of specified node.
|
---|
[41] | 141 | @param box the bounding box of the scene node to be rendered */
|
---|
| 142 | void renderBoundingBox( AxisAlignedBox *box );
|
---|
[32] | 143 | /** Returns one half of the bounding box.
|
---|
[41] | 144 | @param half the half index of the bouding box (0 or 1)
|
---|
[32] | 145 | */
|
---|
| 146 | SolidHalfBoundingBox *getSolidHalfBoundingBox( int half );
|
---|
| 147 |
|
---|
[42] | 148 | /** Initialises the distance queue. */
|
---|
| 149 | virtual void initDistanceQueue(Camera *cam);
|
---|
[32] | 150 |
|
---|
| 151 | std::vector<HardwareOcclusionQuery *> mOcclusionQueries;
|
---|
| 152 | // two halfes of a aabb
|
---|
| 153 | SolidHalfBoundingBox *mHalfBoundingBox[2];
|
---|
| 154 |
|
---|
| 155 | int mCurrentAlgorithm;
|
---|
| 156 |
|
---|
| 157 | unsigned int mFrameId;
|
---|
| 158 | unsigned int mVisibilityThreshold;
|
---|
| 159 |
|
---|
| 160 | SceneManager *mSceneManager;
|
---|
| 161 | RenderSystem *mRenderSystem;
|
---|
[42] | 162 |
|
---|
[32] | 163 | int mCurrentTestIdx;
|
---|
| 164 | int mQueryMode;
|
---|
| 165 |
|
---|
[41] | 166 | unsigned int mNumQueries;
|
---|
| 167 |
|
---|
[32] | 168 | //--- statistics |
---|
| 169 | unsigned int mNumSceneNodes; |
---|
| 170 | unsigned int mNumTraversedNodes; |
---|
| 171 | unsigned int mNumQueryCulledNodes; |
---|
| 172 | unsigned int mNumFrustumCulledNodes; |
---|
| 173 | unsigned int mNumRenderedGeometry;
|
---|
[51] | 174 | unsigned int mNumRenderedNodes;
|
---|
[52] | 175 |
|
---|
[42] | 176 | private:
|
---|
| 177 | // the scene root
|
---|
| 178 | SceneNode *mSceneRoot;
|
---|
| 179 | // we use a priority queue ordered by distance
|
---|
| 180 | PriorityQueue *mDistanceQueue;
|
---|
[32] | 181 | };
|
---|
| 182 |
|
---|
| 183 | }
|
---|
| 184 | #endif // OCCLUSIONCULLINGSCENEMANAGER_H |
---|