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 SceneTraverser
|
---|
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 | SceneTraverser(SceneManager *sm, RenderSystem *rsys);
|
---|
46 | ~SceneTraverser();
|
---|
47 |
|
---|
48 | /** Sets pointer to the current scene manager.
|
---|
49 | @param the scene manager */
|
---|
50 | void setSceneManager( SceneManager *sm );
|
---|
51 |
|
---|
52 | /** Sets pointer to the current render system
|
---|
53 | @param the rendersystem */
|
---|
54 | void setRenderSystem( RenderSystem *rsys );
|
---|
55 |
|
---|
56 | /** Sets the root of the scene hierarchy. */
|
---|
57 | void setSceneRoot(SceneNode *root);
|
---|
58 |
|
---|
59 | /** Doing some necessary preprocessing.
|
---|
60 | @comment e.g., initialises occlusion queries */
|
---|
61 | //void preprocess( void );
|
---|
62 |
|
---|
63 | protected:
|
---|
64 |
|
---|
65 | /** returns true if current node is leaf of the hierarchy */
|
---|
66 | bool isLeaf( ) = 0;
|
---|
67 |
|
---|
68 | void traverseNode( HierarchyNode *node);
|
---|
69 | /** Renders current scene node
|
---|
70 | @param cam current camera
|
---|
71 | @param node current scene node to be rendered
|
---|
72 | */
|
---|
73 | void renderNode( HierarchyNode *node);
|
---|
74 | |
---|
75 | /** Issue a occlusion query for this node.
|
---|
76 | @param box the axis aligned bounding box of the node
|
---|
77 | @wasVisible if the node was visible in previous frame
|
---|
78 | */
|
---|
79 | HardwareOcclusionQuery *issueOcclusionQuery( AxisAlignedBox *box, bool wasVisible );
|
---|
80 | /** Returns next available occlusion query or creates new one.
|
---|
81 | @return the next occlusion query
|
---|
82 | */
|
---|
83 | HardwareOcclusionQuery *getNextOcclusionQuery(void);
|
---|
84 | /** Pulls up the visibility from the child node.
|
---|
85 | @param the child node
|
---|
86 | */
|
---|
87 | void pullUpVisibility(SceneNode *node);
|
---|
88 | /** delete all previously defined occlusion queries */
|
---|
89 | void deleteQueries();
|
---|
90 | /** Renders bounding box of specified node.
|
---|
91 | @param box the bounding box of the scene node to be rendered */
|
---|
92 | void renderBoundingBox( AxisAlignedBox *box );
|
---|
93 | /** Returns one half of the bounding box.
|
---|
94 | @param half the half index of the bouding box (0 or 1)
|
---|
95 | */
|
---|
96 | SolidHalfBoundingBox *getSolidHalfBoundingBox( int half );
|
---|
97 |
|
---|
98 | /** Initialises the distance queue. */
|
---|
99 | virtual void initDistanceQueue(Camera *cam);
|
---|
100 |
|
---|
101 | std::vector<HardwareOcclusionQuery *> mOcclusionQueries;
|
---|
102 | // two halfes of a aabb
|
---|
103 | SolidHalfBoundingBox *mHalfBoundingBox[2];
|
---|
104 |
|
---|
105 | int mCurrentAlgorithm;
|
---|
106 |
|
---|
107 | unsigned int mFrameId;
|
---|
108 | unsigned int mVisibilityThreshold;
|
---|
109 |
|
---|
110 | SceneManager *mSceneManager;
|
---|
111 | RenderSystem *mRenderSystem;
|
---|
112 |
|
---|
113 | int mCurrentTestIdx;
|
---|
114 | int mQueryMode;
|
---|
115 |
|
---|
116 | unsigned int mNumQueries;
|
---|
117 |
|
---|
118 | //--- statistics |
---|
119 | unsigned int mNumSceneNodes; |
---|
120 | unsigned int mNumTraversedNodes; |
---|
121 | unsigned int mNumQueryCulledNodes; |
---|
122 | unsigned int mNumFrustumCulledNodes; |
---|
123 | unsigned int mNumRenderedGeometry;
|
---|
124 | unsigned int mNumRenderedNodes;
|
---|
125 |
|
---|
126 | private:
|
---|
127 | // the scene root
|
---|
128 | HierarchyNode *mHierarchyRoot;
|
---|
129 | // we use a priority queue ordered by distance
|
---|
130 | PriorityQueue *mDistanceQueue;
|
---|
131 | };
|
---|
132 |
|
---|
133 | }
|
---|
134 | #endif // SCENETRAVERSER_H |
---|