Changeset 3072 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
- Timestamp:
- 10/27/08 00:25:53 (16 years ago)
- File:
-
- 1 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 }
Note: See TracChangeset
for help on using the changeset viewer.