[2755] | 1 | #include "Bvh.h"
|
---|
| 2 | #include "Camera.h"
|
---|
| 3 | #include "Plane3.h"
|
---|
| 4 | #include "glInterface.h"
|
---|
| 5 | #include "Triangle3.h"
|
---|
[2756] | 6 | #include "SceneEntity.h"
|
---|
| 7 | #include "Geometry.h"
|
---|
[2773] | 8 | #include "RenderState.h"
|
---|
[2825] | 9 | #include "Material.h"
|
---|
[3264] | 10 | #include "BvhConstructor.h"
|
---|
[2795] | 11 | #include "gzstream.h"
|
---|
[2751] | 12 |
|
---|
[3021] | 13 | #include <queue>
|
---|
| 14 | #include <stack>
|
---|
| 15 | #include <fstream>
|
---|
| 16 | #include <iostream>
|
---|
| 17 | #include <iomanip>
|
---|
[2773] | 18 |
|
---|
[3021] | 19 |
|
---|
| 20 | #ifdef _CRT_SET
|
---|
| 21 | #define _CRTDBG_MAP_ALLOC
|
---|
| 22 | #include <stdlib.h>
|
---|
| 23 | #include <crtdbg.h>
|
---|
| 24 |
|
---|
| 25 | // redefine new operator
|
---|
| 26 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
| 27 | #define new DEBUG_NEW
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
[2746] | 30 | #define INVALID_TEST ((unsigned int)-1)
|
---|
| 31 |
|
---|
| 32 | using namespace std;
|
---|
| 33 |
|
---|
[3072] | 34 |
|
---|
[3065] | 35 | namespace CHCDemoEngine
|
---|
| 36 | {
|
---|
[2746] | 37 |
|
---|
[2895] | 38 | int BvhNode::sCurrentState = 0;
|
---|
[2746] | 39 |
|
---|
[2895] | 40 |
|
---|
[2746] | 41 | /*
|
---|
| 42 | 3 x---------x 2
|
---|
| 43 | |\ \
|
---|
| 44 | | \ \
|
---|
| 45 | | \ \
|
---|
| 46 | | 4 x---------x 5
|
---|
| 47 | | | |
|
---|
| 48 | 0 x | x 1 |
|
---|
| 49 | \ | |
|
---|
| 50 | \ | |
|
---|
| 51 | \| |
|
---|
| 52 | 7 x---------x 6
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | static unsigned int sIndices[] =
|
---|
| 56 | {7, // degenerated
|
---|
| 57 | 7, 6, 4, 5, 3, 2, 0, 1,
|
---|
| 58 | 1, 4, // degenerated
|
---|
| 59 | 4, 3, 7, 0, 6, 1, 5, 2,
|
---|
| 60 | 2 // degenerated
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 |
|
---|
[3202] | 64 | const static int NUM_INDICES_PER_BOX = 20;
|
---|
[2746] | 65 |
|
---|
| 66 | /* Order of vertices
|
---|
| 67 | 0 = (0, 0, 0)
|
---|
| 68 | 1 = (1, 0, 0)
|
---|
| 69 | 2 = (1, 1, 0)
|
---|
| 70 | 3 = (0, 1, 0)
|
---|
| 71 | 4 = (0, 1, 1)
|
---|
| 72 | 5 = (1, 1, 1)
|
---|
| 73 | 6 = (1, 0, 1)
|
---|
| 74 | 7 = (0, 0, 1)
|
---|
| 75 | */
|
---|
| 76 |
|
---|
[2755] | 77 | static Plane3 sNearPlane;
|
---|
[3065] | 78 | static float sNear;
|
---|
[2911] | 79 | static Frustum sFrustum;
|
---|
[2746] | 80 |
|
---|
[2755] | 81 | /// these values are valid for all nodes
|
---|
| 82 | static int sClipPlaneAABBVertexIndices[12];
|
---|
| 83 |
|
---|
[3201] | 84 | //#define ALIGN_INDICES
|
---|
[2795] | 85 |
|
---|
[3072] | 86 |
|
---|
[2746] | 87 | BvhNode::BvhNode(BvhNode *parent):
|
---|
| 88 | mParent(parent),
|
---|
| 89 | mAxis(-1),
|
---|
| 90 | mDepth(0),
|
---|
| 91 | mFirst(-1),
|
---|
| 92 | mLast(-1),
|
---|
| 93 | mNumTestNodes(1),
|
---|
| 94 | mTestNodesIdx(-1),
|
---|
[2755] | 95 | mIndicesPtr(-1),
|
---|
[2761] | 96 | mId(0),
|
---|
| 97 | mIsMaxDepthForVirtualLeaf(false),
|
---|
| 98 | mIsVirtualLeaf(false)
|
---|
[2746] | 99 | {
|
---|
[2895] | 100 | for (int i = 0; i < NUM_STATES; ++ i)
|
---|
| 101 | {
|
---|
| 102 | mPlaneMask[i] = 0;
|
---|
| 103 | mPreferredPlane[i]= 0;
|
---|
[2897] | 104 | mLastRenderedFrame[i] = -1;
|
---|
[2895] | 105 | }
|
---|
[2746] | 106 | }
|
---|
| 107 |
|
---|
| 108 |
|
---|
[2761] | 109 | BvhNode::~BvhNode()
|
---|
| 110 | {
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
[2746] | 114 | void BvhNode::ResetVisibility()
|
---|
| 115 | {
|
---|
[2895] | 116 | for (int i = 0; i < NUM_STATES; ++ i)
|
---|
| 117 | {
|
---|
| 118 | mVisibility[i].Reset();
|
---|
[2897] | 119 | mLastRenderedFrame[i] = -1;
|
---|
| 120 | mPlaneMask[i] = 0;
|
---|
| 121 | mPreferredPlane[i]= 0;
|
---|
[2895] | 122 | }
|
---|
[2746] | 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | void BvhNode::VisibilityInfo::Reset()
|
---|
| 127 | {
|
---|
| 128 | mIsVisible = false;
|
---|
[2770] | 129 | mAssumedVisibleFrameId = 0;
|
---|
[2746] | 130 | mLastVisitedFrame = -1;
|
---|
| 131 | mTimesInvisible = 0;
|
---|
| 132 | mIsFrustumCulled = false;
|
---|
| 133 | mIsNew = true;
|
---|
[2800] | 134 | mLastQueriedFrame = -1;
|
---|
[2746] | 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
[2792] | 138 | BvhInterior::~BvhInterior()
|
---|
| 139 | {
|
---|
| 140 | DEL_PTR(mBack);
|
---|
| 141 | DEL_PTR(mFront);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
[2746] | 145 | BvhLeaf::~BvhLeaf()
|
---|
| 146 | {
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[3262] | 149 | BvhInterior::BvhInterior(BvhNode *parent):
|
---|
| 150 | mBack(NULL), mFront(NULL), BvhNode(parent)
|
---|
| 151 | {}
|
---|
[2746] | 152 |
|
---|
| 153 |
|
---|
[3262] | 154 | bool BvhInterior::IsLeaf() const
|
---|
| 155 | {
|
---|
| 156 | return false;
|
---|
| 157 | }
|
---|
[2746] | 158 |
|
---|
[3070] | 159 |
|
---|
| 160 |
|
---|
[3262] | 161 | BvhLeaf::BvhLeaf(BvhNode *parent): BvhNode(parent)
|
---|
| 162 | {}
|
---|
[3072] | 163 |
|
---|
[3262] | 164 |
|
---|
| 165 | bool BvhLeaf::IsLeaf() const
|
---|
| 166 | {
|
---|
| 167 | return true;
|
---|
[3070] | 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
[3262] | 171 |
|
---|
| 172 | /**********************************************************/
|
---|
| 173 | /* class Bvh implementation */
|
---|
| 174 | /**********************************************************/
|
---|
| 175 |
|
---|
| 176 |
|
---|
[2773] | 177 | Bvh::Bvh()
|
---|
| 178 | {
|
---|
| 179 | Init();
|
---|
| 180 | }
|
---|
[2746] | 181 |
|
---|
[2760] | 182 |
|
---|
[3072] | 183 | Bvh::Bvh(const SceneEntityContainer &staticEntities,
|
---|
| 184 | const SceneEntityContainer &dynamicEntities)
|
---|
[2746] | 185 | {
|
---|
[2773] | 186 | Init();
|
---|
| 187 |
|
---|
[3072] | 188 | mGeometrySize = staticEntities.size() + dynamicEntities.size();
|
---|
[2760] | 189 | mGeometry = new SceneEntity*[mGeometrySize];
|
---|
[2796] | 190 |
|
---|
[3072] | 191 | mStaticGeometrySize = staticEntities.size();
|
---|
| 192 | mDynamicGeometrySize = dynamicEntities.size();
|
---|
[2746] | 193 |
|
---|
[3072] | 194 | for (size_t i = 0; i < mStaticGeometrySize; ++ i)
|
---|
[2746] | 195 | {
|
---|
[3072] | 196 | mGeometry[i] = staticEntities[i];
|
---|
[2746] | 197 | }
|
---|
[3072] | 198 |
|
---|
| 199 | for (size_t i = 0; i < mDynamicGeometrySize; ++ i)
|
---|
| 200 | {
|
---|
| 201 | mGeometry[mStaticGeometrySize + i] = dynamicEntities[i];
|
---|
| 202 | }
|
---|
[3125] | 203 |
|
---|
| 204 | cout << "max depth for testing children" << mMaxDepthForTestingChildren << endl;
|
---|
[2760] | 205 | }
|
---|
[2746] | 206 |
|
---|
| 207 |
|
---|
[3123] | 208 | Bvh::Bvh(const SceneEntityContainer &staticEntities,
|
---|
| 209 | const SceneEntityContainer &dynamicEntities,
|
---|
| 210 | int maxDepthForTestingChildren)
|
---|
| 211 | {
|
---|
| 212 | Init();
|
---|
| 213 |
|
---|
| 214 | mGeometrySize = staticEntities.size() + dynamicEntities.size();
|
---|
| 215 | mGeometry = new SceneEntity*[mGeometrySize];
|
---|
| 216 |
|
---|
| 217 | mStaticGeometrySize = staticEntities.size();
|
---|
| 218 | mDynamicGeometrySize = dynamicEntities.size();
|
---|
| 219 |
|
---|
| 220 | for (size_t i = 0; i < mStaticGeometrySize; ++ i)
|
---|
| 221 | {
|
---|
| 222 | mGeometry[i] = staticEntities[i];
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | for (size_t i = 0; i < mDynamicGeometrySize; ++ i)
|
---|
| 226 | {
|
---|
| 227 | mGeometry[mStaticGeometrySize + i] = dynamicEntities[i];
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | mMaxDepthForTestingChildren = maxDepthForTestingChildren;
|
---|
[3125] | 231 | cout << "max depth for testing children" << mMaxDepthForTestingChildren << endl;
|
---|
[3123] | 232 | }
|
---|
| 233 |
|
---|
| 234 |
|
---|
[2746] | 235 | Bvh::~Bvh()
|
---|
| 236 | {
|
---|
[3074] | 237 | DEL_ARRAY_PTR(mVertices);
|
---|
| 238 | DEL_ARRAY_PTR(mIndices);
|
---|
| 239 | DEL_ARRAY_PTR(mTestIndices);
|
---|
| 240 | DEL_ARRAY_PTR(mGeometry);
|
---|
[2746] | 241 |
|
---|
| 242 | if (mRoot) delete mRoot;
|
---|
[2792] | 243 | // delete vbo
|
---|
| 244 | glDeleteBuffersARB(1, &mVboId);
|
---|
[2746] | 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
[2773] | 248 | void Bvh::Init()
|
---|
| 249 | {
|
---|
[3074] | 250 | mStaticRoot = NULL;
|
---|
[3064] | 251 | mDynamicRoot = NULL;
|
---|
[2773] | 252 | mRoot = NULL;
|
---|
[3064] | 253 |
|
---|
[2773] | 254 | mVertices = NULL;
|
---|
| 255 | mIndices = NULL;
|
---|
| 256 | mTestIndices = NULL;
|
---|
| 257 | mCurrentIndicesPtr = 0;
|
---|
| 258 | mNumNodes = 0;
|
---|
[2795] | 259 |
|
---|
[3064] | 260 | // nodes are tested using the subnodes from 3 levels below
|
---|
[3066] | 261 | mMaxDepthForTestingChildren = 3;
|
---|
[3064] | 262 | // the ratio of area between node and subnodes where
|
---|
| 263 | // testing the subnodes as proxy is still considered feasable
|
---|
[2795] | 264 | mAreaRatioThreshold = 2.0f;
|
---|
| 265 | //mAreaRatioThreshold = 1.4f;
|
---|
| 266 |
|
---|
[2773] | 267 | mVboId = -1;
|
---|
[3265] | 268 | // bounds the maximal depth of the dynamic branch
|
---|
| 269 | mMaxDepthForDynamicBranch = 10;
|
---|
[2773] | 270 | }
|
---|
| 271 |
|
---|
| 272 |
|
---|
[3064] | 273 |
|
---|
| 274 | //////////////////////
|
---|
| 275 | //-- functions that are used during the main traversal
|
---|
| 276 |
|
---|
| 277 | void Bvh::PullUpLastVisited(BvhNode *node, int frameId) const
|
---|
[2746] | 278 | {
|
---|
| 279 | BvhNode *parent = node->GetParent();
|
---|
| 280 |
|
---|
| 281 | while (parent && (parent->GetLastVisitedFrame() != frameId))
|
---|
| 282 | {
|
---|
| 283 | parent->SetLastVisitedFrame(frameId);
|
---|
| 284 | parent = parent->GetParent();
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | void Bvh::MakeParentsVisible(BvhNode *node)
|
---|
| 290 | {
|
---|
| 291 | BvhNode *parent = node->GetParent();
|
---|
| 292 |
|
---|
| 293 | while (parent && (!parent->IsVisible()))
|
---|
| 294 | {
|
---|
| 295 | parent->SetVisible(true);
|
---|
| 296 | parent = parent->GetParent();
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
[3064] | 301 | ////////////////////////////////
|
---|
| 302 |
|
---|
[2746] | 303 | void Bvh::CollectLeaves(BvhNode *node, BvhLeafContainer &leaves)
|
---|
| 304 | {
|
---|
| 305 | stack<BvhNode *> tStack;
|
---|
| 306 | tStack.push(node);
|
---|
| 307 |
|
---|
| 308 | while (!tStack.empty())
|
---|
| 309 | {
|
---|
| 310 | BvhNode *node = tStack.top();
|
---|
| 311 |
|
---|
| 312 | tStack.pop();
|
---|
| 313 |
|
---|
| 314 | if (!node->IsLeaf())
|
---|
| 315 | {
|
---|
| 316 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 317 |
|
---|
| 318 | tStack.push(interior->mFront);
|
---|
| 319 | tStack.push(interior->mBack);
|
---|
| 320 | }
|
---|
| 321 | else
|
---|
| 322 | {
|
---|
| 323 | leaves.push_back(static_cast<BvhLeaf *>(node));
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 |
|
---|
[3259] | 329 | int Bvh::CountNumNodes(BvhNode *node) const
|
---|
| 330 | {
|
---|
| 331 | int numNodes = 0;
|
---|
| 332 |
|
---|
| 333 | stack<BvhNode *> tStack;
|
---|
| 334 |
|
---|
| 335 | tStack.push(node);
|
---|
| 336 |
|
---|
| 337 | while (!tStack.empty())
|
---|
| 338 | {
|
---|
| 339 | BvhNode *node = tStack.top();
|
---|
| 340 | tStack.pop();
|
---|
| 341 |
|
---|
| 342 | ++ numNodes;
|
---|
| 343 |
|
---|
| 344 | if (!node->IsLeaf())
|
---|
| 345 | {
|
---|
| 346 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 347 |
|
---|
| 348 | tStack.push(interior->mFront);
|
---|
| 349 | tStack.push(interior->mBack);
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | return numNodes;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 |
|
---|
[2746] | 357 | void Bvh::CollectNodes(BvhNode *node, BvhNodeContainer &nodes)
|
---|
| 358 | {
|
---|
| 359 | stack<BvhNode *> tStack;
|
---|
| 360 |
|
---|
| 361 | tStack.push(node);
|
---|
| 362 |
|
---|
| 363 | while (!tStack.empty())
|
---|
| 364 | {
|
---|
| 365 | BvhNode *node = tStack.top();
|
---|
| 366 | tStack.pop();
|
---|
| 367 |
|
---|
| 368 | nodes.push_back(node);
|
---|
| 369 |
|
---|
| 370 | if (!node->IsLeaf())
|
---|
| 371 | {
|
---|
| 372 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 373 |
|
---|
| 374 | tStack.push(interior->mFront);
|
---|
| 375 | tStack.push(interior->mBack);
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 |
|
---|
| 381 | typedef pair<BvhNode *, int> tPair;
|
---|
| 382 |
|
---|
[2755] | 383 | void Bvh::CollectNodes(BvhNode *root, BvhNodeContainer &nodes, int depth)
|
---|
[2746] | 384 | {
|
---|
| 385 | stack<tPair> tStack;
|
---|
| 386 | tStack.push(tPair(root, 0));
|
---|
| 387 |
|
---|
| 388 | while (!tStack.empty())
|
---|
| 389 | {
|
---|
| 390 | BvhNode *node = tStack.top().first;
|
---|
| 391 | const int d = tStack.top().second;
|
---|
| 392 |
|
---|
| 393 | tStack.pop();
|
---|
| 394 |
|
---|
[3065] | 395 | // found node in specified depth => take this node
|
---|
[2755] | 396 | if ((d == depth) || (node->IsLeaf()))
|
---|
[2746] | 397 | {
|
---|
| 398 | nodes.push_back(node);
|
---|
| 399 | }
|
---|
[2755] | 400 | else
|
---|
[2951] | 401 | {
|
---|
[2746] | 402 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 403 |
|
---|
| 404 | tStack.push(tPair(interior->mFront, d + 1));
|
---|
| 405 | tStack.push(tPair(interior->mBack, d + 1));
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 |
|
---|
[2951] | 411 | SceneEntity **Bvh::GetGeometry(BvhNode *node, int &geometrySize) const
|
---|
[2746] | 412 | {
|
---|
| 413 | geometrySize = node->CountPrimitives();
|
---|
| 414 | return mGeometry + node->mFirst;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 |
|
---|
[2762] | 418 | bool Bvh::TestPlane(BvhNode *node, int i, bool &bIntersect)
|
---|
| 419 | {
|
---|
| 420 | // do the test only if necessary
|
---|
[2895] | 421 | if (!(node->mPlaneMask[BvhNode::sCurrentState] & (1 << i)))
|
---|
[3065] | 422 | {
|
---|
[2763] | 423 | return true;
|
---|
[3065] | 424 | }
|
---|
| 425 |
|
---|
| 426 |
|
---|
[2763] | 427 | ////////
|
---|
| 428 | //-- test the n-vertex
|
---|
| 429 |
|
---|
| 430 | if ((node->mBox.GetDistance(sClipPlaneAABBVertexIndices[i * 2 + 0], sFrustum.mClipPlanes[i]) > 0.0f))
|
---|
| 431 | {
|
---|
| 432 | // outside
|
---|
[2895] | 433 | node->mPreferredPlane[BvhNode::sCurrentState] = i;
|
---|
[2763] | 434 | return false;
|
---|
[2762] | 435 | }
|
---|
| 436 |
|
---|
[2763] | 437 | ////////////
|
---|
| 438 | //-- test the p-vertex
|
---|
| 439 |
|
---|
| 440 | if (node->mBox.GetDistance(sClipPlaneAABBVertexIndices[i * 2 + 1], sFrustum.mClipPlanes[i]) <= 0.0f)
|
---|
| 441 | {
|
---|
| 442 | // completely inside: children don't need to check against this plane no more
|
---|
[2895] | 443 | node->mPlaneMask[BvhNode::sCurrentState] ^= 1 << i;
|
---|
[2763] | 444 | }
|
---|
| 445 | else
|
---|
| 446 | {
|
---|
| 447 | bIntersect = true;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
[2762] | 450 | return true;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 |
|
---|
[2755] | 454 | int Bvh::IsWithinViewFrustum(BvhNode *node)
|
---|
[2746] | 455 | {
|
---|
| 456 | bool bIntersect = false;
|
---|
| 457 |
|
---|
| 458 | if (node->GetParent())
|
---|
[2895] | 459 | node->mPlaneMask[BvhNode::sCurrentState] = node->GetParent()->mPlaneMask[BvhNode::sCurrentState];
|
---|
[2746] | 460 |
|
---|
| 461 | ////////
|
---|
[3065] | 462 | //-- apply frustum culling for the planes with index mPreferredPlane to 6
|
---|
[2763] | 463 |
|
---|
[2895] | 464 | for (int i = node->mPreferredPlane[BvhNode::sCurrentState]; i < 6; ++ i)
|
---|
[2762] | 465 | if (!TestPlane(node, i, bIntersect)) return 0;
|
---|
[2763] | 466 |
|
---|
[2895] | 467 |
|
---|
[2746] | 468 | //////////
|
---|
[3065] | 469 | //-- apply frustum culling for the planes with index 0 to mPreferredPlane
|
---|
[2746] | 470 |
|
---|
[2895] | 471 | for (int i = 0; i < node->mPreferredPlane[BvhNode::sCurrentState]; ++ i)
|
---|
[2762] | 472 | if (!TestPlane(node, i, bIntersect)) return 0;
|
---|
[2763] | 473 |
|
---|
[2746] | 474 | return bIntersect ? -1 : 1;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 |
|
---|
[2911] | 478 | static void CalcNPVertexIndices(const Frustum &frustum, int *indices)
|
---|
| 479 | {
|
---|
| 480 | for (int i = 0; i < 6; ++ i)
|
---|
| 481 | {
|
---|
| 482 | // n-vertex
|
---|
| 483 | indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(frustum.mClipPlanes[i].mNormal);
|
---|
| 484 | // p-vertex
|
---|
| 485 | indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(frustum.mClipPlanes[i].mNormal);
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 |
|
---|
[3074] | 490 | void Bvh::InitFrame(Camera *cam, RenderState *state)
|
---|
[2746] | 491 | {
|
---|
| 492 | // = 0011 1111 which means that at the beginning, all six planes have to frustum culled
|
---|
[2895] | 493 | mRoot->mPlaneMask[BvhNode::sCurrentState] = 0x3f;
|
---|
[2746] | 494 |
|
---|
[2897] | 495 | cam->CalcFrustum(sFrustum);
|
---|
[2911] | 496 | CalcNPVertexIndices(sFrustum, sClipPlaneAABBVertexIndices);
|
---|
[2746] | 497 |
|
---|
[2763] | 498 | // store near plane
|
---|
[2897] | 499 | sNearPlane = Plane3(cam->GetDirection(), cam->GetPosition());
|
---|
[3065] | 500 | sNear = cam->GetNear();
|
---|
[3053] | 501 |
|
---|
[3072] | 502 | // update dynamic part of the hierarchy
|
---|
| 503 | //if (!mDynamicEntities.empty())
|
---|
| 504 | if (mDynamicGeometrySize)
|
---|
[3053] | 505 | {
|
---|
[3070] | 506 | UpdateDynamicBranch(mDynamicRoot);
|
---|
[3074] | 507 | UpdateDynamicBounds(state);
|
---|
[3053] | 508 | }
|
---|
[2746] | 509 | }
|
---|
| 510 |
|
---|
| 511 |
|
---|
[2954] | 512 | void Bvh::UpdateDistance(BvhNode *node) const
|
---|
[2746] | 513 | {
|
---|
[3065] | 514 | // q: should we use distance to center rather than the distance to the near plane?
|
---|
[3068] | 515 | // distance to near plane can also be used for checking near plane intersection
|
---|
| 516 | //node->mDistance = sNearPlane.Distance(node->GetBox().Center());
|
---|
| 517 | node->mDistance = node->GetBox().GetMinDistance(sNearPlane);
|
---|
[2746] | 518 | }
|
---|
| 519 |
|
---|
| 520 |
|
---|
[2947] | 521 | float Bvh::CalcMaxDistance(BvhNode *node) const
|
---|
| 522 | {
|
---|
[3068] | 523 | #if 1
|
---|
[2947] | 524 | return node->GetBox().GetMaxDistance(sNearPlane);
|
---|
[2951] | 525 |
|
---|
[3068] | 526 | #else
|
---|
| 527 | // use bounding boxes of geometry to determine max dist
|
---|
[2951] | 528 | float maxDist = .0f;
|
---|
| 529 |
|
---|
| 530 | int geometrySize;
|
---|
| 531 | SceneEntity **entities = GetGeometry(node, geometrySize);
|
---|
| 532 |
|
---|
| 533 | for (int i = 0; i < geometrySize; ++ i)
|
---|
| 534 | {
|
---|
| 535 | SceneEntity *ent = entities[i];
|
---|
[3070] | 536 | float dist = ent->GetWorldBoundingBox().GetMaxDistance(sNearPlane);
|
---|
[2951] | 537 |
|
---|
| 538 | if (dist > maxDist) maxDist = dist;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | return maxDist;
|
---|
[3068] | 542 | #endif
|
---|
[2947] | 543 | }
|
---|
| 544 |
|
---|
| 545 |
|
---|
[2786] | 546 | void Bvh::RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds)
|
---|
[2746] | 547 | {
|
---|
[3065] | 548 | // hack: use dummy contayiner as wrapper in order to use multibox function
|
---|
| 549 | static BvhNodeContainer dummy(1);
|
---|
| 550 | dummy[0] = node;
|
---|
| 551 | RenderBounds(dummy, state, useTightBounds);
|
---|
[2746] | 552 | }
|
---|
| 553 |
|
---|
| 554 |
|
---|
[2786] | 555 | int Bvh::RenderBounds(const BvhNodeContainer &nodes,
|
---|
| 556 | RenderState *state,
|
---|
| 557 | bool useTightBounds)
|
---|
[2746] | 558 | {
|
---|
[2786] | 559 | int renderedBoxes;
|
---|
| 560 |
|
---|
| 561 | if (!useTightBounds)
|
---|
| 562 | {
|
---|
[3064] | 563 | // if not using tight bounds, rendering boxes in immediate mode
|
---|
| 564 | // is preferable to vertex arrays (less setup time)
|
---|
[2786] | 565 | BvhNodeContainer::const_iterator nit, nit_end = nodes.end();
|
---|
| 566 |
|
---|
| 567 | for (nit = nodes.begin(); nit != nit_end; ++ nit)
|
---|
| 568 | {
|
---|
| 569 | RenderBoundingBoxImmediate((*nit)->GetBox());
|
---|
| 570 | }
|
---|
[3065] | 571 |
|
---|
[2786] | 572 | renderedBoxes = (int)nodes.size();
|
---|
| 573 | }
|
---|
| 574 | else
|
---|
[3065] | 575 | {
|
---|
[2786] | 576 | renderedBoxes = PrepareBoundsWithDrawArrays(nodes);
|
---|
| 577 | RenderBoundsWithDrawArrays(renderedBoxes, state);
|
---|
| 578 | }
|
---|
| 579 |
|
---|
[2746] | 580 | return renderedBoxes;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 |
|
---|
[2786] | 584 | int Bvh::PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes)
|
---|
[2746] | 585 | {
|
---|
[3064] | 586 | ///////////////////
|
---|
| 587 | //-- for the first time we come here => create vbo and indices
|
---|
[3074] | 588 |
|
---|
[2746] | 589 | if (!mIndices)
|
---|
[3064] | 590 | {
|
---|
| 591 | // create list of indices
|
---|
[2746] | 592 | CreateIndices();
|
---|
| 593 | }
|
---|
| 594 |
|
---|
[2773] | 595 | if (mVboId == -1)
|
---|
[2746] | 596 | {
|
---|
| 597 | // prepare the vbo
|
---|
| 598 | PrepareVertices();
|
---|
| 599 | }
|
---|
[3074] | 600 |
|
---|
[2746] | 601 | ///////////////
|
---|
| 602 |
|
---|
| 603 | int numNodes = 0;
|
---|
[3074] | 604 |
|
---|
[2746] | 605 | BvhNodeContainer::const_iterator nit, nit_end = nodes.end();
|
---|
| 606 |
|
---|
| 607 | for (nit = nodes.begin(); nit != nit_end; ++ nit)
|
---|
| 608 | {
|
---|
| 609 | BvhNode *node = *nit;
|
---|
[3202] | 610 | const int numIndices = node->mNumTestNodes * NUM_INDICES_PER_BOX;
|
---|
[2746] | 611 |
|
---|
| 612 | // copy indices
|
---|
[3202] | 613 | memcpy(mIndices + numNodes * NUM_INDICES_PER_BOX,
|
---|
[2746] | 614 | mTestIndices + node->mIndicesPtr,
|
---|
| 615 | numIndices * sizeof(unsigned int));
|
---|
[2795] | 616 |
|
---|
[2746] | 617 | numNodes += node->mNumTestNodes;
|
---|
| 618 | }
|
---|
[3074] | 619 |
|
---|
[2746] | 620 | return numNodes;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 |
|
---|
[2786] | 624 | void Bvh::RenderBoundsWithDrawArrays(int numNodes, RenderState *state)
|
---|
[2746] | 625 | {
|
---|
[3064] | 626 | /////////
|
---|
| 627 | //-- Render the vbo
|
---|
[2773] | 628 |
|
---|
| 629 | if (state->GetCurrentVboId() != mVboId)
|
---|
| 630 | {
|
---|
| 631 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
[2746] | 632 | // set the vertex pointer to the vertex buffer
|
---|
[2773] | 633 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
[2746] | 634 |
|
---|
[2773] | 635 | state->SetCurrentVboId(mVboId);
|
---|
| 636 | }
|
---|
[2746] | 637 |
|
---|
[3202] | 638 | // we don't use the last or the first index (they are generate and only used to connect strips)
|
---|
| 639 | int numElements = numNodes * NUM_INDICES_PER_BOX - 1;
|
---|
[2746] | 640 | // don't render first degenerate index
|
---|
| 641 | glDrawElements(GL_TRIANGLE_STRIP, numElements, GL_UNSIGNED_INT, mIndices + 1);
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 |
|
---|
| 645 | void Bvh::CreateIndices()
|
---|
| 646 | {
|
---|
| 647 | // collect bvh nodes
|
---|
| 648 | BvhNodeContainer nodes;
|
---|
[3202] | 649 | // first collect dynamic nodes so we make sure that they are in the beginning
|
---|
| 650 | CollectNodes(mDynamicRoot, nodes);
|
---|
| 651 | // then collect static nodes
|
---|
| 652 | CollectNodes(mStaticRoot, nodes);
|
---|
| 653 | //CollectNodes(mRoot, nodes);
|
---|
[2746] | 654 |
|
---|
[2755] | 655 | cout << "creating new indices" << endl;
|
---|
[2746] | 656 |
|
---|
| 657 | int numMaxIndices = 0;
|
---|
| 658 |
|
---|
| 659 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
| 660 |
|
---|
| 661 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
| 662 | {
|
---|
[3202] | 663 | int offset = (*lit)->mNumTestNodes * NUM_INDICES_PER_BOX;
|
---|
[2795] | 664 | #ifdef ALIGN_INDICES
|
---|
[3064] | 665 | // align with 32 in order to speed up memcopy
|
---|
[2746] | 666 | offset = (offset / 32) * 32 + 32;
|
---|
| 667 | #endif
|
---|
| 668 | numMaxIndices += offset;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
[2755] | 671 | cout << "creating global indices buffer" << endl;
|
---|
[2746] | 672 |
|
---|
[3202] | 673 | if (mIndices) delete [] mIndices;
|
---|
[2746] | 674 | if (mTestIndices) delete [] mTestIndices;
|
---|
| 675 |
|
---|
| 676 | // global buffer: create it once so we don't have
|
---|
[3202] | 677 | // to allocate memory for each individual chunk of the node
|
---|
[2746] | 678 | mIndices = new unsigned int[numMaxIndices];
|
---|
| 679 | // create new index buffer for the individual nodes
|
---|
| 680 | mTestIndices = new unsigned int[numMaxIndices];
|
---|
| 681 |
|
---|
| 682 | mCurrentIndicesPtr = 0;
|
---|
| 683 |
|
---|
| 684 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
| 685 | {
|
---|
| 686 | BvhNode *node = *lit;
|
---|
| 687 |
|
---|
| 688 | // resize array
|
---|
| 689 | node->mIndicesPtr = mCurrentIndicesPtr;
|
---|
| 690 | int numIndices = 0;
|
---|
| 691 |
|
---|
[3065] | 692 | // the bounding boxes of the test nodes are rendered instead of the node itself
|
---|
[2746] | 693 | // => store their indices
|
---|
[3202] | 694 | for (int i = 0; i < node->mNumTestNodes; ++ i, numIndices += NUM_INDICES_PER_BOX)
|
---|
[2746] | 695 | {
|
---|
[2755] | 696 | BvhNode *testNode = mTestNodes[node->mTestNodesIdx + i];
|
---|
[2746] | 697 |
|
---|
[3202] | 698 | // add vertex indices of boxes to root node
|
---|
| 699 | for (int j = 0; j < NUM_INDICES_PER_BOX; ++ j)
|
---|
[2746] | 700 | {
|
---|
| 701 | mTestIndices[mCurrentIndicesPtr + numIndices + j] = sIndices[j] + testNode->GetId() * 8;
|
---|
| 702 | }
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | // align with 32
|
---|
[2795] | 706 | #ifdef ALIGN_INDICES
|
---|
[2746] | 707 | const int offset = (numIndices / 32) * 32 + 32;
|
---|
| 708 | #else
|
---|
| 709 | const int offset = numIndices;
|
---|
| 710 | #endif
|
---|
| 711 | mCurrentIndicesPtr += offset;
|
---|
| 712 | }
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 |
|
---|
| 716 | void Bvh::ComputeIds()
|
---|
| 717 | {
|
---|
[2756] | 718 | // collect all nodes
|
---|
[2755] | 719 | BvhNodeContainer nodes;
|
---|
[3074] | 720 | //CollectNodes(mRoot, nodes);
|
---|
| 721 | // first collect dynamic nodes so we make sure that they are in the beginning
|
---|
| 722 | CollectNodes(mDynamicRoot, nodes);
|
---|
| 723 | // then collect static nodes
|
---|
| 724 | CollectNodes(mStaticRoot, nodes);
|
---|
| 725 | // also add root
|
---|
| 726 | nodes.push_back(mRoot);
|
---|
[2746] | 727 |
|
---|
[3074] | 728 | // assign ids to all nodes of the hierarchy
|
---|
[2746] | 729 | int i = 0;
|
---|
[2756] | 730 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
[2746] | 731 |
|
---|
| 732 | for (lit = nodes.begin(); lit != lit_end; ++ lit, ++ i)
|
---|
| 733 | {
|
---|
| 734 | (*lit)->SetId(i);
|
---|
| 735 | }
|
---|
| 736 | }
|
---|
| 737 |
|
---|
[2756] | 738 |
|
---|
[2746] | 739 | void Bvh::PrepareVertices()
|
---|
| 740 | {
|
---|
[2755] | 741 | // collect all nodes
|
---|
| 742 | BvhNodeContainer nodes;
|
---|
[2746] | 743 |
|
---|
[3073] | 744 | nodes.reserve(GetNumNodes());
|
---|
[3074] | 745 | // first collect dynamic nodes so we make sure that they are in the beginning
|
---|
| 746 | CollectNodes(mDynamicRoot, nodes);
|
---|
| 747 | // then collect static nodes
|
---|
| 748 | CollectNodes(mStaticRoot, nodes);
|
---|
| 749 | // also add root
|
---|
| 750 | nodes.push_back(mRoot);
|
---|
[2746] | 751 |
|
---|
| 752 | const unsigned int bufferSize = 8 * (int)nodes.size();
|
---|
[2755] | 753 | mVertices = new Vector3[bufferSize];
|
---|
[2746] | 754 |
|
---|
| 755 | int i = 0;
|
---|
| 756 |
|
---|
| 757 | // store bounding box vertices
|
---|
[2755] | 758 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
[2746] | 759 |
|
---|
| 760 | for (lit = nodes.begin(); lit != lit_end; ++ lit, i += 8)
|
---|
| 761 | {
|
---|
[2755] | 762 | BvhNode *node = *lit;
|
---|
[2746] | 763 |
|
---|
[3202] | 764 | Vector3 v;
|
---|
| 765 |
|
---|
[2746] | 766 | for (int j = 0; j < 8; ++ j)
|
---|
[3202] | 767 | {
|
---|
| 768 | node->GetBox().GetVertex2(j, v);
|
---|
| 769 | (static_cast<Vector3 *>(mVertices))[node->GetId() * 8 + j] = v;
|
---|
| 770 | }
|
---|
[2746] | 771 | }
|
---|
| 772 |
|
---|
[2773] | 773 | glGenBuffersARB(1, &mVboId);
|
---|
| 774 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
| 775 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
[2746] | 776 |
|
---|
[2773] | 777 | glBufferDataARB(GL_ARRAY_BUFFER_ARB,
|
---|
| 778 | bufferSize * sizeof(Vector3),
|
---|
| 779 | mVertices,
|
---|
[3074] | 780 | //GL_STATIC_DRAW_ARB);
|
---|
| 781 | GL_DYNAMIC_DRAW_ARB);
|
---|
[2746] | 782 |
|
---|
[2800] | 783 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
[2746] | 784 |
|
---|
[2773] | 785 | // data handled by graphics driver from now on
|
---|
[3074] | 786 | DEL_ARRAY_PTR(mVertices);
|
---|
[2746] | 787 |
|
---|
[3064] | 788 | cout << "******** created vbos for tighter bounds *********" << endl;
|
---|
[2746] | 789 | }
|
---|
| 790 |
|
---|
| 791 |
|
---|
[3074] | 792 | void Bvh::UpdateDynamicBounds(RenderState *state)
|
---|
| 793 | {
|
---|
| 794 | // vbos not created yet
|
---|
| 795 | if (mVboId == -1) return;
|
---|
| 796 |
|
---|
| 797 | // collect all nodes
|
---|
| 798 | static BvhNodeContainer nodes;
|
---|
| 799 | nodes.clear();
|
---|
[3259] | 800 |
|
---|
[3074] | 801 | CollectNodes(mDynamicRoot, nodes);
|
---|
| 802 |
|
---|
| 803 | const unsigned int bufferSize = 8 * (int)nodes.size();
|
---|
| 804 | if (!mVertices) mVertices = new Vector3[bufferSize];
|
---|
| 805 |
|
---|
| 806 | int i = 0;
|
---|
| 807 |
|
---|
| 808 | // store bounding box vertices
|
---|
| 809 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
| 810 |
|
---|
| 811 | for (lit = nodes.begin(); lit != lit_end; ++ lit, i += 8)
|
---|
| 812 | {
|
---|
| 813 | BvhNode *node = *lit;
|
---|
| 814 |
|
---|
| 815 | for (int j = 0; j < 8; ++ j)
|
---|
| 816 | (static_cast<Vector3 *>(mVertices))[node->GetId() * 8 + j] = node->GetBox().GetVertex(j);
|
---|
| 817 | }
|
---|
| 818 |
|
---|
| 819 | if (state->GetCurrentVboId() != mVboId)
|
---|
| 820 | {
|
---|
| 821 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
| 822 | // set the vertex pointer to the vertex buffer
|
---|
| 823 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
| 824 | state->SetCurrentVboId(mVboId);
|
---|
| 825 | }
|
---|
| 826 |
|
---|
| 827 | glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0,
|
---|
| 828 | bufferSize * sizeof(Vector3),
|
---|
| 829 | mVertices);
|
---|
| 830 | }
|
---|
| 831 |
|
---|
| 832 |
|
---|
[2755] | 833 | void Bvh::SetMaxDepthForTestingChildren(int maxDepth)
|
---|
[2746] | 834 | {
|
---|
| 835 | if (maxDepth != mMaxDepthForTestingChildren)
|
---|
| 836 | {
|
---|
| 837 | mMaxDepthForTestingChildren = maxDepth;
|
---|
| 838 | RecomputeBounds();
|
---|
| 839 | }
|
---|
| 840 | }
|
---|
| 841 |
|
---|
| 842 |
|
---|
[2755] | 843 | void Bvh::SetAreaRatioThresholdForTestingChildren(float ratio)
|
---|
[2746] | 844 | {
|
---|
| 845 | if (ratio != mAreaRatioThreshold)
|
---|
| 846 | {
|
---|
| 847 | mAreaRatioThreshold = ratio;
|
---|
| 848 | RecomputeBounds();
|
---|
| 849 | }
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 |
|
---|
| 853 | void Bvh::RecomputeBounds()
|
---|
| 854 | {
|
---|
| 855 | // collect all nodes
|
---|
| 856 | BvhNodeContainer nodes;
|
---|
[3065] | 857 | CollectNodes(mRoot, nodes);
|
---|
[2746] | 858 |
|
---|
[2755] | 859 | cout << "recomputing bounds, children will be tested in depth " << mMaxDepthForTestingChildren << endl;
|
---|
[2746] | 860 |
|
---|
| 861 | int success = 0;
|
---|
| 862 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
| 863 |
|
---|
| 864 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
| 865 | {
|
---|
| 866 | BvhNode *node = *lit;
|
---|
| 867 |
|
---|
[2795] | 868 | // recreate list of nodes that will be queried as a proxy
|
---|
[2755] | 869 | if (CreateNodeRenderList(node))
|
---|
| 870 | ++ success;
|
---|
[2746] | 871 | }
|
---|
| 872 |
|
---|
[2755] | 873 | float p = 100.0f * (float)success / nodes.size();
|
---|
| 874 | cout << "created tighter bounds for " << p << " percent of the nodes" << endl;
|
---|
[2746] | 875 |
|
---|
| 876 | // recreate indices used for indirect mode rendering
|
---|
[2894] | 877 | if (mIndices) CreateIndices();
|
---|
[2746] | 878 | }
|
---|
| 879 |
|
---|
| 880 |
|
---|
| 881 | bool Bvh::CreateNodeRenderList(BvhNode *node)
|
---|
| 882 | {
|
---|
[2755] | 883 | BvhNodeContainer children;
|
---|
[2746] | 884 |
|
---|
[2755] | 885 | // collect nodes that will be tested instead of the leaf node
|
---|
| 886 | // in order to get a tighter bounding box test
|
---|
| 887 | CollectNodes(node, children, mMaxDepthForTestingChildren);
|
---|
| 888 |
|
---|
[2746] | 889 |
|
---|
| 890 | // using the tighter bounds is not feasable in case
|
---|
| 891 | // that the tighter bounds represent nearly the same projected area
|
---|
[3064] | 892 | // as the old bounding box. Test this property using either
|
---|
| 893 | // volume or area heuristics
|
---|
[2746] | 894 |
|
---|
| 895 | float vol = 0;
|
---|
| 896 | float area = 0;
|
---|
| 897 |
|
---|
[2755] | 898 | BvhNodeContainer::const_iterator cit;
|
---|
[2746] | 899 |
|
---|
| 900 | for (cit = children.begin(); cit != children.end(); ++ cit)
|
---|
[2755] | 901 | area += (*cit)->GetBox().SurfaceArea();
|
---|
[2746] | 902 |
|
---|
[2755] | 903 | const float areaRatio = area / node->GetBox().SurfaceArea();
|
---|
[2746] | 904 |
|
---|
| 905 | bool success;
|
---|
| 906 |
|
---|
[2755] | 907 | if (areaRatio < mAreaRatioThreshold)
|
---|
[2746] | 908 | success = true;
|
---|
| 909 | else
|
---|
| 910 | {
|
---|
| 911 | // hack: only store node itself
|
---|
| 912 | children.clear();
|
---|
| 913 | children.push_back(node);
|
---|
| 914 |
|
---|
| 915 | success = false;
|
---|
| 916 | }
|
---|
| 917 |
|
---|
| 918 | // the new test nodes are added at the end of the vector
|
---|
[2755] | 919 | node->mTestNodesIdx = (int)mTestNodes.size();
|
---|
[2746] | 920 |
|
---|
[3072] | 921 | // use the collected nodes as proxy for the occlusion tests
|
---|
[2746] | 922 | for (cit = children.begin(); cit != children.end(); ++ cit)
|
---|
| 923 | {
|
---|
[2755] | 924 | BvhNode *child = *cit;
|
---|
[2746] | 925 | mTestNodes.push_back(child);
|
---|
| 926 | }
|
---|
| 927 |
|
---|
| 928 | node->mNumTestNodes = (int)children.size();
|
---|
| 929 |
|
---|
| 930 | return success;
|
---|
| 931 | }
|
---|
| 932 |
|
---|
| 933 |
|
---|
| 934 | void Bvh::ResetNodeClassifications()
|
---|
| 935 | {
|
---|
| 936 | BvhNodeContainer nodes;
|
---|
| 937 |
|
---|
| 938 | nodes.reserve(GetNumNodes());
|
---|
| 939 | CollectNodes(mRoot, nodes);
|
---|
| 940 |
|
---|
| 941 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
| 942 |
|
---|
| 943 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
| 944 | {
|
---|
| 945 | (*lit)->ResetVisibility();
|
---|
| 946 | }
|
---|
| 947 | }
|
---|
| 948 |
|
---|
| 949 |
|
---|
[3070] | 950 | void Bvh::ComputeBvhStats(BvhNode *root, BvhStats &bvhStats)
|
---|
[2746] | 951 | {
|
---|
[3070] | 952 | bvhStats.Reset();
|
---|
[2746] | 953 | std::stack<BvhNode *> nodeStack;
|
---|
[3070] | 954 | nodeStack.push(root);
|
---|
[2746] | 955 |
|
---|
[2786] | 956 | int numVirtualLeaves = 0;
|
---|
[3070] | 957 | int numGeometry = 0;
|
---|
[2786] | 958 |
|
---|
[2746] | 959 | while (!nodeStack.empty())
|
---|
| 960 | {
|
---|
| 961 | BvhNode *node = nodeStack.top();
|
---|
| 962 | nodeStack.pop();
|
---|
| 963 |
|
---|
[2773] | 964 | if (node->IsVirtualLeaf())
|
---|
[2746] | 965 | {
|
---|
[2786] | 966 | ++ numVirtualLeaves;
|
---|
[3070] | 967 | numGeometry += node->CountPrimitives();
|
---|
[2786] | 968 |
|
---|
[2746] | 969 | BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
|
---|
| 970 |
|
---|
[3070] | 971 | bvhStats.mTriangles += CountTriangles(leaf);
|
---|
| 972 | bvhStats.mLeafSA += leaf->mBox.SurfaceArea();
|
---|
| 973 | bvhStats.mLeafVol += leaf->mBox.GetVolume();
|
---|
[2746] | 974 | }
|
---|
| 975 | else
|
---|
| 976 | {
|
---|
[3070] | 977 | bvhStats.mInteriorSA += node->mBox.SurfaceArea();
|
---|
| 978 | bvhStats.mInteriorVol += node->mBox.GetVolume();
|
---|
[2746] | 979 |
|
---|
| 980 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 981 |
|
---|
| 982 | nodeStack.push(interior->mBack);
|
---|
| 983 | nodeStack.push(interior->mFront);
|
---|
| 984 | }
|
---|
| 985 | }
|
---|
| 986 |
|
---|
[3070] | 987 | bvhStats.mGeometryRatio = (float)numGeometry / numVirtualLeaves;
|
---|
| 988 | bvhStats.mTriangleRatio = (float)bvhStats.mTriangles / numVirtualLeaves;
|
---|
| 989 | bvhStats.mLeaves = numVirtualLeaves;
|
---|
[2746] | 990 | }
|
---|
| 991 |
|
---|
| 992 |
|
---|
[3070] | 993 | void Bvh::PrintBvhStats(const BvhStats &bvhStats) const
|
---|
[2746] | 994 | {
|
---|
[3070] | 995 | cout << "\n============ BVH statistics =============" << endl;
|
---|
| 996 | cout << "interiorNodesSA = " << bvhStats.mInteriorSA / mRoot->mBox.SurfaceArea() << endl;
|
---|
| 997 | cout << "leafNodesSA = " << bvhStats.mLeafSA / mRoot->mBox.SurfaceArea() << endl;
|
---|
| 998 | cout << "interiorNodesVolume = " << bvhStats.mInteriorVol / mRoot->mBox.GetVolume() << endl;
|
---|
| 999 | cout << "leafNodesVolume = " << bvhStats.mLeafVol / mRoot->mBox.GetVolume() << endl;
|
---|
[2746] | 1000 |
|
---|
[3070] | 1001 | cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl;
|
---|
| 1002 | cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl;
|
---|
| 1003 | cout << "===========================================" << endl << endl;
|
---|
[2746] | 1004 | }
|
---|
| 1005 |
|
---|
| 1006 |
|
---|
| 1007 | int Bvh::CountTriangles(BvhNode *node) const
|
---|
| 1008 | {
|
---|
| 1009 | int numTriangles = 0;
|
---|
| 1010 |
|
---|
| 1011 | for (int i = node->mFirst; i <= node->mLast; ++ i)
|
---|
[2774] | 1012 | {
|
---|
[2848] | 1013 | numTriangles += mGeometry[i]->CountNumTriangles(0);
|
---|
[2774] | 1014 | }
|
---|
| 1015 |
|
---|
[2746] | 1016 | return numTriangles;
|
---|
| 1017 | }
|
---|
| 1018 |
|
---|
| 1019 |
|
---|
[2756] | 1020 | float Bvh::GetArea(BvhNode *node) const
|
---|
[2746] | 1021 | {
|
---|
| 1022 | return node->mArea;
|
---|
| 1023 | }
|
---|
| 1024 |
|
---|
[2760] | 1025 |
|
---|
| 1026 | void Bvh::UpdateNumLeaves(BvhNode *node) const
|
---|
| 1027 | {
|
---|
| 1028 | if (node->IsLeaf())
|
---|
| 1029 | {
|
---|
| 1030 | node->mNumLeaves = 1;
|
---|
| 1031 | }
|
---|
| 1032 | else
|
---|
| 1033 | {
|
---|
| 1034 | BvhNode *f = static_cast<BvhInterior *>(node)->GetFront();
|
---|
| 1035 | BvhNode *b = static_cast<BvhInterior *>(node)->GetBack();
|
---|
| 1036 |
|
---|
| 1037 | UpdateNumLeaves(f);
|
---|
| 1038 | UpdateNumLeaves(b);
|
---|
| 1039 |
|
---|
| 1040 | node->mNumLeaves = f->mNumLeaves + b->mNumLeaves;
|
---|
| 1041 | }
|
---|
[2746] | 1042 | }
|
---|
[2760] | 1043 |
|
---|
| 1044 |
|
---|
[2761] | 1045 | void Bvh::CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves)
|
---|
| 1046 | {
|
---|
| 1047 | stack<BvhNode *> tStack;
|
---|
| 1048 | tStack.push(node);
|
---|
| 1049 |
|
---|
| 1050 | while (!tStack.empty())
|
---|
| 1051 | {
|
---|
| 1052 | BvhNode *node = tStack.top();
|
---|
| 1053 | tStack.pop();
|
---|
| 1054 |
|
---|
| 1055 | if (node->mIsVirtualLeaf)
|
---|
| 1056 | {
|
---|
| 1057 | leaves.push_back(node);
|
---|
| 1058 | }
|
---|
| 1059 | else if (!node->IsLeaf())
|
---|
| 1060 | {
|
---|
| 1061 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 1062 |
|
---|
| 1063 | tStack.push(interior->mFront);
|
---|
| 1064 | tStack.push(interior->mBack);
|
---|
| 1065 | }
|
---|
| 1066 | }
|
---|
[2760] | 1067 | }
|
---|
[2761] | 1068 |
|
---|
| 1069 |
|
---|
| 1070 | void Bvh::SetVirtualLeaves(int numTriangles)
|
---|
| 1071 | {
|
---|
[3064] | 1072 | // first invalidate old virtual leaves
|
---|
[2761] | 1073 | BvhNodeContainer leaves;
|
---|
[3065] | 1074 | CollectVirtualLeaves(mRoot, leaves);
|
---|
[2761] | 1075 |
|
---|
| 1076 | BvhNodeContainer::const_iterator bit, bit_end = leaves.end();
|
---|
| 1077 |
|
---|
| 1078 | for (bit = leaves.begin(); bit != bit_end; ++ bit)
|
---|
| 1079 | {
|
---|
| 1080 | (*bit)->mIsVirtualLeaf = false;
|
---|
| 1081 | }
|
---|
| 1082 |
|
---|
[2764] | 1083 | mNumVirtualNodes = 0;
|
---|
[3064] | 1084 | // assign new virtual leaves based on specified #triangles per leaf
|
---|
[2761] | 1085 | std::stack<BvhNode *> nodeStack;
|
---|
| 1086 | nodeStack.push(mRoot);
|
---|
| 1087 |
|
---|
| 1088 | while (!nodeStack.empty())
|
---|
| 1089 | {
|
---|
| 1090 | BvhNode *node = nodeStack.top();
|
---|
| 1091 | nodeStack.pop();
|
---|
| 1092 |
|
---|
[2764] | 1093 | ++ mNumVirtualNodes;
|
---|
| 1094 |
|
---|
[2761] | 1095 | if (node->IsLeaf())
|
---|
| 1096 | {
|
---|
| 1097 | BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
|
---|
| 1098 | leaf->mIsVirtualLeaf = true;
|
---|
| 1099 | }
|
---|
| 1100 | else
|
---|
| 1101 | {
|
---|
| 1102 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 1103 |
|
---|
| 1104 | BvhNode *f = interior->mFront;
|
---|
| 1105 | BvhNode *b = interior->mBack;
|
---|
| 1106 |
|
---|
[3064] | 1107 | if (node->mIsMaxDepthForVirtualLeaf ||
|
---|
| 1108 | (CountTriangles(node) <= numTriangles))
|
---|
[2761] | 1109 | {
|
---|
| 1110 | node->mIsVirtualLeaf = true;
|
---|
| 1111 | }
|
---|
| 1112 | else
|
---|
| 1113 | {
|
---|
| 1114 | nodeStack.push(interior->mBack);
|
---|
| 1115 | nodeStack.push(interior->mFront);
|
---|
| 1116 | }
|
---|
| 1117 | }
|
---|
| 1118 | }
|
---|
[2800] | 1119 |
|
---|
[3064] | 1120 | /// Reset the node states
|
---|
[2800] | 1121 | ResetNodeClassifications();
|
---|
[2761] | 1122 | }
|
---|
| 1123 |
|
---|
| 1124 |
|
---|
[3072] | 1125 | void Bvh::ComputeMaxDepthForVirtualLeaves()
|
---|
[2761] | 1126 | {
|
---|
[3072] | 1127 | // We initialize the maximal depth for virtual leaves
|
---|
[3064] | 1128 | // of this bvh, i.e., the nodes that are used as
|
---|
| 1129 | // leaves of the hierarchy during traversal.
|
---|
| 1130 |
|
---|
| 1131 | // Initially they are set either
|
---|
| 1132 | // a) to the real leaves of the hierarchy or
|
---|
| 1133 | // b) the point where the subdivision on object level ends
|
---|
| 1134 | // and the subsequent nodes are just used to provide tighter bounds
|
---|
| 1135 | // (see article for the notations)
|
---|
| 1136 |
|
---|
[2761] | 1137 | std::stack<BvhNode *> nodeStack;
|
---|
[3065] | 1138 | nodeStack.push(mRoot);
|
---|
[2761] | 1139 |
|
---|
| 1140 | while (!nodeStack.empty())
|
---|
| 1141 | {
|
---|
| 1142 | BvhNode *node = nodeStack.top();
|
---|
| 1143 | nodeStack.pop();
|
---|
| 1144 |
|
---|
| 1145 | if (node->IsLeaf())
|
---|
| 1146 | {
|
---|
| 1147 | node->mIsMaxDepthForVirtualLeaf = true;
|
---|
| 1148 | }
|
---|
| 1149 | else
|
---|
| 1150 | {
|
---|
| 1151 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 1152 |
|
---|
| 1153 | BvhNode *f = interior->mFront;
|
---|
| 1154 | BvhNode *b = interior->mBack;
|
---|
| 1155 |
|
---|
| 1156 | if ((f->mFirst == b->mFirst) && (f->mLast == b->mLast))
|
---|
| 1157 | {
|
---|
[3064] | 1158 | // point reached where beyond there would be no further reduction
|
---|
| 1159 | // as both subtrees contain the same objects => stop here
|
---|
| 1160 | // The tree beyond the current node is used to describe
|
---|
| 1161 | // tighter bounds on the geometry contained in it
|
---|
[2761] | 1162 | node->mIsMaxDepthForVirtualLeaf = true;
|
---|
| 1163 | }
|
---|
| 1164 | else
|
---|
| 1165 | {
|
---|
| 1166 | nodeStack.push(f);
|
---|
| 1167 | nodeStack.push(b);
|
---|
| 1168 | }
|
---|
| 1169 | }
|
---|
| 1170 | }
|
---|
| 1171 | }
|
---|
| 1172 |
|
---|
| 1173 |
|
---|
[3072] | 1174 | // this function must be called once after hierarchy creation
|
---|
| 1175 | void Bvh::PostProcess()
|
---|
| 1176 | {
|
---|
| 1177 | CreateRoot();
|
---|
| 1178 |
|
---|
| 1179 | ComputeMaxDepthForVirtualLeaves();
|
---|
| 1180 | // set virtual leaves for specified number of triangles
|
---|
| 1181 | SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES);
|
---|
| 1182 | /// for each node compute the number of leaves under this node
|
---|
| 1183 | UpdateNumLeaves(mRoot);
|
---|
[3074] | 1184 | // compute new ids
|
---|
| 1185 | ComputeIds();
|
---|
[3072] | 1186 | // specify bounds for occlusion tests
|
---|
| 1187 | RecomputeBounds();
|
---|
| 1188 |
|
---|
[3074] | 1189 | mBox = mRoot->GetBox();
|
---|
| 1190 |
|
---|
[3244] | 1191 | // compute and print stats for whole (static + dynamic) bvh
|
---|
[3072] | 1192 | ComputeBvhStats(mRoot, mBvhStats);
|
---|
| 1193 | PrintBvhStats(mBvhStats);
|
---|
| 1194 |
|
---|
[3244] | 1195 | // also output dynamic stats
|
---|
[3072] | 1196 | if (mDynamicGeometrySize)
|
---|
| 1197 | {
|
---|
| 1198 | BvhStats bvhStats;
|
---|
| 1199 | ComputeBvhStats(mDynamicRoot, bvhStats);
|
---|
| 1200 |
|
---|
| 1201 | cout << "\n=========== Dynamic BVH statistics: =========" << endl;
|
---|
| 1202 | cout << "leaves = " << bvhStats.mLeaves << endl;
|
---|
| 1203 | cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl;
|
---|
| 1204 | cout << "leafNodesSA = " << bvhStats.mLeafSA << endl;
|
---|
| 1205 | cout << "interiorNodesVolume = " << bvhStats.mInteriorVol << endl;
|
---|
| 1206 | cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl;
|
---|
| 1207 |
|
---|
| 1208 | cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl;
|
---|
| 1209 | cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl;
|
---|
| 1210 | cout << "=============================================" << endl << endl;
|
---|
| 1211 | }
|
---|
| 1212 | }
|
---|
| 1213 |
|
---|
| 1214 |
|
---|
[2763] | 1215 | void Bvh::RenderBoundingBoxImmediate(const AxisAlignedBox3 &box)
|
---|
| 1216 | {
|
---|
| 1217 | const Vector3 l = box.Min();
|
---|
| 1218 | const Vector3 u = box.Max();
|
---|
| 1219 |
|
---|
[3065] | 1220 | ///////////
|
---|
| 1221 | //-- render AABB as triangle strips
|
---|
| 1222 |
|
---|
[2764] | 1223 | glBegin(GL_TRIANGLE_STRIP);
|
---|
[2763] | 1224 |
|
---|
[3065] | 1225 | // render first half of AABB
|
---|
| 1226 | glVertex3f(l.x, l.y, u.z);
|
---|
| 1227 | glVertex3f(u.x, l.y, u.z);
|
---|
| 1228 | glVertex3f(l.x, u.y, u.z);
|
---|
| 1229 | glVertex3f(u.x, u.y, u.z);
|
---|
| 1230 | glVertex3f(l.x, u.y, l.z);
|
---|
| 1231 | glVertex3f(u.x, u.y, l.z);
|
---|
| 1232 | glVertex3f(l.x, l.y, l.z);
|
---|
| 1233 | glVertex3f(u.x, l.y, l.z);
|
---|
[2763] | 1234 |
|
---|
[3065] | 1235 | glPrimitiveRestartNV();
|
---|
[2763] | 1236 |
|
---|
[3065] | 1237 | // render second half of AABB
|
---|
| 1238 | glVertex3f(l.x, u.y, u.z);
|
---|
| 1239 | glVertex3f(l.x, u.y, l.z);
|
---|
| 1240 | glVertex3f(l.x, l.y, u.z);
|
---|
| 1241 | glVertex3f(l.x, l.y, l.z);
|
---|
| 1242 | glVertex3f(u.x, l.y, u.z);
|
---|
| 1243 | glVertex3f(u.x, l.y, l.z);
|
---|
| 1244 | glVertex3f(u.x, u.y, u.z);
|
---|
| 1245 | glVertex3f(u.x, u.y, l.z);
|
---|
| 1246 |
|
---|
[2764] | 1247 | glEnd();
|
---|
[2761] | 1248 | }
|
---|
[2763] | 1249 |
|
---|
[2790] | 1250 |
|
---|
| 1251 | static void RenderBoxForViz(const AxisAlignedBox3 &box)
|
---|
| 1252 | {
|
---|
| 1253 | glBegin(GL_LINE_LOOP);
|
---|
| 1254 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
| 1255 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
| 1256 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
| 1257 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
| 1258 | glEnd();
|
---|
| 1259 |
|
---|
| 1260 | glBegin(GL_LINE_LOOP);
|
---|
| 1261 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
| 1262 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
| 1263 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
| 1264 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
| 1265 | glEnd();
|
---|
| 1266 |
|
---|
| 1267 | glBegin(GL_LINE_LOOP);
|
---|
| 1268 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
| 1269 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
| 1270 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
| 1271 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
| 1272 | glEnd();
|
---|
| 1273 |
|
---|
| 1274 | glBegin(GL_LINE_LOOP);
|
---|
| 1275 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
| 1276 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
| 1277 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
| 1278 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
| 1279 | glEnd();
|
---|
| 1280 |
|
---|
| 1281 | glBegin(GL_LINE_LOOP);
|
---|
| 1282 | glVertex3d(box.Min().x, box.Min().y, box.Min().z);
|
---|
| 1283 | glVertex3d(box.Max().x, box.Min().y, box.Min().z);
|
---|
| 1284 | glVertex3d(box.Max().x, box.Min().y, box.Max().z);
|
---|
| 1285 | glVertex3d(box.Min().x, box.Min().y, box.Max().z);
|
---|
| 1286 | glEnd();
|
---|
| 1287 |
|
---|
| 1288 | glBegin(GL_LINE_LOOP);
|
---|
| 1289 | glVertex3d(box.Min().x, box.Max().y, box.Min().z);
|
---|
| 1290 | glVertex3d(box.Max().x, box.Max().y, box.Min().z);
|
---|
| 1291 | glVertex3d(box.Max().x, box.Max().y, box.Max().z);
|
---|
| 1292 | glVertex3d(box.Min().x, box.Max().y, box.Max().z);
|
---|
| 1293 |
|
---|
| 1294 | glEnd();
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 |
|
---|
[3066] | 1298 | static Technique GetVizTechnique()
|
---|
| 1299 | {
|
---|
| 1300 | Technique tech;
|
---|
| 1301 | tech.Init();
|
---|
| 1302 |
|
---|
| 1303 | //tech.SetLightingEnabled(false);
|
---|
| 1304 | //tech.SetDepthWriteEnabled(false);
|
---|
| 1305 |
|
---|
| 1306 | tech.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
|
---|
| 1307 | tech.SetDiffuse(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
|
---|
| 1308 | tech.SetAmbient(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
|
---|
| 1309 |
|
---|
| 1310 | return tech;
|
---|
| 1311 | }
|
---|
| 1312 |
|
---|
| 1313 |
|
---|
[3064] | 1314 | void Bvh::RenderBoundsForViz(BvhNode *node,
|
---|
| 1315 | RenderState *state,
|
---|
| 1316 | bool useTightBounds)
|
---|
[2790] | 1317 | {
|
---|
[3066] | 1318 | Technique *oldTech = state->GetState();
|
---|
| 1319 | // we set a simple material
|
---|
| 1320 | static Technique boxMat = GetVizTechnique();
|
---|
[2825] | 1321 | boxMat.Render(state);
|
---|
| 1322 |
|
---|
[2790] | 1323 | if (!useTightBounds)
|
---|
| 1324 | {
|
---|
[3245] | 1325 | #if 1
|
---|
[3066] | 1326 | RenderBoxForViz(node->GetBox());
|
---|
[3245] | 1327 | #else
|
---|
| 1328 | glPolygonMode(GL_FRONT, GL_LINE);
|
---|
[3065] | 1329 | RenderBoundingBoxImmediate(node->GetBox());
|
---|
[3245] | 1330 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
---|
| 1331 | #endif
|
---|
[2790] | 1332 | }
|
---|
| 1333 | else
|
---|
| 1334 | {
|
---|
[3203] | 1335 | for (int i = 0; i < node->mNumTestNodes; ++ i)
|
---|
[2790] | 1336 | {
|
---|
| 1337 | RenderBoxForViz(mTestNodes[node->mTestNodesIdx + i]->GetBox());
|
---|
[3203] | 1338 | }
|
---|
[2790] | 1339 | }
|
---|
[3066] | 1340 |
|
---|
| 1341 | if (oldTech) oldTech->Render(state);
|
---|
[2790] | 1342 | }
|
---|
| 1343 |
|
---|
| 1344 |
|
---|
| 1345 |
|
---|
[3053] | 1346 | ////////////////////////
|
---|
[3064] | 1347 | //-- functions for construction of the dynamic hierarchy
|
---|
[3053] | 1348 |
|
---|
| 1349 | int Bvh::SortTriangles(BvhLeaf *leaf,
|
---|
| 1350 | int axis,
|
---|
[3070] | 1351 | float position
|
---|
| 1352 | )
|
---|
[3053] | 1353 | {
|
---|
| 1354 | int i = leaf->mFirst;
|
---|
| 1355 | int j = leaf->mLast;
|
---|
| 1356 |
|
---|
[3070] | 1357 | while (1)
|
---|
[3053] | 1358 | {
|
---|
[3265] | 1359 | //while (mGeometry[i]->GetWorldCenter()[axis] < position) ++ i;
|
---|
| 1360 | //while (position < mGeometry[j]->GetWorldCenter()[axis]) -- j;
|
---|
[3070] | 1361 |
|
---|
[3265] | 1362 | while ((i < leaf->mLast) && (mGeometry[i]->GetWorldCenter()[axis] < position)) ++ i;
|
---|
| 1363 | while ((j > leaf->mFirst) && (position < mGeometry[j]->GetWorldCenter()[axis])) -- j;
|
---|
| 1364 |
|
---|
[3064] | 1365 | // sorting finished
|
---|
| 1366 | if (i >= j) break;
|
---|
| 1367 |
|
---|
| 1368 | // swap entities
|
---|
[3070] | 1369 | swap(mGeometry[i], mGeometry[j]);
|
---|
[3053] | 1370 |
|
---|
[3064] | 1371 | ++ i;
|
---|
| 1372 | -- j;
|
---|
[3053] | 1373 | }
|
---|
| 1374 |
|
---|
| 1375 | return j;
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
| 1378 |
|
---|
| 1379 | int Bvh::SortTrianglesSpatialMedian(BvhLeaf *leaf,
|
---|
[3070] | 1380 | int axis
|
---|
| 1381 | )
|
---|
[3053] | 1382 | {
|
---|
| 1383 | // spatial median
|
---|
| 1384 | float m = leaf->mBox.Center()[axis];
|
---|
[3070] | 1385 | return SortTriangles(leaf, axis, m);
|
---|
[3053] | 1386 | }
|
---|
| 1387 |
|
---|
| 1388 |
|
---|
| 1389 | BvhNode *Bvh::SubdivideLeaf(BvhLeaf *leaf,
|
---|
[3070] | 1390 | int parentAxis
|
---|
| 1391 | )
|
---|
[3053] | 1392 | {
|
---|
| 1393 | if (TerminationCriteriaMet(leaf))
|
---|
[3064] | 1394 | {
|
---|
[3265] | 1395 | if (leaf->CountPrimitives() == 0)
|
---|
| 1396 | cout << "error: leaf constructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl;
|
---|
| 1397 |
|
---|
[3053] | 1398 | return leaf;
|
---|
[3064] | 1399 | }
|
---|
| 1400 |
|
---|
[3070] | 1401 | //const int axis = (parentAxis + 1) % 3;
|
---|
| 1402 | const int axis = leaf->mBox.MajorAxis();
|
---|
[3053] | 1403 |
|
---|
[3070] | 1404 | const int scale = 20;
|
---|
| 1405 |
|
---|
[3053] | 1406 | // position of the split in the partailly sorted array of triangles
|
---|
| 1407 | // corresponding to this leaf
|
---|
| 1408 | int split = -1;
|
---|
| 1409 | float pos = -1.0f;
|
---|
[3070] | 1410 |
|
---|
[3053] | 1411 | // Spatial median subdivision
|
---|
[3070] | 1412 | split = SortTrianglesSpatialMedian(leaf, axis);
|
---|
[3053] | 1413 | pos = leaf->mBox.Center()[axis];
|
---|
| 1414 |
|
---|
[3265] | 1415 | //if ((split >= leaf->mLast) || (split < leaf->mFirst))
|
---|
[3053] | 1416 | if (split == leaf->mLast)
|
---|
| 1417 | {
|
---|
| 1418 | // no split could be achieved => just halve number of objects
|
---|
[3265] | 1419 | split = (leaf->mLast + leaf->mFirst) / 2;
|
---|
| 1420 | cerr << "no reduction " << leaf->CountPrimitives() << " " << leaf->mFirst << " " << leaf->mLast << " " << split << endl;
|
---|
[3053] | 1421 | }
|
---|
| 1422 |
|
---|
| 1423 | // create two more nodes
|
---|
| 1424 | mNumNodes += 2;
|
---|
| 1425 | BvhInterior *parent = new BvhInterior(leaf->GetParent());
|
---|
[3072] | 1426 | parent->mFirst = leaf->mFirst;
|
---|
| 1427 | parent->mLast = leaf->mLast;
|
---|
[3053] | 1428 |
|
---|
| 1429 | parent->mAxis = axis;
|
---|
| 1430 | parent->mBox = leaf->mBox;
|
---|
| 1431 | parent->mDepth = leaf->mDepth;
|
---|
| 1432 |
|
---|
[3072] | 1433 | BvhLeaf *front = new BvhLeaf(parent);
|
---|
| 1434 |
|
---|
[3053] | 1435 | parent->mBack = leaf;
|
---|
| 1436 | parent->mFront = front;
|
---|
[3072] | 1437 |
|
---|
[3053] | 1438 | // now assign the triangles to the subnodes
|
---|
| 1439 | front->mFirst = split + 1;
|
---|
| 1440 | front->mLast = leaf->mLast;
|
---|
| 1441 | front->mDepth = leaf->mDepth + 1;
|
---|
[3064] | 1442 |
|
---|
[3265] | 1443 | if (leaf->mFirst > leaf->mLast)
|
---|
| 1444 | cerr << "erorr!!! " << leaf->CountPrimitives() << " " << leaf->mFirst << " " << leaf->mLast << " " << split << endl;
|
---|
| 1445 |
|
---|
[3053] | 1446 | leaf->mLast = split;
|
---|
| 1447 | leaf->mDepth = front->mDepth;
|
---|
| 1448 | leaf->mParent = parent;
|
---|
| 1449 |
|
---|
[3262] | 1450 | front->mBox = SceneEntity::ComputeBoundingBox(mGeometry + front->mFirst, front->CountPrimitives());
|
---|
| 1451 | leaf->mBox = SceneEntity::ComputeBoundingBox(mGeometry + leaf->mFirst, leaf->CountPrimitives());
|
---|
[3070] | 1452 |
|
---|
[3053] | 1453 | // recursively continue subdivision
|
---|
[3070] | 1454 | parent->mBack = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mBack), axis);
|
---|
| 1455 | parent->mFront = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mFront), axis);
|
---|
[3053] | 1456 |
|
---|
| 1457 | return parent;
|
---|
| 1458 | }
|
---|
| 1459 |
|
---|
| 1460 |
|
---|
| 1461 | bool Bvh::TerminationCriteriaMet(BvhLeaf *leaf) const
|
---|
| 1462 | {
|
---|
| 1463 | const bool criteriaMet =
|
---|
| 1464 | (leaf->mDepth > mMaxDepthForDynamicBranch) ||
|
---|
[3265] | 1465 | (leaf->CountPrimitives() <= 1);
|
---|
[3053] | 1466 |
|
---|
| 1467 | return criteriaMet;
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
| 1470 |
|
---|
[3070] | 1471 | void Bvh::UpdateDynamicBranch(BvhNode *node)
|
---|
[3053] | 1472 | {
|
---|
[3070] | 1473 | if (node->IsLeaf())
|
---|
| 1474 | {
|
---|
| 1475 | int numEntities;
|
---|
| 1476 | SceneEntity **entities = GetGeometry(node, numEntities);
|
---|
| 1477 |
|
---|
[3262] | 1478 | node->mBox = SceneEntity::ComputeBoundingBox(entities, numEntities);
|
---|
[3070] | 1479 | //cout << "box: " << node->mBox << endl;
|
---|
| 1480 | }
|
---|
| 1481 | else
|
---|
| 1482 | {
|
---|
| 1483 | BvhNode *f = static_cast<BvhInterior *>(node)->GetFront();
|
---|
| 1484 | BvhNode *b = static_cast<BvhInterior *>(node)->GetBack();
|
---|
| 1485 |
|
---|
| 1486 | UpdateDynamicBranch(f);
|
---|
| 1487 | UpdateDynamicBranch(b);
|
---|
| 1488 |
|
---|
| 1489 | node->mBox = f->mBox;
|
---|
| 1490 | node->mBox.Include(b->mBox);
|
---|
| 1491 | }
|
---|
| 1492 | }
|
---|
| 1493 |
|
---|
| 1494 |
|
---|
| 1495 | void Bvh::CreateDynamicBranch()
|
---|
| 1496 | {
|
---|
[3069] | 1497 | // the bvh has two main branches
|
---|
[3262] | 1498 | // a static branch (the old root), and a dynamic branch
|
---|
[3069] | 1499 | // we create a 'dynamic' leaf which basically is a container
|
---|
| 1500 | // for all dynamic objects underneath
|
---|
| 1501 |
|
---|
| 1502 | // the bounding boxes of the dynamic tree must be updated
|
---|
| 1503 | // once each frame in order to be able to incorporate
|
---|
| 1504 | // the movements of the objects within
|
---|
| 1505 |
|
---|
[3264] | 1506 | cout << "creating dynamic bvh branch" << endl;
|
---|
| 1507 |
|
---|
[3069] | 1508 | DEL_PTR(mDynamicRoot);
|
---|
[3264] | 1509 | -- mNumNodes;
|
---|
[3053] | 1510 |
|
---|
[3265] | 1511 | #if 1
|
---|
[3264] | 1512 | BvhConstructor bvhConstructor(mGeometry, (int)mStaticGeometrySize, (int)mGeometrySize - 1);
|
---|
| 1513 |
|
---|
| 1514 | int numNodes;
|
---|
| 1515 | mDynamicRoot = bvhConstructor.Construct(numNodes);
|
---|
| 1516 |
|
---|
| 1517 | mNumNodes += numNodes;
|
---|
| 1518 |
|
---|
| 1519 | #else
|
---|
[3265] | 1520 |
|
---|
[3070] | 1521 | BvhLeaf *l = new BvhLeaf(mRoot);
|
---|
[3072] | 1522 | mDynamicRoot = l;
|
---|
[3068] | 1523 |
|
---|
[3262] | 1524 | l->mBox = SceneEntity::ComputeBoundingBox(mGeometry + mStaticGeometrySize,
|
---|
| 1525 | (int)mDynamicGeometrySize);
|
---|
[3070] | 1526 |
|
---|
[3072] | 1527 | l->mFirst = (int)mStaticGeometrySize;
|
---|
[3070] | 1528 | l->mLast = (int)mGeometrySize - 1;
|
---|
| 1529 | l->mArea = l->mBox.SurfaceArea();
|
---|
[3069] | 1530 |
|
---|
[3072] | 1531 | if (mDynamicGeometrySize)
|
---|
[3262] | 1532 | {
|
---|
[3072] | 1533 | mDynamicRoot = SubdivideLeaf(l, 0);
|
---|
[3262] | 1534 | }
|
---|
[3264] | 1535 | #endif
|
---|
[3053] | 1536 | }
|
---|
| 1537 |
|
---|
[3064] | 1538 |
|
---|
[3065] | 1539 | bool Bvh::IntersectsNearPlane(BvhNode *node) const
|
---|
| 1540 | {
|
---|
[3068] | 1541 | // note: we have problems with large scale object penetrating the near plane
|
---|
| 1542 | // (e.g., objects in the distance which are always handled to be visible)
|
---|
| 1543 | // especially annoying is this problem when using the frustum
|
---|
| 1544 | // fitting on the visible objects for shadow mapping
|
---|
[3072] | 1545 | // but don't see how to solve this issue without using costly calculations
|
---|
[3068] | 1546 |
|
---|
[3065] | 1547 | // we stored the near plane distance => we can use it also here
|
---|
[3068] | 1548 | float distanceToNearPlane = node->GetDistance();
|
---|
| 1549 | //float distanceToNearPlane = node->GetBox().GetMinDistance(sNearPlane);
|
---|
[3065] | 1550 |
|
---|
[3068] | 1551 | return (distanceToNearPlane < sNear);
|
---|
[3065] | 1552 | }
|
---|
| 1553 |
|
---|
| 1554 |
|
---|
[3072] | 1555 | void Bvh::CreateRoot()
|
---|
| 1556 | {
|
---|
| 1557 | // create new root
|
---|
| 1558 | mRoot = new BvhInterior(NULL);
|
---|
[3074] | 1559 |
|
---|
[3072] | 1560 | // the separation is a purely logical one
|
---|
| 1561 | // the bounding boxes of the child nodes are
|
---|
| 1562 | // identical to those of the root node
|
---|
| 1563 |
|
---|
| 1564 | mRoot->mBox = mStaticRoot->mBox;
|
---|
| 1565 | mRoot->mBox.Include(mDynamicRoot->mBox);
|
---|
| 1566 |
|
---|
| 1567 | mRoot->mArea = mRoot->mBox.SurfaceArea();
|
---|
| 1568 |
|
---|
| 1569 | mRoot->mFirst = 0;
|
---|
| 1570 | mRoot->mLast = (int)mGeometrySize - 1;
|
---|
| 1571 |
|
---|
[3127] | 1572 | //cout<<"f: " << mRoot->mFirst<< " l: " <<mRoot->mLast << endl;
|
---|
[3072] | 1573 | // add static root on left subtree
|
---|
| 1574 | mRoot->mFront = mStaticRoot;
|
---|
| 1575 | mStaticRoot->mParent = mRoot;
|
---|
| 1576 |
|
---|
| 1577 | // add dynamic root on left subtree
|
---|
| 1578 | mRoot->mBack = mDynamicRoot;
|
---|
| 1579 | mDynamicRoot->mParent = mRoot;
|
---|
| 1580 | }
|
---|
[3259] | 1581 |
|
---|
[3072] | 1582 |
|
---|
[2764] | 1583 | } |
---|