1 | #include "OgreOctreeHierarchyInterface.h"
|
---|
2 | //#include "OgreVisibilityOctreeSceneManager.h"
|
---|
3 | #include <OgreOctree.h>
|
---|
4 | #include <OgreLogManager.h>
|
---|
5 | #include <OgreStringConverter.h>
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace Ogre {
|
---|
9 |
|
---|
10 | //-----------------------------------------------------------------------
|
---|
11 | OctreeHierarchyInterface::OctreeHierarchyInterface(OctreeSceneManager *sm,
|
---|
12 | RenderSystem *rsys):
|
---|
13 | SceneNodeHierarchyInterface(sm, rsys)
|
---|
14 | {}
|
---|
15 | //-----------------------------------------------------------------------
|
---|
16 | void OctreeHierarchyInterface::TraverseNode(GtpVisibility::HierarchyNode *node)
|
---|
17 | {
|
---|
18 | ++ mNumTraversedNodes;
|
---|
19 |
|
---|
20 | Octree *octree = static_cast<Octree *>(node);
|
---|
21 |
|
---|
22 | // if we come across some renderable geometry => render it
|
---|
23 | if (!octree->mNodes.empty())
|
---|
24 | {
|
---|
25 | RenderNode(node);
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | // if not all subtrees are empty
|
---|
30 | if (!IsLeaf(node))
|
---|
31 | {
|
---|
32 | Octree *nextChild;
|
---|
33 |
|
---|
34 | if ((nextChild = octree->mChildren[0][0][0]) != NULL)
|
---|
35 | mDistanceQueue->push(nextChild);
|
---|
36 | if ((nextChild = octree->mChildren[0][0][1]) != NULL)
|
---|
37 | mDistanceQueue->push(nextChild);
|
---|
38 | if ((nextChild = octree->mChildren[0][1][0]) != NULL)
|
---|
39 | mDistanceQueue->push(nextChild);
|
---|
40 | if ((nextChild = octree->mChildren[0][1][1]) != NULL)
|
---|
41 | mDistanceQueue->push(nextChild);
|
---|
42 | if ((nextChild = octree->mChildren[1][0][0]) != NULL)
|
---|
43 | mDistanceQueue->push(nextChild);
|
---|
44 | if ((nextChild = octree->mChildren[1][0][1]) != NULL)
|
---|
45 | mDistanceQueue->push(nextChild);
|
---|
46 | if ((nextChild = octree->mChildren[1][1][0]) != NULL)
|
---|
47 | mDistanceQueue->push(nextChild);
|
---|
48 | if ((nextChild = octree->mChildren[1][1][1]) != NULL)
|
---|
49 | mDistanceQueue->push(nextChild);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | //-----------------------------------------------------------------------
|
---|
53 | GtpVisibility::HierarchyNode *OctreeHierarchyInterface::GetRandomLeaf(GtpVisibility::HierarchyNode *root)
|
---|
54 | {
|
---|
55 | if (IsLeaf(root))
|
---|
56 | return root;
|
---|
57 |
|
---|
58 | Octree *octree = static_cast<Octree *>(root);
|
---|
59 |
|
---|
60 | // random decision
|
---|
61 | Octree *child = NULL;
|
---|
62 |
|
---|
63 | std::vector<Octree *> nodes;
|
---|
64 |
|
---|
65 | //LogManager::getSingleton().logMessage("***");
|
---|
66 | for (int i = 0; i < 8; ++ i)
|
---|
67 | {
|
---|
68 | int x = (i & 4) / 4;
|
---|
69 | int y = (i & 2) / 2;
|
---|
70 | int z = i & 1;
|
---|
71 |
|
---|
72 | if ((child = octree->mChildren[x][y][z]) != NULL)
|
---|
73 | {
|
---|
74 | nodes.push_back(child);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (nodes.empty())
|
---|
79 | return NULL;
|
---|
80 |
|
---|
81 | int r = (int) (rand() * (float)nodes.size());
|
---|
82 |
|
---|
83 | return GetRandomLeaf(nodes[r]);
|
---|
84 | // assume that at least one child is not NULL ...
|
---|
85 | /*while (!child)
|
---|
86 | {
|
---|
87 | int mask = (int) (rand() * 8.0f);
|
---|
88 | child = octree->mChildren[(mask & 4) / 4][(mask & 2) / 2][mask & 1];
|
---|
89 | }
|
---|
90 |
|
---|
91 | return GetRandomLeaf(child);*/
|
---|
92 | }
|
---|
93 | //-----------------------------------------------------------------------
|
---|
94 | void OctreeHierarchyInterface::TraverseNode2(GtpVisibility::HierarchyNode *node)
|
---|
95 | {
|
---|
96 | ++ mNumTraversedNodes;
|
---|
97 |
|
---|
98 | Octree *octree = static_cast<Octree *>(node);
|
---|
99 |
|
---|
100 | // if we come across some renderable geometry => render it
|
---|
101 | if (!octree->mNodes.empty())
|
---|
102 | {
|
---|
103 | // render everything from here
|
---|
104 | if (octree->isOctreeFullyVisible())
|
---|
105 | {
|
---|
106 | RenderNodeRecursive(node);
|
---|
107 | return;
|
---|
108 | }
|
---|
109 |
|
---|
110 | RenderNode(node);
|
---|
111 | }
|
---|
112 |
|
---|
113 | // if not all subtrees are empty
|
---|
114 | if (!IsLeaf(node))
|
---|
115 | {
|
---|
116 | Octree *nextChild;
|
---|
117 |
|
---|
118 | if ((nextChild = octree->mChildren[0][0][0]) != NULL)
|
---|
119 | mDistanceQueue->push(nextChild);
|
---|
120 | if ((nextChild = octree->mChildren[0][0][1]) != NULL)
|
---|
121 | mDistanceQueue->push(nextChild);
|
---|
122 | if ((nextChild = octree->mChildren[0][1][0]) != NULL)
|
---|
123 | mDistanceQueue->push(nextChild);
|
---|
124 | if ((nextChild = octree->mChildren[0][1][1]) != NULL)
|
---|
125 | mDistanceQueue->push(nextChild);
|
---|
126 | if ((nextChild = octree->mChildren[1][0][0]) != NULL)
|
---|
127 | mDistanceQueue->push(nextChild);
|
---|
128 | if ((nextChild = octree->mChildren[1][0][1]) != NULL)
|
---|
129 | mDistanceQueue->push(nextChild);
|
---|
130 | if ((nextChild = octree->mChildren[1][1][0]) != NULL)
|
---|
131 | mDistanceQueue->push(nextChild);
|
---|
132 | if ((nextChild = octree->mChildren[1][1][1]) != NULL)
|
---|
133 | mDistanceQueue->push(nextChild);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | //-----------------------------------------------------------------------
|
---|
137 | bool OctreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
|
---|
138 | {
|
---|
139 | Octree *octree = static_cast<Octree *>(node);
|
---|
140 |
|
---|
141 | // HACK: if there are subtrees, they are empty => we are not interested in them
|
---|
142 | return octree->numNodes() == (int)octree->mNodes.size();
|
---|
143 | }
|
---|
144 | //-----------------------------------------------------------------------
|
---|
145 | bool OctreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
|
---|
146 | {
|
---|
147 | return !(static_cast<Octree *>(node))->mNodes.empty();
|
---|
148 | }
|
---|
149 | //-----------------------------------------------------------------------
|
---|
150 | float OctreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
|
---|
151 | {
|
---|
152 | const Vector3 bmin = static_cast<Octree *>(node)->mBox.getMinimum();
|
---|
153 | const Vector3 bmax = static_cast<Octree *>(node)->mBox.getMaximum();
|
---|
154 |
|
---|
155 | const Vector3 pos = (bmax - bmin) * 0.5 + bmin;
|
---|
156 |
|
---|
157 | /* std::stringstream d;
|
---|
158 | d << "a: " << (mCameraPosition - pos).squaredLength()
|
---|
159 | << " b: " << (mCullCamera->getDerivedPosition() - pos).squaredLength();
|
---|
160 | LogManager::getSingleton().logMessage(d.str());*/
|
---|
161 | return (mCameraPosition - pos).squaredLength();
|
---|
162 | }
|
---|
163 | //-----------------------------------------------------------------------
|
---|
164 | void OctreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node,
|
---|
165 | const bool visible) const
|
---|
166 | {
|
---|
167 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
168 | static_cast<Octree *>(node)->setOctreeVisible(visible);
|
---|
169 | #endif
|
---|
170 | }
|
---|
171 | //-----------------------------------------------------------------------
|
---|
172 | void OctreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node,
|
---|
173 | const unsigned int frameId) const
|
---|
174 | {
|
---|
175 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
176 | static_cast<Octree *>(node)->setLastVisited(frameId);
|
---|
177 | #endif
|
---|
178 | }
|
---|
179 | //-----------------------------------------------------------------------
|
---|
180 | void OctreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node) const
|
---|
181 | {
|
---|
182 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
183 | Octree *octant = static_cast<Octree *>(node);
|
---|
184 |
|
---|
185 | while (octant && !octant->isOctreeVisible())
|
---|
186 | {
|
---|
187 | octant->setOctreeVisible(true);
|
---|
188 | octant = octant->getParent();
|
---|
189 | }
|
---|
190 | #endif
|
---|
191 | }
|
---|
192 | //-----------------------------------------------------------------------
|
---|
193 | void OctreeHierarchyInterface::DetermineFullVisibility(GtpVisibility::HierarchyNode *node) const
|
---|
194 | {
|
---|
195 | Octree *octant = static_cast<Octree *>(node);
|
---|
196 |
|
---|
197 | // leaf node: terminate recursion
|
---|
198 | if (IsLeaf(node))
|
---|
199 | {
|
---|
200 | octant->setOctreeFullyVisible(octant->isOctreeVisible());
|
---|
201 |
|
---|
202 | return;
|
---|
203 | }
|
---|
204 |
|
---|
205 | octant->setOctreeFullyVisible(true);
|
---|
206 |
|
---|
207 | Octree *nextChild;
|
---|
208 | //LogManager::getSingleton().logMessage("***");
|
---|
209 | for (int i = 0; i < 8; ++ i)
|
---|
210 | {
|
---|
211 | int x = (i & 4) / 4;
|
---|
212 | int y = (i & 2) / 2;
|
---|
213 | int z = i & 1;
|
---|
214 | Ogre::LogManager::getSingleton().logMessage("y");
|
---|
215 | //std::stringstream d; d << "x " << x << " y " << y << " z " << z;
|
---|
216 | //LogManager::getSingleton().logMessage(d.str());
|
---|
217 |
|
---|
218 | if ((nextChild = octant->mChildren[x][y][z]) != NULL)
|
---|
219 | {
|
---|
220 | DetermineFullVisibility(nextChild);
|
---|
221 | // this leaf is not fully visible => break
|
---|
222 | if (!nextChild->isOctreeFullyVisible())
|
---|
223 | octant->setOctreeFullyVisible(false);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 | //-----------------------------------------------------------------------
|
---|
228 | void OctreeHierarchyInterface::RenderNode(GtpVisibility::HierarchyNode *node)
|
---|
229 | {
|
---|
230 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
231 | Octree *octant = static_cast<Octree *>(node);
|
---|
232 |
|
---|
233 | if (octant->lastRendered() != mFrameId)
|
---|
234 | {
|
---|
235 | octant->setLastRendered(mFrameId);
|
---|
236 | OctreeSceneManager *ocm =
|
---|
237 | static_cast<OctreeSceneManager *>(mSceneManager);
|
---|
238 |
|
---|
239 | ocm->_renderOctant(mCamera,
|
---|
240 | octant,
|
---|
241 | mOnlyShadowCasters,
|
---|
242 | mLeavePassesInQueue);
|
---|
243 |
|
---|
244 | mVisibleNodes.push_back(node);
|
---|
245 | }
|
---|
246 | #endif
|
---|
247 | }
|
---|
248 | //-----------------------------------------------------------------------
|
---|
249 | void OctreeHierarchyInterface::RenderNodeRecursive(GtpVisibility::HierarchyNode *node)
|
---|
250 | {
|
---|
251 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
252 | Octree *octant = static_cast<Octree *>(node);
|
---|
253 |
|
---|
254 | if (octant->lastRendered() != mFrameId)
|
---|
255 | {
|
---|
256 | octant->setLastRendered(mFrameId);
|
---|
257 | OctreeSceneManager *ocm =
|
---|
258 | static_cast<OctreeSceneManager *>(mSceneManager);
|
---|
259 |
|
---|
260 | ocm->_renderOctantRecursive(mCamera,
|
---|
261 | octant,
|
---|
262 | mOnlyShadowCasters,
|
---|
263 | mLeavePassesInQueue);
|
---|
264 |
|
---|
265 | mVisibleNodes.push_back(node);
|
---|
266 | }
|
---|
267 | #endif
|
---|
268 | }
|
---|
269 | //-----------------------------------------------------------------------
|
---|
270 | bool OctreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
|
---|
271 | {
|
---|
272 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
273 | return static_cast<Octree *>(node)->isOctreeVisible();
|
---|
274 | #else
|
---|
275 | return true;
|
---|
276 | #endif
|
---|
277 | }
|
---|
278 | //-----------------------------------------------------------------------
|
---|
279 | bool OctreeHierarchyInterface::IsNodeFullyVisible(GtpVisibility::HierarchyNode *node) const
|
---|
280 | {
|
---|
281 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
282 | return static_cast<Octree *>(node)->isOctreeFullyVisible();
|
---|
283 | #else
|
---|
284 | return true;
|
---|
285 | #endif
|
---|
286 | }
|
---|
287 | //-----------------------------------------------------------------------
|
---|
288 | unsigned int OctreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
|
---|
289 | {
|
---|
290 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
291 | return static_cast<Octree *>(node)->lastVisited();
|
---|
292 | #else
|
---|
293 | return 0;
|
---|
294 | #endif
|
---|
295 | }
|
---|
296 | //-----------------------------------------------------------------------
|
---|
297 | AxisAlignedBox *OctreeHierarchyInterface::GetBoundingBox(GtpVisibility::HierarchyNode *node)
|
---|
298 | {
|
---|
299 | // reuse box if node is the same
|
---|
300 | // only create renderable bounding box for new node
|
---|
301 | if (node != mSavedNode)
|
---|
302 | {
|
---|
303 | mSavedNode = node;
|
---|
304 | //static_cast<Octree *>(node)->_getCullBounds(&mBox);
|
---|
305 | mBox = static_cast<Octree *>(node)->_getWorldAABB();
|
---|
306 | }
|
---|
307 |
|
---|
308 | return &mBox;
|
---|
309 | }
|
---|
310 | //-----------------------------------------------------------------------
|
---|
311 | void OctreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
|
---|
312 | GtpVisibility::CullingType type) const
|
---|
313 | {
|
---|
314 | WireBoundingBox *box = static_cast<Octree *>(node)->getWireBoundingBox();
|
---|
315 |
|
---|
316 | if (type == GtpVisibility::FRUSTUM_CULLED)
|
---|
317 | {
|
---|
318 | box->setMaterial("FrustumCulledNodesMaterial");
|
---|
319 | }
|
---|
320 | else // type == GtpVisibility::QUERY_CULLED
|
---|
321 | {
|
---|
322 | box->setMaterial("QueryCulledNodesMaterial");
|
---|
323 | }
|
---|
324 |
|
---|
325 | static_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
|
---|
326 | }
|
---|
327 | //-----------------------------------------------------------------------
|
---|
328 | void OctreeHierarchyInterface::GetNodeGeometryList(GtpVisibility::HierarchyNode *node,
|
---|
329 | GeometryVector *geometryList,
|
---|
330 | bool includeChildren)
|
---|
331 | {
|
---|
332 | NodeList::const_iterator nodeIt, nodeIt_end;
|
---|
333 | nodeIt_end = static_cast<Octree *>(node)->mNodes.end();
|
---|
334 |
|
---|
335 | for (nodeIt = static_cast<Octree *>(node)->mNodes.begin(); nodeIt != nodeIt_end; ++nodeIt)
|
---|
336 | {
|
---|
337 | SceneNodeHierarchyInterface::GetNodeGeometryList(*nodeIt, geometryList, includeChildren);
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | } // namespace Ogre
|
---|