1 | #include "OgreOcclusionCullingSceneTraverser.h"
|
---|
2 | #include "OgreMath.h"
|
---|
3 | #include "OgreIteratorWrappers.h"
|
---|
4 | #include "OgreCamera.h"
|
---|
5 | #include "OgreHardwareOcclusionQuery.h"
|
---|
6 | #include "OgreSolidHalfBoundingBox.h"
|
---|
7 |
|
---|
8 |
|
---|
9 | #include <windows.h>
|
---|
10 |
|
---|
11 | namespace Ogre {
|
---|
12 |
|
---|
13 | //-----------------------------------------------------------------------
|
---|
14 | SceneTraverser::OcclusionCullingSceneTraverser(SceneManager *sm, RenderSystem *rsys):
|
---|
15 | mFrameId(1), mDistanceQueue(NULL), mVisibilityThreshold(0), mCurrentTestIdx(0),
|
---|
16 | mNumSceneNodes(0), mNumTraversedNodes(0), mNumQueryCulledNodes(0), mNumFrustumCulledNodes(0),
|
---|
17 | mNumRenderedNodes(0),mNumRenderedGeometry(0), mSceneManager(sm), mRenderSystem(rsys), mSceneRoot(NULL)
|
---|
18 | {
|
---|
19 | mHalfBoundingBox[0] = mHalfBoundingBox[1] = 0;
|
---|
20 | }
|
---|
21 | //-----------------------------------------------------------------------
|
---|
22 | SceneTraverser::~OcclusionCullingSceneTraverser()
|
---|
23 | {
|
---|
24 | if(mHalfBoundingBox[0]) delete mHalfBoundingBox[0];
|
---|
25 | if(mHalfBoundingBox[1]) delete mHalfBoundingBox[1];
|
---|
26 |
|
---|
27 | deleteQueries();
|
---|
28 |
|
---|
29 | if(mDistanceQueue)
|
---|
30 | delete mDistanceQueue;
|
---|
31 | //SceneManager::~SceneManager();
|
---|
32 | }
|
---|
33 | //-----------------------------------------------------------------------
|
---|
34 | void SceneTraverser::traverseNode( Camera *cam, HierarchyNode *node )
|
---|
35 | {
|
---|
36 | mNumTraversedNodes ++;
|
---|
37 |
|
---|
38 | if(node->numAttachedObjects() > 0)
|
---|
39 | {
|
---|
40 | renderSceneNode(cam, node);
|
---|
41 | }
|
---|
42 |
|
---|
43 | // internal node: add children to priority queue for further processing
|
---|
44 | Node::ChildNodeIterator it = node->getChildIterator();
|
---|
45 |
|
---|
46 | while (it.hasMoreElements())
|
---|
47 | {
|
---|
48 | SceneNode* sceneChild = static_cast<SceneNode*>(it.getNext());
|
---|
49 | mDistanceQueue->push(sceneChild);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | //-----------------------------------------------------------------------
|
---|
53 | void SceneTraverser::renderSceneNode( Camera *cam, HierarchyNode *node )
|
---|
54 | {
|
---|
55 | if(node->lastRendered() != node->lastVisited())
|
---|
56 | {
|
---|
57 | node->setLastRendered(node->lastVisited());
|
---|
58 | mNumRenderedNodes ++;
|
---|
59 |
|
---|
60 | mSceneManager->_renderSceneNode(cam, node);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | //-----------------------------------------------------------------------
|
---|
64 | void SceneTraverser::setSceneManager( SceneManager *sm )
|
---|
65 | {
|
---|
66 | mSceneManager = sm;
|
---|
67 | }
|
---|
68 | //-----------------------------------------------------------------------
|
---|
69 | void SceneTraverser::setRenderSystem( RenderSystem *rsys )
|
---|
70 | {
|
---|
71 | mRenderSystem = rsys;
|
---|
72 | }
|
---|
73 | //-----------------------------------------------------------------------
|
---|
74 | bool SceneTraverser::isLeaf( SceneNode *node )
|
---|
75 | {
|
---|
76 | return (node->numChildren() == 0);
|
---|
77 | }
|
---|
78 | //-----------------------------------------------------------------------
|
---|
79 | void SceneTraverser::pullUpVisibility(SceneNode *node )
|
---|
80 | {
|
---|
81 | while(node && !node->isNodeVisible()) |
---|
82 | { |
---|
83 | node->setNodeVisible(true); |
---|
84 | node = node->getParentSceneNode(); |
---|
85 | }
|
---|
86 | }
|
---|
87 | //-----------------------------------------------------------------------
|
---|
88 | void SceneTraverser::deleteQueries( void )
|
---|
89 | {
|
---|
90 | for(unsigned int i=0; i < (unsigned int)mOcclusionQueries.size(); ++i)
|
---|
91 | delete mOcclusionQueries[i];
|
---|
92 |
|
---|
93 | mOcclusionQueries.clear();
|
---|
94 | }
|
---|
95 | //-----------------------------------------------------------------------
|
---|
96 | void SceneTraverser::renderBoundingBox( AxisAlignedBox *box )
|
---|
97 | {
|
---|
98 | // Render two halfes of the bounding box (using triangle fans)
|
---|
99 | for(int half = 0; half < 2; half ++)
|
---|
100 | {
|
---|
101 | static Matrix4 xform[256];
|
---|
102 | //TODO: this should be full bounding box
|
---|
103 | SolidHalfBoundingBox *halfbox = getSolidHalfBoundingBox(half);
|
---|
104 | halfbox->setupBoundingBox(*box);
|
---|
105 |
|
---|
106 | /*mRenderSystem->_setWorldMatrix(Matrix4::IDENTITY); |
---|
107 | |
---|
108 | halfbox->getWorldTransforms(xform); |
---|
109 | |
---|
110 | int numMatrices = halfbox->getNumWorldTransforms(); |
---|
111 | if (numMatrices > 1) |
---|
112 | { |
---|
113 | mRenderSystem->_setWorldMatrices(xform, numMatrices); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | mRenderSystem->_setWorldMatrix(*xform); |
---|
118 | } |
---|
119 | mRenderSystem->setClipPlanes(halfbox->getClipPlanes()); |
---|
120 | |
---|
121 | static RenderOperation ro; |
---|
122 | |
---|
123 | mSceneManager->useRenderableViewProjMode(halfbox);
|
---|
124 | halfbox->getRenderOperation(ro);
|
---|
125 | ro.srcRenderable = halfbox;
|
---|
126 | mRenderSystem->_render(ro);*/
|
---|
127 |
|
---|
128 | mSceneManager->myrenderSingleObject(getSolidHalfBoundingBox(half),
|
---|
129 | getSolidHalfBoundingBox(half)->getTechnique()->getPass(0), true);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | //-----------------------------------------------------------------------
|
---|
133 | SolidHalfBoundingBox *SceneTraverser::getSolidHalfBoundingBox( int half )
|
---|
134 | {
|
---|
135 | if(!mHalfBoundingBox[half])
|
---|
136 | mHalfBoundingBox[half] = new SolidHalfBoundingBox(half == 1);
|
---|
137 |
|
---|
138 | return mHalfBoundingBox[half];
|
---|
139 | }
|
---|
140 | //-----------------------------------------------------------------------
|
---|
141 | void OcclusionCullingSceneTraverser::setNumSceneNodes(int num)
|
---|
142 | {
|
---|
143 | mNumSceneNodes = num;
|
---|
144 | }
|
---|
145 | //-----------------------------------------------------------------------
|
---|
146 | void SceneTraverser::setSceneRoot(SceneNode *root)
|
---|
147 | {
|
---|
148 | mSceneRoot = root;
|
---|
149 | }
|
---|
150 | //-----------------------------------------------------------------------
|
---|
151 | void SceneTraverser::initDistanceQueue(Camera *cam)
|
---|
152 | {
|
---|
153 | mDistanceQueue = new PriorityQueue(myless<SceneNode *>(cam));
|
---|
154 | mDistanceQueue->push(mSceneRoot);
|
---|
155 | }
|
---|
156 | //-----------------------------------------------------------------------
|
---|
157 | HardwareOcclusionQuery *SceneTraverser::getNextOcclusionQuery(void)
|
---|
158 | {
|
---|
159 | if(mCurrentTestIdx == mOcclusionQueries.size())
|
---|
160 | {
|
---|
161 | mOcclusionQueries.push_back(mRenderSystem->createHardwareOcclusionQuery());
|
---|
162 | }
|
---|
163 |
|
---|
164 | return mOcclusionQueries[mCurrentTestIdx ++];
|
---|
165 | }
|
---|
166 | //-----------------------------------------------------------------------
|
---|
167 | bool OcclusionCullingSceneTraverser::setOption( const String & key, const void * val )
|
---|
168 | {
|
---|
169 | if ( key == "Algorithm" )
|
---|
170 | {
|
---|
171 | mCurrentAlgorithm = * static_cast < const int * > ( val );
|
---|
172 | return true;
|
---|
173 | }
|
---|
174 | if ( key == "Threshold" )
|
---|
175 | {
|
---|
176 | mVisibilityThreshold = * static_cast < const int * > ( val );
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 |
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 | //-----------------------------------------------------------------------
|
---|
183 | bool SceneTraverser::getOption( const String & key, void *val )
|
---|
184 | {
|
---|
185 | if ( key == "Algorithm" )
|
---|
186 | {
|
---|
187 | * static_cast < int * > ( val ) = mCurrentAlgorithm;
|
---|
188 | return true;
|
---|
189 | }
|
---|
190 | if ( key == "Threshold" )
|
---|
191 | {
|
---|
192 | * static_cast < unsigned int * > ( val ) = mVisibilityThreshold;
|
---|
193 | return true;
|
---|
194 | }
|
---|
195 | if ( key == "NumSceneNodes" )
|
---|
196 | {
|
---|
197 | * static_cast < unsigned int * > ( val ) = mNumSceneNodes;
|
---|
198 | return true;
|
---|
199 | }
|
---|
200 | if ( key == "NumTraversedNodes" )
|
---|
201 | {
|
---|
202 | * static_cast < unsigned int * > ( val ) = mNumTraversedNodes;
|
---|
203 | return true;
|
---|
204 | }
|
---|
205 | if ( key == "NumQueryCulledNodes" )
|
---|
206 | {
|
---|
207 | * static_cast < unsigned int * > ( val ) = mNumQueryCulledNodes;
|
---|
208 | return true;
|
---|
209 | }
|
---|
210 | if ( key == "NumFrustumCulledNodes" )
|
---|
211 | {
|
---|
212 | * static_cast < unsigned int * > ( val ) = mNumFrustumCulledNodes;
|
---|
213 | return true;
|
---|
214 | }
|
---|
215 | if ( key == "NumRenderedNodes" )
|
---|
216 | {
|
---|
217 | * static_cast < unsigned int * > ( val ) = mNumRenderedNodes;
|
---|
218 | return true;
|
---|
219 | }
|
---|
220 | return false;
|
---|
221 | }
|
---|
222 | //-----------------------------------------------------------------------
|
---|
223 | bool SceneTraverser::getOptionKeys( StringVector & refKeys )
|
---|
224 | {
|
---|
225 | refKeys.push_back( "Algorithm" );
|
---|
226 | refKeys.push_back( "Threshold" );
|
---|
227 | refKeys.push_back( "NumSceneNodes" ); |
---|
228 | refKeys.push_back( "NumTraversedNodes" ); |
---|
229 | refKeys.push_back( "NumQueryCulledNodes" ); |
---|
230 | refKeys.push_back( "NumFrustumCulledNodes" ); |
---|
231 | refKeys.push_back( "mNumRenderedGeometry" );
|
---|
232 |
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 | }
|
---|