Changeset 3065 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
- Timestamp:
- 10/24/08 12:29:30 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r3064 r3065 27 27 #endif 28 28 29 #define INVALID_TEST ((unsigned int)-1) 30 31 using namespace std; 29 32 30 33 namespace CHCDemoEngine 31 34 { 32 33 #define INVALID_TEST ((unsigned int)-1)34 35 using namespace std;36 37 35 38 36 int BvhNode::sCurrentState = 0; … … 76 74 77 75 static Plane3 sNearPlane; 76 static float sNear; 78 77 static Frustum sFrustum; 79 78 … … 147 146 148 147 149 /********************************************************** */150 /* class Bvh implementation 151 /********************************************************** */148 /**********************************************************/ 149 /* class Bvh implementation */ 150 /**********************************************************/ 152 151 153 152 … … 183 182 184 183 if (mRoot) delete mRoot; 185 186 184 // delete vbo 187 185 glDeleteBuffersARB(1, &mVboId); … … 191 189 void Bvh::Init() 192 190 { 193 mStaticRoot = NULL;191 //mStaticRoot = NULL; 194 192 mDynamicRoot = NULL; 195 193 mRoot = NULL; … … 202 200 203 201 // nodes are tested using the subnodes from 3 levels below 204 mMaxDepthForTestingChildren = 3;202 mMaxDepthForTestingChildren = 0;//3; 205 203 //mMaxDepthForTestingChildren = 4; 206 204 … … 311 309 tStack.pop(); 312 310 313 // found depth => take this node311 // found node in specified depth => take this node 314 312 if ((d == depth) || (node->IsLeaf())) 315 313 { … … 338 336 // do the test only if necessary 339 337 if (!(node->mPlaneMask[BvhNode::sCurrentState] & (1 << i))) 338 { 340 339 return true; 341 340 } 341 342 342 343 //////// 343 344 //-- test the n-vertex … … 375 376 376 377 //////// 377 //-- apply frustum culling for the planes [mPreferredPlane - 5]378 //-- apply frustum culling for the planes with index mPreferredPlane to 6 378 379 379 380 for (int i = node->mPreferredPlane[BvhNode::sCurrentState]; i < 6; ++ i) … … 382 383 383 384 ////////// 384 //-- do the view frustum culling for the planes [0 - m_iPreferredPlane)385 //-- apply frustum culling for the planes with index 0 to mPreferredPlane 385 386 386 387 for (int i = 0; i < node->mPreferredPlane[BvhNode::sCurrentState]; ++ i) … … 413 414 // store near plane 414 415 sNearPlane = Plane3(cam->GetDirection(), cam->GetPosition()); 416 sNear = cam->GetNear(); 415 417 416 418 // rebuild dynamic part of the hierarchy … … 424 426 void Bvh::UpdateDistance(BvhNode *node) const 425 427 { 426 // use distance to center rather than the distance to the near plane because 427 // otherwise problems with big object penetrating the near plane 428 // q: should we use distance to center rather than the distance to the near plane? 429 430 // because otherwise problems with big object penetrating the near plane 428 431 // (e.g., big objects in the distance which are then always visible) 429 432 // especially annoying is this problem when using the frustum 430 433 // fitting on the visible objects for shadow mapping 431 432 // node->mDistance = node->GetBox()GetMinDistance(sNearPlane);434 // on the other hand, distance to near plane can also be used 435 // for near plane intersection 433 436 node->mDistance = sNearPlane.Distance(node->GetBox().Center()); 437 //node->mDistance = node->GetBox().GetMinDistance(sNearPlane); 434 438 } 435 439 … … 458 462 void Bvh::RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds) 459 463 { 460 // if not using tight bounds, rendering boxes in immediate mode 461 // is preferable to vertex arrays (less setup time) 462 if (!useTightBounds) 463 { 464 RenderBoundingBoxImmediate(node->GetBox()); 465 } 466 else 467 { 468 // hack: use dummy wrapper in order to use function 469 static BvhNodeContainer dummy(1); 470 dummy[0] = node; 471 RenderBounds(dummy, state, useTightBounds); 472 } 464 // hack: use dummy contayiner as wrapper in order to use multibox function 465 static BvhNodeContainer dummy(1); 466 dummy[0] = node; 467 RenderBounds(dummy, state, useTightBounds); 473 468 } 474 469 … … 490 485 RenderBoundingBoxImmediate((*nit)->GetBox()); 491 486 } 492 cout<<"y"; 487 493 488 renderedBoxes = (int)nodes.size(); 494 489 } 495 490 else 496 { cout<<"x";491 { 497 492 renderedBoxes = PrepareBoundsWithDrawArrays(nodes); 498 493 RenderBoundsWithDrawArrays(renderedBoxes, state); … … 529 524 { 530 525 BvhNode *node = *nit; 531 532 526 const int numIndices = node->mNumTestNodes * sNumIndicesPerBox; 533 527 … … 569 563 // collect bvh nodes 570 564 BvhNodeContainer nodes; 571 CollectNodes(m StaticRoot, nodes);565 CollectNodes(mRoot, nodes); 572 566 573 567 cout << "creating new indices" << endl; … … 587 581 } 588 582 589 590 583 cout << "creating global indices buffer" << endl; 591 584 … … 607 600 // resize array 608 601 node->mIndicesPtr = mCurrentIndicesPtr; 609 610 602 int numIndices = 0; 611 603 612 // the bounding box of the test nodes are rendered instead of the root node604 // the bounding boxes of the test nodes are rendered instead of the node itself 613 605 // => store their indices 614 606 for (int i = 0; i < node->mNumTestNodes; ++ i, numIndices += sNumIndicesPerBox) … … 657 649 658 650 nodes.reserve(GetNumStaticNodes()); 659 CollectNodes(m StaticRoot, nodes);651 CollectNodes(mRoot, nodes); 660 652 661 653 const unsigned int bufferSize = 8 * (int)nodes.size(); … … 717 709 // collect all nodes 718 710 BvhNodeContainer nodes; 719 CollectNodes(m StaticRoot, nodes);711 CollectNodes(mRoot, nodes); 720 712 721 713 cout << "recomputing bounds, children will be tested in depth " << mMaxDepthForTestingChildren << endl; … … 930 922 // first invalidate old virtual leaves 931 923 BvhNodeContainer leaves; 932 CollectVirtualLeaves(m StaticRoot, leaves);924 CollectVirtualLeaves(mRoot, leaves); 933 925 934 926 BvhNodeContainer::const_iterator bit, bit_end = leaves.end(); … … 998 990 999 991 std::stack<BvhNode *> nodeStack; 1000 nodeStack.push(m StaticRoot);992 nodeStack.push(mRoot); 1001 993 1002 994 while (!nodeStack.empty()) … … 1039 1031 const Vector3 u = box.Max(); 1040 1032 1033 /////////// 1034 //-- render AABB as triangle strips 1035 1041 1036 glBegin(GL_TRIANGLE_STRIP); 1042 { 1043 /////////// 1044 //-- render AABB as triangle strips 1045 1046 // render first half of AABB 1047 glVertex3f(l.x, l.y, u.z); 1048 glVertex3f(u.x, l.y, u.z); 1049 glVertex3f(l.x, u.y, u.z); 1050 glVertex3f(u.x, u.y, u.z); 1051 glVertex3f(l.x, u.y, l.z); 1052 glVertex3f(u.x, u.y, l.z); 1053 glVertex3f(l.x, l.y, l.z); 1054 glVertex3f(u.x, l.y, l.z); 1055 1056 glPrimitiveRestartNV(); 1057 1058 // render second half of AABB 1059 glVertex3f(l.x, u.y, u.z); 1060 glVertex3f(l.x, u.y, l.z); 1061 glVertex3f(l.x, l.y, u.z); 1062 glVertex3f(l.x, l.y, l.z); 1063 glVertex3f(u.x, l.y, u.z); 1064 glVertex3f(u.x, l.y, l.z); 1065 glVertex3f(u.x, u.y, u.z); 1066 glVertex3f(u.x, u.y, l.z); 1067 } 1037 1038 // render first half of AABB 1039 glVertex3f(l.x, l.y, u.z); 1040 glVertex3f(u.x, l.y, u.z); 1041 glVertex3f(l.x, u.y, u.z); 1042 glVertex3f(u.x, u.y, u.z); 1043 glVertex3f(l.x, u.y, l.z); 1044 glVertex3f(u.x, u.y, l.z); 1045 glVertex3f(l.x, l.y, l.z); 1046 glVertex3f(u.x, l.y, l.z); 1047 1048 glPrimitiveRestartNV(); 1049 1050 // render second half of AABB 1051 glVertex3f(l.x, u.y, u.z); 1052 glVertex3f(l.x, u.y, l.z); 1053 glVertex3f(l.x, l.y, u.z); 1054 glVertex3f(l.x, l.y, l.z); 1055 glVertex3f(u.x, l.y, u.z); 1056 glVertex3f(u.x, l.y, l.z); 1057 glVertex3f(u.x, u.y, u.z); 1058 glVertex3f(u.x, u.y, l.z); 1059 1068 1060 glEnd(); 1069 1061 } 1070 1071 1072 1062 1073 1063 … … 1123 1113 bool useTightBounds) 1124 1114 { 1125 glDisable(GL_TEXTURE_2D);1126 glDisable(GL_LIGHTING);1127 1115 glColor3f(1, 1, 1); 1128 1116 1129 1117 // hack: for deferred shading we have to define a material 1130 1118 static Technique boxMat; 1119 boxMat.SetLightingEnabled(false); 1120 1131 1121 boxMat.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)); 1132 1122 … … 1135 1125 if (!useTightBounds) 1136 1126 { 1137 RenderBoxForViz(node->GetBox()); 1127 //RenderBoxForViz(node->GetBox()); 1128 glPolygonMode(GL_FRONT, GL_LINE); 1129 RenderBoundingBoxImmediate(node->GetBox()); 1130 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 1138 1131 } 1139 1132 else … … 1144 1137 } 1145 1138 } 1146 1147 glEnable(GL_LIGHTING);1148 glEnable(GL_TEXTURE_2D);1149 1139 } 1150 1140 … … 1297 1287 1298 1288 1299 } 1289 bool Bvh::IntersectsNearPlane(BvhNode *node) const 1290 { 1291 float distanceToNearPlane = node->GetBox().GetMinDistance(sNearPlane); 1292 // we stored the near plane distance => we can use it also here 1293 //float distanceToNearPlane = node->GetDistance(); 1294 1295 return distanceToNearPlane < sNear; 1296 } 1297 1298 1299 }
Note: See TracChangeset
for help on using the changeset viewer.