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