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 *> query_pair;
|
---|
33 | typedef priority_queue<SceneNode *, vector<SceneNode *>, myless<vector<SceneNode *>::value_type> > PriorityQueue;
|
---|
34 | typedef queue<query_pair> 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 | protected:
|
---|
96 | /** query mode (= without color) or RENDER_MODE */
|
---|
97 | enum {MODE_QUERY, MODE_RENDER};
|
---|
98 | /** returns true if node is leaf of the hierarchy */
|
---|
99 | bool isLeaf( SceneNode *node );
|
---|
100 | //HACK
|
---|
101 | //unsigned int countSceneNodes(SceneNode *node);
|
---|
102 | void traverseNode( Camera *cam, SceneNode *node );
|
---|
103 | /** Renders current scene node
|
---|
104 | @param cam current camera
|
---|
105 | @param node current scene node to be rendered
|
---|
106 | */
|
---|
107 | void renderSceneNode( Camera *cam, SceneNode *node);
|
---|
108 | /** Sets rendering mode, e.g. query mode or render mode*/
|
---|
109 | void setRenderingMode( int mode );
|
---|
110 | /** Renders the scene with view frustum culling only. */ |
---|
111 | void renderCullFrustum( Camera *cam ); |
---|
112 | /** Renders the scene with the hierarchical stop and wait algorithm. */ |
---|
113 | void renderStopAndWait( Camera *cam ); |
---|
114 | /** Renders the scene with the coherent hierarchical algorithm and the query queye. */ |
---|
115 | void renderCoherentWithQueue( Camera *cam );
|
---|
116 | /** Issue a occlusion query for this node. */
|
---|
117 | HardwareOcclusionQuery *issueOcclusionQuery( SceneNode *node, bool wasVisible );
|
---|
118 | /** Pulls up the visibility from the child nodes. */
|
---|
119 | void pullUpVisibility( SceneNode *node );
|
---|
120 | /** delete all previously defined occlusion queries */
|
---|
121 | void deleteQueries();
|
---|
122 | /** Renders bounding box of specified node.
|
---|
123 | @param the scene node contained in the bounding box to be rendered
|
---|
124 | */
|
---|
125 | void renderBoundingBox( SceneNode *node );
|
---|
126 | /** Returns one half of the bounding box.
|
---|
127 | @param the half of the bouding box
|
---|
128 | */
|
---|
129 | SolidHalfBoundingBox *getSolidHalfBoundingBox( int half );
|
---|
130 |
|
---|
131 | // we use a priority queue rather than a renderstack
|
---|
132 | PriorityQueue *mDistanceQueue;
|
---|
133 |
|
---|
134 | std::vector<HardwareOcclusionQuery *> mOcclusionQueries;
|
---|
135 | // two halfes of a aabb
|
---|
136 | SolidHalfBoundingBox *mHalfBoundingBox[2];
|
---|
137 |
|
---|
138 | int mCurrentAlgorithm;
|
---|
139 |
|
---|
140 | unsigned int mFrameId;
|
---|
141 | unsigned int mVisibilityThreshold;
|
---|
142 |
|
---|
143 | SceneManager *mSceneManager;
|
---|
144 | RenderSystem *mRenderSystem;
|
---|
145 |
|
---|
146 | int mCurrentTestIdx;
|
---|
147 | int mQueryMode;
|
---|
148 |
|
---|
149 | //--- statistics |
---|
150 | unsigned int mNumSceneNodes; |
---|
151 | unsigned int mNumTraversedNodes; |
---|
152 | unsigned int mNumQueryCulledNodes; |
---|
153 | unsigned int mNumFrustumCulledNodes; |
---|
154 | unsigned int mNumRenderedGeometry;
|
---|
155 | };
|
---|
156 |
|
---|
157 | }
|
---|
158 | #endif // OCCLUSIONCULLINGSCENEMANAGER_H |
---|