| 314 | } |
| 315 | |
| 316 | void AABBTree::BuildRecursive(uint32_t nodeIndex, uint32_t* faces, uint32_t numFaces) |
| 317 | { |
| 318 | const uint32_t kMaxFacesPerLeaf = 6; |
| 319 | |
| 320 | // if we've run out of nodes allocate some more |
| 321 | if (nodeIndex >= m_nodes.size()) |
| 322 | { |
| 323 | uint32_t s = std::max(uint32_t(1.5f*m_nodes.size()), 512U); |
| 324 | |
| 325 | //cout << "Resizing tree, current size: " << m_nodes.size()*sizeof(Node) << " new size: " << s*sizeof(Node) << endl; |
| 326 | |
| 327 | m_nodes.resize(s); |
| 328 | } |
| 329 | |
| 330 | // a reference to the current node, need to be careful here as this reference may become invalid if array is resized |
| 331 | Node& n = m_nodes[nodeIndex]; |
| 332 | |
| 333 | // track max tree depth |
| 334 | ++s_depth; |
| 335 | m_treeDepth = max(m_treeDepth, s_depth); |
| 336 | |
| 337 | CalculateFaceBounds(faces, numFaces, n.m_minExtents, n.m_maxExtents); |
| 338 | |
| 339 | // calculate bounds of faces and add node |
| 340 | if (numFaces <= kMaxFacesPerLeaf) |
| 341 | { |
| 342 | n.m_faces = faces; |
| 343 | n.m_numFaces = numFaces; |
| 344 | |
| 345 | ++m_leafNodes; |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | ++m_innerNodes; |
| 350 | |
| 351 | // face counts for each branch |
| 352 | //const uint32_t leftCount = PartitionMedian(n, faces, numFaces); |
| 353 | const uint32_t leftCount = PartitionSAH(n, faces, numFaces); |
| 354 | const uint32_t rightCount = numFaces-leftCount; |
| 355 | |
| 356 | // alloc 2 nodes |
| 357 | m_nodes[nodeIndex].m_children = m_freeNode; |
| 358 | |
| 359 | // allocate two nodes |
| 360 | m_freeNode += 2; |
| 361 | |
| 362 | // split faces in half and build each side recursively |
| 363 | BuildRecursive(m_nodes[nodeIndex].m_children+0, faces, leftCount); |
| 364 | BuildRecursive(m_nodes[nodeIndex].m_children+1, faces+leftCount, rightCount); |
| 365 | } |
| 366 | |
| 367 | --s_depth; |
| 368 | } |
| 369 | |
| 370 | struct StackEntry |
| 371 | { |