- Timestamp:
- 10/27/08 00:25:53 (16 years ago)
- Location:
- GTP/trunk/App/Demos/Vis/FriendlyCulling/src
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r3071 r3072 30 30 31 31 using namespace std; 32 32 33 33 34 namespace CHCDemoEngine … … 80 81 static int sClipPlaneAABBVertexIndices[12]; 81 82 82 83 83 #define ALIGN_INDICES 84 84 85 85 86 BvhNode::BvhNode(BvhNode *parent): … … 154 155 inline AxisAlignedBox3 ComputeBoundingBox(SceneEntity **entities, int numEntities) 155 156 { 156 AxisAlignedBox3 box = entities[0]->GetWorldBoundingBox(); 157 158 for (int i = 1; i < numEntities; ++ i) 159 { 160 box.Include(entities[i]->GetWorldBoundingBox()); 157 AxisAlignedBox3 box; 158 159 if (!numEntities) 160 { 161 // no box=> just initialize 162 box.Initialize(); 163 } 164 else 165 { 166 box = entities[0]->GetWorldBoundingBox(); 167 168 for (int i = 1; i < numEntities; ++ i) 169 { 170 box.Include(entities[i]->GetWorldBoundingBox()); 171 } 161 172 } 162 173 … … 171 182 172 183 173 Bvh::Bvh(const SceneEntityContainer &entities) 184 Bvh::Bvh(const SceneEntityContainer &staticEntities, 185 const SceneEntityContainer &dynamicEntities) 174 186 { 175 187 Init(); 176 188 177 mGeometrySize = entities.size();189 mGeometrySize = staticEntities.size() + dynamicEntities.size(); 178 190 mGeometry = new SceneEntity*[mGeometrySize]; 179 191 180 SceneEntityContainer::const_iterator it, it_end = entities.end(); 181 182 int i = 0; 183 for (it = entities.begin(); it != it_end; ++ it, ++ i) 184 { 185 mGeometry[i] = (*it); 192 mStaticGeometrySize = staticEntities.size(); 193 mDynamicGeometrySize = dynamicEntities.size(); 194 195 for (size_t i = 0; i < mStaticGeometrySize; ++ i) 196 { 197 mGeometry[i] = staticEntities[i]; 198 } 199 200 for (size_t i = 0; i < mDynamicGeometrySize; ++ i) 201 { 202 mGeometry[mStaticGeometrySize + i] = dynamicEntities[i]; 186 203 } 187 204 } … … 430 447 sNear = cam->GetNear(); 431 448 432 // rebuild dynamic part of the hierarchy 433 if (!mDynamicEntities.empty()) 434 { 435 if (!mDynamicRoot) 436 CreateDynamicBranch(); 437 449 // update dynamic part of the hierarchy 450 //if (!mDynamicEntities.empty()) 451 if (mDynamicGeometrySize) 452 { 438 453 UpdateDynamicBranch(mDynamicRoot); 439 454 } … … 647 662 CollectNodes(mRoot, nodes); 648 663 649 // assign unique ids to all nodes of thehierarchy664 // assign ids to all nodes of the "regular" hierarchy 650 665 int i = 0; 651 666 BvhNodeContainer::const_iterator lit, lit_end = nodes.end(); … … 788 803 node->mTestNodesIdx = (int)mTestNodes.size(); 789 804 790 // use the found nodes as nodes duringthe occlusion tests805 // use the collected nodes as proxy for the occlusion tests 791 806 for (cit = children.begin(); cit != children.end(); ++ cit) 792 807 { … … 992 1007 993 1008 994 void Bvh::PostProcess() 995 { 996 // this function must be called once after hierarchy creation 997 998 // We initialize the virtual leaves 1009 void Bvh::ComputeMaxDepthForVirtualLeaves() 1010 { 1011 // We initialize the maximal depth for virtual leaves 999 1012 // of this bvh, i.e., the nodes that are used as 1000 1013 // leaves of the hierarchy during traversal. … … 1043 1056 1044 1057 1058 // this function must be called once after hierarchy creation 1059 void Bvh::PostProcess() 1060 { 1061 CreateRoot(); 1062 1063 ComputeIds(); 1064 ComputeMaxDepthForVirtualLeaves(); 1065 // set virtual leaves for specified number of triangles 1066 SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES); 1067 /// for each node compute the number of leaves under this node 1068 UpdateNumLeaves(mRoot); 1069 // specify bounds for occlusion tests 1070 RecomputeBounds(); 1071 1072 // compute and print stats 1073 ComputeBvhStats(mRoot, mBvhStats); 1074 PrintBvhStats(mBvhStats); 1075 1076 if (mDynamicGeometrySize) 1077 { 1078 BvhStats bvhStats; 1079 ComputeBvhStats(mDynamicRoot, bvhStats); 1080 1081 cout << "\n=========== Dynamic BVH statistics: =========" << endl; 1082 cout << "leaves = " << bvhStats.mLeaves << endl; 1083 cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl; 1084 cout << "leafNodesSA = " << bvhStats.mLeafSA << endl; 1085 cout << "interiorNodesVolume = " << bvhStats.mInteriorVol << endl; 1086 cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl; 1087 1088 cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 1089 cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl; 1090 cout << "=============================================" << endl << endl; 1091 } 1092 } 1093 1094 1045 1095 void Bvh::RenderBoundingBoxImmediate(const AxisAlignedBox3 &box) 1046 1096 { … … 1219 1269 leaf->mIsVirtualLeaf = true; 1220 1270 leaf->mIsMaxDepthForVirtualLeaf = true; 1221 cout << "leaf contructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl;1271 //cout << "leaf constructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl; 1222 1272 return leaf; 1223 1273 } … … 1225 1275 //const int axis = (parentAxis + 1) % 3; 1226 1276 const int axis = leaf->mBox.MajorAxis(); 1227 1228 1277 1229 1278 const int scale = 20; … … 1248 1297 mNumNodes += 2; 1249 1298 BvhInterior *parent = new BvhInterior(leaf->GetParent()); 1250 BvhLeaf *front = new BvhLeaf(parent); 1299 parent->mFirst = leaf->mFirst; 1300 parent->mLast = leaf->mLast; 1251 1301 1252 1302 parent->mAxis = axis; … … 1254 1304 parent->mDepth = leaf->mDepth; 1255 1305 1306 BvhLeaf *front = new BvhLeaf(parent); 1307 1256 1308 parent->mBack = leaf; 1257 1309 parent->mFront = front; 1258 //parent->mPosition = pos; 1259 1310 1260 1311 // now assign the triangles to the subnodes 1261 1312 front->mFirst = split + 1; … … 1267 1318 leaf->mParent = parent; 1268 1319 1269 // reset box1270 //leaf->mBox.Initialize();1271 1272 1320 front->mBox = ComputeBoundingBox(mGeometry + front->mFirst, front->CountPrimitives()); 1273 1321 leaf->mBox = ComputeBoundingBox(mGeometry + leaf->mFirst, leaf->CountPrimitives()); … … 1329 1377 1330 1378 BvhLeaf *l = new BvhLeaf(mRoot); 1331 1332 l->mBox.Initialize(); 1333 1334 SceneEntityContainer::const_iterator sit, sit_end = mDynamicEntities.end(); 1335 1336 for (sit = mDynamicEntities.begin(); sit != sit_end; ++ sit) 1337 { 1338 l->mBox.Include((*sit)->GetWorldBoundingBox()); 1339 } 1340 1341 l->mFirst = (int)(mGeometrySize - mDynamicEntities.size()); 1379 mDynamicRoot = l; 1380 1381 l->mBox = ComputeBoundingBox(mGeometry + mStaticGeometrySize, (int)mDynamicGeometrySize); 1382 1383 l->mFirst = (int)mStaticGeometrySize; 1342 1384 l->mLast = (int)mGeometrySize - 1; 1343 1385 l->mArea = l->mBox.SurfaceArea(); … … 1345 1387 cout << "updating dynamic branch " << l->mFirst << " " << l->mLast << endl; 1346 1388 1347 mDynamicRoot = SubdivideLeaf(l, 0); 1348 1349 BvhStats bvhStats; 1350 ComputeBvhStats(mDynamicRoot, bvhStats); 1351 1352 cout << "\n=========== Dynamic BVH statistics: =========" << endl; 1353 cout << "leaves = " << bvhStats.mLeaves << endl; 1354 cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl; 1355 cout << "leafNodesSA = " << bvhStats.mLeafSA << endl; 1356 cout << "interiorNodesVolume = " << bvhStats.mInteriorVol << endl; 1357 cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl; 1358 1359 cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 1360 cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl; 1361 cout << "=============================================" << endl << endl; 1362 } 1363 1364 1365 void Bvh::AddDynamicObjects(const SceneEntityContainer &entities) 1366 { 1367 // copy old entities 1368 SceneEntity **newGeom = new SceneEntity*[mGeometrySize + (int)entities.size()]; 1369 1370 memcpy(newGeom, mGeometry, mGeometrySize * sizeof(SceneEntity *)); 1371 1372 delete [] mGeometry; 1373 mGeometry = newGeom; 1374 1375 // now add new entities 1376 SceneEntityContainer::const_iterator it, it_end = entities.end(); 1377 1378 size_t i = mGeometrySize; 1379 for (it = entities.begin(); it != it_end; ++ it, ++ i) 1380 { 1381 mGeometry[i] = (*it); 1382 mDynamicEntities.push_back(*it); 1383 } 1384 1385 1386 mGeometrySize += entities.size(); 1389 if (mDynamicGeometrySize) 1390 mDynamicRoot = SubdivideLeaf(l, 0); 1387 1391 } 1388 1392 … … 1394 1398 // especially annoying is this problem when using the frustum 1395 1399 // fitting on the visible objects for shadow mapping 1396 // but don't see how to solve this issue without using much costliercalculations1400 // but don't see how to solve this issue without using costly calculations 1397 1401 1398 1402 // we stored the near plane distance => we can use it also here … … 1404 1408 1405 1409 1406 } 1410 void Bvh::CreateRoot() 1411 { 1412 // create new root 1413 mRoot = new BvhInterior(NULL); 1414 1415 // the separation is a purely logical one 1416 // the bounding boxes of the child nodes are 1417 // identical to those of the root node 1418 1419 mRoot->mBox = mStaticRoot->mBox; 1420 mRoot->mBox.Include(mDynamicRoot->mBox); 1421 1422 mRoot->mArea = mRoot->mBox.SurfaceArea(); 1423 1424 mRoot->mFirst = 0; 1425 mRoot->mLast = (int)mGeometrySize - 1; 1426 1427 cout<<"f: " << mRoot->mFirst<< " l: " <<mRoot->mLast << endl; 1428 // add static root on left subtree 1429 mRoot->mFront = mStaticRoot; 1430 mStaticRoot->mParent = mRoot; 1431 1432 // add dynamic root on left subtree 1433 mRoot->mBack = mDynamicRoot; 1434 mDynamicRoot->mParent = mRoot; 1435 } 1436 1437 1438 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
r3070 r3072 137 137 */ 138 138 inline void SetId(int id); 139 139 140 140 141 141 ////////// … … 648 648 void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds); 649 649 650 void AddDynamicObjects(const SceneEntityContainer &entities);651 652 653 650 654 651 protected: … … 660 657 Bvh(); 661 658 /** Protected constructor taking scene geometry into account 662 */ 663 const Bvh(const SceneEntityContainer &entities); 659 Sets the static and dynamic objects for the hierarchy. 660 */ 661 const Bvh(const SceneEntityContainer &staticEntities, 662 const SceneEntityContainer &dynamicEntities); 664 663 /** Called by the constructor. Initializes important members. 665 664 */ … … 740 739 inline int GetNumStaticNodes() const { return mNumNodes; } 741 740 741 void ComputeMaxDepthForVirtualLeaves(); 742 743 void CreateRoot(); 744 745 742 746 //////////////////////// 743 747 … … 745 749 AxisAlignedBox3 mBox; 746 750 /// the root of the hierarchy 747 Bvh Node*mRoot;751 BvhInterior *mRoot; 748 752 /// the root of static part of the the hierarchy 749 //BvhNode *mStaticRoot;753 BvhNode *mStaticRoot; 750 754 /// the root of dynamic part of the the hierarchy 751 755 BvhNode *mDynamicRoot; … … 754 758 /// #of entities 755 759 size_t mGeometrySize; 760 /// #of static entities 761 size_t mStaticGeometrySize; 762 /// #of dynamic entities 763 size_t mDynamicGeometrySize; 756 764 757 765 … … 799 807 int mMaxDepthForDynamicBranch; 800 808 801 SceneEntityContainer mDynamicEntities;809 //SceneEntityContainer mDynamicEntities; 802 810 }; 803 811 -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
r3070 r3072 63 63 64 64 Bvh *BvhLoader::Load(const string &filename, 65 const SceneEntityContainer &entities) 65 const SceneEntityContainer &staticEntities, 66 const SceneEntityContainer &dynamicEntities) 66 67 { 67 68 queue<BvhNode *> tQueue; … … 72 73 cout << "loading bvh" << endl; 73 74 74 Bvh *bvh = new Bvh( entities);75 Bvh *bvh = new Bvh(staticEntities, dynamicEntities); 75 76 76 77 BvhNode *root = LoadNextNode(stream, NULL); 77 78 78 bvh->mRoot = root; 79 //bvh->mRoot = bvh->mStaticRoot; 80 bvh->mNumNodes = 1; 79 bvh->mStaticRoot = root; 80 bvh->mNumNodes = 3; 81 81 82 tQueue.push( bvh->mRoot);82 tQueue.push(root); 83 83 84 84 while(!tQueue.empty()) … … 108 108 109 109 110 bvh->mBox = bvh->mRoot->GetBox(); 110 //////////// 111 //-- create dynamic part of the hierarchy 111 112 112 cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl; 113 cout << "scene box: " << bvh->mBox << endl; 113 bvh->CreateDynamicBranch(); 114 114 115 115 … … 119 119 /// this function must be called once after creation 120 120 bvh->PostProcess(); 121 122 // set virtual leaves for specified number of triangles123 bvh->SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES);124 /// update the numleaves parameter of the bvh nodes125 bvh->UpdateNumLeaves(bvh->mRoot);126 // compute unique ids127 bvh->ComputeIds();128 // specify bounds for occlusion tests129 bvh->RecomputeBounds();130 121 131 // compute and print stats 132 bvh->ComputeBvhStats(bvh->mRoot, bvh->mBvhStats);133 bvh->PrintBvhStats(bvh->mBvhStats);122 123 cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl; 124 cout << "scene box: " << bvh->mBox << endl; 134 125 135 126 return bvh; -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h
r2795 r3072 17 17 { 18 18 public: 19 20 Bvh *Load(const std::string &filename, const SceneEntityContainer &entities); 19 /** Creates and loads bvh with static and dynamic objects. 20 The static part of the hierarchy is loaded from the disc, 21 the dynamic part is created and changed on the fly. 22 */ 23 Bvh *Load(const std::string &filename, 24 const SceneEntityContainer &staticEntities, 25 const SceneEntityContainer &dynamicEntities); 21 26 22 27 protected: -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp
r3071 r3072 157 157 // add root node to queue 158 158 EnqueueNode(mBvh->GetRoot()); 159 // add dynamic root 160 if (mBvh->GetDynamicRoot()) 161 EnqueueNode(mBvh->GetDynamicRoot()); 162 159 163 160 164 161 /////////// -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.cpp
r3071 r3072 5 5 #include "SceneEntity.h" 6 6 #include "Transform3.h" 7 #include "glInterface.h" 8 7 9 8 10 … … 26 28 if (mMaterial) 27 29 mMaterial->Render(state); 28 30 //mDepthWriteEnabled = true; 31 glDepthMask(GL_TRUE); glDepthFunc(GL_LESS); 32 glEnable(GL_DEPTH_TEST); 29 33 mGeometry->Render(state); 30 34 } -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h
r3071 r3072 2 2 #define __SHAPE_H 3 3 4 #include "glInterface.h"5 #include <Cg/cg.h>6 #include <Cg/cgGL.h>7 4 #include "common.h" 8 5 #include "Vector3.h" -
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
r3071 r3072 184 184 185 185 bool useOptimization = false; 186 bool useTightBounds = true;187 //bool useTightBounds = false;186 //bool useTightBounds = true; 187 bool useTightBounds = false; 188 188 bool useRenderQueue = true; 189 189 bool useMultiQueries = true; … … 453 453 LoadModel("city.dem", sceneEntities); 454 454 455 455 /* 456 ////////// 457 //-- load some dynamic stuff 458 459 LoadModel("hbuddha.dem", dynamicObjects); 460 buddha = dynamicObjects.back(); 461 462 const Vector3 sceneCenter(470.398f, 240.364f, 182.5f); 463 Matrix4x4 transl = TranslationMatrix(sceneCenter); 464 buddha->GetTransform()->SetMatrix(transl); 465 466 for (int i = 0; i < 10; ++ i) 467 { 468 SceneEntity *ent = new SceneEntity(*buddha); 469 resourceManager->AddSceneEntity(ent); 470 471 Vector3 offs = Vector3::ZERO(); 472 473 offs.x = RandomValue(.0f, 150.0f); 474 offs.y = RandomValue(.0f, 150.0f); 475 476 transl = TranslationMatrix(sceneCenter + offs); 477 Transform3 *transform = resourceManager->CreateTransform(transl); 478 479 ent->SetTransform(transform); 480 dynamicObjects.push_back(ent); 481 } 482 483 */ 456 484 /////////// 457 485 //-- load the associated static bvh 458 486 459 487 const string bvh_filename = string(model_path + "city.bvh"); 488 460 489 BvhLoader bvhLoader; 461 bvh = bvhLoader.Load(bvh_filename, sceneEntities );490 bvh = bvhLoader.Load(bvh_filename, sceneEntities, dynamicObjects); 462 491 463 492 if (!bvh) … … 494 523 // the bird-eye visualization 495 524 visualization = new Visualization(bvh, camera, NULL, &state); 496 497 LoadModel("hbuddha.dem", dynamicObjects);498 buddha = dynamicObjects.back();499 500 const Vector3 sceneCenter(470.398f, 240.364f, 182.5f);501 Matrix4x4 transl = TranslationMatrix(sceneCenter);502 buddha->GetTransform()->SetMatrix(transl);503 504 for (int i = 0; i < 10; ++ i)505 {506 SceneEntity *ent = new SceneEntity(*buddha);507 resourceManager->AddSceneEntity(ent);508 509 Vector3 offs = Vector3::ZERO();510 511 offs.x = RandomValue(.0f, 150.0f);512 offs.y = RandomValue(.0f, 150.0f);513 514 transl = TranslationMatrix(sceneCenter + offs);515 Transform3 *transform = resourceManager->CreateTransform(transl);516 517 ent->SetTransform(transform);518 dynamicObjects.push_back(ent);519 }520 521 bvh->AddDynamicObjects(dynamicObjects);522 525 523 526 // this function assign the render queue bucket ids of the materials in beforehand … … 1003 1006 } 1004 1007 else 1005 { 1008 {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1006 1009 // actually render the scene geometry using the specified algorithm 1007 1010 traverser->RenderScene(); 1011 //for (int i = 0; i < sceneEntities.size(); ++ i) 1012 // sceneEntities[i]->Render(&state); 1008 1013 } 1009 1014
Note: See TracChangeset
for help on using the changeset viewer.