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