Changeset 3053 for GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
- Timestamp:
- 10/21/08 12:47:27 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
r3052 r3053 204 204 205 205 mVboId = -1; 206 207 mMaxDepthForDynamicBranch = 10; 206 208 } 207 209 … … 397 399 // store near plane 398 400 sNearPlane = Plane3(cam->GetDirection(), cam->GetPosition()); 401 402 // rebuild dynamic part of the hierarchy 403 if (!mDynamicEntities.empty()) 404 { 405 UpdateDynamicBranch(); 406 } 399 407 } 400 408 … … 1110 1118 1111 1119 1112 } 1120 //////////////////////// 1121 // construction of the dynamic hierarchy 1122 1123 int Bvh::SortTriangles(BvhLeaf *leaf, 1124 int axis, 1125 float position, 1126 SceneEntityContainer &entities) 1127 { 1128 int i = leaf->mFirst; 1129 int j = leaf->mLast; 1130 1131 while (1) 1132 { 1133 while (entities[i]->GetCenter()[axis] < position) 1134 ++ i; 1135 1136 while (position < entities[j]->GetCenter()[axis]) 1137 -- j; 1138 1139 if (i < j) 1140 { 1141 swap(entities[i], entities[j]); 1142 1143 ++ i; 1144 -- j; 1145 } 1146 else 1147 { 1148 break; 1149 } 1150 } 1151 1152 return j; 1153 } 1154 1155 1156 int Bvh::SortTrianglesSpatialMedian(BvhLeaf *leaf, 1157 int axis, 1158 SceneEntityContainer &entities) 1159 { 1160 // spatial median 1161 float m = leaf->mBox.Center()[axis]; 1162 return SortTriangles(leaf, axis, m, entities); 1163 } 1164 1165 1166 BvhNode *Bvh::SubdivideLeaf(BvhLeaf *leaf, 1167 int parentAxis, 1168 SceneEntityContainer &entities) 1169 { 1170 if (TerminationCriteriaMet(leaf)) 1171 return leaf; 1172 1173 //int axis = leaf->mBox.MajorAxis(); 1174 int axis = (parentAxis + 1) % 3; 1175 1176 // position of the split in the partailly sorted array of triangles 1177 // corresponding to this leaf 1178 int split = -1; 1179 const int scale = 20; 1180 1181 float pos = -1.0f; 1182 1183 // Spatial median subdivision 1184 split = SortTrianglesSpatialMedian(leaf, axis, entities); 1185 pos = leaf->mBox.Center()[axis]; 1186 1187 if (split == leaf->mLast) 1188 { 1189 // no split could be achieved => just halve number of objects 1190 split = (leaf->mLast - leaf->mFirst) / 2; 1191 cerr << "no reduction " << leaf->CountPrimitives() << " " << leaf->mFirst << " " << split << " " << leaf->mLast << endl; 1192 } 1193 1194 // create two more nodes 1195 mNumNodes += 2; 1196 BvhInterior *parent = new BvhInterior(leaf->GetParent()); 1197 BvhLeaf *front = new BvhLeaf(parent); 1198 1199 parent->mAxis = axis; 1200 parent->mBox = leaf->mBox; 1201 parent->mDepth = leaf->mDepth; 1202 1203 parent->mBack = leaf; 1204 parent->mFront = front; 1205 //parent->mPosition = pos; 1206 1207 // now assign the triangles to the subnodes 1208 front->mFirst = split + 1; 1209 front->mLast = leaf->mLast; 1210 front->mDepth = leaf->mDepth + 1; 1211 leaf->mLast = split; 1212 leaf->mDepth = front->mDepth; 1213 leaf->mParent = parent; 1214 1215 // reset box 1216 leaf->mBox.Initialize(); 1217 1218 // recursively continue subdivision 1219 parent->mBack = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mBack), axis, entities); 1220 parent->mFront = SubdivideLeaf(static_cast<BvhLeaf *>(parent->mFront), axis, entities); 1221 1222 return parent; 1223 } 1224 1225 1226 bool Bvh::TerminationCriteriaMet(BvhLeaf *leaf) const 1227 { 1228 const bool criteriaMet = 1229 (leaf->mDepth > mMaxDepthForDynamicBranch) || 1230 (leaf->CountPrimitives() == 1); 1231 1232 return criteriaMet; 1233 } 1234 1235 1236 void Bvh::UpdateDynamicBranch() 1237 { 1238 BvhNode *dynamicRoot = static_cast<BvhInterior *>(mRoot)->mBack; 1239 1240 cout << "updating dynamic branch" << endl; 1241 1242 // delete old branch 1243 if (!dynamicRoot->IsLeaf()) 1244 { 1245 cout << "deleting old branch" << endl; 1246 1247 DEL_PTR(dynamicRoot); 1248 1249 dynamicRoot = new BvhLeaf(mRoot); 1250 dynamicRoot->mBox = mRoot->mBox; 1251 } 1252 1253 dynamicRoot = SubdivideLeaf(static_cast<BvhLeaf *>(dynamicRoot), 0, mDynamicEntities); 1254 } 1255 1256 }
Note: See TracChangeset
for help on using the changeset viewer.