1 | #include <queue>
|
---|
2 | #include <stack>
|
---|
3 | #include <fstream>
|
---|
4 | #include <iostream>
|
---|
5 | #include <iomanip>
|
---|
6 |
|
---|
7 | #include "Bvh.h"
|
---|
8 | #include "Camera.h"
|
---|
9 | #include "Plane3.h"
|
---|
10 | #include "glInterface.h"
|
---|
11 | #include "Triangle3.h"
|
---|
12 | #include "SceneEntity.h"
|
---|
13 | #include "Geometry.h"
|
---|
14 |
|
---|
15 | namespace CHCDemo
|
---|
16 | {
|
---|
17 |
|
---|
18 | #define INVALID_TEST ((unsigned int)-1)
|
---|
19 |
|
---|
20 | const static bool useVbos = true;
|
---|
21 | unsigned int Bvh::sCurrentVboId = -1;
|
---|
22 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 |
|
---|
26 |
|
---|
27 | /*
|
---|
28 | 3 x---------x 2
|
---|
29 | |\ \
|
---|
30 | | \ \
|
---|
31 | | \ \
|
---|
32 | | 4 x---------x 5
|
---|
33 | | | |
|
---|
34 | 0 x | x 1 |
|
---|
35 | \ | |
|
---|
36 | \ | |
|
---|
37 | \| |
|
---|
38 | 7 x---------x 6
|
---|
39 | */
|
---|
40 |
|
---|
41 | static unsigned int sIndices[] =
|
---|
42 | {7, // degenerated
|
---|
43 | 7, 6, 4, 5, 3, 2, 0, 1,
|
---|
44 | 1, 4, // degenerated
|
---|
45 | 4, 3, 7, 0, 6, 1, 5, 2,
|
---|
46 | 2 // degenerated
|
---|
47 | };
|
---|
48 |
|
---|
49 |
|
---|
50 | const static int sNumIndicesPerBox = 20;
|
---|
51 |
|
---|
52 | /* Order of vertices
|
---|
53 | 0 = (0, 0, 0)
|
---|
54 | 1 = (1, 0, 0)
|
---|
55 | 2 = (1, 1, 0)
|
---|
56 | 3 = (0, 1, 0)
|
---|
57 | 4 = (0, 1, 1)
|
---|
58 | 5 = (1, 1, 1)
|
---|
59 | 6 = (1, 0, 1)
|
---|
60 | 7 = (0, 0, 1)
|
---|
61 | */
|
---|
62 |
|
---|
63 | static Plane3 sNearPlane;
|
---|
64 | static Camera::Frustum sFrustum;
|
---|
65 |
|
---|
66 | /// these values are valid for all nodes
|
---|
67 | static int sClipPlaneAABBVertexIndices[12];
|
---|
68 |
|
---|
69 |
|
---|
70 | BvhNode::BvhNode(BvhNode *parent):
|
---|
71 | mParent(parent),
|
---|
72 | mAxis(-1),
|
---|
73 | mDepth(0),
|
---|
74 | mPlaneMask(0),
|
---|
75 | mPreferredPlane(0),
|
---|
76 | mLastRenderedFrame(-999),
|
---|
77 | mFirst(-1),
|
---|
78 | mLast(-1),
|
---|
79 | mNumTestNodes(1),
|
---|
80 | mTestNodesIdx(-1),
|
---|
81 | mIndicesPtr(-1),
|
---|
82 | mIndices(NULL),
|
---|
83 | mId(0)
|
---|
84 | {
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | void BvhNode::ResetVisibility()
|
---|
89 | {
|
---|
90 | mVisibility.Reset();
|
---|
91 |
|
---|
92 | mLastRenderedFrame = -999;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | BvhNode::~BvhNode()
|
---|
97 | {
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | void BvhNode::VisibilityInfo::Reset()
|
---|
102 | {
|
---|
103 | mIsVisible = false;
|
---|
104 |
|
---|
105 | mAssumedVisibleFrames = 0;
|
---|
106 |
|
---|
107 | mLastVisitedFrame = -1;
|
---|
108 |
|
---|
109 | mTimesInvisible = 0;
|
---|
110 | mIsFrustumCulled = false;
|
---|
111 |
|
---|
112 | mIsNew = true;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | BvhLeaf::~BvhLeaf()
|
---|
117 | {
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /***********************************************************/
|
---|
122 | /* class Bvh implementation */
|
---|
123 | /***********************************************************/
|
---|
124 |
|
---|
125 |
|
---|
126 | Bvh::Bvh():
|
---|
127 | mCamera(NULL),
|
---|
128 | mFrameId(-1),
|
---|
129 | mRoot(NULL),
|
---|
130 | mVertices(NULL),
|
---|
131 | mIndices(NULL),
|
---|
132 | mTestIndices(NULL),
|
---|
133 | mCurrentIndicesPtr(0)
|
---|
134 | {}
|
---|
135 |
|
---|
136 | /*
|
---|
137 | Bvh::Bvh(const GeometryVector &geometry,
|
---|
138 | DistanceSortedRenderAction *const renderer):
|
---|
139 | mCamera(NULL),
|
---|
140 | mFrameId(-1),
|
---|
141 | mRoot(NULL),
|
---|
142 | mVertices(NULL),
|
---|
143 | mIndices(NULL),
|
---|
144 | mTestIndices(NULL),
|
---|
145 | mCurrentIndicesPtr(0),
|
---|
146 | {
|
---|
147 | mGeometry = new NodeGeometry*[geometry.size()];
|
---|
148 | mGeometrySize = geometry.size();
|
---|
149 |
|
---|
150 | BoundingBox sceneBox;
|
---|
151 |
|
---|
152 | // compute scene extent
|
---|
153 | for (size_t i = 0; i < mGeometrySize; ++ i)
|
---|
154 | {
|
---|
155 | mGeometry[i] = geometry[i];
|
---|
156 | sceneBox.combine(&mGeometry[i]->mBox);
|
---|
157 |
|
---|
158 | mBvhStats.mTriangles += mGeometry[i]->mNumTriangles;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | ////////
|
---|
163 | //-- create root
|
---|
164 |
|
---|
165 | BvhLeaf *leaf = new BvhLeaf(NULL);
|
---|
166 | leaf->mDepth = 0;
|
---|
167 | leaf->mFirst = 0;
|
---|
168 | leaf->mLast = (int)geometry.size() - 1;
|
---|
169 | leaf->mBox = sceneBox;
|
---|
170 |
|
---|
171 | OUT1("geometry in root: " << leaf->mLast);
|
---|
172 |
|
---|
173 | mRoot = leaf;
|
---|
174 | mNumNodes = 1;
|
---|
175 |
|
---|
176 | // parameters
|
---|
177 | mMaxGeometry = Settings::Global()->get_nvocc_bvh_max_objects();
|
---|
178 | mMaxTriangles = Settings::Global()->get_nvocc_bvh_max_triangles();
|
---|
179 | mMaxDepth = Settings::Global()->get_nvocc_bvh_max_depth();
|
---|
180 | mSplitType = Settings::Global()->get_nvocc_bvh_split_type();
|
---|
181 |
|
---|
182 | float sceneArea = sceneBox.getSurface();
|
---|
183 | float minAreaRatio = Settings::Global()->get_nvocc_bvh_min_area();
|
---|
184 |
|
---|
185 | mMinArea = minAreaRatio * sceneArea;
|
---|
186 |
|
---|
187 | mMaxDepthForTestingChildren = Settings::Global()->get_nvocc_bvh_max_depth_for_testing_children();
|
---|
188 |
|
---|
189 | mAreaRatioThreshold = Settings::Global()->get_nvocc_area_ratio_threshold();
|
---|
190 | mVolRatioThreshold = Settings::Global()->get_nvocc_vol_ratio_threshold();
|
---|
191 | }
|
---|
192 | */
|
---|
193 |
|
---|
194 | Bvh::~Bvh()
|
---|
195 | {
|
---|
196 | if (mVertices) delete []mVertices;
|
---|
197 | if (mIndices) delete [] mIndices;
|
---|
198 | if (mTestIndices) delete [] mTestIndices;
|
---|
199 | if (mGeometry) delete [] mGeometry;
|
---|
200 |
|
---|
201 | if (mRoot) delete mRoot;
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | void Bvh::PullUpLastVisited(BvhNode *node, const int frameId) const
|
---|
206 | {
|
---|
207 | BvhNode *parent = node->GetParent();
|
---|
208 |
|
---|
209 | while (parent && (parent->GetLastVisitedFrame() != frameId))
|
---|
210 | {
|
---|
211 | parent->SetLastVisitedFrame(frameId);
|
---|
212 | parent = parent->GetParent();
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | void Bvh::MakeParentsVisible(BvhNode *node)
|
---|
218 | {
|
---|
219 | BvhNode *parent = node->GetParent();
|
---|
220 |
|
---|
221 | while (parent && (!parent->IsVisible()))
|
---|
222 | {
|
---|
223 | parent->SetVisible(true);
|
---|
224 | parent = parent->GetParent();
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | void Bvh::CollectLeaves(BvhNode *node, BvhLeafContainer &leaves)
|
---|
230 | {
|
---|
231 | stack<BvhNode *> tStack;
|
---|
232 | tStack.push(node);
|
---|
233 |
|
---|
234 | while (!tStack.empty())
|
---|
235 | {
|
---|
236 | BvhNode *node = tStack.top();
|
---|
237 |
|
---|
238 | tStack.pop();
|
---|
239 |
|
---|
240 | if (!node->IsLeaf())
|
---|
241 | {
|
---|
242 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
243 |
|
---|
244 | tStack.push(interior->mFront);
|
---|
245 | tStack.push(interior->mBack);
|
---|
246 | }
|
---|
247 | else
|
---|
248 | {
|
---|
249 | leaves.push_back(static_cast<BvhLeaf *>(node));
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | void Bvh::CollectNodes(BvhNode *node, BvhNodeContainer &nodes)
|
---|
256 | {
|
---|
257 | stack<BvhNode *> tStack;
|
---|
258 |
|
---|
259 | tStack.push(node);
|
---|
260 |
|
---|
261 | while (!tStack.empty())
|
---|
262 | {
|
---|
263 | BvhNode *node = tStack.top();
|
---|
264 | tStack.pop();
|
---|
265 |
|
---|
266 | nodes.push_back(node);
|
---|
267 |
|
---|
268 | if (!node->IsLeaf())
|
---|
269 | {
|
---|
270 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
271 |
|
---|
272 | tStack.push(interior->mFront);
|
---|
273 | tStack.push(interior->mBack);
|
---|
274 | }
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | typedef pair<BvhNode *, int> tPair;
|
---|
280 |
|
---|
281 | void Bvh::CollectNodes(BvhNode *root, BvhNodeContainer &nodes, int depth)
|
---|
282 | {
|
---|
283 | stack<tPair> tStack;
|
---|
284 | tStack.push(tPair(root, 0));
|
---|
285 |
|
---|
286 | while (!tStack.empty())
|
---|
287 | {
|
---|
288 | BvhNode *node = tStack.top().first;
|
---|
289 | const int d = tStack.top().second;
|
---|
290 |
|
---|
291 | tStack.pop();
|
---|
292 |
|
---|
293 | // found depth => take this node
|
---|
294 | if ((d == depth) || (node->IsLeaf()))
|
---|
295 | {
|
---|
296 | nodes.push_back(node);
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
301 |
|
---|
302 | tStack.push(tPair(interior->mFront, d + 1));
|
---|
303 | tStack.push(tPair(interior->mBack, d + 1));
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | SceneEntity **Bvh::GetGeometry(BvhNode *node, int &geometrySize)
|
---|
310 | {
|
---|
311 | geometrySize = node->CountPrimitives();
|
---|
312 | return mGeometry + node->mFirst;
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | int Bvh::IsWithinViewFrustum(BvhNode *node)
|
---|
317 | {
|
---|
318 | bool bIntersect = false;
|
---|
319 |
|
---|
320 | if (node->GetParent())
|
---|
321 | node->mPlaneMask = node->GetParent()->mPlaneMask;
|
---|
322 |
|
---|
323 |
|
---|
324 | ////////
|
---|
325 | //-- do the view frustum culling for the planes [mPreferredPlane - 5]
|
---|
326 |
|
---|
327 | for (int i = node->mPreferredPlane; i < 6; ++ i)
|
---|
328 | {
|
---|
329 | //-- do the test only if necessary
|
---|
330 | if (node->mPlaneMask & (1 << i))
|
---|
331 | {
|
---|
332 | //-- test the n-vertex
|
---|
333 | if (node->mBox.GetDistance(sClipPlaneAABBVertexIndices[i * 2 + 0], sFrustum.mClipPlanes[i]) > 0.0f)
|
---|
334 | {
|
---|
335 | //-- outside
|
---|
336 | node->mPreferredPlane = i;
|
---|
337 | return 0;
|
---|
338 | }
|
---|
339 |
|
---|
340 | //-- test the p-vertex
|
---|
341 | if (node->mBox.GetDistance(sClipPlaneAABBVertexIndices[i * 2 + 1], sFrustum.mClipPlanes[i]) <= 0.0f)
|
---|
342 | {
|
---|
343 | //-- completely inside: children don't need to check against this plane no more
|
---|
344 | node->mPlaneMask^= 1 << i;
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | bIntersect = true;
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | //////////
|
---|
354 | //-- do the view frustum culling for the planes [0 - m_iPreferredPlane)
|
---|
355 |
|
---|
356 | for (int i = 0; i < node->mPreferredPlane; ++ i)
|
---|
357 | {
|
---|
358 | // do the test only if necessary
|
---|
359 | if (node->mPlaneMask & (1 << i))
|
---|
360 | {
|
---|
361 | //-- test the n-vertex
|
---|
362 | if (node->mBox.GetDistance(sClipPlaneAABBVertexIndices[i * 2 + 0], sFrustum.mClipPlanes[i]) > 0.0f)
|
---|
363 | {
|
---|
364 | // outside
|
---|
365 | node->mPreferredPlane = i;
|
---|
366 | return 0;
|
---|
367 | }
|
---|
368 |
|
---|
369 | //-- test the p-vertex
|
---|
370 | if (node->mBox.GetDistance(sClipPlaneAABBVertexIndices[i * 2 + 1], sFrustum.mClipPlanes[i]) <= 0.0f)
|
---|
371 | {
|
---|
372 | // completely inside: children don't need to check against this plane no more
|
---|
373 | node->mPlaneMask^= 1 << i;
|
---|
374 | }
|
---|
375 | else
|
---|
376 | {
|
---|
377 | bIntersect = true;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | return bIntersect ? -1 : 1;
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | void Bvh::InitFrame(Camera *camera, int currentFrameId)
|
---|
387 | {
|
---|
388 | // = 0011 1111 which means that at the beginning, all six planes have to frustum culled
|
---|
389 | mRoot->mPlaneMask = 0x3f;
|
---|
390 | mCamera = camera;
|
---|
391 |
|
---|
392 | mFrameId = currentFrameId;
|
---|
393 |
|
---|
394 | mCamera->CalcFrustum(sFrustum);
|
---|
395 | sFrustum.CalcNPVertexIndices(sClipPlaneAABBVertexIndices);
|
---|
396 |
|
---|
397 | // calc near plane
|
---|
398 | sNearPlane = Plane3(mCamera->GetDirection(),
|
---|
399 | -mCamera->GetDirection() * mCamera->GetPosition());
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | float Bvh::CalcDistance(BvhNode *node) const
|
---|
404 | {
|
---|
405 | return node->GetBox().GetMinVisibleDistance(sNearPlane);
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | void Bvh::RenderBoundingBox(BvhNode *node)
|
---|
410 | {
|
---|
411 | static BvhNodeContainer dummy(1);
|
---|
412 | dummy[0] = node;
|
---|
413 | RenderBoundingBoxes(dummy);
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 |
|
---|
418 | int Bvh::RenderBoundingBoxes(const BvhNodeContainer &nodes)
|
---|
419 | {
|
---|
420 | int renderedBoxes = PrepareBoundingBoxesWithDrawArrays(nodes);
|
---|
421 | RenderBoundingBoxesWithDrawArrays(renderedBoxes);
|
---|
422 |
|
---|
423 | return renderedBoxes;
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | int Bvh::PrepareBoundingBoxesWithDrawArrays(const BvhNodeContainer &nodes)
|
---|
428 | {
|
---|
429 | //////
|
---|
430 | //-- for the first time we come here ...
|
---|
431 |
|
---|
432 | if (!mIndices)
|
---|
433 | { // create list of indices
|
---|
434 | CreateIndices();
|
---|
435 | }
|
---|
436 |
|
---|
437 | if (sCurrentVboId == -1)
|
---|
438 | {
|
---|
439 | // prepare the vbo
|
---|
440 | PrepareVertices();
|
---|
441 | }
|
---|
442 |
|
---|
443 | ///////////////
|
---|
444 |
|
---|
445 | int numNodes = 0;
|
---|
446 |
|
---|
447 | BvhNodeContainer::const_iterator nit, nit_end = nodes.end();
|
---|
448 |
|
---|
449 | for (nit = nodes.begin(); nit != nit_end; ++ nit)
|
---|
450 | {
|
---|
451 | BvhNode *node = *nit;
|
---|
452 |
|
---|
453 | const int numIndices = node->mNumTestNodes * sNumIndicesPerBox;
|
---|
454 |
|
---|
455 | // copy indices
|
---|
456 | memcpy(mIndices + numNodes * sNumIndicesPerBox,
|
---|
457 | mTestIndices + node->mIndicesPtr,
|
---|
458 | #if 0 //ALIGN_INDICES
|
---|
459 | ((numIndices / 32) * 32 + 32) * sizeof(unsigned int));
|
---|
460 | #else
|
---|
461 | numIndices * sizeof(unsigned int));
|
---|
462 | #endif
|
---|
463 | numNodes += node->mNumTestNodes;
|
---|
464 | }
|
---|
465 |
|
---|
466 | return numNodes;
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | void Bvh::RenderBoundingBoxesWithDrawArrays(int numNodes)
|
---|
471 | {
|
---|
472 | //////
|
---|
473 | //-- Rendering the vbo
|
---|
474 | if (useVbos)
|
---|
475 | // set the vertex pointer to the vertex buffer
|
---|
476 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
477 | else
|
---|
478 | glVertexPointer(3, GL_FLOAT, 0, mVertices);
|
---|
479 |
|
---|
480 | // we do use the last or the first index (they are generate and only used to connect strips)
|
---|
481 | const int numElements = numNodes * sNumIndicesPerBox - 1;
|
---|
482 |
|
---|
483 | // don't render first degenerate index
|
---|
484 | glDrawElements(GL_TRIANGLE_STRIP, numElements, GL_UNSIGNED_INT, mIndices + 1);
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | #define ALIGN_INDICES 1
|
---|
489 |
|
---|
490 | void Bvh::CreateIndices()
|
---|
491 | {
|
---|
492 | // collect bvh nodes
|
---|
493 | BvhNodeContainer nodes;
|
---|
494 | CollectNodes(mRoot, nodes);
|
---|
495 |
|
---|
496 | cout << "creating new indices" << endl;
|
---|
497 |
|
---|
498 | int numMaxIndices = 0;
|
---|
499 |
|
---|
500 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
501 |
|
---|
502 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
503 | {
|
---|
504 | int offset = (*lit)->mNumTestNodes * sNumIndicesPerBox;
|
---|
505 | #if ALIGN_INDICES
|
---|
506 | // align with 32
|
---|
507 | offset = (offset / 32) * 32 + 32;
|
---|
508 | #endif
|
---|
509 | numMaxIndices += offset;
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 | cout << "creating global indices buffer" << endl;
|
---|
514 |
|
---|
515 | if (mIndices) delete [] mIndices;
|
---|
516 | if (mTestIndices) delete [] mTestIndices;
|
---|
517 |
|
---|
518 | // global buffer: create it once so we don't have
|
---|
519 | // to allocate memory from the chunks of the node
|
---|
520 | mIndices = new unsigned int[numMaxIndices];
|
---|
521 |
|
---|
522 | // create new index buffer for the individual nodes
|
---|
523 | mTestIndices = new unsigned int[numMaxIndices];
|
---|
524 |
|
---|
525 | mCurrentIndicesPtr = 0;
|
---|
526 |
|
---|
527 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
528 | {
|
---|
529 | BvhNode *node = *lit;
|
---|
530 |
|
---|
531 | // resize array
|
---|
532 | node->mIndicesPtr = mCurrentIndicesPtr;
|
---|
533 |
|
---|
534 | int numIndices = 0;
|
---|
535 |
|
---|
536 | // the bounding box of the test nodes are rendered instead of the root node
|
---|
537 | // => store their indices
|
---|
538 | for (int i = 0; i < node->mNumTestNodes; ++ i, numIndices += sNumIndicesPerBox)
|
---|
539 | {
|
---|
540 | BvhNode *testNode = mTestNodes[node->mTestNodesIdx + i];
|
---|
541 |
|
---|
542 | // add indices to root node
|
---|
543 | for (int j = 0; j < sNumIndicesPerBox; ++ j)
|
---|
544 | {
|
---|
545 | mTestIndices[mCurrentIndicesPtr + numIndices + j] = sIndices[j] + testNode->GetId() * 8;
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | // align with 32
|
---|
550 | #if ALIGN_INDICES
|
---|
551 | const int offset = (numIndices / 32) * 32 + 32;
|
---|
552 | #else
|
---|
553 | const int offset = numIndices;
|
---|
554 | #endif
|
---|
555 | mCurrentIndicesPtr += offset;
|
---|
556 | }
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | void Bvh::ComputeIds()
|
---|
561 | {
|
---|
562 | // collect all nodes
|
---|
563 | BvhNodeContainer nodes;
|
---|
564 | CollectNodes(mRoot, nodes);
|
---|
565 |
|
---|
566 | // assign ids to all nodes of the "regular" hierarchy
|
---|
567 | int i = 0;
|
---|
568 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
569 |
|
---|
570 | for (lit = nodes.begin(); lit != lit_end; ++ lit, ++ i)
|
---|
571 | {
|
---|
572 | (*lit)->SetId(i);
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | void Bvh::PrepareVertices()
|
---|
578 | {
|
---|
579 | // collect all nodes
|
---|
580 | BvhNodeContainer nodes;
|
---|
581 |
|
---|
582 | nodes.reserve(GetNumNodes());
|
---|
583 | CollectNodes(mRoot, nodes);
|
---|
584 |
|
---|
585 | const unsigned int bufferSize = 8 * (int)nodes.size();
|
---|
586 | mVertices = new Vector3[bufferSize];
|
---|
587 |
|
---|
588 | int i = 0;
|
---|
589 |
|
---|
590 | // store bounding box vertices
|
---|
591 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
592 |
|
---|
593 | for (lit = nodes.begin(); lit != lit_end; ++ lit, i += 8)
|
---|
594 | {
|
---|
595 | BvhNode *node = *lit;
|
---|
596 |
|
---|
597 | for (int j = 0; j < 8; ++ j)
|
---|
598 | ((Vector3 *)mVertices)[node->GetId() * 8 + j] = node->GetBox().GetVertex(j);
|
---|
599 | }
|
---|
600 |
|
---|
601 | if (useVbos)
|
---|
602 | {
|
---|
603 | glGenBuffersARB(1, &sCurrentVboId);
|
---|
604 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, sCurrentVboId);
|
---|
605 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
606 |
|
---|
607 | glBufferDataARB(GL_ARRAY_BUFFER_ARB,
|
---|
608 | bufferSize * sizeof(Vector3),
|
---|
609 | mVertices,
|
---|
610 | GL_STATIC_DRAW_ARB);
|
---|
611 |
|
---|
612 | //glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
613 |
|
---|
614 | // data handled by graphics driver from now on
|
---|
615 | DEL_PTR(mVertices);
|
---|
616 |
|
---|
617 | cout << "***** created vbos *********" << endl;
|
---|
618 | }
|
---|
619 | else
|
---|
620 | {
|
---|
621 | sCurrentVboId = 0;
|
---|
622 | glVertexPointer(3, GL_FLOAT, 0, mVertices);
|
---|
623 |
|
---|
624 | cout << "******* created vertex arrays ********" << endl;
|
---|
625 | }
|
---|
626 | }
|
---|
627 |
|
---|
628 |
|
---|
629 | void Bvh::SetMaxDepthForTestingChildren(int maxDepth)
|
---|
630 | {
|
---|
631 | if (maxDepth != mMaxDepthForTestingChildren)
|
---|
632 | {
|
---|
633 | mMaxDepthForTestingChildren = maxDepth;
|
---|
634 | RecomputeBounds();
|
---|
635 | }
|
---|
636 | }
|
---|
637 |
|
---|
638 |
|
---|
639 | void Bvh::SetAreaRatioThresholdForTestingChildren(float ratio)
|
---|
640 | {
|
---|
641 | if (ratio != mAreaRatioThreshold)
|
---|
642 | {
|
---|
643 | mAreaRatioThreshold = ratio;
|
---|
644 | RecomputeBounds();
|
---|
645 | }
|
---|
646 | }
|
---|
647 |
|
---|
648 |
|
---|
649 | void Bvh::RecomputeBounds()
|
---|
650 | {
|
---|
651 | // clear old list
|
---|
652 | mTestNodes.clear();
|
---|
653 |
|
---|
654 | // collect all nodes
|
---|
655 | BvhNodeContainer nodes;
|
---|
656 | CollectNodes(mRoot, nodes);
|
---|
657 |
|
---|
658 | cout << "recomputing bounds, children will be tested in depth " << mMaxDepthForTestingChildren << endl;
|
---|
659 |
|
---|
660 | int success = 0;
|
---|
661 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
662 |
|
---|
663 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
664 | {
|
---|
665 | BvhNode *node = *lit;
|
---|
666 |
|
---|
667 | // recreate list of nodes that will be tested as a proxy ...
|
---|
668 | if (CreateNodeRenderList(node))
|
---|
669 | ++ success;
|
---|
670 | }
|
---|
671 |
|
---|
672 | float p = 100.0f * (float)success / nodes.size();
|
---|
673 | cout << "created tighter bounds for " << p << " percent of the nodes" << endl;
|
---|
674 |
|
---|
675 | // recreate indices used for indirect mode rendering
|
---|
676 | if (mIndices)
|
---|
677 | CreateIndices();
|
---|
678 | }
|
---|
679 |
|
---|
680 |
|
---|
681 | bool Bvh::CreateNodeRenderList(BvhNode *node)
|
---|
682 | {
|
---|
683 | BvhNodeContainer children;
|
---|
684 |
|
---|
685 | // collect nodes that will be tested instead of the leaf node
|
---|
686 | // in order to get a tighter bounding box test
|
---|
687 | CollectNodes(node, children, mMaxDepthForTestingChildren);
|
---|
688 |
|
---|
689 |
|
---|
690 | // using the tighter bounds is not feasable in case
|
---|
691 | // that the tighter bounds represent nearly the same projected area
|
---|
692 | // as the old bounding box. Find this out using either volume or area
|
---|
693 | // heuristics
|
---|
694 |
|
---|
695 | float vol = 0;
|
---|
696 | float area = 0;
|
---|
697 |
|
---|
698 | BvhNodeContainer::const_iterator cit;
|
---|
699 |
|
---|
700 | for (cit = children.begin(); cit != children.end(); ++ cit)
|
---|
701 | area += (*cit)->GetBox().SurfaceArea();
|
---|
702 |
|
---|
703 | const float areaRatio = area / node->GetBox().SurfaceArea();
|
---|
704 |
|
---|
705 | bool success;
|
---|
706 |
|
---|
707 | if (areaRatio < mAreaRatioThreshold)
|
---|
708 | success = true;
|
---|
709 | else
|
---|
710 | {
|
---|
711 | // hack: only store node itself
|
---|
712 | children.clear();
|
---|
713 | children.push_back(node);
|
---|
714 |
|
---|
715 | success = false;
|
---|
716 | }
|
---|
717 |
|
---|
718 | // the new test nodes are added at the end of the vector
|
---|
719 | node->mTestNodesIdx = (int)mTestNodes.size();
|
---|
720 |
|
---|
721 | // use the found nodes as nodes during the occlusion tests
|
---|
722 | for (cit = children.begin(); cit != children.end(); ++ cit)
|
---|
723 | {
|
---|
724 | BvhNode *child = *cit;
|
---|
725 | mTestNodes.push_back(child);
|
---|
726 | }
|
---|
727 |
|
---|
728 | node->mNumTestNodes = (int)children.size();
|
---|
729 |
|
---|
730 | return success;
|
---|
731 | }
|
---|
732 |
|
---|
733 |
|
---|
734 | void Bvh::ResetNodeClassifications()
|
---|
735 | {
|
---|
736 | BvhNodeContainer nodes;
|
---|
737 |
|
---|
738 | nodes.reserve(GetNumNodes());
|
---|
739 | CollectNodes(mRoot, nodes);
|
---|
740 |
|
---|
741 | BvhNodeContainer::const_iterator lit, lit_end = nodes.end();
|
---|
742 |
|
---|
743 | for (lit = nodes.begin(); lit != lit_end; ++ lit)
|
---|
744 | {
|
---|
745 | (*lit)->ResetVisibility();
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 |
|
---|
750 | int Bvh::CountTriangles(BvhLeaf *leaf) const
|
---|
751 | {
|
---|
752 | int triangleCount = 0;
|
---|
753 |
|
---|
754 | for (int i = leaf->mFirst; i <= leaf->mLast; ++ i)
|
---|
755 | triangleCount += mGeometry[i]->GetGeometry()->CountTriangles();
|
---|
756 |
|
---|
757 | return triangleCount;
|
---|
758 | }
|
---|
759 |
|
---|
760 |
|
---|
761 | void Bvh::ComputeBvhStats()
|
---|
762 | {
|
---|
763 | std::stack<BvhNode *> nodeStack;
|
---|
764 | nodeStack.push(mRoot);
|
---|
765 |
|
---|
766 | while (!nodeStack.empty())
|
---|
767 | {
|
---|
768 | BvhNode *node = nodeStack.top();
|
---|
769 | nodeStack.pop();
|
---|
770 |
|
---|
771 | if (node->IsLeaf())
|
---|
772 | {
|
---|
773 | BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
|
---|
774 |
|
---|
775 | mBvhStats.mLeafSA += leaf->mBox.SurfaceArea();
|
---|
776 | mBvhStats.mLeafVol += leaf->mBox.GetVolume();
|
---|
777 | }
|
---|
778 | else
|
---|
779 | {
|
---|
780 | mBvhStats.mInteriorSA += node->mBox.SurfaceArea();
|
---|
781 | mBvhStats.mInteriorVol += node->mBox.GetVolume();
|
---|
782 |
|
---|
783 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
784 |
|
---|
785 | nodeStack.push(interior->mBack);
|
---|
786 | nodeStack.push(interior->mFront);
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | mBvhStats.mGeometryRatio = mGeometrySize / (float)GetNumLeaves();
|
---|
791 | mBvhStats.mTriangleRatio = mBvhStats.mTriangles / (float)GetNumLeaves();
|
---|
792 | }
|
---|
793 |
|
---|
794 |
|
---|
795 | void Bvh::PrintBvhStats() const
|
---|
796 | {
|
---|
797 | cout << "bvh stats:" << endl;
|
---|
798 | cout << "interiorNodesSA = " << mBvhStats.mInteriorSA / mRoot->mBox.SurfaceArea() << endl;
|
---|
799 | cout << "leafNodesSA = " << mBvhStats.mLeafSA / mRoot->mBox.SurfaceArea() << endl;
|
---|
800 | cout << "interiorNodesVolume = " << mBvhStats.mInteriorVol / mRoot->mBox.GetVolume() << endl;
|
---|
801 | cout << "leafNodesVolume = " << mBvhStats.mLeafVol / mRoot->mBox.GetVolume() << endl;
|
---|
802 |
|
---|
803 | cout << "boundsInteriorNodesSA = " << mBvhStats.mBoundsInteriorSA / mBvhStats.mLeafSA << endl;
|
---|
804 | cout << "boundsLeafNodesSA = " << mBvhStats.mBoundsLeafSA / mBvhStats.mLeafSA << endl;
|
---|
805 | cout << "boundsInteriorNodesVolume = " << mBvhStats.mBoundsInteriorVol / mBvhStats.mLeafVol << endl;
|
---|
806 | cout << "boundsLeafNodesVolume = " << mBvhStats.mBoundsLeafVol / mBvhStats.mLeafVol << "\n" << endl;
|
---|
807 |
|
---|
808 | cout << "boundsLeaves: " << (float)mBvhStats.mBoundsLeavesCount / (float)GetNumLeaves() << "\n" << endl;
|
---|
809 | cout << "geometry per leaf: " << mBvhStats.mGeometryRatio << endl;
|
---|
810 | cout << "triangles per leaf: " << mBvhStats.mTriangleRatio << endl;
|
---|
811 | }
|
---|
812 |
|
---|
813 |
|
---|
814 | int Bvh::CountTriangles(BvhNode *node) const
|
---|
815 | {
|
---|
816 | int numTriangles = 0;
|
---|
817 |
|
---|
818 | for (int i = node->mFirst; i <= node->mLast; ++ i)
|
---|
819 | numTriangles += mGeometry[i]->GetGeometry()->CountTriangles();
|
---|
820 |
|
---|
821 | return numTriangles;
|
---|
822 | }
|
---|
823 |
|
---|
824 |
|
---|
825 | float Bvh::GetArea(BvhNode *node) const
|
---|
826 | {
|
---|
827 | return node->mArea;
|
---|
828 | }
|
---|
829 |
|
---|
830 | }
|
---|