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