Changeset 3064
- Timestamp:
- 10/23/08 23:10:16 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
r3003 r3064 10 10 winHeight=768 11 11 camPosition=483.398f 242.364f 186.078f 12 #camDirection=1 0 012 camDirection=1 0 0 13 13 lightDirection=-0.8f 1.0f -0.7f 14 14 #lightDirection=0.0f 0.0f -1.0f -
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 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
r3059 r3064 478 478 */ 479 479 BvhNode *GetRoot() { return mRoot; } 480 /** Sets the scene camera 481 */ 482 //void SetCamera(Camera * camera) { mCamera = camera; } 483 480 484 481 /////////////// 485 482 //-- functions collecting nodes based on some criteria 486 483 484 /** Collect all nodes higher or equal to a given depth. 485 */ 487 486 void CollectNodes(BvhNode *node, BvhNodeContainer &nodes, int depth); 488 487 void CollectNodes(BvhNode *node, BvhNodeContainer &nodes); 488 /** Collect the "physical" leaves of the hierarchy 489 */ 489 490 void CollectLeaves(BvhNode *node, BvhLeafContainer &leaves); 490 /** Collect only the virtual leaves .491 /** Collect only the virtual leaves (can be anywhere in the Žhierarchy). 491 492 */ 492 493 void CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves); … … 543 544 */ 544 545 void ResetNodeClassifications(); 545 /** Helper function that renders the bounding boxes of the leaves as546 wireframes for visualization purpose.547 */548 //void RenderBoundingBoxesForViz(int mode);549 546 /** Count triangles the node contains. 550 547 */ … … 557 554 void ComputeIds(); 558 555 /** Assign virtual leaves based on specified number of triangles per leaf. 556 That means that we try to set the leaves at a point where we 557 have less than numTriangles triangles within this leaf and beyond. 558 559 Virtual leaves are nodes that are determined to be the leaves 560 of the hierarchy during render traversal. These nodes are not necessarily 561 the same as the real leaves of the hierarchy and can be anywhere 562 in the hierarchy. 563 Please refer to the article for more info about virtual leaves 559 564 */ 560 565 void SetVirtualLeaves(int numTriangles); 561 566 562 567 563 //////// 564 //-- functions influencing t ighter bounds568 //////////// 569 //-- functions influencing the tightness of the bounds that are used for testing 565 570 566 571 567 572 /** Sets maximal depth for taking the bounding boxes to test the 568 573 visibility of a node. 569 Deeper => the bounds adapt more to the geometry. 574 Deeper level means that the bounds adapt more to the geometry but 575 also that more boxes are rendered 570 576 */ 571 577 void SetMaxDepthForTestingChildren(int maxDepth); 572 578 /** The ratio of area between node and subnodes where 579 testing the subnodes as proxy is still considered feasable 580 */ 573 581 void SetAreaRatioThresholdForTestingChildren(float ratio); 574 582 … … 583 591 void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds); 584 592 593 void AddDynamicObject(SceneEntity *ent); 594 585 595 586 596 protected: … … 608 618 609 619 void PrepareVertices(); 610 620 /** Prepare nodes for vbo rendering. 621 */ 611 622 int PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes); 623 /** Render the nodes from the vbo prepared previously. 624 */ 612 625 void RenderBoundsWithDrawArrays(int numNodes, RenderState *state); 613 626 … … 643 656 644 657 ///////////////////////////// 645 658 // functions that are required to build the dynamic part of the hierarchy 646 659 int SortTriangles(BvhLeaf *leaf, 647 660 int axis, … … 656 669 int parentAxis, 657 670 SceneEntityContainer &entities); 658 659 671 /** Recompute the dynamic branch of the hierarchy. 660 672 */ … … 663 675 inline bool TerminationCriteriaMet(BvhLeaf *leaf) const; 664 676 677 /** Returns number of bvh nodes. 678 */ 679 inline int GetNumStaticNodes() const { return mNumNodes; } 665 680 666 681 //////////////////////// … … 670 685 /// the root of the hierarchy 671 686 BvhNode *mRoot; 687 /// the root of static part of the the hierarchy 688 BvhNode *mStaticRoot; 689 /// the root of dynamic part of the the hierarchy 690 BvhNode *mDynamicRoot; 672 691 /// pointers to the geometry associated with this node 673 692 SceneEntity **mGeometry; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
r3052 r3064 73 73 74 74 Bvh *bvh = new Bvh(entities); 75 bvh->mStaticRoot = LoadNextNode(stream, NULL); 75 76 76 BvhNode *root = LoadNextNode(stream, NULL); 77 #if 1 78 bvh->mRoot = bvh->mStaticRoot; 79 bvh->mNumNodes = 1; 80 #else 77 81 78 #if 0 82 // we are setting a new root node 83 // and adds one level of indirection to the bvh 84 // It allows us to use dynamic objects 79 85 80 bvh->mRoot = root;81 82 #else83 // we are setting a new root node84 // which adds one level of indirection to the bvh but85 // allows us to use dynamic objects86 86 // the new bvh has two main branches 87 // one is the static branch (the old root), 88 // one is the dynamic branch 87 // a static branch (the old root), and adynamic branch 89 88 // we create a 'dynamic' leaf which basically is a container 90 89 // for all dynamic objects underneath … … 93 92 // the movements of the objects within 94 93 94 // create new root 95 95 BvhInterior *newRoot = new BvhInterior(NULL); 96 96 bvh->mRoot = newRoot; 97 97 98 BvhLeaf *dynamicLeaf = new BvhLeaf(newRoot); 98 // the separation is a purely logical one 99 // the bounding boxes of the child nodes are 100 // identical to those of the root node 99 101 100 newRoot->mFront = root; 101 newRoot->mBack = dynamicLeaf; 102 root->mParent = newRoot; 102 newRoot->mBox = bvh->mStaticRoot->mBox; 103 newRoot->mArea = newRoot->mBox.SurfaceArea(); 103 104 104 // the separation is a purely logical one 105 // the bounding boxes of the child nodes is the 106 // same as for the root node 107 newRoot->mBox = root->mBox; 108 dynamicLeaf->mBox = root->mBox; 105 newRoot->mFirst = bvh->mStaticRoot->mFirst; 106 newRoot->mLast = bvh->mStaticRoot->mLast; 109 107 110 newRoot->mArea = newRoot->mBox.SurfaceArea(); 111 dynamicLeaf->mArea = dynamicLeaf->mBox.SurfaceArea(); 108 // add static root on left subtree 109 newRoot->mFront = bvh->mStaticRoot; 110 bvh->mStaticRoot->mParent = newRoot; 111 112 // create and add dynamic root 113 BvhLeaf *dynamicRoot = new BvhLeaf(newRoot); 114 115 dynamicRoot->mFirst = 0; 116 dynamicRoot->mLast = 0; 117 dynamicRoot->mBox = bvh->mStaticRoot->mBox; 118 dynamicRoot->mArea = dynamicRoot->mBox.SurfaceArea(); 112 119 113 newRoot->mFirst = root->mFirst;114 newRoot->m Last = root->mLast;120 bvh->mDynamicRoot = dynamicRoot; 121 newRoot->mBack = dynamicRoot; 115 122 116 dynamicLeaf->mFirst = 0;117 dynamicLeaf->mLast = 0; 123 bvh->mNumNodes = 1; 124 118 125 #endif 119 126 120 tQueue.push(root); 121 bvh->mNumNodes = 1; 127 tQueue.push(bvh->mStaticRoot); 122 128 123 129 while(!tQueue.empty()) -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r3063 r3064 178 178 179 179 bool useOptimization = false; 180 bool useTightBounds = true; 180 //bool useTightBounds = true; 181 bool useTightBounds = false; 181 182 bool useRenderQueue = true; 182 183 bool useMultiQueries = true; … … 483 484 484 485 buddha->GetTransform()->SetMatrix(transl); 486 487 bvh->AddDynamicObject(buddha); 488 485 489 486 490 // this function assign the render queue bucket ids of the materials in beforehand
Note: See TracChangeset
for help on using the changeset viewer.