[59] | 1 | #include "OgreOctreeHierarchyInterface.h"
|
---|
[925] | 2 | //#include "OgreVisibilityOctreeSceneManager.h"
|
---|
[59] | 3 | #include <OgreOctree.h>
|
---|
[94] | 4 | #include <OgreLogManager.h>
|
---|
[113] | 5 | #include <OgreStringConverter.h>
|
---|
[59] | 6 |
|
---|
[115] | 7 |
|
---|
[59] | 8 | namespace Ogre {
|
---|
[94] | 9 |
|
---|
[59] | 10 | //-----------------------------------------------------------------------
|
---|
[2258] | 11 | OctreeHierarchyInterface::OctreeHierarchyInterface(OctreeSceneManager *sm,
|
---|
| 12 | RenderSystem *rsys):
|
---|
[158] | 13 | SceneNodeHierarchyInterface(sm, rsys)
|
---|
[2258] | 14 | {}
|
---|
[59] | 15 | //-----------------------------------------------------------------------
|
---|
[158] | 16 | void OctreeHierarchyInterface::TraverseNode(GtpVisibility::HierarchyNode *node)
|
---|
[59] | 17 | {
|
---|
[139] | 18 | ++ mNumTraversedNodes;
|
---|
[59] | 19 |
|
---|
[87] | 20 | Octree *octree = static_cast<Octree *>(node);
|
---|
| 21 |
|
---|
[59] | 22 | // if we come across some renderable geometry => render it
|
---|
[726] | 23 | if (!octree->mNodes.empty())
|
---|
[87] | 24 | {
|
---|
| 25 | RenderNode(node);
|
---|
| 26 | }
|
---|
[59] | 27 |
|
---|
[925] | 28 |
|
---|
[726] | 29 | // if not all subtrees are empty
|
---|
[94] | 30 | if (!IsLeaf(node))
|
---|
[59] | 31 | {
|
---|
[726] | 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);
|
---|
[59] | 50 | }
|
---|
| 51 | }
|
---|
| 52 | //-----------------------------------------------------------------------
|
---|
[2259] | 53 | GtpVisibility::HierarchyNode *OctreeHierarchyInterface::GetRandomLeaf(GtpVisibility::HierarchyNode *root)
|
---|
[2258] | 54 | {
|
---|
[2259] | 55 | if (IsLeaf(root))
|
---|
| 56 | return root;
|
---|
| 57 |
|
---|
| 58 | Octree *octree = static_cast<Octree *>(root);
|
---|
| 59 |
|
---|
| 60 | // random decision
|
---|
[2278] | 61 | Octree *child = NULL;
|
---|
| 62 |
|
---|
[2281] | 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);
|
---|
[2278] | 88 | child = octree->mChildren[(mask & 4) / 4][(mask & 2) / 2][mask & 1];
|
---|
[2281] | 89 | }
|
---|
[2259] | 90 |
|
---|
[2281] | 91 | return GetRandomLeaf(child);*/
|
---|
[2258] | 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 | //-----------------------------------------------------------------------
|
---|
[74] | 137 | bool OctreeHierarchyInterface::IsLeaf(GtpVisibility::HierarchyNode *node) const
|
---|
[59] | 138 | {
|
---|
[94] | 139 | Octree *octree = static_cast<Octree *>(node);
|
---|
[944] | 140 |
|
---|
[94] | 141 | // HACK: if there are subtrees, they are empty => we are not interested in them
|
---|
| 142 | return octree->numNodes() == (int)octree->mNodes.size();
|
---|
[59] | 143 | }
|
---|
| 144 | //-----------------------------------------------------------------------
|
---|
[74] | 145 | bool OctreeHierarchyInterface::HasGeometry(GtpVisibility::HierarchyNode *node) const
|
---|
[59] | 146 | {
|
---|
[722] | 147 | return !(static_cast<Octree *>(node))->mNodes.empty();
|
---|
[59] | 148 | }
|
---|
| 149 | //-----------------------------------------------------------------------
|
---|
[87] | 150 | float OctreeHierarchyInterface::GetSquaredDistance(GtpVisibility::HierarchyNode *node) const
|
---|
[59] | 151 | {
|
---|
[726] | 152 | const Vector3 bmin = static_cast<Octree *>(node)->mBox.getMinimum();
|
---|
| 153 | const Vector3 bmax = static_cast<Octree *>(node)->mBox.getMaximum();
|
---|
[722] | 154 |
|
---|
[726] | 155 | const Vector3 pos = (bmax - bmin) * 0.5 + bmin;
|
---|
[345] | 156 |
|
---|
[944] | 157 | /* std::stringstream d;
|
---|
| 158 | d << "a: " << (mCameraPosition - pos).squaredLength()
|
---|
| 159 | << " b: " << (mCullCamera->getDerivedPosition() - pos).squaredLength();
|
---|
| 160 | LogManager::getSingleton().logMessage(d.str());*/
|
---|
[726] | 161 | return (mCameraPosition - pos).squaredLength();
|
---|
[59] | 162 | }
|
---|
| 163 | //-----------------------------------------------------------------------
|
---|
[74] | 164 | void OctreeHierarchyInterface::SetNodeVisible(GtpVisibility::HierarchyNode *node,
|
---|
[345] | 165 | const bool visible) const
|
---|
[59] | 166 | {
|
---|
| 167 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 168 | static_cast<Octree *>(node)->setOctreeVisible(visible);
|
---|
| 169 | #endif
|
---|
| 170 | }
|
---|
| 171 | //-----------------------------------------------------------------------
|
---|
[74] | 172 | void OctreeHierarchyInterface::SetLastVisited(GtpVisibility::HierarchyNode *node,
|
---|
[345] | 173 | const unsigned int frameId) const
|
---|
[59] | 174 | {
|
---|
| 175 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 176 | static_cast<Octree *>(node)->setLastVisited(frameId);
|
---|
| 177 | #endif
|
---|
| 178 | }
|
---|
| 179 | //-----------------------------------------------------------------------
|
---|
[345] | 180 | void OctreeHierarchyInterface::PullUpVisibility(GtpVisibility::HierarchyNode *node) const
|
---|
[59] | 181 | {
|
---|
| 182 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 183 | Octree *octant = static_cast<Octree *>(node);
|
---|
| 184 |
|
---|
[155] | 185 | while (octant && !octant->isOctreeVisible())
|
---|
[59] | 186 | {
|
---|
| 187 | octant->setOctreeVisible(true);
|
---|
| 188 | octant = octant->getParent();
|
---|
| 189 | }
|
---|
| 190 | #endif
|
---|
| 191 | }
|
---|
| 192 | //-----------------------------------------------------------------------
|
---|
[2258] | 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());
|
---|
[2278] | 201 |
|
---|
[2258] | 202 | return;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[2278] | 205 | octant->setOctreeFullyVisible(true);
|
---|
| 206 |
|
---|
[2258] | 207 | Octree *nextChild;
|
---|
[2278] | 208 | //LogManager::getSingleton().logMessage("***");
|
---|
[2258] | 209 | for (int i = 0; i < 8; ++ i)
|
---|
| 210 | {
|
---|
[2278] | 211 | int x = (i & 4) / 4;
|
---|
| 212 | int y = (i & 2) / 2;
|
---|
[2258] | 213 | int z = i & 1;
|
---|
[2280] | 214 | Ogre::LogManager::getSingleton().logMessage("y");
|
---|
[2278] | 215 | //std::stringstream d; d << "x " << x << " y " << y << " z " << z;
|
---|
| 216 | //LogManager::getSingleton().logMessage(d.str());
|
---|
| 217 |
|
---|
[2258] | 218 | if ((nextChild = octant->mChildren[x][y][z]) != NULL)
|
---|
| 219 | {
|
---|
| 220 | DetermineFullVisibility(nextChild);
|
---|
[2278] | 221 | // this leaf is not fully visible => break
|
---|
[2258] | 222 | if (!nextChild->isOctreeFullyVisible())
|
---|
[2278] | 223 | octant->setOctreeFullyVisible(false);
|
---|
[2258] | 224 | }
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | //-----------------------------------------------------------------------
|
---|
[59] | 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);
|
---|
[726] | 236 | OctreeSceneManager *ocm =
|
---|
[2066] | 237 | static_cast<OctreeSceneManager *>(mSceneManager);
|
---|
[938] | 238 |
|
---|
[2258] | 239 | ocm->_renderOctant(mCamera,
|
---|
| 240 | octant,
|
---|
| 241 | mOnlyShadowCasters,
|
---|
| 242 | mLeavePassesInQueue);
|
---|
[59] | 243 |
|
---|
[174] | 244 | mVisibleNodes.push_back(node);
|
---|
[59] | 245 | }
|
---|
[2258] | 246 | #endif
|
---|
[59] | 247 | }
|
---|
| 248 | //-----------------------------------------------------------------------
|
---|
[2259] | 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 | //-----------------------------------------------------------------------
|
---|
[74] | 270 | bool OctreeHierarchyInterface::IsNodeVisible(GtpVisibility::HierarchyNode *node) const
|
---|
[59] | 271 | {
|
---|
| 272 | #ifdef GTP_VISIBILITY_MODIFIED_OGRE
|
---|
| 273 | return static_cast<Octree *>(node)->isOctreeVisible();
|
---|
| 274 | #else
|
---|
| 275 | return true;
|
---|
| 276 | #endif
|
---|
| 277 | }
|
---|
| 278 | //-----------------------------------------------------------------------
|
---|
[2259] | 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 | //-----------------------------------------------------------------------
|
---|
[74] | 288 | unsigned int OctreeHierarchyInterface::LastVisited(GtpVisibility::HierarchyNode *node) const
|
---|
[59] | 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 | {
|
---|
[925] | 299 | // reuse box if node is the same
|
---|
| 300 | // only create renderable bounding box for new node
|
---|
[938] | 301 | if (node != mSavedNode)
|
---|
[85] | 302 | {
|
---|
[155] | 303 | mSavedNode = node;
|
---|
[880] | 304 | //static_cast<Octree *>(node)->_getCullBounds(&mBox);
|
---|
[135] | 305 | mBox = static_cast<Octree *>(node)->_getWorldAABB();
|
---|
[85] | 306 | }
|
---|
[59] | 307 |
|
---|
| 308 | return &mBox;
|
---|
| 309 | }
|
---|
[112] | 310 | //-----------------------------------------------------------------------
|
---|
| 311 | void OctreeHierarchyInterface::VisualizeCulledNode(GtpVisibility::HierarchyNode *node,
|
---|
[345] | 312 | GtpVisibility::CullingType type) const
|
---|
[112] | 313 | {
|
---|
| 314 | WireBoundingBox *box = static_cast<Octree *>(node)->getWireBoundingBox();
|
---|
[59] | 315 |
|
---|
[112] | 316 | if (type == GtpVisibility::FRUSTUM_CULLED)
|
---|
| 317 | {
|
---|
| 318 | box->setMaterial("FrustumCulledNodesMaterial");
|
---|
| 319 | }
|
---|
| 320 | else // type == GtpVisibility::QUERY_CULLED
|
---|
| 321 | {
|
---|
| 322 | box->setMaterial("QueryCulledNodesMaterial");
|
---|
| 323 | }
|
---|
[113] | 324 |
|
---|
[2066] | 325 | static_cast<OctreeSceneManager *>(mSceneManager)->getBoxes()->push_back(box);
|
---|
[112] | 326 | }
|
---|
[130] | 327 | //-----------------------------------------------------------------------
|
---|
[726] | 328 | void OctreeHierarchyInterface::GetNodeGeometryList(GtpVisibility::HierarchyNode *node,
|
---|
[2280] | 329 | GeometryVector *geometryList,
|
---|
[726] | 330 | bool includeChildren)
|
---|
[130] | 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 | {
|
---|
[155] | 337 | SceneNodeHierarchyInterface::GetNodeGeometryList(*nodeIt, geometryList, includeChildren);
|
---|
[130] | 338 | }
|
---|
| 339 | }
|
---|
[174] | 340 |
|
---|
[2258] | 341 |
|
---|
[59] | 342 | } // namespace Ogre
|
---|