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 |
|
---|
32 | typedef pair<SceneNode *, HardwareOcclusionQuery *> QueryPair;
|
---|
33 | typedef priority_queue<SceneNode *, vector<SceneNode *>, myless<vector<SceneNode *>::value_type> > PriorityQueue;
|
---|
34 | typedef queue<QueryPair> QueryQueue;
|
---|
35 | /**
|
---|
36 | Class which implements a scene mangager which uses occlusion queries for culling occluded objects
|
---|
37 | */
|
---|
38 | class OcclusionCullingSceneTraverser
|
---|
39 | {
|
---|
40 | public:
|
---|
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);
|
---|
46 | ~OcclusionCullingSceneTraverser();
|
---|
47 |
|
---|
48 | enum {RENDER_CULL_FRUSTUM, RENDER_STOP_AND_WAIT, RENDER_COHERENT, NUM_RENDERMODES};
|
---|
49 |
|
---|
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: |
---|
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 |
---|
58 | |
---|
59 | @param cam current camera
|
---|
60 | @param root root of hierarchy
|
---|
61 | */
|
---|
62 | void renderScene( Camera *cam, SceneNode *root );
|
---|
63 |
|
---|
64 |
|
---|
65 | /** Sets the given option for the scene traverser.
|
---|
66 | @remarks
|
---|
67 | Options are:
|
---|
68 | "Algorithm", int *;
|
---|
69 | */
|
---|
70 | bool setOption( const String &, const void * );
|
---|
71 | /** Gets the given option for the scene traverser.
|
---|
72 | @remarks
|
---|
73 | See setOption
|
---|
74 | */
|
---|
75 | bool getOption( const String &, void * );
|
---|
76 | bool getOptionKeys( StringVector &refKeys );
|
---|
77 |
|
---|
78 | /** Sets pointer to the current scene manager.
|
---|
79 | @param the scene manager */
|
---|
80 | void setSceneManager( SceneManager *sm );
|
---|
81 |
|
---|
82 | /** Sets pointer to the current render system
|
---|
83 | @param the rendersystem */
|
---|
84 | void setRenderSystem( RenderSystem *rsys );
|
---|
85 |
|
---|
86 | /** Doing some necessary preprocessing.
|
---|
87 | @comment e.g., initialises occlusion queries */
|
---|
88 | void preprocess( void );
|
---|
89 |
|
---|
90 | /** Sets the current number of scene nodes in the scene.
|
---|
91 | @param num number of scene nodes
|
---|
92 | */
|
---|
93 | void setNumSceneNodes(int num );
|
---|
94 |
|
---|
95 | /** Sets the required number of occlusion queries.
|
---|
96 | @param num number occlusion queries
|
---|
97 | */
|
---|
98 | void setNumQueries( int num );
|
---|
99 |
|
---|
100 | protected:
|
---|
101 | /** query mode (= without color) or RENDER_MODE */
|
---|
102 | enum {MODE_QUERY, MODE_RENDER};
|
---|
103 | /** returns true if node is leaf of the hierarchy */
|
---|
104 | bool isLeaf( SceneNode *node );
|
---|
105 | //HACK
|
---|
106 | //unsigned int countSceneNodes(SceneNode *node);
|
---|
107 | void traverseNode( Camera *cam, SceneNode *node );
|
---|
108 | /** Renders current scene node
|
---|
109 | @param cam current camera
|
---|
110 | @param node current scene node to be rendered
|
---|
111 | */
|
---|
112 | void renderSceneNode( Camera *cam, SceneNode *node);
|
---|
113 | /** Sets rendering mode, e.g. query mode or render mode*/
|
---|
114 | void setRenderingMode( int mode );
|
---|
115 | /** Renders the scene with view frustum culling only. */ |
---|
116 | void renderCullFrustum( Camera *cam ); |
---|
117 | /** Renders the scene with the hierarchical stop and wait algorithm. */ |
---|
118 | void renderStopAndWait( Camera *cam ); |
---|
119 | /** Renders the scene with the coherent hierarchical algorithm and the query queye. */ |
---|
120 | void renderCoherentWithQueue( Camera *cam );
|
---|
121 | /** Issue a occlusion query for this node.
|
---|
122 | @param box the axis aligned bounding box of the node
|
---|
123 | @wasVisible if the node was visible in previous frame
|
---|
124 | */
|
---|
125 | HardwareOcclusionQuery *issueOcclusionQuery( AxisAlignedBox *box, bool wasVisible );
|
---|
126 | /** Pulls up the visibility from the child nodes. */
|
---|
127 | void pullUpVisibility( SceneNode *node );
|
---|
128 | /** delete all previously defined occlusion queries */
|
---|
129 | void deleteQueries();
|
---|
130 | /** Renders bounding box of specified node.
|
---|
131 | @param box the bounding box of the scene node to be rendered */
|
---|
132 | void renderBoundingBox( AxisAlignedBox *box );
|
---|
133 | /** Returns one half of the bounding box.
|
---|
134 | @param half the half index of the bouding box (0 or 1)
|
---|
135 | */
|
---|
136 | SolidHalfBoundingBox *getSolidHalfBoundingBox( int half );
|
---|
137 |
|
---|
138 | // we use a priority queue rather than a renderstack
|
---|
139 | PriorityQueue *mDistanceQueue;
|
---|
140 |
|
---|
141 | std::vector<HardwareOcclusionQuery *> mOcclusionQueries;
|
---|
142 | // two halfes of a aabb
|
---|
143 | SolidHalfBoundingBox *mHalfBoundingBox[2];
|
---|
144 |
|
---|
145 | int mCurrentAlgorithm;
|
---|
146 |
|
---|
147 | unsigned int mFrameId;
|
---|
148 | unsigned int mVisibilityThreshold;
|
---|
149 |
|
---|
150 | SceneManager *mSceneManager;
|
---|
151 | RenderSystem *mRenderSystem;
|
---|
152 |
|
---|
153 | int mCurrentTestIdx;
|
---|
154 | int mQueryMode;
|
---|
155 |
|
---|
156 | unsigned int mNumQueries;
|
---|
157 |
|
---|
158 | //--- statistics |
---|
159 | unsigned int mNumSceneNodes; |
---|
160 | unsigned int mNumTraversedNodes; |
---|
161 | unsigned int mNumQueryCulledNodes; |
---|
162 | unsigned int mNumFrustumCulledNodes; |
---|
163 | unsigned int mNumRenderedGeometry;
|
---|
164 | };
|
---|
165 |
|
---|
166 | }
|
---|
167 | #endif // OCCLUSIONCULLINGSCENEMANAGER_H |
---|