[2746] | 1 | // This file has been written by Jiri Bittner, October 2006
|
---|
| 2 |
|
---|
| 3 | #ifndef __BVH_H
|
---|
| 4 | #define __BVH_H
|
---|
| 5 |
|
---|
[2756] | 6 | #include "common.h"
|
---|
| 7 | #include "Vector3.h"
|
---|
| 8 | #include "AxisAlignedBox3.h"
|
---|
| 9 | //#include "SceneEntity.h"
|
---|
[2751] | 10 |
|
---|
[2746] | 11 |
|
---|
[2776] | 12 | namespace CHCDemoEngine
|
---|
[2751] | 13 | {
|
---|
| 14 |
|
---|
[2895] | 15 | // the number of visibility states
|
---|
| 16 | #define NUM_STATES 2
|
---|
| 17 |
|
---|
[2746] | 18 | ////////
|
---|
[2760] | 19 | //-- Forward declarations
|
---|
[2746] | 20 |
|
---|
[2755] | 21 | class SceneEntity;
|
---|
[2746] | 22 | class Camera;
|
---|
[2760] | 23 | class RenderState;
|
---|
[2746] | 24 |
|
---|
| 25 |
|
---|
| 26 | /** A node in the bv hierarchy.
|
---|
| 27 | */
|
---|
[2752] | 28 | class BvhNode
|
---|
[2746] | 29 | {
|
---|
| 30 | friend class Bvh;
|
---|
[2755] | 31 | friend class BvhLoader;
|
---|
[2767] | 32 | friend class mygreaterdistance;
|
---|
[2746] | 33 |
|
---|
| 34 | public:
|
---|
| 35 |
|
---|
| 36 | /** visibility related options
|
---|
| 37 | */
|
---|
| 38 | struct VisibilityInfo
|
---|
| 39 | {
|
---|
| 40 | VisibilityInfo() { Reset(); }
|
---|
| 41 | /** Reset the visibility info.
|
---|
| 42 | */
|
---|
| 43 | void Reset();
|
---|
| 44 |
|
---|
| 45 | /// if the node is visible
|
---|
| 46 | bool mIsVisible;
|
---|
[2770] | 47 | /// frame id until this node is assumed to stay visible
|
---|
| 48 | int mAssumedVisibleFrameId;
|
---|
[2746] | 49 | /// the frame when this node was last touched during traversal
|
---|
| 50 | int mLastVisitedFrame;
|
---|
| 51 | /// #times this node was invisible (only valid if the node actually is invisible)
|
---|
| 52 | int mTimesInvisible;
|
---|
| 53 | /// if the node is view frustum culled
|
---|
| 54 | bool mIsFrustumCulled;
|
---|
[2752] | 55 | /// if the node is newly processed with no prior history available
|
---|
[2746] | 56 | bool mIsNew;
|
---|
[2800] | 57 | /// the frame this node was last queried
|
---|
| 58 | int mLastQueriedFrame;
|
---|
[2746] | 59 | };
|
---|
| 60 |
|
---|
| 61 | /** Constructor taking the parent node.
|
---|
| 62 | */
|
---|
| 63 | BvhNode(BvhNode *parent);
|
---|
| 64 | /** Returns true if this node is a leaf.
|
---|
| 65 | */
|
---|
| 66 | //virtual bool IsPseudoLeaf() = 0;
|
---|
| 67 | /** Virtual destructor doing nothing.
|
---|
| 68 | */
|
---|
| 69 | virtual ~BvhNode();
|
---|
| 70 | /** Depth of this node in the tree.
|
---|
| 71 | */
|
---|
[2752] | 72 | inline int GetDepth() const {return mDepth;}
|
---|
[2746] | 73 | /** Returns parent of this bvh node, NULL if it is root.
|
---|
| 74 | */
|
---|
| 75 | inline BvhNode *GetParent() {return mParent;}
|
---|
| 76 | /** Number of leaves in the subtree induced by this node.
|
---|
| 77 | */
|
---|
| 78 | inline int GetNumLeaves() const {return mNumLeaves;}
|
---|
| 79 | /** Reset the status of this node.
|
---|
| 80 | */
|
---|
| 81 | virtual void ResetVisibility();
|
---|
| 82 |
|
---|
[2761] | 83 | virtual bool IsLeaf() const = 0;
|
---|
[2746] | 84 |
|
---|
[2761] | 85 | bool IsVirtualLeaf() const { return mIsVirtualLeaf; }
|
---|
[2746] | 86 |
|
---|
[2761] | 87 |
|
---|
[2755] | 88 | ////////////////
|
---|
[2746] | 89 | //-- visibility culling related functions
|
---|
| 90 |
|
---|
| 91 | inline int GetLastVisitedFrame() const;
|
---|
| 92 |
|
---|
[2755] | 93 | inline void SetLastVisitedFrame(int lastVisited);
|
---|
[2746] | 94 | /** If this node is considered visible.
|
---|
| 95 | */
|
---|
| 96 | inline bool IsVisible() const;
|
---|
| 97 | /** Set visibility flag of the node.
|
---|
| 98 | */
|
---|
[2755] | 99 | inline void SetVisible(bool visible);
|
---|
[2770] | 100 | /** The frame id until this node is assumed visible
|
---|
[2746] | 101 | */
|
---|
[2770] | 102 | inline void SetAssumedVisibleFrameId(int t);
|
---|
[2746] | 103 | /** See set.
|
---|
| 104 | */
|
---|
[2770] | 105 | inline int GetAssumedVisibleFrameId() const;
|
---|
[2746] | 106 | /** Increases the #times this node was
|
---|
| 107 | successfully tested invisible.
|
---|
| 108 | */
|
---|
[2773] | 109 | inline void IncTimesTestedInvisible();
|
---|
[2752] | 110 |
|
---|
[2773] | 111 | inline int GetTimesTestedInvisible() const;
|
---|
| 112 | inline void SetTimesTestedInvisible(int t);
|
---|
[2746] | 113 |
|
---|
| 114 | inline int GetTurnedVisibleFrame() const;
|
---|
[2755] | 115 | inline void SetTurnedVisibleFrame(int turnedVisibleFrame);
|
---|
[2746] | 116 |
|
---|
[2800] | 117 | inline int GetLastQueriedFrame() const;
|
---|
| 118 | inline void SetLastQueriedFrame(int lastTested);
|
---|
[2746] | 119 |
|
---|
| 120 | inline bool IsViewFrustumCulled() const;
|
---|
[2755] | 121 | inline void SetViewFrustumCulled(bool frustumCulled);
|
---|
[2746] | 122 |
|
---|
| 123 | inline bool IsNew() const;
|
---|
[2755] | 124 | inline void SetIsNew(bool isNew);
|
---|
[2746] | 125 |
|
---|
| 126 |
|
---|
[2755] | 127 | /** Returns the bounding box of this node.
|
---|
| 128 | */
|
---|
| 129 | inline const AxisAlignedBox3 &GetBox() { return mBox; }
|
---|
| 130 | /** Return index of this node.
|
---|
| 131 | */
|
---|
| 132 | inline int GetId() const { return mId; }
|
---|
| 133 | /** See get
|
---|
| 134 | */
|
---|
| 135 | inline void SetId(int id) { mId = id; }
|
---|
| 136 |
|
---|
| 137 |
|
---|
[2746] | 138 | //////////
|
---|
| 139 | //-- rendering
|
---|
| 140 |
|
---|
| 141 | /** Returns the frame in which this node was last rendered.
|
---|
| 142 | */
|
---|
| 143 | inline int GetLastRenderedFrame() const;
|
---|
| 144 | /** See get.
|
---|
| 145 | */
|
---|
| 146 | inline void SetLastRenderedFrame(int lastRenderedFrame);
|
---|
| 147 | /** Does this node contain renderable geometry?
|
---|
| 148 | */
|
---|
| 149 | inline bool Empty() const;
|
---|
| 150 | /** Counts #geometry stored in the subtree.
|
---|
| 151 | */
|
---|
| 152 | inline int CountPrimitives() const;
|
---|
| 153 |
|
---|
[2895] | 154 | static void SetCurrentState(int _state) { sCurrentState = _state; }
|
---|
[2746] | 155 |
|
---|
[2895] | 156 |
|
---|
[2746] | 157 | protected:
|
---|
| 158 |
|
---|
| 159 | /// the depth of this node
|
---|
| 160 | unsigned char mDepth;
|
---|
| 161 | /// the split axis
|
---|
| 162 | char mAxis;
|
---|
| 163 | /// the parent node
|
---|
| 164 | BvhNode *mParent;
|
---|
| 165 |
|
---|
[2895] | 166 |
|
---|
| 167 | //////////////
|
---|
| 168 | //-- these members define the current state
|
---|
| 169 |
|
---|
| 170 | /// stores the visibility related info
|
---|
| 171 | VisibilityInfo mVisibility[NUM_STATES];
|
---|
| 172 |
|
---|
[2746] | 173 | /////////
|
---|
[2894] | 174 | //-- used for view frustum culling
|
---|
[2746] | 175 |
|
---|
[2895] | 176 | int mPlaneMask[NUM_STATES];
|
---|
| 177 | int mPreferredPlane[NUM_STATES];
|
---|
[2746] | 178 |
|
---|
[2895] | 179 | // the current state
|
---|
| 180 | static int sCurrentState;
|
---|
[2746] | 181 |
|
---|
[2895] | 182 |
|
---|
| 183 | ////////////////////
|
---|
| 184 |
|
---|
| 185 |
|
---|
[2746] | 186 | ////////////
|
---|
| 187 | //-- rendering related options
|
---|
| 188 |
|
---|
| 189 | /// when the node was last rendered
|
---|
| 190 | int mLastRenderedFrame;
|
---|
| 191 | /// #leaves under this node
|
---|
| 192 | int mNumLeaves;
|
---|
| 193 |
|
---|
| 194 | // Indices to first and last triangle in the triangle array
|
---|
[2752] | 195 | // assumes the triangle are placed in continuous chunk of memory
|
---|
[2746] | 196 | // however this need not be a global array!
|
---|
| 197 |
|
---|
| 198 | /// the index of the first triangle
|
---|
| 199 | int mFirst;
|
---|
| 200 | /// the index of the last triangle
|
---|
| 201 | int mLast;
|
---|
| 202 |
|
---|
[2792] | 203 | /// the bounding boxes of these nodes are queries instead of the current node
|
---|
[2746] | 204 | int mTestNodesIdx;
|
---|
[2792] | 205 | /// the number of tighter bounding volumes used for this node
|
---|
[2746] | 206 | int mNumTestNodes;
|
---|
[2792] | 207 | /// used for efficient element array rendering
|
---|
[2746] | 208 | int mIndicesPtr;
|
---|
[2755] | 209 |
|
---|
[2746] | 210 | /// Area of this node
|
---|
| 211 | float mArea;
|
---|
[2755] | 212 | /// distance to the camera
|
---|
| 213 | float mDistance;
|
---|
| 214 | /// the index of this node
|
---|
| 215 | unsigned int mId;
|
---|
| 216 | /// the bounding box
|
---|
| 217 | AxisAlignedBox3 mBox;
|
---|
[2761] | 218 | /// if this node is a virtual leaf
|
---|
| 219 | bool mIsVirtualLeaf;
|
---|
| 220 | /** This marks the maximal depth where a virtual leaf can be defined.
|
---|
| 221 | From this point on it makes no sense to traverse down further, as all
|
---|
| 222 | nodes below contain the same geometry, so no further refinement of visibility
|
---|
| 223 | can be archieved. All nodes below this point can merely used to define the
|
---|
| 224 | tighter bounds.
|
---|
| 225 | */
|
---|
| 226 | bool mIsMaxDepthForVirtualLeaf;
|
---|
[2746] | 227 | };
|
---|
| 228 |
|
---|
| 229 |
|
---|
[2800] | 230 |
|
---|
[2746] | 231 | /////////////////
|
---|
| 232 | //-- public inline functions
|
---|
| 233 |
|
---|
[2800] | 234 |
|
---|
[2746] | 235 | int BvhNode::GetLastVisitedFrame() const
|
---|
| 236 | {
|
---|
[2895] | 237 | return mVisibility[sCurrentState].mLastVisitedFrame;
|
---|
[2746] | 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | void BvhNode::SetLastVisitedFrame(const int lastVisited)
|
---|
| 242 | {
|
---|
[2895] | 243 | mVisibility[sCurrentState].mLastVisitedFrame = lastVisited;
|
---|
[2746] | 244 | }
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 | bool BvhNode::IsVisible() const
|
---|
| 248 | {
|
---|
[2895] | 249 | return mVisibility[sCurrentState].mIsVisible;
|
---|
[2746] | 250 | }
|
---|
| 251 |
|
---|
| 252 |
|
---|
[2776] | 253 | void BvhNode::SetVisible(bool visible)
|
---|
[2746] | 254 | {
|
---|
[2895] | 255 | mVisibility[sCurrentState].mIsVisible = visible;
|
---|
[2746] | 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[2773] | 259 | void BvhNode::IncTimesTestedInvisible()
|
---|
[2746] | 260 | {
|
---|
[2895] | 261 | ++ mVisibility[sCurrentState].mTimesInvisible;
|
---|
[2746] | 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
[2773] | 265 | int BvhNode::GetTimesTestedInvisible() const
|
---|
[2746] | 266 | {
|
---|
[2895] | 267 | return mVisibility[sCurrentState].mTimesInvisible;
|
---|
[2746] | 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
[2776] | 271 | void BvhNode::SetTimesTestedInvisible(int t)
|
---|
[2746] | 272 | {
|
---|
[2895] | 273 | mVisibility[sCurrentState].mTimesInvisible = t;
|
---|
[2746] | 274 | }
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 | bool BvhNode::IsViewFrustumCulled() const
|
---|
| 278 | {
|
---|
[2895] | 279 | return mVisibility[sCurrentState].mIsFrustumCulled;
|
---|
[2746] | 280 | }
|
---|
| 281 |
|
---|
| 282 |
|
---|
[2800] | 283 | void BvhNode::SetViewFrustumCulled(bool frustumCulled)
|
---|
[2746] | 284 | {
|
---|
[2895] | 285 | mVisibility[sCurrentState].mIsFrustumCulled = frustumCulled;
|
---|
[2746] | 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | bool BvhNode::IsNew() const
|
---|
| 290 | {
|
---|
[2895] | 291 | return mVisibility[sCurrentState].mIsNew;
|
---|
[2746] | 292 | }
|
---|
| 293 |
|
---|
| 294 |
|
---|
[2800] | 295 | void BvhNode::SetIsNew(bool isNew)
|
---|
[2746] | 296 | {
|
---|
[2895] | 297 | mVisibility[sCurrentState].mIsNew = isNew;
|
---|
[2746] | 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
[2895] | 301 | void BvhNode::SetAssumedVisibleFrameId(int t)
|
---|
| 302 | {
|
---|
| 303 | mVisibility[sCurrentState].mAssumedVisibleFrameId = t;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | int BvhNode::GetAssumedVisibleFrameId() const
|
---|
| 308 | {
|
---|
| 309 | return mVisibility[sCurrentState].mAssumedVisibleFrameId;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | int BvhNode::GetLastQueriedFrame() const
|
---|
| 314 | {
|
---|
| 315 | return mVisibility[sCurrentState].mLastQueriedFrame;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | void BvhNode::SetLastQueriedFrame(int lastTested)
|
---|
| 320 | {
|
---|
| 321 | mVisibility[sCurrentState].mLastQueriedFrame = lastTested;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[2746] | 324 | int BvhNode::GetLastRenderedFrame() const
|
---|
| 325 | {
|
---|
| 326 | return mLastRenderedFrame;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 | void BvhNode::SetLastRenderedFrame(int lastRenderedFrame)
|
---|
| 331 | {
|
---|
| 332 | mLastRenderedFrame = lastRenderedFrame;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 |
|
---|
| 336 | bool BvhNode::Empty() const
|
---|
| 337 | {
|
---|
| 338 | return mFirst == -1;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
| 342 | int BvhNode::CountPrimitives() const
|
---|
| 343 | {
|
---|
| 344 | return mLast - mFirst + 1;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 |
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 |
|
---|
| 351 | /** Internal node of the bv hierarchy.
|
---|
| 352 | */
|
---|
| 353 | class BvhInterior: public BvhNode
|
---|
| 354 | {
|
---|
[2755] | 355 | friend class Bvh;
|
---|
| 356 | friend class BvhLoader;
|
---|
[2746] | 357 |
|
---|
| 358 | public:
|
---|
[2755] | 359 |
|
---|
[2746] | 360 | BvhInterior(BvhNode *parent): mBack(NULL), mFront(NULL), BvhNode(parent)
|
---|
| 361 | {}
|
---|
[2761] | 362 | virtual bool IsLeaf() const { return false; }
|
---|
[2746] | 363 | /** Back child.
|
---|
| 364 | */
|
---|
[2761] | 365 | inline BvhNode *GetBack() { return mBack; }
|
---|
[2746] | 366 | /** Front child.
|
---|
| 367 | */
|
---|
[2761] | 368 | inline BvhNode *GetFront() { return mFront; }
|
---|
[2792] | 369 | /** Recursivly delete hierarchy.
|
---|
[2746] | 370 | */
|
---|
[2792] | 371 | virtual ~BvhInterior();
|
---|
[2746] | 372 |
|
---|
| 373 | protected:
|
---|
| 374 |
|
---|
| 375 | BvhNode *mBack;
|
---|
| 376 | BvhNode *mFront;
|
---|
| 377 | };
|
---|
| 378 |
|
---|
| 379 |
|
---|
| 380 | class BvhLeaf: public BvhNode
|
---|
| 381 | {
|
---|
[2755] | 382 | friend class Bvh;
|
---|
| 383 | friend class BvhLoader;
|
---|
[2746] | 384 |
|
---|
| 385 | public:
|
---|
| 386 |
|
---|
[2752] | 387 | BvhLeaf(BvhNode *parent): BvhNode(parent)
|
---|
[2746] | 388 | {}
|
---|
| 389 |
|
---|
| 390 | ~BvhLeaf();
|
---|
| 391 |
|
---|
[2761] | 392 | virtual bool IsLeaf() const { return true; }
|
---|
[2746] | 393 | };
|
---|
| 394 |
|
---|
| 395 |
|
---|
[2755] | 396 | /** This class implements the compare operator for the priority queue.
|
---|
| 397 | a lower distance has a higher value in the queue
|
---|
| 398 | */
|
---|
[2767] | 399 | class mygreaterdistance
|
---|
[2755] | 400 | {
|
---|
| 401 | public:
|
---|
| 402 | bool operator() (BvhNode *v1, BvhNode *v2) const
|
---|
| 403 | {
|
---|
| 404 | return (v1->mDistance > v2->mDistance);
|
---|
| 405 | }
|
---|
| 406 | };
|
---|
| 407 |
|
---|
[2767] | 408 | typedef std::priority_queue<BvhNode *, std::vector<BvhNode *>, mygreaterdistance> TraversalQueue;
|
---|
[2755] | 409 |
|
---|
[2767] | 410 |
|
---|
[2746] | 411 | /** Class representing a bounding volume hierarchy.
|
---|
| 412 | */
|
---|
| 413 | class Bvh
|
---|
| 414 | {
|
---|
[2755] | 415 | friend class BvhLoader;
|
---|
| 416 |
|
---|
[2746] | 417 | /** Bvh properties
|
---|
| 418 | */
|
---|
| 419 | struct BvhStats
|
---|
| 420 | {
|
---|
| 421 | BvhStats():
|
---|
| 422 | mInteriorSA(0),
|
---|
| 423 | mLeafSA(0),
|
---|
| 424 | mInteriorVol(0),
|
---|
| 425 | mLeafVol(0),
|
---|
| 426 | mTriangles(0),
|
---|
| 427 | mTriangleRatio(0),
|
---|
| 428 | mGeometryRatio(0),
|
---|
| 429 | mMaxGeometry(0),
|
---|
| 430 | mMaxTriangles(0)
|
---|
| 431 | {}
|
---|
| 432 |
|
---|
| 433 | float mInteriorSA;
|
---|
| 434 | float mLeafSA;
|
---|
| 435 | float mInteriorVol;
|
---|
| 436 | float mLeafVol;
|
---|
[2760] | 437 |
|
---|
[2746] | 438 | int mTriangles;
|
---|
| 439 |
|
---|
| 440 | float mTriangleRatio;
|
---|
| 441 | float mGeometryRatio;
|
---|
| 442 |
|
---|
| 443 | int mMaxGeometry;
|
---|
| 444 | int mMaxTriangles;
|
---|
| 445 | };
|
---|
| 446 |
|
---|
| 447 | public:
|
---|
[2763] | 448 |
|
---|
[2756] | 449 | /** Destructor.
|
---|
| 450 | */
|
---|
| 451 | ~Bvh();
|
---|
[2746] | 452 | /** Returns number of bvh nodes.
|
---|
| 453 | */
|
---|
[2756] | 454 | inline int GetNumNodes() const { return mNumNodes; }
|
---|
[2746] | 455 | /** Returns number of bvh leaves.
|
---|
| 456 | */
|
---|
[2756] | 457 | inline int GetNumLeaves() const { return mNumNodes / 2 + 1;}
|
---|
[2800] | 458 | /** Returns number of 'virtual' nodes in the hierarchy, i.e.
|
---|
| 459 | the number of nodes actually used for traversal.
|
---|
| 460 | */
|
---|
| 461 | int GetNumVirtualNodes() const { return mNumVirtualNodes; }
|
---|
| 462 | /** Returns number of bvh leaves.
|
---|
| 463 | */
|
---|
| 464 | inline int GetNumVirtualLeaves() const { return mNumVirtualNodes / 2 + 1;}
|
---|
[2746] | 465 | /** Returns root node of the bvh.
|
---|
| 466 | */
|
---|
[2755] | 467 | BvhNode *GetRoot() { return mRoot; }
|
---|
[2771] | 468 | /** Sets the scene camera
|
---|
| 469 | */
|
---|
| 470 | void SetCamera(Camera * camera) { mCamera = camera; }
|
---|
[2746] | 471 |
|
---|
[2771] | 472 | ///////////////
|
---|
| 473 | //-- functions collecting nodes based on some criteria
|
---|
[2746] | 474 |
|
---|
[2755] | 475 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes, int depth);
|
---|
| 476 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes);
|
---|
| 477 | void CollectLeaves(BvhNode *node, BvhLeafContainer &leaves);
|
---|
[2761] | 478 | /** Collect only the virtual leaves.
|
---|
| 479 | */
|
---|
| 480 | void CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves);
|
---|
[2755] | 481 |
|
---|
| 482 |
|
---|
[2746] | 483 | //////////////////////
|
---|
| 484 |
|
---|
| 485 | /** Returns geometry by reference (faster).
|
---|
| 486 | */
|
---|
[2755] | 487 | SceneEntity **GetGeometry(BvhNode *node, int &size);
|
---|
[2746] | 488 |
|
---|
| 489 |
|
---|
| 490 | /////////////
|
---|
| 491 | //-- Rendering related options
|
---|
| 492 |
|
---|
[2786] | 493 | /** Renders the bounds of this node
|
---|
| 494 | (the box of the node or the tigher bounds of some subnodes).
|
---|
[2746] | 495 | */
|
---|
[2786] | 496 | void RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 497 | /** Renders bounding boxes of the collection of nodes.
|
---|
| 498 | @returns #rendered boxes
|
---|
| 499 | */
|
---|
[2786] | 500 | int RenderBounds(const BvhNodeContainer &nodes, RenderState *state, bool useTightBounds);
|
---|
[2760] | 501 | /** Returns the bounding box of this bvh.
|
---|
| 502 | */
|
---|
| 503 | inline const AxisAlignedBox3 &GetBox() { return mBox; }
|
---|
[2746] | 504 |
|
---|
| 505 |
|
---|
| 506 | //////////////
|
---|
| 507 | //-- Traversal related options
|
---|
| 508 |
|
---|
| 509 | /** Pulls up visible classification.
|
---|
| 510 | */
|
---|
| 511 | void MakeParentsVisible(BvhNode *node);
|
---|
| 512 | /** Does the view frustum culling for this node with respect to the previous culls
|
---|
| 513 | @returns: 0 if completely outside, 1 if completely inside, -1 if intersecting (partly inside),
|
---|
| 514 | */
|
---|
| 515 | int IsWithinViewFrustum(BvhNode *node);
|
---|
[2771] | 516 | /** Sets frame dependent values
|
---|
[2746] | 517 | */
|
---|
[2771] | 518 | void InitFrame();
|
---|
[2755] | 519 | /** This gives the orthogonal distance from the viewpoint to the nearest bounding box vertex
|
---|
[2746] | 520 | note that negative values can appear because culling is done only afterwards
|
---|
| 521 | */
|
---|
[2764] | 522 | void CalcDistance(BvhNode *node) const;
|
---|
| 523 | /** Returns the stored distance.
|
---|
| 524 | */
|
---|
| 525 | float GetDistance(BvhNode *node) const { return node->mDistance; }
|
---|
[2746] | 526 | /** Pulls up the last visited classification in the bvh.
|
---|
| 527 | */
|
---|
[2755] | 528 | void PullUpLastVisited(BvhNode *node, int frameId) const;
|
---|
[2746] | 529 | /** Resets the node classifications in the tree.
|
---|
| 530 | */
|
---|
| 531 | void ResetNodeClassifications();
|
---|
| 532 | /** Helper function that renders the bounding boxes of the leaves as
|
---|
| 533 | wireframes for visualization purpose.
|
---|
| 534 | */
|
---|
[2755] | 535 | //void RenderBoundingBoxesForViz(int mode);
|
---|
[2746] | 536 | /** Count triangles the node contains.
|
---|
| 537 | */
|
---|
| 538 | int CountTriangles(BvhNode *node) const;
|
---|
[2756] | 539 | /** Returns area of the the node.
|
---|
[2746] | 540 | */
|
---|
[2756] | 541 | float GetArea(BvhNode *node) const;
|
---|
| 542 | /** Compute unique ids for the nodes.
|
---|
| 543 | */
|
---|
| 544 | void ComputeIds();
|
---|
[2761] | 545 | /** Assign virtual leaves based on specified number of triangles per leaf.
|
---|
| 546 | */
|
---|
| 547 | void SetVirtualLeaves(int numTriangles);
|
---|
[2800] | 548 |
|
---|
[2746] | 549 |
|
---|
[2755] | 550 | ////////
|
---|
| 551 | //-- functions influencing tighter bounds
|
---|
[2746] | 552 |
|
---|
| 553 |
|
---|
[2755] | 554 | /** Sets maximal depth for taking the bounding boxes to test the
|
---|
| 555 | visibility of a node.
|
---|
| 556 | Deeper => the bounds adapt more to the geometry.
|
---|
| 557 | */
|
---|
| 558 | void SetMaxDepthForTestingChildren(int maxDepth);
|
---|
[2746] | 559 |
|
---|
[2755] | 560 | void SetAreaRatioThresholdForTestingChildren(float ratio);
|
---|
[2746] | 561 |
|
---|
[2800] | 562 |
|
---|
| 563 | ////////////////////////////
|
---|
| 564 |
|
---|
[2755] | 565 | /** Returns stats.
|
---|
| 566 | */
|
---|
[2773] | 567 | const BvhStats &GetBvhStats() const { return mBvhStats; }
|
---|
[2790] | 568 | /** Render wireframe bvh for visualization purpose.
|
---|
| 569 | */
|
---|
[2825] | 570 | void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 571 |
|
---|
[2760] | 572 |
|
---|
[2746] | 573 | protected:
|
---|
| 574 |
|
---|
| 575 | ////////////////////
|
---|
| 576 |
|
---|
| 577 | /** protected constructor: do nothing.
|
---|
| 578 | */
|
---|
[2755] | 579 | Bvh();
|
---|
[2760] | 580 | /** Protected constructor taking scene geometry into account
|
---|
| 581 | */
|
---|
| 582 | const Bvh(const SceneEntityContainer &entities);
|
---|
[2773] | 583 | /** Called by the constructor. Initializes important members.
|
---|
| 584 | */
|
---|
| 585 | void Init();
|
---|
[2746] | 586 |
|
---|
| 587 | /////////////
|
---|
| 588 |
|
---|
| 589 | void ComputeBvhStats();
|
---|
| 590 | void PrintBvhStats() const;
|
---|
| 591 |
|
---|
| 592 |
|
---|
| 593 | //////////
|
---|
| 594 | //-- Helper methods for bounding box rendering in immediate and vbo mode.
|
---|
| 595 |
|
---|
| 596 | void PrepareVertices();
|
---|
| 597 |
|
---|
[2786] | 598 | int PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes);
|
---|
| 599 | void RenderBoundsWithDrawArrays(int numNodes, RenderState *state);
|
---|
[2746] | 600 |
|
---|
| 601 | /** Create the indices that each node needs to use vbo rendering.
|
---|
| 602 | */
|
---|
| 603 | void CreateIndices();
|
---|
| 604 | /** Create the list of nodes whose bounding boxes are tested instead of the
|
---|
| 605 | bounding box of the node itself.
|
---|
| 606 | */
|
---|
| 607 | bool CreateNodeRenderList(BvhNode *node);
|
---|
| 608 | /** Recursivly updates indices so we can
|
---|
| 609 | render also interior nodes without traversing hierarchy
|
---|
| 610 | */
|
---|
| 611 | void UpdateInteriors(BvhNode *node);
|
---|
[2755] | 612 | /** Recomputes the boundaries of the nodes.
|
---|
| 613 | This function is always called if some boundary options are changed.
|
---|
[2746] | 614 | */
|
---|
| 615 | void RecomputeBounds();
|
---|
[2760] | 616 | /** Does some postprocessing on the nodes.
|
---|
[2746] | 617 | */
|
---|
[2761] | 618 | void PostProcess();
|
---|
[2760] | 619 | /** Helper method that updates the number of leaves in the subtree under
|
---|
| 620 | this node.
|
---|
| 621 | */
|
---|
| 622 | void UpdateNumLeaves(BvhNode *node) const;
|
---|
[2762] | 623 | /** Frustum tests the ith plane.
|
---|
| 624 | */
|
---|
| 625 | inline bool TestPlane(BvhNode *node, int i, bool &bIntersect);
|
---|
[2786] | 626 | /** Renders a bounding box in immediate mode.
|
---|
| 627 | */
|
---|
[2763] | 628 | void RenderBoundingBoxImmediate(const AxisAlignedBox3 &box);
|
---|
[2762] | 629 |
|
---|
[2764] | 630 |
|
---|
| 631 |
|
---|
[2746] | 632 | ////////////////////////
|
---|
[2894] | 633 |
|
---|
| 634 | /// the bounding box of the bvh
|
---|
| 635 | AxisAlignedBox3 mBox;
|
---|
[2746] | 636 | /// the root of the hierarchy
|
---|
| 637 | BvhNode *mRoot;
|
---|
| 638 | /// pointers to the geometry associated with this node
|
---|
[2755] | 639 | SceneEntity **mGeometry;
|
---|
[2752] | 640 | /// #of entities
|
---|
[2746] | 641 | size_t mGeometrySize;
|
---|
| 642 |
|
---|
[2894] | 643 | /// the current camera
|
---|
| 644 | Camera *mCamera;
|
---|
[2746] | 645 |
|
---|
[2894] | 646 |
|
---|
[2746] | 647 | ////////////////
|
---|
[2894] | 648 | //-- tigher bounds termination criteria
|
---|
[2746] | 649 |
|
---|
| 650 | /** maximal depth from which children are fetched for
|
---|
| 651 | testing instead of the current node
|
---|
| 652 | */
|
---|
| 653 | int mMaxDepthForTestingChildren;
|
---|
[2894] | 654 | /// threshold for computing tighter bounds
|
---|
[2746] | 655 | float mAreaRatioThreshold;
|
---|
[2894] | 656 |
|
---|
| 657 |
|
---|
| 658 | ////////////////
|
---|
| 659 | //-- statistics
|
---|
[2746] | 660 |
|
---|
| 661 | BvhStats mBvhStats;
|
---|
[2894] | 662 | /// the overall number of nodes
|
---|
| 663 | int mNumNodes;
|
---|
| 664 | /// the number of "virtual" (=actually used) nodes
|
---|
| 665 | int mNumVirtualNodes;
|
---|
[2746] | 666 |
|
---|
[2894] | 667 |
|
---|
| 668 | ////////////
|
---|
| 669 | //-- rendering stuff
|
---|
| 670 |
|
---|
| 671 | /// these proxy nodes are tested instead of the current node
|
---|
[2755] | 672 | BvhNodeContainer mTestNodes;
|
---|
[2894] | 673 | /// the indices used for vbo index buffering
|
---|
[2746] | 674 | unsigned int *mTestIndices;
|
---|
| 675 | /// a pointer to the end of the indices array
|
---|
| 676 | int mCurrentIndicesPtr;
|
---|
[2894] | 677 | /// the vbo id
|
---|
[2773] | 678 | unsigned int mVboId;
|
---|
[2894] | 679 | /// a vertex array used if working with indexed arrays (without vbo)
|
---|
| 680 | Vector3 *mVertices;
|
---|
| 681 | /// indices used for draw array rendering
|
---|
| 682 | unsigned int *mIndices;
|
---|
[2746] | 683 | };
|
---|
| 684 |
|
---|
[2751] | 685 | }
|
---|
| 686 |
|
---|
[2746] | 687 | #endif // __BVH_H |
---|