[1316] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of the GameTools Project
|
---|
| 4 | http://www.gametools.org
|
---|
| 5 |
|
---|
| 6 | Author: Martin Szydlowski
|
---|
| 7 | -----------------------------------------------------------------------------
|
---|
| 8 | */
|
---|
| 9 |
|
---|
[1319] | 10 | #ifndef _OgreBvHierarchy_H__
|
---|
| 11 | #define _OgreBvHierarchy_H__
|
---|
[1316] | 12 |
|
---|
[1320] | 13 | #define BVHNODE_CAST(a) (static_cast<BvHierarchy::Node>(a))
|
---|
| 14 | #define BVHBRANCH_CAST(a) (static_cast<BvHierarchy::Branch>(a))
|
---|
| 15 | #define BVHLEAF_CAST(a) (static_cast<BvHierarchy::Leaf>(a))
|
---|
| 16 | #define BVHNODEPTR_CAST(a) (static_cast<BvHierarchy::Node *>(a))
|
---|
| 17 | #define BVHBRANCHPTR_CAST(a) (static_cast<BvHierarchy::Branch *>(a))
|
---|
| 18 | #define BVHLEAFPTR_CAST(a) (static_cast<BvHierarchy::Leaf *>(a))
|
---|
[1316] | 19 |
|
---|
[1320] | 20 | #define BvHierarchy_LOGNAME "BvHierarchyBuild.log"
|
---|
[1316] | 21 |
|
---|
| 22 | #include <OgreAxisAlignedBox.h>
|
---|
| 23 | #include <OgreWireBoundingBox.h>
|
---|
| 24 | #include <OgrePlane.h>
|
---|
| 25 | #include <OgreVector3.h>
|
---|
| 26 |
|
---|
| 27 | #include <OgreRoot.h>
|
---|
| 28 |
|
---|
| 29 | #include <stack>
|
---|
| 30 |
|
---|
[1319] | 31 | #include "OgreBvHierarchyCamera.h"
|
---|
[1316] | 32 | #include "HierarchyInterface.h"
|
---|
[1320] | 33 | #include "OgreKdTree.h"
|
---|
[1316] | 34 |
|
---|
| 35 | namespace Ogre
|
---|
| 36 | {
|
---|
[1319] | 37 | class BvHierarchyCamera;
|
---|
[1320] | 38 | class BvhRenderable;
|
---|
| 39 | struct BvhSplitInfo;
|
---|
| 40 |
|
---|
| 41 | class BvhPlaneEvent
|
---|
[1316] | 42 | {
|
---|
| 43 | public:
|
---|
| 44 | enum Type
|
---|
| 45 | {
|
---|
| 46 | PET_END,
|
---|
| 47 | PET_ON,
|
---|
| 48 | PET_START
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | enum Dimension
|
---|
| 52 | {
|
---|
| 53 | PED_X,
|
---|
| 54 | PED_Y,
|
---|
| 55 | PED_Z
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | enum Side
|
---|
| 59 | {
|
---|
| 60 | PES_LEFT = 0x01,
|
---|
| 61 | PES_RIGHT = 0x02,
|
---|
| 62 | PES_BOTH = PES_LEFT | PES_RIGHT
|
---|
| 63 | };
|
---|
| 64 |
|
---|
[1320] | 65 | BvhPlaneEvent(): mRenderable(0), mPosition(Vector3()), mDimension(PED_X), mType(PET_ON)
|
---|
[1316] | 66 | { };
|
---|
| 67 |
|
---|
[1320] | 68 | BvhPlaneEvent(BvhRenderable *rend, const Vector3& pos, BvhPlaneEvent::Dimension dim, BvhPlaneEvent::Type type):
|
---|
[1316] | 69 | mRenderable(rend), mPosition(pos), mDimension(dim), mType(type)
|
---|
| 70 | { };
|
---|
| 71 |
|
---|
[1320] | 72 | ~BvhPlaneEvent() {};
|
---|
[1316] | 73 |
|
---|
| 74 | // the less operator for plane events
|
---|
| 75 | // first sort by position, then by dimension and finally by type
|
---|
[1320] | 76 | bool operator < (const BvhPlaneEvent& e) const
|
---|
[1316] | 77 | {
|
---|
| 78 | if(mPosition[mDimension] < e.mPosition[e.mDimension])
|
---|
| 79 | {
|
---|
| 80 | return true;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | if(mPosition[mDimension] == e.mPosition[e.mDimension])
|
---|
| 84 | {
|
---|
| 85 | if (mDimension < e.mDimension)
|
---|
| 86 | {
|
---|
| 87 | return true;
|
---|
| 88 | }
|
---|
| 89 | if (mDimension == e.mDimension)
|
---|
| 90 | {
|
---|
| 91 | if (mType < e.mType)
|
---|
| 92 | {
|
---|
| 93 | return true;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | return false;
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 | // the equals operator for tree events
|
---|
[1320] | 102 | bool operator == (const BvhPlaneEvent& e) const
|
---|
[1316] | 103 | {
|
---|
| 104 | return (mPosition[mDimension] == e.mPosition[e.mDimension]) &&
|
---|
| 105 | (mDimension == e.mDimension) &&
|
---|
| 106 | (mType == e.mType);
|
---|
| 107 | };
|
---|
| 108 |
|
---|
[1320] | 109 | bool equalsType(const BvhPlaneEvent& e, BvhPlaneEvent::Type t)
|
---|
[1316] | 110 | {
|
---|
| 111 | return (mPosition[mDimension] == e.mPosition[e.mDimension]) &&
|
---|
| 112 | (mDimension == e.mDimension) &&
|
---|
| 113 | (mType == t);
|
---|
| 114 | };
|
---|
| 115 |
|
---|
[1320] | 116 | void classify(const BvhPlaneEvent& e, BvhPlaneEvent::Side side);
|
---|
[1316] | 117 |
|
---|
[1320] | 118 | BvhPlaneEvent clip(AxisAlignedBox& box, BvhPlaneEvent::Dimension dim);
|
---|
[1316] | 119 |
|
---|
| 120 | Plane * getSplitPlane() const
|
---|
| 121 | {
|
---|
| 122 | Vector3 normal(0,0,0);
|
---|
| 123 | normal[mDimension] = 1;
|
---|
| 124 | return new Plane(normal, mPosition);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[1320] | 127 | BvhRenderable * getRenderable() const //??
|
---|
[1316] | 128 | {
|
---|
| 129 | return mRenderable;
|
---|
| 130 | };
|
---|
| 131 |
|
---|
[1320] | 132 | BvhPlaneEvent::Dimension getDimension() const //??
|
---|
[1316] | 133 | {
|
---|
| 134 | return mDimension;
|
---|
| 135 | };
|
---|
| 136 |
|
---|
| 137 | // DEBUG
|
---|
| 138 | String print();
|
---|
| 139 | protected:
|
---|
| 140 | // event info
|
---|
[1320] | 141 | BvhRenderable * mRenderable;
|
---|
[1316] | 142 | Vector3 mPosition;
|
---|
[1320] | 143 | BvhPlaneEvent::Dimension mDimension;
|
---|
| 144 | BvhPlaneEvent::Type mType;
|
---|
[1316] | 145 |
|
---|
| 146 | // ------------------------------------------------------------------------------//
|
---|
| 147 | // functions to determine the cost of splitting the node parent with the plane
|
---|
| 148 | // represented by this event
|
---|
| 149 | // TODO discuss if these functions really belong here, OO & SE - wise
|
---|
| 150 | // pros: convenient, don't have to pass internal data to the outside
|
---|
[1320] | 151 | // cons: BvhSplitInfo ...
|
---|
[1316] | 152 | public:
|
---|
| 153 | // compute "global" surface area heuristic (SAH) for the plane represented by this event
|
---|
| 154 | // use only with priority queue build method
|
---|
[1320] | 155 | void pqSAH(Real globalSA, Real parentSA, int nLeft, int nPlane, int nRight, AxisAlignedBox& parentBox, BvhSplitInfo& split);
|
---|
[1316] | 156 | static Real pqSplitCost(Real p, Real pl, Real pr, int nLeft, int nRight, Real mu);
|
---|
| 157 |
|
---|
| 158 | // compute the surface area heuristic (SAH) for the plane represented by this event
|
---|
[1320] | 159 | void SAH(const AxisAlignedBox& parent, int nLeft, int nPlane, int nRight, BvhSplitInfo& split);
|
---|
[1316] | 160 | // the variables determining the cost of a branch traversal (KT) and a leaf intersection (KI)
|
---|
| 161 | static Real KT;
|
---|
| 162 | static Real KI;
|
---|
| 163 | // helper functions
|
---|
| 164 | static Real splitCost(Real pl, Real pr, int nLeft, int nRight);
|
---|
| 165 | static Real splitCost(Real pl, Real pr, int nLeft, int nRight, Real mu);
|
---|
| 166 | static Real surfaceArea(const AxisAlignedBox& box);
|
---|
| 167 | static Real lookupPenalty(Real p);
|
---|
| 168 | protected:
|
---|
| 169 | Real splitBox(const AxisAlignedBox& parent, AxisAlignedBox& left, AxisAlignedBox& right);
|
---|
| 170 | };
|
---|
| 171 |
|
---|
| 172 | // Holds all the important information on a split
|
---|
[1320] | 173 | struct BvhSplitInfo
|
---|
[1316] | 174 | {
|
---|
| 175 | AxisAlignedBox bleft;
|
---|
| 176 | AxisAlignedBox bright;
|
---|
| 177 | int nleft;
|
---|
| 178 | int nright;
|
---|
| 179 | Real cost;
|
---|
[1320] | 180 | BvhPlaneEvent::Side side;
|
---|
| 181 | BvhPlaneEvent event;
|
---|
[1316] | 182 |
|
---|
| 183 | // DEBUG
|
---|
| 184 | String print();
|
---|
| 185 | };
|
---|
| 186 |
|
---|
[1320] | 187 | typedef std::list<BvhRenderable *> BvhRenderableList;
|
---|
| 188 | typedef std::list<BvhPlaneEvent> BvhPlaneEventList;
|
---|
[1316] | 189 |
|
---|
[1320] | 190 | class BvHierarchy
|
---|
[1316] | 191 | {
|
---|
| 192 | protected:
|
---|
| 193 | class Branch;
|
---|
| 194 | class Leaf;
|
---|
| 195 |
|
---|
| 196 | class Node
|
---|
| 197 | {
|
---|
| 198 | public:
|
---|
[1320] | 199 | Node(BvHierarchy * owner, int level, AxisAlignedBox& aabb, Branch * parent):
|
---|
[1316] | 200 | mOwner(owner),
|
---|
| 201 | mLevel(level),
|
---|
| 202 | mAABB(aabb),
|
---|
| 203 | mParent(parent),
|
---|
| 204 | mWBB(0)
|
---|
| 205 | { };
|
---|
| 206 |
|
---|
| 207 | virtual ~Node()
|
---|
| 208 | {
|
---|
| 209 | delete mWBB;
|
---|
| 210 | };
|
---|
| 211 |
|
---|
| 212 | virtual bool isLeaf() const = 0;
|
---|
| 213 | virtual bool isEmpty() const = 0;
|
---|
| 214 | virtual bool hasGeometry() const = 0;
|
---|
| 215 |
|
---|
| 216 | virtual void mergeLeaves(std::set<Leaf *>& leaves) = 0;
|
---|
| 217 |
|
---|
| 218 | // Gets this node's parent (NULL if this is the root).
|
---|
[1320] | 219 | BvHierarchy::Node *getParent(void) const { return mParent; };
|
---|
[1316] | 220 |
|
---|
| 221 | // Gets the nodes left & right child nodes, or NULL if not present or node is leaf
|
---|
[1320] | 222 | virtual BvHierarchy::Node *getLeftChild(void) const = 0;
|
---|
| 223 | virtual BvHierarchy::Node *getRightChild(void) const = 0;
|
---|
[1316] | 224 |
|
---|
| 225 | // add contained objects to render queue
|
---|
| 226 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
| 227 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false) = 0;
|
---|
| 228 |
|
---|
| 229 | // add contained geometry (Entities) to list
|
---|
[2280] | 230 | virtual void getGeometryList(GeometryVector *geometryList) = 0;
|
---|
[1316] | 231 |
|
---|
| 232 | // create (when necessary), setup and return wire bounding box representing the node
|
---|
| 233 | WireBoundingBox * getWireBoundingBox()
|
---|
| 234 | {
|
---|
| 235 | if (mWBB == 0)
|
---|
| 236 | mWBB = new WireBoundingBox();
|
---|
| 237 |
|
---|
| 238 | if (mOwner->getShowNodes())
|
---|
| 239 | mWBB->setupBoundingBox(mAABB);
|
---|
| 240 | else
|
---|
| 241 | mWBB->setupBoundingBox(mWorldAABB);
|
---|
| 242 |
|
---|
| 243 | if (mOwner->getHiLiteLevel() == mLevel)
|
---|
[1320] | 244 | mWBB->setMaterial("BvHierarchy/BoxHiLite");
|
---|
[1316] | 245 | else
|
---|
| 246 | mWBB->setMaterial("BaseWhiteNoLighting");
|
---|
| 247 |
|
---|
| 248 | return mWBB;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | // returns the level the node is on
|
---|
| 252 | int getLevel(void) const { return mLevel; };
|
---|
| 253 |
|
---|
| 254 | // functions for the CHC hierarchy interface
|
---|
| 255 |
|
---|
| 256 | /** Returns last visited frame id. */
|
---|
| 257 | unsigned int lastVisited(void) { return mLastVisited; };
|
---|
| 258 | /** Set to current frame id.
|
---|
| 259 | @param current frame id.
|
---|
| 260 | */
|
---|
| 261 | void setLastVisited(unsigned int frameid) { mLastVisited = frameid; };
|
---|
| 262 | /** Makes this octree become visible / invisble.
|
---|
| 263 | @param visible Whether this node is to be made visible or invisible
|
---|
| 264 | */
|
---|
| 265 | void setNodeVisible(bool visible) { mVisible = visible; };
|
---|
| 266 | /** Returns true if this node is marked visible, false otherwise.
|
---|
| 267 | */
|
---|
| 268 | bool isNodeVisible(void) { return mVisible; };
|
---|
| 269 | /** Frame id when this octree was last rendered.
|
---|
| 270 | @return last rendered frame id
|
---|
| 271 | */
|
---|
| 272 | unsigned int lastRendered(void) { return mLastRendered; };
|
---|
| 273 | /** Sets frame id when this octree was last rendered.
|
---|
| 274 | @param last rendered frame id
|
---|
| 275 | */
|
---|
| 276 | void setLastRendered(unsigned int frameid) { mLastRendered = frameid; };
|
---|
[1320] | 277 | /** Returns real extent of the BvHierarchy, i.e., the merged extent of the bounding boxes.
|
---|
[1316] | 278 | */
|
---|
| 279 | AxisAlignedBox _getWorldAABB(void) const { return mAABB; };
|
---|
[1320] | 280 | /** Updates bound of the real aabb of BvHierarchy
|
---|
[1316] | 281 | */
|
---|
| 282 | virtual void _updateBounds(bool recurse = true) = 0;
|
---|
| 283 |
|
---|
[1344] | 284 | /** bounding box of the node
|
---|
| 285 | */
|
---|
[1316] | 286 | AxisAlignedBox mAABB;
|
---|
| 287 |
|
---|
[1344] | 288 | /** bounding box of all objects inside the node
|
---|
| 289 | */
|
---|
[1316] | 290 | AxisAlignedBox mWorldAABB;
|
---|
[1344] | 291 |
|
---|
| 292 |
|
---|
[1316] | 293 | protected:
|
---|
[1320] | 294 | BvHierarchy * mOwner;
|
---|
[1316] | 295 | Branch * mParent;
|
---|
| 296 | int mLevel;
|
---|
| 297 |
|
---|
| 298 | WireBoundingBox * mWBB;
|
---|
| 299 |
|
---|
| 300 | // for the CHC hierarchy interface
|
---|
| 301 | unsigned int mLastRendered;
|
---|
| 302 | unsigned int mLastVisited;
|
---|
| 303 | bool mVisible;
|
---|
| 304 | };
|
---|
| 305 |
|
---|
| 306 | class Branch : public Node
|
---|
| 307 | {
|
---|
| 308 | public:
|
---|
[1320] | 309 | Branch(BvHierarchy * owner, int level, AxisAlignedBox& aabb, Branch * parent,
|
---|
| 310 | Plane * splitplane, BvhPlaneEvent::Side side):
|
---|
[1316] | 311 | Node(owner, level, aabb, parent),
|
---|
| 312 | mSplitPlane(splitplane),
|
---|
| 313 | mLeft(0),
|
---|
| 314 | mRight(0),
|
---|
| 315 | mPlaneSide(side)
|
---|
| 316 | { };
|
---|
| 317 |
|
---|
| 318 | virtual ~Branch()
|
---|
| 319 | {
|
---|
| 320 | delete mLeft;
|
---|
| 321 | delete mRight;
|
---|
| 322 | delete mSplitPlane;
|
---|
| 323 | };
|
---|
| 324 |
|
---|
| 325 | // a branch is not a leaf
|
---|
| 326 | virtual bool isLeaf() const { return false; };
|
---|
| 327 |
|
---|
| 328 | // s branch is empty when it does not have children
|
---|
| 329 | virtual bool isEmpty() const { return (mLeft == 0 && mRight == 0); }
|
---|
| 330 |
|
---|
| 331 | // a branch never has geometry
|
---|
| 332 | virtual bool hasGeometry() const { return false; };
|
---|
| 333 |
|
---|
| 334 | virtual void mergeLeaves(std::set<Leaf *>& leaves)
|
---|
| 335 | {
|
---|
| 336 | for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it++)
|
---|
| 337 | leaves.insert(*it);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | // a branch should have at least one child
|
---|
[1320] | 341 | virtual BvHierarchy::Node * getLeftChild() const { return mLeft; };
|
---|
| 342 | virtual BvHierarchy::Node * getRightChild() const { return mRight; };
|
---|
[1316] | 343 |
|
---|
| 344 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
| 345 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false)
|
---|
| 346 | {
|
---|
| 347 | if (showBoxes)
|
---|
| 348 | if (mLevel == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
|
---|
| 349 | queue->addRenderable(getWireBoundingBox());
|
---|
| 350 |
|
---|
| 351 | if (fullVis)
|
---|
| 352 | for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it ++)
|
---|
| 353 | (*it)->queueVisibleObjects(currentFrame, cam, queue, onlyShadowCasters, showBoxes, fullVis);
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | // a branch has no geometry, do nothing
|
---|
[2280] | 357 | virtual void getGeometryList(GeometryVector *geometryList) { }
|
---|
[1316] | 358 |
|
---|
| 359 | // branches do not posses geometry => just merge child aabbs
|
---|
| 360 | virtual void _updateBounds(bool recurse = true)
|
---|
| 361 | {
|
---|
| 362 | // reset box
|
---|
| 363 | mWorldAABB.setNull();
|
---|
| 364 |
|
---|
| 365 | // merge box & leaves
|
---|
| 366 | if (mLeft)
|
---|
| 367 | {
|
---|
| 368 | mWorldAABB.merge(mLeft->mWorldAABB);
|
---|
| 369 | mLeft->mergeLeaves(mLeaves);
|
---|
| 370 | }
|
---|
| 371 | if (mRight)
|
---|
| 372 | {
|
---|
| 373 | mWorldAABB.merge(mRight->mWorldAABB);
|
---|
| 374 | mRight->mergeLeaves(mLeaves);
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | // update parent recursively
|
---|
| 378 | if (recurse && mParent)
|
---|
| 379 | mParent->_updateBounds(recurse);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | Node * mLeft;
|
---|
| 383 | Node * mRight;
|
---|
| 384 | Plane * mSplitPlane;
|
---|
[1320] | 385 | BvhPlaneEvent::Side mPlaneSide;
|
---|
[1316] | 386 | protected:
|
---|
| 387 | std::set<Leaf *> mLeaves;
|
---|
| 388 | };
|
---|
| 389 |
|
---|
| 390 | class Leaf : public Node
|
---|
| 391 | {
|
---|
| 392 | public:
|
---|
[1344] | 393 | Leaf(BvHierarchy * owner, int level, AxisAlignedBox& aabb, Branch *parent):
|
---|
[1316] | 394 | Node(owner, level, aabb, parent)
|
---|
| 395 | {};
|
---|
| 396 |
|
---|
| 397 | virtual ~Leaf();
|
---|
| 398 |
|
---|
| 399 | // a leaf is a leaf, dammit
|
---|
| 400 | virtual bool isLeaf() const { return true; }
|
---|
| 401 |
|
---|
| 402 | // a leaf is empty when it does not posses renderables
|
---|
[1320] | 403 | virtual bool isEmpty() const { return mBvhRenderables.empty(); }
|
---|
[1316] | 404 |
|
---|
| 405 | // a leaf has geometry when it has renderables
|
---|
[1320] | 406 | virtual bool hasGeometry() const { return !mBvhRenderables.empty(); }
|
---|
[1316] | 407 |
|
---|
| 408 | // a leaf adds itself to the leaf set
|
---|
| 409 | virtual void mergeLeaves(std::set<Leaf *>& leaves) { leaves.insert(this); }
|
---|
| 410 |
|
---|
| 411 | // a leaf never has children
|
---|
[1320] | 412 | virtual BvHierarchy::Node * getLeftChild() const { return 0; };
|
---|
| 413 | virtual BvHierarchy::Node * getRightChild() const { return 0; };
|
---|
[1316] | 414 |
|
---|
| 415 | virtual void queueVisibleObjects(unsigned long currentFrame,
|
---|
| 416 | Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false);
|
---|
| 417 |
|
---|
[2280] | 418 | virtual void getGeometryList(GeometryVector *geometryList);
|
---|
[1316] | 419 |
|
---|
| 420 | // update the world aabb based on the contained geometry
|
---|
| 421 | virtual void _updateBounds(bool recurse = true);
|
---|
| 422 |
|
---|
[1320] | 423 | virtual void remove(BvhRenderable * rend)
|
---|
[1316] | 424 | {
|
---|
[1320] | 425 | mBvhRenderables.remove(rend);
|
---|
| 426 | //mBvhRenderables.erase(find(mBvhRenderables.begin(), mBvhRenderables.end(), rend));
|
---|
[1316] | 427 | };
|
---|
| 428 |
|
---|
[1320] | 429 | virtual void insert(BvhRenderable * rend)
|
---|
[1316] | 430 | {
|
---|
[1320] | 431 | mBvhRenderables.push_back(rend);
|
---|
[1316] | 432 | };
|
---|
| 433 |
|
---|
[1320] | 434 | BvhRenderableList mBvhRenderables;
|
---|
[1316] | 435 | };
|
---|
| 436 |
|
---|
[1320] | 437 | struct BvhSubdivisionCandidate
|
---|
[1316] | 438 | {
|
---|
[1320] | 439 | BvhSubdivisionCandidate(BvhPlaneEventList * e, int n, AxisAlignedBox& a,
|
---|
| 440 | BvHierarchy::Branch * p, Real c, Real d, BvhSplitInfo * b, BvhPlaneEvent::Side s):
|
---|
[1316] | 441 | events(e), nObjects(n), aabb(a), parent(p), cost(c), decrease(d), best(b), side(s)
|
---|
| 442 | { };
|
---|
| 443 |
|
---|
[1320] | 444 | bool operator < (const BvhSubdivisionCandidate& rhs) const
|
---|
[1316] | 445 | {
|
---|
| 446 | return decrease < rhs.decrease;
|
---|
| 447 | };
|
---|
| 448 |
|
---|
[1320] | 449 | void operator = (const BvhSubdivisionCandidate& rhs)
|
---|
[1316] | 450 | {
|
---|
| 451 | decrease = rhs.decrease;
|
---|
| 452 | cost = rhs.cost;
|
---|
| 453 | nObjects = rhs.nObjects;
|
---|
| 454 | side = rhs.side;
|
---|
| 455 | aabb = rhs.aabb;
|
---|
| 456 | events = rhs.events;
|
---|
| 457 | parent = rhs.parent;
|
---|
| 458 | best = rhs.best;
|
---|
| 459 | };
|
---|
| 460 |
|
---|
| 461 | // DEBUG
|
---|
| 462 | String print();
|
---|
| 463 |
|
---|
| 464 | Real decrease;
|
---|
| 465 | Real cost;
|
---|
| 466 | int nObjects;
|
---|
[1320] | 467 | BvhPlaneEvent::Side side;
|
---|
[1316] | 468 | AxisAlignedBox aabb;
|
---|
[1320] | 469 | BvhPlaneEventList *events;
|
---|
| 470 | BvHierarchy::Branch * parent;
|
---|
| 471 | BvhSplitInfo * best;
|
---|
[1316] | 472 | };
|
---|
| 473 |
|
---|
[1320] | 474 | // typedef std::stack<BvhSubdivisionCandidate> SplitCandidatePQ;
|
---|
| 475 | typedef std::priority_queue<BvhSubdivisionCandidate> SplitCandidatePQ;
|
---|
[1316] | 476 |
|
---|
| 477 | // nodestack for the stack-based rendering function
|
---|
[1320] | 478 | typedef std::stack<BvHierarchy::Node *> NodeStack;
|
---|
[1316] | 479 | public:
|
---|
[1320] | 480 | friend class BvHierarchyInterface;
|
---|
[1316] | 481 |
|
---|
[1320] | 482 | typedef BvHierarchy::Node * NodePtr;
|
---|
| 483 | typedef BvHierarchy::Branch * BranchPtr;
|
---|
| 484 | typedef BvHierarchy::Leaf * LeafPtr;
|
---|
[1316] | 485 |
|
---|
| 486 | typedef std::list<NodePtr> NodeList;
|
---|
| 487 | typedef std::set<LeafPtr> LeafSet;
|
---|
| 488 |
|
---|
| 489 | struct TreeStats
|
---|
| 490 | {
|
---|
| 491 | unsigned int mNumNodes;
|
---|
| 492 | unsigned int mNumLeaves;
|
---|
| 493 | unsigned int mNumSceneNodes;
|
---|
| 494 |
|
---|
| 495 | void clear(void)
|
---|
| 496 | {
|
---|
| 497 | mNumNodes = 0;
|
---|
| 498 | mNumLeaves = 0;
|
---|
| 499 | mNumSceneNodes = 0;
|
---|
| 500 | }
|
---|
| 501 | };
|
---|
| 502 |
|
---|
| 503 | struct FrameStats
|
---|
| 504 | {
|
---|
| 505 | unsigned int mTraversedNodes;
|
---|
| 506 | unsigned int mRenderedNodes;
|
---|
| 507 | unsigned int mFrustumCulledNodes;
|
---|
| 508 |
|
---|
| 509 | void clear(void)
|
---|
| 510 | {
|
---|
| 511 | mTraversedNodes = 0;
|
---|
| 512 | mRenderedNodes = 0;
|
---|
| 513 | mFrustumCulledNodes = 0;
|
---|
| 514 | }
|
---|
| 515 | };
|
---|
| 516 |
|
---|
| 517 | enum RenderMethod
|
---|
| 518 | {
|
---|
[1320] | 519 | BVHRM_INTERNAL,
|
---|
| 520 | BVHRM_GTP_VFC,
|
---|
| 521 | BVHRM_GTP_SWC,
|
---|
| 522 | BVHRM_GTP_CHC,
|
---|
[1316] | 523 | // invalid modes, just for convenience
|
---|
[1320] | 524 | BVHRM_SIZE,
|
---|
| 525 | BVHRM_NOTSET
|
---|
[1316] | 526 | };
|
---|
| 527 |
|
---|
| 528 | enum BuildMethod
|
---|
| 529 | {
|
---|
[1320] | 530 | BVHBM_RECURSIVE,
|
---|
| 531 | BVHBM_PRIORITYQUEUE,
|
---|
[1316] | 532 | // invalid modes, just for convenience
|
---|
[1320] | 533 | BVHBM_SIZE,
|
---|
| 534 | BVHBM_NOTSET
|
---|
[1316] | 535 | };
|
---|
| 536 |
|
---|
| 537 |
|
---|
| 538 | const static int HILITE_OFF = -1;
|
---|
| 539 |
|
---|
[1320] | 540 | BvHierarchy(int maxdepth, BuildMethod bm);
|
---|
| 541 | BvHierarchy(int maxdepth, BuildMethod bm, int hilite, bool allboxes, bool shownodes);
|
---|
| 542 | virtual ~BvHierarchy();
|
---|
[1316] | 543 |
|
---|
| 544 | // DEBUG
|
---|
| 545 | void dump(void);
|
---|
| 546 | Real calcCost(void);
|
---|
| 547 |
|
---|
| 548 | // sets the level to highlight or turns it off (when hilite < 0)
|
---|
| 549 | inline void setHiLiteLevel(int hilite) { mHiLiteLevel = hilite; };
|
---|
| 550 | inline int getHiLiteLevel(void) { return mHiLiteLevel; };
|
---|
| 551 |
|
---|
[1320] | 552 | // toggles displaying the BvHierarchy boxes
|
---|
[1316] | 553 | inline void setShowAllBoxes(bool show) { mShowAllBoxes = show; };
|
---|
| 554 | inline bool getShowAllBoxes(void) { return mShowAllBoxes; };
|
---|
| 555 |
|
---|
| 556 | // toggles between displaying the bounding box of the node and
|
---|
| 557 | // the box of the contained scene nodes
|
---|
| 558 | inline void setShowNodes(bool show = true) { mShowNodes = show; };
|
---|
| 559 | inline bool getShowNodes(void) { return mShowNodes; };
|
---|
| 560 |
|
---|
| 561 | // changes vis mode (simple/enhanced with NONE/PART/FULL vis)
|
---|
| 562 | void setEnhancedVis(bool enh);
|
---|
| 563 | bool getEnhancedVis(void);
|
---|
| 564 |
|
---|
[1320] | 565 | NodePtr getRoot(void) const { return mBvhRoot; };
|
---|
[1316] | 566 |
|
---|
[1320] | 567 | // insert a new scene node into an existing bvhierarchy
|
---|
| 568 | void insert(BvhRenderable * rend);
|
---|
[1316] | 569 | // remove a scene node from the tree
|
---|
[1320] | 570 | void remove(BvhRenderable * rend);
|
---|
[1316] | 571 | // function to initialize a kd-tree based on the contents of the scene
|
---|
[1320] | 572 | void build(BvhRenderable * sceneRoot);
|
---|
[1316] | 573 |
|
---|
| 574 | // test visibility of objects and add to render queue
|
---|
[1319] | 575 | void queueVisibleObjects(BvHierarchyCamera* cam, RenderQueue* queue, bool onlyShadowCasters,
|
---|
[1320] | 576 | bool showBoxes, BvHierarchy::NodeList& visibleNodes);
|
---|
[1316] | 577 |
|
---|
| 578 | // find visible nodes & place in list
|
---|
| 579 | //void findVisibleNodes(NodeList& visibleNodes, Camera * cam);
|
---|
| 580 |
|
---|
[1320] | 581 | /** Recurses the BvHierarchy, adding any nodes intersecting with the
|
---|
[1316] | 582 | box/sphere/volume/ray into the given list.
|
---|
| 583 | It ignores the exclude scene node.
|
---|
| 584 | */
|
---|
| 585 | void findNodesIn(const AxisAlignedBox &box, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
| 586 | void findNodesIn(const Sphere &sphere, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
| 587 | void findNodesIn(const PlaneBoundedVolume &volume, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
| 588 | void findNodesIn(const Ray &ray, std::list<SceneNode *> &list, SceneNode *exclude = 0);
|
---|
| 589 |
|
---|
| 590 | // self-explanatory ...
|
---|
| 591 | int getMaxDepth(void) { return mMaxDepth; }
|
---|
| 592 | const TreeStats& getTreeStats(void) const { return mTreeStats; }
|
---|
| 593 | const FrameStats& getFramesStats(void) const { return mFrameStats; }
|
---|
[1320] | 594 | AxisAlignedBox getBox(void) { if (mBvhRoot) return mBvhRoot->mAABB; else return AxisAlignedBox(); }
|
---|
[1316] | 595 | void setBuildMethod(BuildMethod bm) { mBuildMethod = bm; }
|
---|
| 596 | protected:
|
---|
| 597 | // init materials, logs and stuff
|
---|
| 598 | void init();
|
---|
| 599 | // recursive insert funciton
|
---|
[1320] | 600 | void recInsert(BvHierarchy::Node * node, BvhRenderable * rend);
|
---|
[1316] | 601 | // helper functions for insert
|
---|
[1320] | 602 | void recInsertNew(BvHierarchy::Branch * parent, BvhPlaneEvent::Side side, BvhRenderable * rend);
|
---|
| 603 | void splitBox(const BvHierarchy::Branch& parent, AxisAlignedBox& left, AxisAlignedBox& right);
|
---|
| 604 | void rebuildSubtree(BvHierarchy::Node * node, BvhRenderable * rend);
|
---|
[1316] | 605 | // build scene from a list of nodes rather than a hierarchy
|
---|
[1320] | 606 | BvHierarchy::Node * buildFromList(BvhRenderableList& nodelist, BvHierarchy::Branch * parent, AxisAlignedBox& aabb);
|
---|
| 607 | void addRendToList(BvHierarchy::Node * node, BvhRenderableList& nodelist);
|
---|
[1316] | 608 |
|
---|
| 609 | // recursively delete empty nodes
|
---|
[1320] | 610 | void recDelete(BvHierarchy::Node * node);
|
---|
[1316] | 611 |
|
---|
| 612 | // find the best plane for node division
|
---|
[1320] | 613 | BvhSplitInfo * pqFindPlane(BvhPlaneEventList * events, int nObjects, AxisAlignedBox& aabb, Real globalSA);
|
---|
[1316] | 614 |
|
---|
| 615 | // priority queue based build function
|
---|
[1320] | 616 | BvHierarchy::Node * pqBuild(BvhPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BvHierarchy::Branch * parent);
|
---|
[1316] | 617 | // recursive build function
|
---|
[1320] | 618 | BvHierarchy::Node * recBuild(BvhPlaneEventList& events, int nObjects, AxisAlignedBox& aabb, BvHierarchy::Branch * parent);
|
---|
[1316] | 619 |
|
---|
| 620 | // recursive rendering function
|
---|
[1320] | 621 | void recQueueVisibleObjects(BvHierarchy::Node * node, unsigned long currentFrame, BvHierarchyCamera* cam,
|
---|
[1316] | 622 | RenderQueue* queue, bool onlyShadowCasters, bool showBoxes,
|
---|
[1320] | 623 | BvHierarchy::NodeList& visibleNodes, bool fullVis = false);
|
---|
[1316] | 624 |
|
---|
| 625 | // recursively find visible nodes
|
---|
[1320] | 626 | //void recFindVisibleNodes(BvHierarchy::Node * node, NodeList& visibleNodes, Camera * cam);
|
---|
[1316] | 627 |
|
---|
[1320] | 628 | /** Recurses the BvHierarchy, adding any nodes intersecting with the
|
---|
[1316] | 629 | box/sphere/volume/ray into the given list.
|
---|
| 630 | It ignores the exclude scene node.
|
---|
| 631 | */
|
---|
| 632 | void recFindNodesIn(const AxisAlignedBox &box, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
| 633 | void recFindNodesIn(const Sphere &sphere, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
| 634 | void recFindNodesIn(const PlaneBoundedVolume &volume, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
| 635 | void recFindNodesIn(const Ray &ray, std::list<SceneNode *> &list, SceneNode *exclude, Node * node, bool full = false);
|
---|
| 636 |
|
---|
[1320] | 637 | // the root node of the BvHierarchy
|
---|
| 638 | BvHierarchy::Node * mBvhRoot;
|
---|
[1316] | 639 | // Maximum depth of the tree
|
---|
| 640 | int mMaxDepth;
|
---|
| 641 |
|
---|
| 642 | // how to build the tree
|
---|
| 643 | BuildMethod mBuildMethod;
|
---|
| 644 |
|
---|
| 645 | // logfile for tree creation
|
---|
| 646 | Log * mBuildLog;
|
---|
| 647 |
|
---|
| 648 | // statistical information on the tree
|
---|
| 649 | TreeStats mTreeStats;
|
---|
| 650 |
|
---|
| 651 | // statistical info on a single rendered frame
|
---|
| 652 | FrameStats mFrameStats;
|
---|
| 653 |
|
---|
| 654 | /** Visualization flags **/
|
---|
[1320] | 655 | // show/highlight selected level in BvHierarchy
|
---|
[1316] | 656 | int mHiLiteLevel;
|
---|
| 657 | // show whole kd-tree
|
---|
| 658 | bool mShowAllBoxes;
|
---|
| 659 | // show node or object boxes
|
---|
| 660 | bool mShowNodes;
|
---|
| 661 |
|
---|
| 662 | // function pointer to the getVisibility function
|
---|
| 663 | // allows choosing between regular vis (NONE/PART, same es isVisible)
|
---|
| 664 | // and enhaced vis (NONE/PART/FULL) for early traversal abort
|
---|
[1319] | 665 | BvHierarchyCamera::NodeVisibility (BvHierarchyCamera::*getVisibility)(const AxisAlignedBox& box) const;
|
---|
[1316] | 666 |
|
---|
| 667 | // DEBUG
|
---|
[1320] | 668 | void BvHierarchy::dump(BvHierarchy::Node * node);
|
---|
| 669 | Real BvHierarchy::calcCost(BvHierarchy::Node * node, Real vs);
|
---|
| 670 | }; // class BvHierarchy
|
---|
[1316] | 671 |
|
---|
| 672 | } // namespace Ogre
|
---|
| 673 |
|
---|
[1319] | 674 | #endif // _OgreBvHierarchy_H__
|
---|