[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"
|
---|
[2751] | 9 |
|
---|
[2746] | 10 |
|
---|
[3053] | 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 | /** Virtual destructor doing nothing.
|
---|
| 65 | */
|
---|
| 66 | virtual ~BvhNode();
|
---|
[3065] | 67 | /** Reset the status of this node.
|
---|
| 68 | */
|
---|
| 69 | virtual void ResetVisibility();
|
---|
| 70 | /** Returns true if this node is a leaf.
|
---|
| 71 | */
|
---|
| 72 | virtual bool IsLeaf() const = 0;
|
---|
[2746] | 73 | /** Depth of this node in the tree.
|
---|
| 74 | */
|
---|
[3065] | 75 | inline int GetDepth() const;
|
---|
[2746] | 76 | /** Returns parent of this bvh node, NULL if it is root.
|
---|
| 77 | */
|
---|
[3065] | 78 | inline BvhNode *GetParent();
|
---|
[2746] | 79 | /** Number of leaves in the subtree induced by this node.
|
---|
| 80 | */
|
---|
[3065] | 81 | inline int GetNumLeaves() const;
|
---|
| 82 | /** Returns true if this node is a "virtual" leaf,
|
---|
| 83 | i.e., the node is handled as a leaf during traversal.
|
---|
[2746] | 84 | */
|
---|
[3065] | 85 | inline bool IsVirtualLeaf() const;
|
---|
[2947] | 86 | /** Returns the stored distance to the near plane.
|
---|
| 87 | */
|
---|
[3065] | 88 | inline float GetDistance() const;
|
---|
[2761] | 89 |
|
---|
[2947] | 90 |
|
---|
[2755] | 91 | ////////////////
|
---|
[2746] | 92 | //-- visibility culling related functions
|
---|
| 93 |
|
---|
| 94 | inline int GetLastVisitedFrame() const;
|
---|
| 95 |
|
---|
[2755] | 96 | inline void SetLastVisitedFrame(int lastVisited);
|
---|
[2746] | 97 | /** If this node is considered visible.
|
---|
| 98 | */
|
---|
| 99 | inline bool IsVisible() const;
|
---|
| 100 | /** Set visibility flag of the node.
|
---|
| 101 | */
|
---|
[2755] | 102 | inline void SetVisible(bool visible);
|
---|
[2770] | 103 | /** The frame id until this node is assumed visible
|
---|
[2746] | 104 | */
|
---|
[2770] | 105 | inline void SetAssumedVisibleFrameId(int t);
|
---|
[2746] | 106 | /** See set.
|
---|
| 107 | */
|
---|
[2770] | 108 | inline int GetAssumedVisibleFrameId() const;
|
---|
[2746] | 109 | /** Increases the #times this node was
|
---|
| 110 | successfully tested invisible.
|
---|
| 111 | */
|
---|
[2773] | 112 | inline void IncTimesTestedInvisible();
|
---|
[2752] | 113 |
|
---|
[2773] | 114 | inline int GetTimesTestedInvisible() const;
|
---|
| 115 | inline void SetTimesTestedInvisible(int t);
|
---|
[2746] | 116 |
|
---|
| 117 | inline int GetTurnedVisibleFrame() const;
|
---|
[2755] | 118 | inline void SetTurnedVisibleFrame(int turnedVisibleFrame);
|
---|
[2746] | 119 |
|
---|
[2800] | 120 | inline int GetLastQueriedFrame() const;
|
---|
| 121 | inline void SetLastQueriedFrame(int lastTested);
|
---|
[2746] | 122 |
|
---|
| 123 | inline bool IsViewFrustumCulled() const;
|
---|
[2755] | 124 | inline void SetViewFrustumCulled(bool frustumCulled);
|
---|
[2746] | 125 |
|
---|
| 126 | inline bool IsNew() const;
|
---|
[2755] | 127 | inline void SetIsNew(bool isNew);
|
---|
[2746] | 128 |
|
---|
| 129 |
|
---|
[2755] | 130 | /** Returns the bounding box of this node.
|
---|
| 131 | */
|
---|
[3065] | 132 | inline const AxisAlignedBox3 &GetBox() const;
|
---|
[2755] | 133 | /** Return index of this node.
|
---|
| 134 | */
|
---|
[3065] | 135 | inline int GetId() const;
|
---|
[2755] | 136 | /** See get
|
---|
| 137 | */
|
---|
[3065] | 138 | inline void SetId(int id);
|
---|
[3072] | 139 |
|
---|
[2755] | 140 |
|
---|
[2746] | 141 | //////////
|
---|
| 142 | //-- rendering
|
---|
| 143 |
|
---|
| 144 | /** Returns the frame in which this node was last rendered.
|
---|
| 145 | */
|
---|
| 146 | inline int GetLastRenderedFrame() const;
|
---|
| 147 | /** See get.
|
---|
| 148 | */
|
---|
| 149 | inline void SetLastRenderedFrame(int lastRenderedFrame);
|
---|
| 150 | /** Does this node contain renderable geometry?
|
---|
| 151 | */
|
---|
| 152 | inline bool Empty() const;
|
---|
| 153 | /** Counts #geometry stored in the subtree.
|
---|
| 154 | */
|
---|
| 155 | inline int CountPrimitives() const;
|
---|
[3059] | 156 | /** This is an imporantent function once you have one or more
|
---|
| 157 | view point changes within a frame (e.g., camera view and shadow view for
|
---|
| 158 | shadow mapping), because each camera needs its own state in
|
---|
| 159 | order to not break temporal coherency.
|
---|
| 160 | */
|
---|
[3065] | 161 | inline static void SetCurrentState(int _state) { sCurrentState = _state; }
|
---|
[2746] | 162 |
|
---|
[2895] | 163 |
|
---|
[2746] | 164 | protected:
|
---|
| 165 |
|
---|
| 166 | /// the depth of this node
|
---|
| 167 | unsigned char mDepth;
|
---|
| 168 | /// the split axis
|
---|
| 169 | char mAxis;
|
---|
| 170 | /// the parent node
|
---|
| 171 | BvhNode *mParent;
|
---|
| 172 |
|
---|
[2895] | 173 |
|
---|
| 174 | //////////////
|
---|
[3065] | 175 | //-- members definig the current visibility state (one state per camera)
|
---|
[2895] | 176 |
|
---|
[3059] | 177 | /// the currently used state
|
---|
| 178 | static int sCurrentState;
|
---|
| 179 |
|
---|
[2895] | 180 | /// stores the visibility related info
|
---|
| 181 | VisibilityInfo mVisibility[NUM_STATES];
|
---|
[2897] | 182 |
|
---|
[3059] | 183 | // members used to speed up view frustum culling
|
---|
| 184 | /// masks out planes of the frustum that do not have to be tested against
|
---|
[2895] | 185 | int mPlaneMask[NUM_STATES];
|
---|
[3059] | 186 | /// the plane that is usually responsible for culling this node.
|
---|
| 187 | /** if our educated guess was right, we save up to 5 comparisons.
|
---|
| 188 | */
|
---|
[2895] | 189 | int mPreferredPlane[NUM_STATES];
|
---|
[2897] | 190 | /// when the node was last rendered
|
---|
| 191 | int mLastRenderedFrame[NUM_STATES];
|
---|
| 192 |
|
---|
[2895] | 193 | ////////////////////
|
---|
| 194 |
|
---|
| 195 |
|
---|
[2897] | 196 | /// #leaves under this node
|
---|
| 197 | int mNumLeaves;
|
---|
[3065] | 198 | /// Area of this node
|
---|
| 199 | float mArea;
|
---|
| 200 | /// distance to the camera
|
---|
| 201 | float mDistance;
|
---|
| 202 | /// the index of this node
|
---|
| 203 | unsigned int mId;
|
---|
| 204 | /// the bounding box
|
---|
| 205 | AxisAlignedBox3 mBox;
|
---|
| 206 | /// if this node is a virtual leaf
|
---|
| 207 | bool mIsVirtualLeaf;
|
---|
| 208 | /** This marks the maximal depth where a virtual leaf can be defined.
|
---|
| 209 | From this point on it makes no sense to traverse down further, as all
|
---|
| 210 | nodes below contain the same geometry, so no further refinement of visibility
|
---|
| 211 | can be archieved. All nodes below this point can merely used to define the
|
---|
| 212 | tighter bounds.
|
---|
| 213 | */
|
---|
| 214 | bool mIsMaxDepthForVirtualLeaf;
|
---|
[2897] | 215 |
|
---|
| 216 |
|
---|
[2746] | 217 | ////////////
|
---|
| 218 | //-- rendering related options
|
---|
| 219 |
|
---|
| 220 | // Indices to first and last triangle in the triangle array
|
---|
[2752] | 221 | // assumes the triangle are placed in continuous chunk of memory
|
---|
[2746] | 222 | // however this need not be a global array!
|
---|
| 223 |
|
---|
| 224 | /// the index of the first triangle
|
---|
| 225 | int mFirst;
|
---|
| 226 | /// the index of the last triangle
|
---|
| 227 | int mLast;
|
---|
| 228 |
|
---|
[3065] | 229 |
|
---|
| 230 | ////////////////
|
---|
| 231 | //-- rendering related options
|
---|
| 232 |
|
---|
| 233 | /// the bounding boxes of the test nodes are queries instead of the actual node
|
---|
[2746] | 234 | int mTestNodesIdx;
|
---|
[3065] | 235 | /// the number of these test nodes
|
---|
[2746] | 236 | int mNumTestNodes;
|
---|
[2792] | 237 | /// used for efficient element array rendering
|
---|
[2746] | 238 | int mIndicesPtr;
|
---|
| 239 | };
|
---|
| 240 |
|
---|
| 241 |
|
---|
[2800] | 242 |
|
---|
[2746] | 243 | /////////////////
|
---|
| 244 | //-- public inline functions
|
---|
| 245 |
|
---|
[2800] | 246 |
|
---|
[2746] | 247 | int BvhNode::GetLastVisitedFrame() const
|
---|
| 248 | {
|
---|
[2895] | 249 | return mVisibility[sCurrentState].mLastVisitedFrame;
|
---|
[2746] | 250 | }
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | void BvhNode::SetLastVisitedFrame(const int lastVisited)
|
---|
| 254 | {
|
---|
[2895] | 255 | mVisibility[sCurrentState].mLastVisitedFrame = lastVisited;
|
---|
[2746] | 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | bool BvhNode::IsVisible() const
|
---|
| 260 | {
|
---|
[2895] | 261 | return mVisibility[sCurrentState].mIsVisible;
|
---|
[2746] | 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
[2776] | 265 | void BvhNode::SetVisible(bool visible)
|
---|
[2746] | 266 | {
|
---|
[2895] | 267 | mVisibility[sCurrentState].mIsVisible = visible;
|
---|
[2746] | 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
[2773] | 271 | void BvhNode::IncTimesTestedInvisible()
|
---|
[2746] | 272 | {
|
---|
[2895] | 273 | ++ mVisibility[sCurrentState].mTimesInvisible;
|
---|
[2746] | 274 | }
|
---|
| 275 |
|
---|
| 276 |
|
---|
[2773] | 277 | int BvhNode::GetTimesTestedInvisible() const
|
---|
[2746] | 278 | {
|
---|
[2895] | 279 | return mVisibility[sCurrentState].mTimesInvisible;
|
---|
[2746] | 280 | }
|
---|
| 281 |
|
---|
| 282 |
|
---|
[2776] | 283 | void BvhNode::SetTimesTestedInvisible(int t)
|
---|
[2746] | 284 | {
|
---|
[2895] | 285 | mVisibility[sCurrentState].mTimesInvisible = t;
|
---|
[2746] | 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | bool BvhNode::IsViewFrustumCulled() const
|
---|
| 290 | {
|
---|
[2895] | 291 | return mVisibility[sCurrentState].mIsFrustumCulled;
|
---|
[2746] | 292 | }
|
---|
| 293 |
|
---|
| 294 |
|
---|
[2800] | 295 | void BvhNode::SetViewFrustumCulled(bool frustumCulled)
|
---|
[2746] | 296 | {
|
---|
[2895] | 297 | mVisibility[sCurrentState].mIsFrustumCulled = frustumCulled;
|
---|
[2746] | 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 | bool BvhNode::IsNew() const
|
---|
| 302 | {
|
---|
[2895] | 303 | return mVisibility[sCurrentState].mIsNew;
|
---|
[2746] | 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
[2800] | 307 | void BvhNode::SetIsNew(bool isNew)
|
---|
[2746] | 308 | {
|
---|
[2895] | 309 | mVisibility[sCurrentState].mIsNew = isNew;
|
---|
[2746] | 310 | }
|
---|
| 311 |
|
---|
| 312 |
|
---|
[2895] | 313 | void BvhNode::SetAssumedVisibleFrameId(int t)
|
---|
| 314 | {
|
---|
| 315 | mVisibility[sCurrentState].mAssumedVisibleFrameId = t;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | int BvhNode::GetAssumedVisibleFrameId() const
|
---|
| 320 | {
|
---|
| 321 | return mVisibility[sCurrentState].mAssumedVisibleFrameId;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
| 325 | int BvhNode::GetLastQueriedFrame() const
|
---|
| 326 | {
|
---|
| 327 | return mVisibility[sCurrentState].mLastQueriedFrame;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 | void BvhNode::SetLastQueriedFrame(int lastTested)
|
---|
| 332 | {
|
---|
| 333 | mVisibility[sCurrentState].mLastQueriedFrame = lastTested;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[2897] | 336 |
|
---|
[2746] | 337 | int BvhNode::GetLastRenderedFrame() const
|
---|
| 338 | {
|
---|
[2897] | 339 | return mLastRenderedFrame[sCurrentState];
|
---|
[2746] | 340 | }
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | void BvhNode::SetLastRenderedFrame(int lastRenderedFrame)
|
---|
| 344 | {
|
---|
[2897] | 345 | mLastRenderedFrame[sCurrentState] = lastRenderedFrame;
|
---|
[2746] | 346 | }
|
---|
| 347 |
|
---|
| 348 |
|
---|
| 349 | bool BvhNode::Empty() const
|
---|
| 350 | {
|
---|
[3053] | 351 | return (mFirst == -1);
|
---|
[2746] | 352 | }
|
---|
| 353 |
|
---|
| 354 |
|
---|
| 355 | int BvhNode::CountPrimitives() const
|
---|
| 356 | {
|
---|
| 357 | return mLast - mFirst + 1;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[3065] | 360 | int BvhNode::GetDepth() const
|
---|
| 361 | {
|
---|
| 362 | return mDepth;
|
---|
| 363 | }
|
---|
[2746] | 364 |
|
---|
| 365 |
|
---|
[3065] | 366 | BvhNode *BvhNode::GetParent()
|
---|
| 367 | {
|
---|
| 368 | return mParent;
|
---|
| 369 | }
|
---|
[2746] | 370 |
|
---|
| 371 |
|
---|
[3065] | 372 | int BvhNode::GetNumLeaves() const
|
---|
| 373 | {
|
---|
| 374 | return mNumLeaves;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 |
|
---|
| 378 | bool BvhNode::IsVirtualLeaf() const
|
---|
| 379 | {
|
---|
| 380 | return mIsVirtualLeaf;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 |
|
---|
| 384 | float BvhNode::GetDistance() const
|
---|
| 385 | {
|
---|
| 386 | return mDistance;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 |
|
---|
| 390 | const AxisAlignedBox3 &BvhNode::GetBox() const
|
---|
| 391 | {
|
---|
| 392 | return mBox;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 |
|
---|
| 396 | int BvhNode::GetId() const
|
---|
| 397 | {
|
---|
| 398 | return mId;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 |
|
---|
| 402 | void BvhNode::SetId(int id)
|
---|
| 403 | {
|
---|
| 404 | mId = id;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 |
|
---|
| 408 |
|
---|
[2746] | 409 | /** Internal node of the bv hierarchy.
|
---|
| 410 | */
|
---|
| 411 | class BvhInterior: public BvhNode
|
---|
| 412 | {
|
---|
[2755] | 413 | friend class Bvh;
|
---|
| 414 | friend class BvhLoader;
|
---|
[2746] | 415 |
|
---|
| 416 | public:
|
---|
[2755] | 417 |
|
---|
[2746] | 418 | BvhInterior(BvhNode *parent): mBack(NULL), mFront(NULL), BvhNode(parent)
|
---|
| 419 | {}
|
---|
[2761] | 420 | virtual bool IsLeaf() const { return false; }
|
---|
[2746] | 421 | /** Back child.
|
---|
| 422 | */
|
---|
[2761] | 423 | inline BvhNode *GetBack() { return mBack; }
|
---|
[2746] | 424 | /** Front child.
|
---|
| 425 | */
|
---|
[2761] | 426 | inline BvhNode *GetFront() { return mFront; }
|
---|
[2792] | 427 | /** Recursivly delete hierarchy.
|
---|
[2746] | 428 | */
|
---|
[2792] | 429 | virtual ~BvhInterior();
|
---|
[2746] | 430 |
|
---|
[3065] | 431 |
|
---|
[2746] | 432 | protected:
|
---|
| 433 |
|
---|
| 434 | BvhNode *mBack;
|
---|
| 435 | BvhNode *mFront;
|
---|
| 436 | };
|
---|
| 437 |
|
---|
| 438 |
|
---|
| 439 | class BvhLeaf: public BvhNode
|
---|
| 440 | {
|
---|
[2755] | 441 | friend class Bvh;
|
---|
| 442 | friend class BvhLoader;
|
---|
[2746] | 443 |
|
---|
| 444 | public:
|
---|
| 445 |
|
---|
[2752] | 446 | BvhLeaf(BvhNode *parent): BvhNode(parent)
|
---|
[2746] | 447 | {}
|
---|
| 448 |
|
---|
| 449 | ~BvhLeaf();
|
---|
| 450 |
|
---|
[2761] | 451 | virtual bool IsLeaf() const { return true; }
|
---|
[2746] | 452 | };
|
---|
| 453 |
|
---|
| 454 |
|
---|
[2755] | 455 | /** This class implements the compare operator for the priority queue.
|
---|
| 456 | a lower distance has a higher value in the queue
|
---|
| 457 | */
|
---|
[3065] | 458 | class GtDistance
|
---|
[2755] | 459 | {
|
---|
| 460 | public:
|
---|
| 461 | bool operator() (BvhNode *v1, BvhNode *v2) const
|
---|
| 462 | {
|
---|
[2947] | 463 | return (v1->GetDistance() > v2->GetDistance());
|
---|
[2755] | 464 | }
|
---|
| 465 | };
|
---|
| 466 |
|
---|
[3065] | 467 | typedef std::priority_queue<BvhNode *, std::vector<BvhNode *>, GtDistance> TraversalQueue;
|
---|
[2755] | 468 |
|
---|
[2767] | 469 |
|
---|
[2746] | 470 | /** Class representing a bounding volume hierarchy.
|
---|
| 471 | */
|
---|
| 472 | class Bvh
|
---|
| 473 | {
|
---|
[2755] | 474 | friend class BvhLoader;
|
---|
| 475 |
|
---|
[2746] | 476 | /** Bvh properties
|
---|
| 477 | */
|
---|
| 478 | struct BvhStats
|
---|
| 479 | {
|
---|
[3070] | 480 | BvhStats() { Reset(); }
|
---|
| 481 |
|
---|
| 482 | void Reset()
|
---|
| 483 | {
|
---|
| 484 | mInteriorSA = .0f;
|
---|
| 485 | mLeafSA = .0f;
|
---|
| 486 | mInteriorVol = .0f;
|
---|
| 487 | mLeafVol = .0f;
|
---|
| 488 | mTriangleRatio = .0f;
|
---|
| 489 | mGeometryRatio = .0f;
|
---|
| 490 |
|
---|
| 491 | mTriangles = 0;
|
---|
| 492 | mLeaves = 0;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | ///////////////////
|
---|
| 496 |
|
---|
[2746] | 497 | float mInteriorSA;
|
---|
| 498 | float mLeafSA;
|
---|
| 499 | float mInteriorVol;
|
---|
| 500 | float mLeafVol;
|
---|
[2760] | 501 |
|
---|
[2746] | 502 | float mTriangleRatio;
|
---|
| 503 | float mGeometryRatio;
|
---|
| 504 |
|
---|
[3070] | 505 | int mTriangles;
|
---|
| 506 | int mLeaves;
|
---|
[2746] | 507 | };
|
---|
| 508 |
|
---|
| 509 | public:
|
---|
[2763] | 510 |
|
---|
[2756] | 511 | /** Destructor.
|
---|
| 512 | */
|
---|
| 513 | ~Bvh();
|
---|
[2746] | 514 | /** Returns number of bvh nodes.
|
---|
| 515 | */
|
---|
[3065] | 516 | inline int GetNumNodes() const;
|
---|
[2746] | 517 | /** Returns number of bvh leaves.
|
---|
| 518 | */
|
---|
[3065] | 519 | inline int GetNumLeaves() const;
|
---|
[2800] | 520 | /** Returns number of 'virtual' nodes in the hierarchy, i.e.
|
---|
| 521 | the number of nodes actually used for traversal.
|
---|
| 522 | */
|
---|
[3065] | 523 | inline int GetNumVirtualNodes() const;
|
---|
[2800] | 524 | /** Returns number of bvh leaves.
|
---|
| 525 | */
|
---|
[3065] | 526 | inline int GetNumVirtualLeaves() const;
|
---|
[2746] | 527 | /** Returns root node of the bvh.
|
---|
| 528 | */
|
---|
[3065] | 529 | inline BvhNode *GetRoot();
|
---|
[3102] | 530 | /** Returns the static root node of the bvh.
|
---|
| 531 | */
|
---|
| 532 | inline BvhNode *GetStaticRoot();
|
---|
| 533 | /** Returns the dynamic root node of the bvh.
|
---|
| 534 | */
|
---|
[3070] | 535 | inline BvhNode *GetDynamicRoot();
|
---|
[3065] | 536 | /** Returns the bounding box of this bvh.
|
---|
| 537 | */
|
---|
| 538 | inline const AxisAlignedBox3 &GetBox() const;
|
---|
| 539 | /** Returns true if the current bvh node intersects the near plane.
|
---|
| 540 | */
|
---|
| 541 | bool IntersectsNearPlane(BvhNode *node) const;
|
---|
| 542 |
|
---|
| 543 |
|
---|
[2771] | 544 | ///////////////
|
---|
| 545 | //-- functions collecting nodes based on some criteria
|
---|
[2746] | 546 |
|
---|
[3065] | 547 | /** Collect all child nodes in a given depth from the specified node.
|
---|
[3064] | 548 | */
|
---|
[2755] | 549 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes, int depth);
|
---|
| 550 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes);
|
---|
[3064] | 551 | /** Collect the "physical" leaves of the hierarchy
|
---|
| 552 | */
|
---|
[2755] | 553 | void CollectLeaves(BvhNode *node, BvhLeafContainer &leaves);
|
---|
[3065] | 554 | /** Collect only the virtual leaves (can be anywhere in the hierarchy).
|
---|
[2761] | 555 | */
|
---|
| 556 | void CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves);
|
---|
[2755] | 557 |
|
---|
| 558 |
|
---|
[2746] | 559 | //////////////////////
|
---|
| 560 |
|
---|
| 561 | /** Returns geometry by reference (faster).
|
---|
| 562 | */
|
---|
[2951] | 563 | SceneEntity **GetGeometry(BvhNode *node, int &size) const;
|
---|
[2746] | 564 |
|
---|
| 565 |
|
---|
| 566 | /////////////
|
---|
| 567 | //-- Rendering related options
|
---|
| 568 |
|
---|
[2786] | 569 | /** Renders the bounds of this node
|
---|
| 570 | (the box of the node or the tigher bounds of some subnodes).
|
---|
[2746] | 571 | */
|
---|
[2786] | 572 | void RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 573 | /** Renders bounding boxes of the collection of nodes.
|
---|
| 574 | @returns #rendered boxes
|
---|
| 575 | */
|
---|
[2786] | 576 | int RenderBounds(const BvhNodeContainer &nodes, RenderState *state, bool useTightBounds);
|
---|
[3065] | 577 |
|
---|
[2746] | 578 |
|
---|
| 579 |
|
---|
| 580 | //////////////
|
---|
| 581 | //-- Traversal related options
|
---|
| 582 |
|
---|
| 583 | /** Pulls up visible classification.
|
---|
| 584 | */
|
---|
| 585 | void MakeParentsVisible(BvhNode *node);
|
---|
| 586 | /** Does the view frustum culling for this node with respect to the previous culls
|
---|
| 587 | @returns: 0 if completely outside, 1 if completely inside, -1 if intersecting (partly inside),
|
---|
| 588 | */
|
---|
| 589 | int IsWithinViewFrustum(BvhNode *node);
|
---|
[2771] | 590 | /** Sets frame dependent values
|
---|
[2746] | 591 | */
|
---|
[3074] | 592 | void InitFrame(Camera *camera, RenderState *state);
|
---|
[2954] | 593 | /** Stores the orthogonal distance from the viewpoint to a point on the node.
|
---|
| 594 | We choose the the nearest bounding box vertex .
|
---|
[2947] | 595 | Note that negative values can appear because culling is done only afterwards
|
---|
[2746] | 596 | */
|
---|
[2954] | 597 | void UpdateDistance(BvhNode *node) const;
|
---|
[2947] | 598 | /** Returns the maximum distance from the near plane to this node.
|
---|
[2764] | 599 | */
|
---|
[2947] | 600 | float CalcMaxDistance(BvhNode *node) const;
|
---|
[2746] | 601 | /** Pulls up the last visited classification in the bvh.
|
---|
| 602 | */
|
---|
[2755] | 603 | void PullUpLastVisited(BvhNode *node, int frameId) const;
|
---|
[2746] | 604 | /** Resets the node classifications in the tree.
|
---|
| 605 | */
|
---|
| 606 | void ResetNodeClassifications();
|
---|
| 607 | /** Count triangles the node contains.
|
---|
| 608 | */
|
---|
| 609 | int CountTriangles(BvhNode *node) const;
|
---|
[2756] | 610 | /** Returns area of the the node.
|
---|
[2746] | 611 | */
|
---|
[2756] | 612 | float GetArea(BvhNode *node) const;
|
---|
| 613 | /** Compute unique ids for the nodes.
|
---|
| 614 | */
|
---|
| 615 | void ComputeIds();
|
---|
[2761] | 616 | /** Assign virtual leaves based on specified number of triangles per leaf.
|
---|
[3064] | 617 | That means that we try to set the leaves at a point where we
|
---|
| 618 | have less than numTriangles triangles within this leaf and beyond.
|
---|
| 619 |
|
---|
| 620 | Virtual leaves are nodes that are determined to be the leaves
|
---|
| 621 | of the hierarchy during render traversal. These nodes are not necessarily
|
---|
| 622 | the same as the real leaves of the hierarchy and can be anywhere
|
---|
| 623 | in the hierarchy.
|
---|
| 624 | Please refer to the article for more info about virtual leaves
|
---|
[2761] | 625 | */
|
---|
| 626 | void SetVirtualLeaves(int numTriangles);
|
---|
[2800] | 627 |
|
---|
[2746] | 628 |
|
---|
[3064] | 629 | ////////////
|
---|
| 630 | //-- functions influencing the tightness of the bounds that are used for testing
|
---|
[2746] | 631 |
|
---|
| 632 |
|
---|
[2755] | 633 | /** Sets maximal depth for taking the bounding boxes to test the
|
---|
| 634 | visibility of a node.
|
---|
[3064] | 635 | Deeper level means that the bounds adapt more to the geometry but
|
---|
| 636 | also that more boxes are rendered
|
---|
[2755] | 637 | */
|
---|
| 638 | void SetMaxDepthForTestingChildren(int maxDepth);
|
---|
[3064] | 639 | /** The ratio of area between node and subnodes where
|
---|
| 640 | testing the subnodes as proxy is still considered feasable
|
---|
| 641 | */
|
---|
[2755] | 642 | void SetAreaRatioThresholdForTestingChildren(float ratio);
|
---|
[2746] | 643 |
|
---|
[2800] | 644 |
|
---|
| 645 | ////////////////////////////
|
---|
| 646 |
|
---|
[2755] | 647 | /** Returns stats.
|
---|
| 648 | */
|
---|
[2773] | 649 | const BvhStats &GetBvhStats() const { return mBvhStats; }
|
---|
[2790] | 650 | /** Render wireframe bvh for visualization purpose.
|
---|
| 651 | */
|
---|
[2825] | 652 | void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 653 |
|
---|
[2760] | 654 |
|
---|
[2746] | 655 | protected:
|
---|
| 656 |
|
---|
| 657 | ////////////////////
|
---|
| 658 |
|
---|
| 659 | /** protected constructor: do nothing.
|
---|
| 660 | */
|
---|
[2755] | 661 | Bvh();
|
---|
[2760] | 662 | /** Protected constructor taking scene geometry into account
|
---|
[3072] | 663 | Sets the static and dynamic objects for the hierarchy.
|
---|
[2760] | 664 | */
|
---|
[3072] | 665 | const Bvh(const SceneEntityContainer &staticEntities,
|
---|
| 666 | const SceneEntityContainer &dynamicEntities);
|
---|
[2773] | 667 | /** Called by the constructor. Initializes important members.
|
---|
| 668 | */
|
---|
| 669 | void Init();
|
---|
[2746] | 670 |
|
---|
| 671 | /////////////
|
---|
[3070] | 672 | /** Traverse hierarchy and compute stats.
|
---|
| 673 | */
|
---|
| 674 | void ComputeBvhStats(BvhNode *root, BvhStats &bvhStats);
|
---|
| 675 | /** Output the bvh statistics.
|
---|
| 676 | */
|
---|
| 677 | void PrintBvhStats(const BvhStats &bvhStats) const;
|
---|
[2746] | 678 |
|
---|
| 679 |
|
---|
| 680 | //////////
|
---|
| 681 | //-- Helper methods for bounding box rendering in immediate and vbo mode.
|
---|
| 682 |
|
---|
| 683 | void PrepareVertices();
|
---|
[3064] | 684 | /** Prepare nodes for vbo rendering.
|
---|
| 685 | */
|
---|
[2786] | 686 | int PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes);
|
---|
[3064] | 687 | /** Render the nodes from the vbo prepared previously.
|
---|
| 688 | */
|
---|
[2786] | 689 | void RenderBoundsWithDrawArrays(int numNodes, RenderState *state);
|
---|
[2746] | 690 |
|
---|
| 691 | /** Create the indices that each node needs to use vbo rendering.
|
---|
| 692 | */
|
---|
| 693 | void CreateIndices();
|
---|
| 694 | /** Create the list of nodes whose bounding boxes are tested instead of the
|
---|
| 695 | bounding box of the node itself.
|
---|
| 696 | */
|
---|
| 697 | bool CreateNodeRenderList(BvhNode *node);
|
---|
| 698 | /** Recursivly updates indices so we can
|
---|
| 699 | render also interior nodes without traversing hierarchy
|
---|
| 700 | */
|
---|
| 701 | void UpdateInteriors(BvhNode *node);
|
---|
[2755] | 702 | /** Recomputes the boundaries of the nodes.
|
---|
| 703 | This function is always called if some boundary options are changed.
|
---|
[2746] | 704 | */
|
---|
| 705 | void RecomputeBounds();
|
---|
[2760] | 706 | /** Does some postprocessing on the nodes.
|
---|
[2746] | 707 | */
|
---|
[2761] | 708 | void PostProcess();
|
---|
[2760] | 709 | /** Helper method that updates the number of leaves in the subtree under
|
---|
| 710 | this node.
|
---|
| 711 | */
|
---|
| 712 | void UpdateNumLeaves(BvhNode *node) const;
|
---|
[2762] | 713 | /** Frustum tests the ith plane.
|
---|
| 714 | */
|
---|
| 715 | inline bool TestPlane(BvhNode *node, int i, bool &bIntersect);
|
---|
[2786] | 716 | /** Renders a bounding box in immediate mode.
|
---|
| 717 | */
|
---|
[2763] | 718 | void RenderBoundingBoxImmediate(const AxisAlignedBox3 &box);
|
---|
[2762] | 719 |
|
---|
[2764] | 720 |
|
---|
[3053] | 721 | /////////////////////////////
|
---|
[3065] | 722 | //-- functions used to construct the dynamic part of the hierarchy
|
---|
| 723 |
|
---|
[3053] | 724 | int SortTriangles(BvhLeaf *leaf,
|
---|
| 725 | int axis,
|
---|
[3070] | 726 | float position);
|
---|
[3053] | 727 |
|
---|
| 728 | int SortTrianglesSpatialMedian(BvhLeaf *leaf,
|
---|
[3070] | 729 | int axis);
|
---|
[3053] | 730 |
|
---|
| 731 | BvhNode *SubdivideLeaf(BvhLeaf *leaf,
|
---|
[3070] | 732 | int parentAxis);
|
---|
[3053] | 733 | /** Recompute the dynamic branch of the hierarchy.
|
---|
| 734 | */
|
---|
[3070] | 735 | void CreateDynamicBranch();
|
---|
[3053] | 736 |
|
---|
[3070] | 737 | void UpdateDynamicBranch(BvhNode *node);
|
---|
| 738 |
|
---|
[3053] | 739 | inline bool TerminationCriteriaMet(BvhLeaf *leaf) const;
|
---|
| 740 |
|
---|
[3072] | 741 | void ComputeMaxDepthForVirtualLeaves();
|
---|
| 742 |
|
---|
| 743 | void CreateRoot();
|
---|
| 744 |
|
---|
[3074] | 745 | void UpdateDynamicBounds(RenderState *state);
|
---|
[3072] | 746 |
|
---|
[3074] | 747 |
|
---|
[2746] | 748 | ////////////////////////
|
---|
[2894] | 749 |
|
---|
| 750 | /// the bounding box of the bvh
|
---|
| 751 | AxisAlignedBox3 mBox;
|
---|
[2746] | 752 | /// the root of the hierarchy
|
---|
[3072] | 753 | BvhInterior *mRoot;
|
---|
[3064] | 754 | /// the root of static part of the the hierarchy
|
---|
[3072] | 755 | BvhNode *mStaticRoot;
|
---|
[3064] | 756 | /// the root of dynamic part of the the hierarchy
|
---|
| 757 | BvhNode *mDynamicRoot;
|
---|
[2746] | 758 | /// pointers to the geometry associated with this node
|
---|
[2755] | 759 | SceneEntity **mGeometry;
|
---|
[2752] | 760 | /// #of entities
|
---|
[2746] | 761 | size_t mGeometrySize;
|
---|
[3072] | 762 | /// #of static entities
|
---|
| 763 | size_t mStaticGeometrySize;
|
---|
| 764 | /// #of dynamic entities
|
---|
| 765 | size_t mDynamicGeometrySize;
|
---|
[2746] | 766 |
|
---|
| 767 |
|
---|
| 768 | ////////////////
|
---|
[2894] | 769 | //-- tigher bounds termination criteria
|
---|
[2746] | 770 |
|
---|
| 771 | /** maximal depth from which children are fetched for
|
---|
| 772 | testing instead of the current node
|
---|
| 773 | */
|
---|
| 774 | int mMaxDepthForTestingChildren;
|
---|
[2894] | 775 | /// threshold for computing tighter bounds
|
---|
[2746] | 776 | float mAreaRatioThreshold;
|
---|
[2894] | 777 |
|
---|
| 778 |
|
---|
| 779 | ////////////////
|
---|
| 780 | //-- statistics
|
---|
[2746] | 781 |
|
---|
| 782 | BvhStats mBvhStats;
|
---|
[2894] | 783 | /// the overall number of nodes
|
---|
| 784 | int mNumNodes;
|
---|
| 785 | /// the number of "virtual" (=actually used) nodes
|
---|
| 786 | int mNumVirtualNodes;
|
---|
[2746] | 787 |
|
---|
[2894] | 788 |
|
---|
| 789 | ////////////
|
---|
| 790 | //-- rendering stuff
|
---|
| 791 |
|
---|
| 792 | /// these proxy nodes are tested instead of the current node
|
---|
[2755] | 793 | BvhNodeContainer mTestNodes;
|
---|
[2894] | 794 | /// the indices used for vbo index buffering
|
---|
[2746] | 795 | unsigned int *mTestIndices;
|
---|
| 796 | /// a pointer to the end of the indices array
|
---|
| 797 | int mCurrentIndicesPtr;
|
---|
[2894] | 798 | /// the vbo id
|
---|
[2773] | 799 | unsigned int mVboId;
|
---|
[2894] | 800 | /// a vertex array used if working with indexed arrays (without vbo)
|
---|
| 801 | Vector3 *mVertices;
|
---|
| 802 | /// indices used for draw array rendering
|
---|
| 803 | unsigned int *mIndices;
|
---|
[3053] | 804 |
|
---|
| 805 |
|
---|
| 806 | ///////////
|
---|
| 807 | //-- termination criteria for dynamic branch
|
---|
| 808 |
|
---|
| 809 | int mMaxDepthForDynamicBranch;
|
---|
| 810 |
|
---|
[3072] | 811 | //SceneEntityContainer mDynamicEntities;
|
---|
[2746] | 812 | };
|
---|
| 813 |
|
---|
[3065] | 814 |
|
---|
| 815 |
|
---|
| 816 | /////////////////
|
---|
| 817 | //-- public inline functions
|
---|
| 818 |
|
---|
| 819 |
|
---|
| 820 | int Bvh::GetNumNodes() const
|
---|
| 821 | {
|
---|
| 822 | return mNumNodes;
|
---|
[2751] | 823 | }
|
---|
| 824 |
|
---|
[3065] | 825 |
|
---|
| 826 | int Bvh::GetNumLeaves() const
|
---|
| 827 | {
|
---|
| 828 | return mNumNodes / 2 + 1;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 |
|
---|
| 832 | int Bvh::GetNumVirtualNodes() const
|
---|
| 833 | {
|
---|
| 834 | return mNumVirtualNodes;
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 |
|
---|
| 838 | int Bvh::GetNumVirtualLeaves() const
|
---|
| 839 | {
|
---|
| 840 | return mNumVirtualNodes / 2 + 1;
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 |
|
---|
| 844 | BvhNode *Bvh::GetRoot()
|
---|
| 845 | {
|
---|
| 846 | return mRoot;
|
---|
| 847 | }
|
---|
| 848 |
|
---|
| 849 |
|
---|
[3070] | 850 | BvhNode *Bvh::GetDynamicRoot()
|
---|
| 851 | {
|
---|
| 852 | return mDynamicRoot;
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 |
|
---|
[3102] | 856 | BvhNode *Bvh::GetStaticRoot()
|
---|
| 857 | {
|
---|
| 858 | return mStaticRoot;
|
---|
| 859 | }
|
---|
| 860 |
|
---|
| 861 |
|
---|
[3065] | 862 | const AxisAlignedBox3 &Bvh::GetBox() const
|
---|
| 863 | {
|
---|
| 864 | return mBox;
|
---|
| 865 | }
|
---|
| 866 |
|
---|
| 867 |
|
---|
| 868 | }
|
---|
| 869 |
|
---|
[2746] | 870 | #endif // __BVH_H |
---|