[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 | //////////////
|
---|
[2897] | 168 | //-- members that define the current state
|
---|
[2895] | 169 |
|
---|
| 170 | /// stores the visibility related info
|
---|
| 171 | VisibilityInfo mVisibility[NUM_STATES];
|
---|
[2897] | 172 |
|
---|
| 173 | /// used for view frustum culling
|
---|
[2895] | 174 | int mPlaneMask[NUM_STATES];
|
---|
| 175 | int mPreferredPlane[NUM_STATES];
|
---|
[2746] | 176 |
|
---|
[2897] | 177 | /// when the node was last rendered
|
---|
| 178 | int mLastRenderedFrame[NUM_STATES];
|
---|
| 179 |
|
---|
| 180 |
|
---|
[2895] | 181 | // the current state
|
---|
| 182 | static int sCurrentState;
|
---|
[2746] | 183 |
|
---|
[2895] | 184 |
|
---|
| 185 | ////////////////////
|
---|
| 186 |
|
---|
| 187 |
|
---|
[2897] | 188 | /// #leaves under this node
|
---|
| 189 | int mNumLeaves;
|
---|
| 190 |
|
---|
| 191 |
|
---|
[2746] | 192 | ////////////
|
---|
| 193 | //-- rendering related options
|
---|
| 194 |
|
---|
| 195 |
|
---|
| 196 | // Indices to first and last triangle in the triangle array
|
---|
[2752] | 197 | // assumes the triangle are placed in continuous chunk of memory
|
---|
[2746] | 198 | // however this need not be a global array!
|
---|
| 199 |
|
---|
| 200 | /// the index of the first triangle
|
---|
| 201 | int mFirst;
|
---|
| 202 | /// the index of the last triangle
|
---|
| 203 | int mLast;
|
---|
| 204 |
|
---|
[2792] | 205 | /// the bounding boxes of these nodes are queries instead of the current node
|
---|
[2746] | 206 | int mTestNodesIdx;
|
---|
[2792] | 207 | /// the number of tighter bounding volumes used for this node
|
---|
[2746] | 208 | int mNumTestNodes;
|
---|
[2792] | 209 | /// used for efficient element array rendering
|
---|
[2746] | 210 | int mIndicesPtr;
|
---|
[2755] | 211 |
|
---|
[2746] | 212 | /// Area of this node
|
---|
| 213 | float mArea;
|
---|
[2755] | 214 | /// distance to the camera
|
---|
| 215 | float mDistance;
|
---|
| 216 | /// the index of this node
|
---|
| 217 | unsigned int mId;
|
---|
| 218 | /// the bounding box
|
---|
| 219 | AxisAlignedBox3 mBox;
|
---|
[2761] | 220 | /// if this node is a virtual leaf
|
---|
| 221 | bool mIsVirtualLeaf;
|
---|
| 222 | /** This marks the maximal depth where a virtual leaf can be defined.
|
---|
| 223 | From this point on it makes no sense to traverse down further, as all
|
---|
| 224 | nodes below contain the same geometry, so no further refinement of visibility
|
---|
| 225 | can be archieved. All nodes below this point can merely used to define the
|
---|
| 226 | tighter bounds.
|
---|
| 227 | */
|
---|
| 228 | bool mIsMaxDepthForVirtualLeaf;
|
---|
[2746] | 229 | };
|
---|
| 230 |
|
---|
| 231 |
|
---|
[2800] | 232 |
|
---|
[2746] | 233 | /////////////////
|
---|
| 234 | //-- public inline functions
|
---|
| 235 |
|
---|
[2800] | 236 |
|
---|
[2746] | 237 | int BvhNode::GetLastVisitedFrame() const
|
---|
| 238 | {
|
---|
[2895] | 239 | return mVisibility[sCurrentState].mLastVisitedFrame;
|
---|
[2746] | 240 | }
|
---|
| 241 |
|
---|
| 242 |
|
---|
| 243 | void BvhNode::SetLastVisitedFrame(const int lastVisited)
|
---|
| 244 | {
|
---|
[2895] | 245 | mVisibility[sCurrentState].mLastVisitedFrame = lastVisited;
|
---|
[2746] | 246 | }
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | bool BvhNode::IsVisible() const
|
---|
| 250 | {
|
---|
[2895] | 251 | return mVisibility[sCurrentState].mIsVisible;
|
---|
[2746] | 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
[2776] | 255 | void BvhNode::SetVisible(bool visible)
|
---|
[2746] | 256 | {
|
---|
[2895] | 257 | mVisibility[sCurrentState].mIsVisible = visible;
|
---|
[2746] | 258 | }
|
---|
| 259 |
|
---|
| 260 |
|
---|
[2773] | 261 | void BvhNode::IncTimesTestedInvisible()
|
---|
[2746] | 262 | {
|
---|
[2895] | 263 | ++ mVisibility[sCurrentState].mTimesInvisible;
|
---|
[2746] | 264 | }
|
---|
| 265 |
|
---|
| 266 |
|
---|
[2773] | 267 | int BvhNode::GetTimesTestedInvisible() const
|
---|
[2746] | 268 | {
|
---|
[2895] | 269 | return mVisibility[sCurrentState].mTimesInvisible;
|
---|
[2746] | 270 | }
|
---|
| 271 |
|
---|
| 272 |
|
---|
[2776] | 273 | void BvhNode::SetTimesTestedInvisible(int t)
|
---|
[2746] | 274 | {
|
---|
[2895] | 275 | mVisibility[sCurrentState].mTimesInvisible = t;
|
---|
[2746] | 276 | }
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | bool BvhNode::IsViewFrustumCulled() const
|
---|
| 280 | {
|
---|
[2895] | 281 | return mVisibility[sCurrentState].mIsFrustumCulled;
|
---|
[2746] | 282 | }
|
---|
| 283 |
|
---|
| 284 |
|
---|
[2800] | 285 | void BvhNode::SetViewFrustumCulled(bool frustumCulled)
|
---|
[2746] | 286 | {
|
---|
[2895] | 287 | mVisibility[sCurrentState].mIsFrustumCulled = frustumCulled;
|
---|
[2746] | 288 | }
|
---|
| 289 |
|
---|
| 290 |
|
---|
| 291 | bool BvhNode::IsNew() const
|
---|
| 292 | {
|
---|
[2895] | 293 | return mVisibility[sCurrentState].mIsNew;
|
---|
[2746] | 294 | }
|
---|
| 295 |
|
---|
| 296 |
|
---|
[2800] | 297 | void BvhNode::SetIsNew(bool isNew)
|
---|
[2746] | 298 | {
|
---|
[2895] | 299 | mVisibility[sCurrentState].mIsNew = isNew;
|
---|
[2746] | 300 | }
|
---|
| 301 |
|
---|
| 302 |
|
---|
[2895] | 303 | void BvhNode::SetAssumedVisibleFrameId(int t)
|
---|
| 304 | {
|
---|
| 305 | mVisibility[sCurrentState].mAssumedVisibleFrameId = t;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 |
|
---|
| 309 | int BvhNode::GetAssumedVisibleFrameId() const
|
---|
| 310 | {
|
---|
| 311 | return mVisibility[sCurrentState].mAssumedVisibleFrameId;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 |
|
---|
| 315 | int BvhNode::GetLastQueriedFrame() const
|
---|
| 316 | {
|
---|
| 317 | return mVisibility[sCurrentState].mLastQueriedFrame;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 |
|
---|
| 321 | void BvhNode::SetLastQueriedFrame(int lastTested)
|
---|
| 322 | {
|
---|
| 323 | mVisibility[sCurrentState].mLastQueriedFrame = lastTested;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[2897] | 326 |
|
---|
[2746] | 327 | int BvhNode::GetLastRenderedFrame() const
|
---|
| 328 | {
|
---|
[2897] | 329 | return mLastRenderedFrame[sCurrentState];
|
---|
[2746] | 330 | }
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 | void BvhNode::SetLastRenderedFrame(int lastRenderedFrame)
|
---|
| 334 | {
|
---|
[2897] | 335 | mLastRenderedFrame[sCurrentState] = lastRenderedFrame;
|
---|
[2746] | 336 | }
|
---|
| 337 |
|
---|
| 338 |
|
---|
| 339 | bool BvhNode::Empty() const
|
---|
| 340 | {
|
---|
| 341 | return mFirst == -1;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 |
|
---|
| 345 | int BvhNode::CountPrimitives() const
|
---|
| 346 | {
|
---|
| 347 | return mLast - mFirst + 1;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 |
|
---|
| 351 |
|
---|
| 352 |
|
---|
| 353 |
|
---|
| 354 | /** Internal node of the bv hierarchy.
|
---|
| 355 | */
|
---|
| 356 | class BvhInterior: public BvhNode
|
---|
| 357 | {
|
---|
[2755] | 358 | friend class Bvh;
|
---|
| 359 | friend class BvhLoader;
|
---|
[2746] | 360 |
|
---|
| 361 | public:
|
---|
[2755] | 362 |
|
---|
[2746] | 363 | BvhInterior(BvhNode *parent): mBack(NULL), mFront(NULL), BvhNode(parent)
|
---|
| 364 | {}
|
---|
[2761] | 365 | virtual bool IsLeaf() const { return false; }
|
---|
[2746] | 366 | /** Back child.
|
---|
| 367 | */
|
---|
[2761] | 368 | inline BvhNode *GetBack() { return mBack; }
|
---|
[2746] | 369 | /** Front child.
|
---|
| 370 | */
|
---|
[2761] | 371 | inline BvhNode *GetFront() { return mFront; }
|
---|
[2792] | 372 | /** Recursivly delete hierarchy.
|
---|
[2746] | 373 | */
|
---|
[2792] | 374 | virtual ~BvhInterior();
|
---|
[2746] | 375 |
|
---|
| 376 | protected:
|
---|
| 377 |
|
---|
| 378 | BvhNode *mBack;
|
---|
| 379 | BvhNode *mFront;
|
---|
| 380 | };
|
---|
| 381 |
|
---|
| 382 |
|
---|
| 383 | class BvhLeaf: public BvhNode
|
---|
| 384 | {
|
---|
[2755] | 385 | friend class Bvh;
|
---|
| 386 | friend class BvhLoader;
|
---|
[2746] | 387 |
|
---|
| 388 | public:
|
---|
| 389 |
|
---|
[2752] | 390 | BvhLeaf(BvhNode *parent): BvhNode(parent)
|
---|
[2746] | 391 | {}
|
---|
| 392 |
|
---|
| 393 | ~BvhLeaf();
|
---|
| 394 |
|
---|
[2761] | 395 | virtual bool IsLeaf() const { return true; }
|
---|
[2746] | 396 | };
|
---|
| 397 |
|
---|
| 398 |
|
---|
[2755] | 399 | /** This class implements the compare operator for the priority queue.
|
---|
| 400 | a lower distance has a higher value in the queue
|
---|
| 401 | */
|
---|
[2767] | 402 | class mygreaterdistance
|
---|
[2755] | 403 | {
|
---|
| 404 | public:
|
---|
| 405 | bool operator() (BvhNode *v1, BvhNode *v2) const
|
---|
| 406 | {
|
---|
| 407 | return (v1->mDistance > v2->mDistance);
|
---|
| 408 | }
|
---|
| 409 | };
|
---|
| 410 |
|
---|
[2767] | 411 | typedef std::priority_queue<BvhNode *, std::vector<BvhNode *>, mygreaterdistance> TraversalQueue;
|
---|
[2755] | 412 |
|
---|
[2767] | 413 |
|
---|
[2746] | 414 | /** Class representing a bounding volume hierarchy.
|
---|
| 415 | */
|
---|
| 416 | class Bvh
|
---|
| 417 | {
|
---|
[2755] | 418 | friend class BvhLoader;
|
---|
| 419 |
|
---|
[2746] | 420 | /** Bvh properties
|
---|
| 421 | */
|
---|
| 422 | struct BvhStats
|
---|
| 423 | {
|
---|
| 424 | BvhStats():
|
---|
| 425 | mInteriorSA(0),
|
---|
| 426 | mLeafSA(0),
|
---|
| 427 | mInteriorVol(0),
|
---|
| 428 | mLeafVol(0),
|
---|
| 429 | mTriangles(0),
|
---|
| 430 | mTriangleRatio(0),
|
---|
| 431 | mGeometryRatio(0),
|
---|
| 432 | mMaxGeometry(0),
|
---|
| 433 | mMaxTriangles(0)
|
---|
| 434 | {}
|
---|
| 435 |
|
---|
| 436 | float mInteriorSA;
|
---|
| 437 | float mLeafSA;
|
---|
| 438 | float mInteriorVol;
|
---|
| 439 | float mLeafVol;
|
---|
[2760] | 440 |
|
---|
[2746] | 441 | int mTriangles;
|
---|
| 442 |
|
---|
| 443 | float mTriangleRatio;
|
---|
| 444 | float mGeometryRatio;
|
---|
| 445 |
|
---|
| 446 | int mMaxGeometry;
|
---|
| 447 | int mMaxTriangles;
|
---|
| 448 | };
|
---|
| 449 |
|
---|
| 450 | public:
|
---|
[2763] | 451 |
|
---|
[2756] | 452 | /** Destructor.
|
---|
| 453 | */
|
---|
| 454 | ~Bvh();
|
---|
[2746] | 455 | /** Returns number of bvh nodes.
|
---|
| 456 | */
|
---|
[2756] | 457 | inline int GetNumNodes() const { return mNumNodes; }
|
---|
[2746] | 458 | /** Returns number of bvh leaves.
|
---|
| 459 | */
|
---|
[2756] | 460 | inline int GetNumLeaves() const { return mNumNodes / 2 + 1;}
|
---|
[2800] | 461 | /** Returns number of 'virtual' nodes in the hierarchy, i.e.
|
---|
| 462 | the number of nodes actually used for traversal.
|
---|
| 463 | */
|
---|
| 464 | int GetNumVirtualNodes() const { return mNumVirtualNodes; }
|
---|
| 465 | /** Returns number of bvh leaves.
|
---|
| 466 | */
|
---|
| 467 | inline int GetNumVirtualLeaves() const { return mNumVirtualNodes / 2 + 1;}
|
---|
[2746] | 468 | /** Returns root node of the bvh.
|
---|
| 469 | */
|
---|
[2755] | 470 | BvhNode *GetRoot() { return mRoot; }
|
---|
[2771] | 471 | /** Sets the scene camera
|
---|
| 472 | */
|
---|
[2897] | 473 | //void SetCamera(Camera * camera) { mCamera = camera; }
|
---|
[2746] | 474 |
|
---|
[2771] | 475 | ///////////////
|
---|
| 476 | //-- functions collecting nodes based on some criteria
|
---|
[2746] | 477 |
|
---|
[2755] | 478 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes, int depth);
|
---|
| 479 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes);
|
---|
| 480 | void CollectLeaves(BvhNode *node, BvhLeafContainer &leaves);
|
---|
[2761] | 481 | /** Collect only the virtual leaves.
|
---|
| 482 | */
|
---|
| 483 | void CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves);
|
---|
[2755] | 484 |
|
---|
| 485 |
|
---|
[2746] | 486 | //////////////////////
|
---|
| 487 |
|
---|
| 488 | /** Returns geometry by reference (faster).
|
---|
| 489 | */
|
---|
[2755] | 490 | SceneEntity **GetGeometry(BvhNode *node, int &size);
|
---|
[2746] | 491 |
|
---|
| 492 |
|
---|
| 493 | /////////////
|
---|
| 494 | //-- Rendering related options
|
---|
| 495 |
|
---|
[2786] | 496 | /** Renders the bounds of this node
|
---|
| 497 | (the box of the node or the tigher bounds of some subnodes).
|
---|
[2746] | 498 | */
|
---|
[2786] | 499 | void RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 500 | /** Renders bounding boxes of the collection of nodes.
|
---|
| 501 | @returns #rendered boxes
|
---|
| 502 | */
|
---|
[2786] | 503 | int RenderBounds(const BvhNodeContainer &nodes, RenderState *state, bool useTightBounds);
|
---|
[2760] | 504 | /** Returns the bounding box of this bvh.
|
---|
| 505 | */
|
---|
| 506 | inline const AxisAlignedBox3 &GetBox() { return mBox; }
|
---|
[2746] | 507 |
|
---|
| 508 |
|
---|
| 509 | //////////////
|
---|
| 510 | //-- Traversal related options
|
---|
| 511 |
|
---|
| 512 | /** Pulls up visible classification.
|
---|
| 513 | */
|
---|
| 514 | void MakeParentsVisible(BvhNode *node);
|
---|
| 515 | /** Does the view frustum culling for this node with respect to the previous culls
|
---|
| 516 | @returns: 0 if completely outside, 1 if completely inside, -1 if intersecting (partly inside),
|
---|
| 517 | */
|
---|
| 518 | int IsWithinViewFrustum(BvhNode *node);
|
---|
[2771] | 519 | /** Sets frame dependent values
|
---|
[2746] | 520 | */
|
---|
[2897] | 521 | void InitFrame(Camera *camera);
|
---|
[2755] | 522 | /** This gives the orthogonal distance from the viewpoint to the nearest bounding box vertex
|
---|
[2746] | 523 | note that negative values can appear because culling is done only afterwards
|
---|
| 524 | */
|
---|
[2764] | 525 | void CalcDistance(BvhNode *node) const;
|
---|
| 526 | /** Returns the stored distance.
|
---|
| 527 | */
|
---|
| 528 | float GetDistance(BvhNode *node) const { return node->mDistance; }
|
---|
[2746] | 529 | /** Pulls up the last visited classification in the bvh.
|
---|
| 530 | */
|
---|
[2755] | 531 | void PullUpLastVisited(BvhNode *node, int frameId) const;
|
---|
[2746] | 532 | /** Resets the node classifications in the tree.
|
---|
| 533 | */
|
---|
| 534 | void ResetNodeClassifications();
|
---|
| 535 | /** Helper function that renders the bounding boxes of the leaves as
|
---|
| 536 | wireframes for visualization purpose.
|
---|
| 537 | */
|
---|
[2755] | 538 | //void RenderBoundingBoxesForViz(int mode);
|
---|
[2746] | 539 | /** Count triangles the node contains.
|
---|
| 540 | */
|
---|
| 541 | int CountTriangles(BvhNode *node) const;
|
---|
[2756] | 542 | /** Returns area of the the node.
|
---|
[2746] | 543 | */
|
---|
[2756] | 544 | float GetArea(BvhNode *node) const;
|
---|
| 545 | /** Compute unique ids for the nodes.
|
---|
| 546 | */
|
---|
| 547 | void ComputeIds();
|
---|
[2761] | 548 | /** Assign virtual leaves based on specified number of triangles per leaf.
|
---|
| 549 | */
|
---|
| 550 | void SetVirtualLeaves(int numTriangles);
|
---|
[2800] | 551 |
|
---|
[2746] | 552 |
|
---|
[2755] | 553 | ////////
|
---|
| 554 | //-- functions influencing tighter bounds
|
---|
[2746] | 555 |
|
---|
| 556 |
|
---|
[2755] | 557 | /** Sets maximal depth for taking the bounding boxes to test the
|
---|
| 558 | visibility of a node.
|
---|
| 559 | Deeper => the bounds adapt more to the geometry.
|
---|
| 560 | */
|
---|
| 561 | void SetMaxDepthForTestingChildren(int maxDepth);
|
---|
[2746] | 562 |
|
---|
[2755] | 563 | void SetAreaRatioThresholdForTestingChildren(float ratio);
|
---|
[2746] | 564 |
|
---|
[2800] | 565 |
|
---|
| 566 | ////////////////////////////
|
---|
| 567 |
|
---|
[2755] | 568 | /** Returns stats.
|
---|
| 569 | */
|
---|
[2773] | 570 | const BvhStats &GetBvhStats() const { return mBvhStats; }
|
---|
[2790] | 571 | /** Render wireframe bvh for visualization purpose.
|
---|
| 572 | */
|
---|
[2825] | 573 | void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 574 |
|
---|
[2760] | 575 |
|
---|
[2746] | 576 | protected:
|
---|
| 577 |
|
---|
| 578 | ////////////////////
|
---|
| 579 |
|
---|
| 580 | /** protected constructor: do nothing.
|
---|
| 581 | */
|
---|
[2755] | 582 | Bvh();
|
---|
[2760] | 583 | /** Protected constructor taking scene geometry into account
|
---|
| 584 | */
|
---|
| 585 | const Bvh(const SceneEntityContainer &entities);
|
---|
[2773] | 586 | /** Called by the constructor. Initializes important members.
|
---|
| 587 | */
|
---|
| 588 | void Init();
|
---|
[2746] | 589 |
|
---|
| 590 | /////////////
|
---|
| 591 |
|
---|
| 592 | void ComputeBvhStats();
|
---|
| 593 | void PrintBvhStats() const;
|
---|
| 594 |
|
---|
| 595 |
|
---|
| 596 | //////////
|
---|
| 597 | //-- Helper methods for bounding box rendering in immediate and vbo mode.
|
---|
| 598 |
|
---|
| 599 | void PrepareVertices();
|
---|
| 600 |
|
---|
[2786] | 601 | int PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes);
|
---|
| 602 | void RenderBoundsWithDrawArrays(int numNodes, RenderState *state);
|
---|
[2746] | 603 |
|
---|
| 604 | /** Create the indices that each node needs to use vbo rendering.
|
---|
| 605 | */
|
---|
| 606 | void CreateIndices();
|
---|
| 607 | /** Create the list of nodes whose bounding boxes are tested instead of the
|
---|
| 608 | bounding box of the node itself.
|
---|
| 609 | */
|
---|
| 610 | bool CreateNodeRenderList(BvhNode *node);
|
---|
| 611 | /** Recursivly updates indices so we can
|
---|
| 612 | render also interior nodes without traversing hierarchy
|
---|
| 613 | */
|
---|
| 614 | void UpdateInteriors(BvhNode *node);
|
---|
[2755] | 615 | /** Recomputes the boundaries of the nodes.
|
---|
| 616 | This function is always called if some boundary options are changed.
|
---|
[2746] | 617 | */
|
---|
| 618 | void RecomputeBounds();
|
---|
[2760] | 619 | /** Does some postprocessing on the nodes.
|
---|
[2746] | 620 | */
|
---|
[2761] | 621 | void PostProcess();
|
---|
[2760] | 622 | /** Helper method that updates the number of leaves in the subtree under
|
---|
| 623 | this node.
|
---|
| 624 | */
|
---|
| 625 | void UpdateNumLeaves(BvhNode *node) const;
|
---|
[2762] | 626 | /** Frustum tests the ith plane.
|
---|
| 627 | */
|
---|
| 628 | inline bool TestPlane(BvhNode *node, int i, bool &bIntersect);
|
---|
[2786] | 629 | /** Renders a bounding box in immediate mode.
|
---|
| 630 | */
|
---|
[2763] | 631 | void RenderBoundingBoxImmediate(const AxisAlignedBox3 &box);
|
---|
[2762] | 632 |
|
---|
[2764] | 633 |
|
---|
| 634 |
|
---|
[2746] | 635 | ////////////////////////
|
---|
[2894] | 636 |
|
---|
| 637 | /// the bounding box of the bvh
|
---|
| 638 | AxisAlignedBox3 mBox;
|
---|
[2746] | 639 | /// the root of the hierarchy
|
---|
| 640 | BvhNode *mRoot;
|
---|
| 641 | /// pointers to the geometry associated with this node
|
---|
[2755] | 642 | SceneEntity **mGeometry;
|
---|
[2752] | 643 | /// #of entities
|
---|
[2746] | 644 | size_t mGeometrySize;
|
---|
| 645 |
|
---|
| 646 |
|
---|
| 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 |
---|