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