[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;
|
---|
[3259] | 361 | /** Counts the number of bvh nodes under this node
|
---|
| 362 | */
|
---|
| 363 | int CountNumNodes(BvhNode *node) const;
|
---|
[2746] | 364 | /** Returns number of bvh leaves.
|
---|
| 365 | */
|
---|
[3065] | 366 | inline int GetNumLeaves() const;
|
---|
[2800] | 367 | /** Returns number of 'virtual' nodes in the hierarchy, i.e.
|
---|
| 368 | the number of nodes actually used for traversal.
|
---|
| 369 | */
|
---|
[3065] | 370 | inline int GetNumVirtualNodes() const;
|
---|
[2800] | 371 | /** Returns number of bvh leaves.
|
---|
| 372 | */
|
---|
[3065] | 373 | inline int GetNumVirtualLeaves() const;
|
---|
[2746] | 374 | /** Returns root node of the bvh.
|
---|
| 375 | */
|
---|
[3065] | 376 | inline BvhNode *GetRoot();
|
---|
[3102] | 377 | /** Returns the static root node of the bvh.
|
---|
| 378 | */
|
---|
| 379 | inline BvhNode *GetStaticRoot();
|
---|
| 380 | /** Returns the dynamic root node of the bvh.
|
---|
| 381 | */
|
---|
[3070] | 382 | inline BvhNode *GetDynamicRoot();
|
---|
[3065] | 383 | /** Returns the bounding box of this bvh.
|
---|
| 384 | */
|
---|
| 385 | inline const AxisAlignedBox3 &GetBox() const;
|
---|
| 386 | /** Returns true if the current bvh node intersects the near plane.
|
---|
| 387 | */
|
---|
| 388 | bool IntersectsNearPlane(BvhNode *node) const;
|
---|
| 389 |
|
---|
| 390 |
|
---|
[2771] | 391 | ///////////////
|
---|
| 392 | //-- functions collecting nodes based on some criteria
|
---|
[2746] | 393 |
|
---|
[3065] | 394 | /** Collect all child nodes in a given depth from the specified node.
|
---|
[3064] | 395 | */
|
---|
[2755] | 396 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes, int depth);
|
---|
[3259] | 397 | /** Collect all child nodes.
|
---|
| 398 | */
|
---|
[2755] | 399 | void CollectNodes(BvhNode *node, BvhNodeContainer &nodes);
|
---|
[3064] | 400 | /** Collect the "physical" leaves of the hierarchy
|
---|
| 401 | */
|
---|
[2755] | 402 | void CollectLeaves(BvhNode *node, BvhLeafContainer &leaves);
|
---|
[3065] | 403 | /** Collect only the virtual leaves (can be anywhere in the hierarchy).
|
---|
[2761] | 404 | */
|
---|
| 405 | void CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves);
|
---|
[2755] | 406 |
|
---|
| 407 |
|
---|
[2746] | 408 | //////////////////////
|
---|
| 409 |
|
---|
| 410 | /** Returns geometry by reference (faster).
|
---|
| 411 | */
|
---|
[2951] | 412 | SceneEntity **GetGeometry(BvhNode *node, int &size) const;
|
---|
[2746] | 413 |
|
---|
| 414 |
|
---|
| 415 | /////////////
|
---|
| 416 | //-- Rendering related options
|
---|
| 417 |
|
---|
[2786] | 418 | /** Renders the bounds of this node
|
---|
| 419 | (the box of the node or the tigher bounds of some subnodes).
|
---|
[2746] | 420 | */
|
---|
[2786] | 421 | void RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 422 | /** Renders bounding boxes of the collection of nodes.
|
---|
| 423 | @returns #rendered boxes
|
---|
| 424 | */
|
---|
[2786] | 425 | int RenderBounds(const BvhNodeContainer &nodes, RenderState *state, bool useTightBounds);
|
---|
[3065] | 426 |
|
---|
[2746] | 427 |
|
---|
| 428 |
|
---|
| 429 | //////////////
|
---|
| 430 | //-- Traversal related options
|
---|
| 431 |
|
---|
| 432 | /** Pulls up visible classification.
|
---|
| 433 | */
|
---|
| 434 | void MakeParentsVisible(BvhNode *node);
|
---|
| 435 | /** Does the view frustum culling for this node with respect to the previous culls
|
---|
| 436 | @returns: 0 if completely outside, 1 if completely inside, -1 if intersecting (partly inside),
|
---|
| 437 | */
|
---|
| 438 | int IsWithinViewFrustum(BvhNode *node);
|
---|
[2771] | 439 | /** Sets frame dependent values
|
---|
[2746] | 440 | */
|
---|
[3074] | 441 | void InitFrame(Camera *camera, RenderState *state);
|
---|
[2954] | 442 | /** Stores the orthogonal distance from the viewpoint to a point on the node.
|
---|
| 443 | We choose the the nearest bounding box vertex .
|
---|
[2947] | 444 | Note that negative values can appear because culling is done only afterwards
|
---|
[2746] | 445 | */
|
---|
[2954] | 446 | void UpdateDistance(BvhNode *node) const;
|
---|
[2947] | 447 | /** Returns the maximum distance from the near plane to this node.
|
---|
[2764] | 448 | */
|
---|
[2947] | 449 | float CalcMaxDistance(BvhNode *node) const;
|
---|
[2746] | 450 | /** Pulls up the last visited classification in the bvh.
|
---|
| 451 | */
|
---|
[2755] | 452 | void PullUpLastVisited(BvhNode *node, int frameId) const;
|
---|
[2746] | 453 | /** Resets the node classifications in the tree.
|
---|
| 454 | */
|
---|
| 455 | void ResetNodeClassifications();
|
---|
[3259] | 456 | /** Counts the number of triangles contained in this node.
|
---|
[2746] | 457 | */
|
---|
| 458 | int CountTriangles(BvhNode *node) const;
|
---|
[2756] | 459 | /** Returns area of the the node.
|
---|
[2746] | 460 | */
|
---|
[2756] | 461 | float GetArea(BvhNode *node) const;
|
---|
| 462 | /** Compute unique ids for the nodes.
|
---|
| 463 | */
|
---|
| 464 | void ComputeIds();
|
---|
[2761] | 465 | /** Assign virtual leaves based on specified number of triangles per leaf.
|
---|
[3064] | 466 | That means that we try to set the leaves at a point where we
|
---|
| 467 | have less than numTriangles triangles within this leaf and beyond.
|
---|
| 468 |
|
---|
| 469 | Virtual leaves are nodes that are determined to be the leaves
|
---|
| 470 | of the hierarchy during render traversal. These nodes are not necessarily
|
---|
| 471 | the same as the real leaves of the hierarchy and can be anywhere
|
---|
| 472 | in the hierarchy.
|
---|
| 473 | Please refer to the article for more info about virtual leaves
|
---|
[2761] | 474 | */
|
---|
| 475 | void SetVirtualLeaves(int numTriangles);
|
---|
[2800] | 476 |
|
---|
[2746] | 477 |
|
---|
[3064] | 478 | ////////////
|
---|
[3262] | 479 | //-- these functions determine the 'tightness' of the bounds that are used for querying
|
---|
[2746] | 480 |
|
---|
| 481 |
|
---|
[2755] | 482 | /** Sets maximal depth for taking the bounding boxes to test the
|
---|
| 483 | visibility of a node.
|
---|
[3064] | 484 | Deeper level means that the bounds adapt more to the geometry but
|
---|
| 485 | also that more boxes are rendered
|
---|
[2755] | 486 | */
|
---|
| 487 | void SetMaxDepthForTestingChildren(int maxDepth);
|
---|
[3064] | 488 | /** The ratio of area between node and subnodes where
|
---|
| 489 | testing the subnodes as proxy is still considered feasable
|
---|
| 490 | */
|
---|
[2755] | 491 | void SetAreaRatioThresholdForTestingChildren(float ratio);
|
---|
[2746] | 492 |
|
---|
[2800] | 493 |
|
---|
| 494 | ////////////////////////////
|
---|
| 495 |
|
---|
[3259] | 496 | /** Returns statistics.
|
---|
[2755] | 497 | */
|
---|
[2773] | 498 | const BvhStats &GetBvhStats() const { return mBvhStats; }
|
---|
[2790] | 499 | /** Render wireframe bvh for visualization purpose.
|
---|
| 500 | */
|
---|
[2825] | 501 | void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds);
|
---|
[2746] | 502 |
|
---|
[2760] | 503 |
|
---|
[2746] | 504 | protected:
|
---|
| 505 |
|
---|
| 506 | ////////////////////
|
---|
| 507 |
|
---|
| 508 | /** protected constructor: do nothing.
|
---|
| 509 | */
|
---|
[2755] | 510 | Bvh();
|
---|
[2760] | 511 | /** Protected constructor taking scene geometry into account
|
---|
[3072] | 512 | Sets the static and dynamic objects for the hierarchy.
|
---|
[2760] | 513 | */
|
---|
[3262] | 514 | Bvh(const SceneEntityContainer &staticEntities,
|
---|
| 515 | const SceneEntityContainer &dynamicEntities);
|
---|
[3123] | 516 | /** Protected constructor taking scene geometry into account
|
---|
| 517 | Sets the static and dynamic objects for the hierarchy.
|
---|
| 518 | */
|
---|
[3262] | 519 | Bvh(const SceneEntityContainer &staticEntities,
|
---|
| 520 | const SceneEntityContainer &dynamicEntities,
|
---|
| 521 | int maxDepthForTestingChildren);
|
---|
[2773] | 522 | /** Called by the constructor. Initializes important members.
|
---|
| 523 | */
|
---|
| 524 | void Init();
|
---|
[2746] | 525 |
|
---|
| 526 | /////////////
|
---|
[3070] | 527 | /** Traverse hierarchy and compute stats.
|
---|
| 528 | */
|
---|
| 529 | void ComputeBvhStats(BvhNode *root, BvhStats &bvhStats);
|
---|
| 530 | /** Output the bvh statistics.
|
---|
| 531 | */
|
---|
[3266] | 532 | void PrintBvhStats(const BvhStats &bvhStats, BvhNode *root) const;
|
---|
[2746] | 533 |
|
---|
| 534 |
|
---|
| 535 | //////////
|
---|
| 536 | //-- Helper methods for bounding box rendering in immediate and vbo mode.
|
---|
| 537 |
|
---|
| 538 | void PrepareVertices();
|
---|
[3064] | 539 | /** Prepare nodes for vbo rendering.
|
---|
| 540 | */
|
---|
[2786] | 541 | int PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes);
|
---|
[3064] | 542 | /** Render the nodes from the vbo prepared previously.
|
---|
| 543 | */
|
---|
[2786] | 544 | void RenderBoundsWithDrawArrays(int numNodes, RenderState *state);
|
---|
[2746] | 545 |
|
---|
| 546 | /** Create the indices that each node needs to use vbo rendering.
|
---|
| 547 | */
|
---|
| 548 | void CreateIndices();
|
---|
| 549 | /** Create the list of nodes whose bounding boxes are tested instead of the
|
---|
| 550 | bounding box of the node itself.
|
---|
| 551 | */
|
---|
| 552 | bool CreateNodeRenderList(BvhNode *node);
|
---|
| 553 | /** Recursivly updates indices so we can
|
---|
| 554 | render also interior nodes without traversing hierarchy
|
---|
| 555 | */
|
---|
| 556 | void UpdateInteriors(BvhNode *node);
|
---|
[2755] | 557 | /** Recomputes the boundaries of the nodes.
|
---|
| 558 | This function is always called if some boundary options are changed.
|
---|
[2746] | 559 | */
|
---|
| 560 | void RecomputeBounds();
|
---|
[2760] | 561 | /** Does some postprocessing on the nodes.
|
---|
[2746] | 562 | */
|
---|
[2761] | 563 | void PostProcess();
|
---|
[2760] | 564 | /** Helper method that updates the number of leaves in the subtree under
|
---|
| 565 | this node.
|
---|
| 566 | */
|
---|
| 567 | void UpdateNumLeaves(BvhNode *node) const;
|
---|
[2762] | 568 | /** Frustum tests the ith plane.
|
---|
| 569 | */
|
---|
| 570 | inline bool TestPlane(BvhNode *node, int i, bool &bIntersect);
|
---|
[2786] | 571 | /** Renders a bounding box in immediate mode.
|
---|
| 572 | */
|
---|
[2763] | 573 | void RenderBoundingBoxImmediate(const AxisAlignedBox3 &box);
|
---|
[2762] | 574 |
|
---|
[2764] | 575 |
|
---|
[3053] | 576 | /////////////////////////////
|
---|
[3065] | 577 | //-- functions used to construct the dynamic part of the hierarchy
|
---|
| 578 |
|
---|
[3053] | 579 | int SortTriangles(BvhLeaf *leaf,
|
---|
| 580 | int axis,
|
---|
[3070] | 581 | float position);
|
---|
[3053] | 582 |
|
---|
| 583 | int SortTrianglesSpatialMedian(BvhLeaf *leaf,
|
---|
[3070] | 584 | int axis);
|
---|
[3053] | 585 |
|
---|
| 586 | BvhNode *SubdivideLeaf(BvhLeaf *leaf,
|
---|
[3070] | 587 | int parentAxis);
|
---|
[3053] | 588 | /** Recompute the dynamic branch of the hierarchy.
|
---|
| 589 | */
|
---|
[3070] | 590 | void CreateDynamicBranch();
|
---|
[3053] | 591 |
|
---|
[3267] | 592 | void UpdateBoundingBoxes(BvhNode *node);
|
---|
[3070] | 593 |
|
---|
[3053] | 594 | inline bool TerminationCriteriaMet(BvhLeaf *leaf) const;
|
---|
| 595 |
|
---|
[3072] | 596 | void ComputeMaxDepthForVirtualLeaves();
|
---|
| 597 |
|
---|
| 598 | void CreateRoot();
|
---|
| 599 |
|
---|
[3074] | 600 | void UpdateDynamicBounds(RenderState *state);
|
---|
[3072] | 601 |
|
---|
[3074] | 602 |
|
---|
[2746] | 603 | ////////////////////////
|
---|
[2894] | 604 |
|
---|
| 605 | /// the bounding box of the bvh
|
---|
| 606 | AxisAlignedBox3 mBox;
|
---|
[2746] | 607 | /// the root of the hierarchy
|
---|
[3072] | 608 | BvhInterior *mRoot;
|
---|
[3064] | 609 | /// the root of static part of the the hierarchy
|
---|
[3072] | 610 | BvhNode *mStaticRoot;
|
---|
[3064] | 611 | /// the root of dynamic part of the the hierarchy
|
---|
| 612 | BvhNode *mDynamicRoot;
|
---|
[2746] | 613 | /// pointers to the geometry associated with this node
|
---|
[2755] | 614 | SceneEntity **mGeometry;
|
---|
[2752] | 615 | /// #of entities
|
---|
[2746] | 616 | size_t mGeometrySize;
|
---|
[3072] | 617 | /// #of static entities
|
---|
| 618 | size_t mStaticGeometrySize;
|
---|
| 619 | /// #of dynamic entities
|
---|
| 620 | size_t mDynamicGeometrySize;
|
---|
[2746] | 621 |
|
---|
| 622 |
|
---|
| 623 | ////////////////
|
---|
[2894] | 624 | //-- tigher bounds termination criteria
|
---|
[2746] | 625 |
|
---|
| 626 | /** maximal depth from which children are fetched for
|
---|
| 627 | testing instead of the current node
|
---|
| 628 | */
|
---|
| 629 | int mMaxDepthForTestingChildren;
|
---|
[2894] | 630 | /// threshold for computing tighter bounds
|
---|
[2746] | 631 | float mAreaRatioThreshold;
|
---|
[2894] | 632 |
|
---|
| 633 |
|
---|
| 634 | ////////////////
|
---|
| 635 | //-- statistics
|
---|
[2746] | 636 |
|
---|
| 637 | BvhStats mBvhStats;
|
---|
[2894] | 638 | /// the overall number of nodes
|
---|
| 639 | int mNumNodes;
|
---|
| 640 | /// the number of "virtual" (=actually used) nodes
|
---|
| 641 | int mNumVirtualNodes;
|
---|
[2746] | 642 |
|
---|
[2894] | 643 |
|
---|
| 644 | ////////////
|
---|
| 645 | //-- rendering stuff
|
---|
| 646 |
|
---|
| 647 | /// these proxy nodes are tested instead of the current node
|
---|
[2755] | 648 | BvhNodeContainer mTestNodes;
|
---|
[2894] | 649 | /// the indices used for vbo index buffering
|
---|
[2746] | 650 | unsigned int *mTestIndices;
|
---|
| 651 | /// a pointer to the end of the indices array
|
---|
| 652 | int mCurrentIndicesPtr;
|
---|
[2894] | 653 | /// the vbo id
|
---|
[2773] | 654 | unsigned int mVboId;
|
---|
[2894] | 655 | /// a vertex array used if working with indexed arrays (without vbo)
|
---|
| 656 | Vector3 *mVertices;
|
---|
| 657 | /// indices used for draw array rendering
|
---|
| 658 | unsigned int *mIndices;
|
---|
[3053] | 659 |
|
---|
| 660 |
|
---|
| 661 | ///////////
|
---|
| 662 | //-- termination criteria for dynamic branch
|
---|
| 663 |
|
---|
| 664 | int mMaxDepthForDynamicBranch;
|
---|
| 665 |
|
---|
[3072] | 666 | //SceneEntityContainer mDynamicEntities;
|
---|
[2746] | 667 | };
|
---|
| 668 |
|
---|
[3065] | 669 |
|
---|
| 670 |
|
---|
| 671 | /////////////////
|
---|
| 672 | //-- public inline functions
|
---|
| 673 |
|
---|
| 674 |
|
---|
[3262] | 675 | inline int BvhNode::GetLastVisitedFrame() const
|
---|
| 676 | {
|
---|
| 677 | return mVisibility[sCurrentState].mLastVisitedFrame;
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 |
|
---|
| 681 | inline void BvhNode::SetLastVisitedFrame(const int lastVisited)
|
---|
| 682 | {
|
---|
| 683 | mVisibility[sCurrentState].mLastVisitedFrame = lastVisited;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 |
|
---|
| 687 | inline bool BvhNode::IsVisible() const
|
---|
| 688 | {
|
---|
| 689 | return mVisibility[sCurrentState].mIsVisible;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 |
|
---|
| 693 | inline void BvhNode::SetVisible(bool visible)
|
---|
| 694 | {
|
---|
| 695 | mVisibility[sCurrentState].mIsVisible = visible;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 |
|
---|
| 699 | inline void BvhNode::IncTimesTestedInvisible()
|
---|
| 700 | {
|
---|
| 701 | ++ mVisibility[sCurrentState].mTimesInvisible;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 |
|
---|
| 705 | inline int BvhNode::GetTimesTestedInvisible() const
|
---|
| 706 | {
|
---|
| 707 | return mVisibility[sCurrentState].mTimesInvisible;
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 |
|
---|
| 711 | inline void BvhNode::SetTimesTestedInvisible(int t)
|
---|
| 712 | {
|
---|
| 713 | mVisibility[sCurrentState].mTimesInvisible = t;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 |
|
---|
| 717 | inline bool BvhNode::IsViewFrustumCulled() const
|
---|
| 718 | {
|
---|
| 719 | return mVisibility[sCurrentState].mIsFrustumCulled;
|
---|
| 720 | }
|
---|
| 721 |
|
---|
| 722 |
|
---|
| 723 | inline void BvhNode::SetViewFrustumCulled(bool frustumCulled)
|
---|
| 724 | {
|
---|
| 725 | mVisibility[sCurrentState].mIsFrustumCulled = frustumCulled;
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 |
|
---|
| 729 | inline bool BvhNode::IsNew() const
|
---|
| 730 | {
|
---|
| 731 | return mVisibility[sCurrentState].mIsNew;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 |
|
---|
| 735 | inline void BvhNode::SetIsNew(bool isNew)
|
---|
| 736 | {
|
---|
| 737 | mVisibility[sCurrentState].mIsNew = isNew;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 |
|
---|
| 741 | inline void BvhNode::SetAssumedVisibleFrameId(int t)
|
---|
| 742 | {
|
---|
| 743 | mVisibility[sCurrentState].mAssumedVisibleFrameId = t;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 |
|
---|
| 747 | inline int BvhNode::GetAssumedVisibleFrameId() const
|
---|
| 748 | {
|
---|
| 749 | return mVisibility[sCurrentState].mAssumedVisibleFrameId;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 |
|
---|
| 753 | inline int BvhNode::GetLastQueriedFrame() const
|
---|
[3065] | 754 | {
|
---|
[3262] | 755 | return mVisibility[sCurrentState].mLastQueriedFrame;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 |
|
---|
| 759 | inline void BvhNode::SetLastQueriedFrame(int lastTested)
|
---|
| 760 | {
|
---|
| 761 | mVisibility[sCurrentState].mLastQueriedFrame = lastTested;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 |
|
---|
| 765 | inline int BvhNode::GetLastRenderedFrame() const
|
---|
| 766 | {
|
---|
| 767 | return mLastRenderedFrame[sCurrentState];
|
---|
| 768 | }
|
---|
| 769 |
|
---|
| 770 |
|
---|
| 771 | inline void BvhNode::SetLastRenderedFrame(int lastRenderedFrame)
|
---|
| 772 | {
|
---|
| 773 | mLastRenderedFrame[sCurrentState] = lastRenderedFrame;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 |
|
---|
| 777 | inline bool BvhNode::Empty() const
|
---|
| 778 | {
|
---|
| 779 | return (mFirst == -1);
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 |
|
---|
| 783 | inline int BvhNode::CountPrimitives() const
|
---|
| 784 | {
|
---|
| 785 | return mLast - mFirst + 1;
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | inline int BvhNode::GetDepth() const
|
---|
| 789 | {
|
---|
| 790 | return mDepth;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 |
|
---|
| 794 | inline BvhNode *BvhNode::GetParent()
|
---|
| 795 | {
|
---|
| 796 | return mParent;
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 |
|
---|
| 800 | inline int BvhNode::GetNumLeaves() const
|
---|
| 801 | {
|
---|
| 802 | return mNumLeaves;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 |
|
---|
| 806 | inline bool BvhNode::IsVirtualLeaf() const
|
---|
| 807 | {
|
---|
| 808 | return mIsVirtualLeaf;
|
---|
| 809 | }
|
---|
| 810 |
|
---|
| 811 |
|
---|
| 812 | inline float BvhNode::GetDistance() const
|
---|
| 813 | {
|
---|
| 814 | return mDistance;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 |
|
---|
| 818 | inline const AxisAlignedBox3 &BvhNode::GetBox() const
|
---|
| 819 | {
|
---|
| 820 | return mBox;
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 |
|
---|
| 824 | inline int BvhNode::GetId() const
|
---|
| 825 | {
|
---|
| 826 | return mId;
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 |
|
---|
| 830 | inline void BvhNode::SetId(int id)
|
---|
| 831 | {
|
---|
| 832 | mId = id;
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 |
|
---|
| 836 | inline void BvhNode::SetCurrentState(int _state)
|
---|
| 837 | {
|
---|
| 838 | sCurrentState = _state;
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 |
|
---|
| 842 | inline BvhNode *BvhInterior::GetBack()
|
---|
| 843 | {
|
---|
| 844 | return mBack;
|
---|
| 845 | }
|
---|
| 846 |
|
---|
| 847 |
|
---|
| 848 | inline BvhNode *BvhInterior::GetFront()
|
---|
| 849 | {
|
---|
| 850 | return mFront;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 |
|
---|
| 854 | inline int Bvh::GetNumNodes() const
|
---|
| 855 | {
|
---|
[3065] | 856 | return mNumNodes;
|
---|
[2751] | 857 | }
|
---|
| 858 |
|
---|
[3065] | 859 |
|
---|
[3262] | 860 | inline int Bvh::GetNumLeaves() const
|
---|
[3065] | 861 | {
|
---|
| 862 | return mNumNodes / 2 + 1;
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 |
|
---|
[3262] | 866 | inline int Bvh::GetNumVirtualNodes() const
|
---|
[3065] | 867 | {
|
---|
| 868 | return mNumVirtualNodes;
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 |
|
---|
[3262] | 872 | inline int Bvh::GetNumVirtualLeaves() const
|
---|
[3065] | 873 | {
|
---|
| 874 | return mNumVirtualNodes / 2 + 1;
|
---|
| 875 | }
|
---|
| 876 |
|
---|
| 877 |
|
---|
[3262] | 878 | inline BvhNode *Bvh::GetRoot()
|
---|
[3065] | 879 | {
|
---|
| 880 | return mRoot;
|
---|
| 881 | }
|
---|
| 882 |
|
---|
| 883 |
|
---|
[3262] | 884 | inline BvhNode *Bvh::GetDynamicRoot()
|
---|
[3070] | 885 | {
|
---|
| 886 | return mDynamicRoot;
|
---|
| 887 | }
|
---|
| 888 |
|
---|
| 889 |
|
---|
[3262] | 890 | inline BvhNode *Bvh::GetStaticRoot()
|
---|
[3102] | 891 | {
|
---|
| 892 | return mStaticRoot;
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 |
|
---|
[3262] | 896 | inline const AxisAlignedBox3 &Bvh::GetBox() const
|
---|
[3065] | 897 | {
|
---|
| 898 | return mBox;
|
---|
| 899 | }
|
---|
| 900 |
|
---|
| 901 |
|
---|
| 902 | }
|
---|
| 903 |
|
---|
[2746] | 904 | #endif // __BVH_H |
---|