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