Changeset 3064 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
- Timestamp:
- 10/23/08 23:10:16 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r3061 r3064 191 191 void Bvh::Init() 192 192 { 193 mStaticRoot = NULL; 194 mDynamicRoot = NULL; 193 195 mRoot = NULL; 196 194 197 mVertices = NULL; 195 198 mIndices = NULL; … … 198 201 mNumNodes = 0; 199 202 203 // nodes are tested using the subnodes from 3 levels below 200 204 mMaxDepthForTestingChildren = 3; 201 205 //mMaxDepthForTestingChildren = 4; 206 207 // the ratio of area between node and subnodes where 208 // testing the subnodes as proxy is still considered feasable 202 209 mAreaRatioThreshold = 2.0f; 203 210 //mAreaRatioThreshold = 1.4f; … … 209 216 210 217 211 void Bvh::PullUpLastVisited(BvhNode *node, const int frameId) const 218 219 220 ////////////////////// 221 //-- functions that are used during the main traversal 222 223 void Bvh::PullUpLastVisited(BvhNode *node, int frameId) const 212 224 { 213 225 BvhNode *parent = node->GetParent(); … … 232 244 } 233 245 246 247 //////////////////////////////// 234 248 235 249 void Bvh::CollectLeaves(BvhNode *node, BvhLeafContainer &leaves) … … 403 417 if (!mDynamicEntities.empty()) 404 418 { 405 UpdateDynamicBranch();419 //UpdateDynamicBranch(); 406 420 } 407 421 } … … 464 478 bool useTightBounds) 465 479 { 466 // if not using tight bounds, rendering boxes in immediate mode467 // is preferable to vertex arrays (less setup time)468 469 480 int renderedBoxes; 470 481 471 482 if (!useTightBounds) 472 483 { 484 // if not using tight bounds, rendering boxes in immediate mode 485 // is preferable to vertex arrays (less setup time) 473 486 BvhNodeContainer::const_iterator nit, nit_end = nodes.end(); 474 487 … … 477 490 RenderBoundingBoxImmediate((*nit)->GetBox()); 478 491 } 479 492 cout<<"y"; 480 493 renderedBoxes = (int)nodes.size(); 481 494 } 482 495 else 483 { 496 {cout<<"x"; 484 497 renderedBoxes = PrepareBoundsWithDrawArrays(nodes); 485 498 RenderBoundsWithDrawArrays(renderedBoxes, state); … … 492 505 int Bvh::PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes) 493 506 { 494 ////// 495 //-- for the first time we come here ...507 /////////////////// 508 //-- for the first time we come here => create vbo and indices 496 509 497 510 if (!mIndices) 498 { // create list of indices 511 { 512 // create list of indices 499 513 CreateIndices(); 500 514 } … … 532 546 void Bvh::RenderBoundsWithDrawArrays(int numNodes, RenderState *state) 533 547 { 534 ////// 535 //-- Render ingthe vbo548 ///////// 549 //-- Render the vbo 536 550 537 551 if (state->GetCurrentVboId() != mVboId) … … 555 569 // collect bvh nodes 556 570 BvhNodeContainer nodes; 557 CollectNodes(m Root, nodes);571 CollectNodes(mStaticRoot, nodes); 558 572 559 573 cout << "creating new indices" << endl; … … 567 581 int offset = (*lit)->mNumTestNodes * sNumIndicesPerBox; 568 582 #ifdef ALIGN_INDICES 569 // align with 32 583 // align with 32 in order to speed up memcopy 570 584 offset = (offset / 32) * 32 + 32; 571 585 #endif … … 582 596 // to allocate memory from the chunks of the node 583 597 mIndices = new unsigned int[numMaxIndices]; 584 585 598 // create new index buffer for the individual nodes 586 599 mTestIndices = new unsigned int[numMaxIndices]; … … 627 640 CollectNodes(mRoot, nodes); 628 641 629 // assign ids to all nodes of the "regular"hierarchy642 // assign unique ids to all nodes of the hierarchy 630 643 int i = 0; 631 644 BvhNodeContainer::const_iterator lit, lit_end = nodes.end(); … … 643 656 BvhNodeContainer nodes; 644 657 645 nodes.reserve(GetNum Nodes());646 CollectNodes(m Root, nodes);658 nodes.reserve(GetNumStaticNodes()); 659 CollectNodes(mStaticRoot, nodes); 647 660 648 661 const unsigned int bufferSize = 8 * (int)nodes.size(); … … 659 672 660 673 for (int j = 0; j < 8; ++ j) 661 ( (Vector3 *)mVertices)[node->GetId() * 8 + j] = node->GetBox().GetVertex(j);674 (static_cast<Vector3 *>(mVertices))[node->GetId() * 8 + j] = node->GetBox().GetVertex(j); 662 675 } 663 676 … … 676 689 DEL_PTR(mVertices); 677 690 678 cout << "***** created vbos for tighter bounds *********" << endl;691 cout << "******** created vbos for tighter bounds *********" << endl; 679 692 } 680 693 … … 704 717 // collect all nodes 705 718 BvhNodeContainer nodes; 706 CollectNodes(m Root, nodes);719 CollectNodes(mStaticRoot, nodes); 707 720 708 721 cout << "recomputing bounds, children will be tested in depth " << mMaxDepthForTestingChildren << endl; … … 739 752 // using the tighter bounds is not feasable in case 740 753 // that the tighter bounds represent nearly the same projected area 741 // as the old bounding box. Find this out using either volume or area742 // heuristics754 // as the old bounding box. Test this property using either 755 // volume or area heuristics 743 756 744 757 float vol = 0; … … 915 928 void Bvh::SetVirtualLeaves(int numTriangles) 916 929 { 917 // first invalidate old leaves930 // first invalidate old virtual leaves 918 931 BvhNodeContainer leaves; 919 920 CollectVirtualLeaves(mRoot, leaves); 932 CollectVirtualLeaves(mStaticRoot, leaves); 921 933 922 934 BvhNodeContainer::const_iterator bit, bit_end = leaves.end(); … … 929 941 mNumVirtualNodes = 0; 930 942 931 // assign new virtual leaves based on specified number oftriangles per leaf943 // assign new virtual leaves based on specified #triangles per leaf 932 944 std::stack<BvhNode *> nodeStack; 945 933 946 nodeStack.push(mRoot); 934 947 … … 952 965 BvhNode *b = interior->mBack; 953 966 954 if (node->mIsMaxDepthForVirtualLeaf || (CountTriangles(node) <= numTriangles)) 967 if (node->mIsMaxDepthForVirtualLeaf || 968 (CountTriangles(node) <= numTriangles)) 955 969 { 956 970 node->mIsVirtualLeaf = true; … … 964 978 } 965 979 980 /// Reset the node states 966 981 ResetNodeClassifications(); 967 982 } … … 970 985 void Bvh::PostProcess() 971 986 { 987 // this function must be called once after hierarchy creation 988 989 // We initialize the virtual leaves 990 // of this bvh, i.e., the nodes that are used as 991 // leaves of the hierarchy during traversal. 992 993 // Initially they are set either 994 // a) to the real leaves of the hierarchy or 995 // b) the point where the subdivision on object level ends 996 // and the subsequent nodes are just used to provide tighter bounds 997 // (see article for the notations) 998 972 999 std::stack<BvhNode *> nodeStack; 973 nodeStack.push(m Root);1000 nodeStack.push(mStaticRoot); 974 1001 975 1002 while (!nodeStack.empty()) … … 989 1016 BvhNode *b = interior->mBack; 990 1017 991 // point reached where we cannot subdivide further on object level992 1018 if ((f->mFirst == b->mFirst) && (f->mLast == b->mLast)) 993 1019 { 1020 // point reached where beyond there would be no further reduction 1021 // as both subtrees contain the same objects => stop here 1022 // The tree beyond the current node is used to describe 1023 // tighter bounds on the geometry contained in it 994 1024 node->mIsMaxDepthForVirtualLeaf = true; 995 1025 } … … 1014 1044 //-- render AABB as triangle strips 1015 1045 1046 // render first half of AABB 1016 1047 glVertex3f(l.x, l.y, u.z); 1017 1048 glVertex3f(u.x, l.y, u.z); … … 1025 1056 glPrimitiveRestartNV(); 1026 1057 1027 // --render second half of AABB1058 // render second half of AABB 1028 1059 glVertex3f(l.x, u.y, u.z); 1029 1060 glVertex3f(l.x, u.y, l.z); … … 1088 1119 1089 1120 1090 void Bvh::RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds) 1121 void Bvh::RenderBoundsForViz(BvhNode *node, 1122 RenderState *state, 1123 bool useTightBounds) 1091 1124 { 1092 1125 glDisable(GL_TEXTURE_2D); … … 1119 1152 1120 1153 //////////////////////// 1121 // construction of the dynamic hierarchy1154 //-- functions for construction of the dynamic hierarchy 1122 1155 1123 1156 int Bvh::SortTriangles(BvhLeaf *leaf, … … 1137 1170 -- j; 1138 1171 1139 if (i < j) 1140 { 1141 swap(entities[i], entities[j]); 1172 // sorting finished 1173 if (i >= j) break; 1174 1175 // swap entities 1176 swap(entities[i], entities[j]); 1142 1177 1143 ++ i; 1144 -- j; 1145 } 1146 else 1147 { 1148 break; 1149 } 1178 ++ i; 1179 -- j; 1150 1180 } 1151 1181 … … 1169 1199 { 1170 1200 if (TerminationCriteriaMet(leaf)) 1201 { 1202 leaf->mIsVirtualLeaf = true; 1171 1203 return leaf; 1172 1204 } 1205 1173 1206 //int axis = leaf->mBox.MajorAxis(); 1174 1207 int axis = (parentAxis + 1) % 3; … … 1209 1242 front->mLast = leaf->mLast; 1210 1243 front->mDepth = leaf->mDepth + 1; 1244 1211 1245 leaf->mLast = split; 1212 1246 leaf->mDepth = front->mDepth; … … 1238 1272 BvhNode *dynamicRoot = static_cast<BvhInterior *>(mRoot)->mBack; 1239 1273 1240 cout << "updating dynamic branch" << endl;1241 1242 1274 // delete old branch 1243 1275 if (!dynamicRoot->IsLeaf()) … … 1251 1283 } 1252 1284 1285 cout << "updating dynamic branch" << endl; 1286 1253 1287 dynamicRoot = SubdivideLeaf(static_cast<BvhLeaf *>(dynamicRoot), 0, mDynamicEntities); 1254 } 1255 1256 } 1288 1289 cout << "finished updating dynamic branch" << endl; 1290 } 1291 1292 1293 void Bvh::AddDynamicObject(SceneEntity *ent) 1294 { 1295 mDynamicEntities.push_back(ent); 1296 } 1297 1298 1299 }
Note: See TracChangeset
for help on using the changeset viewer.