SHARE
TWEET

BVH creation code

a guest Jul 3rd, 2018 222 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BVHNode* CreateBVHNode(BVHNodeBBox* bboxData, int count)
  2. {
  3.     D3DXVECTOR4 minBounds = { FLT_MAX, FLT_MAX, FLT_MAX, 0 };
  4.     D3DXVECTOR4 maxBounds = { -FLT_MAX, -FLT_MAX, -FLT_MAX, 0 };
  5.  
  6.     for (int i = 0; i < count; i++)
  7.     {
  8.         //find bounding box
  9.         D3DXVec4Minimize(&minBounds, &bboxData[i].m_minBounds, &minBounds);
  10.         D3DXVec4Maximize(&maxBounds, &bboxData[i].m_maxBounds, &maxBounds);
  11.     }
  12.  
  13.     D3DXVECTOR4 bboxSize = maxBounds - minBounds;
  14.  
  15.     float maxDim = max(max(bboxSize.x, bboxSize.y), bboxSize.z);
  16.  
  17.     if (maxDim == bboxSize.x)
  18.     {
  19.         //Sort bounding boxes along he X axis
  20.     }
  21.     else if (maxDim == bboxSize.y)
  22.     {
  23.         //Sort bounding boxes along he Y axis
  24.     }
  25.     else
  26.     {
  27.         //Sort bounding boxes along he Z axis
  28.     }
  29.  
  30.     BVHNode* node;
  31.     node = new BVHNode();
  32.  
  33.     if (count == 1)
  34.     {
  35.         //this is a leaf node
  36.         node->m_left = nullptr;
  37.         node->m_right = nullptr;
  38.  
  39.         node->m_box = *bboxData;
  40.     }
  41.     else
  42.     {
  43.         //create the two branches
  44.         node->m_left = CreateBVHNode(bboxData, count / 2);
  45.         node->m_right = CreateBVHNode(bboxData + count / 2, count - count / 2);
  46.  
  47.         //find bounding box
  48.         D3DXVec4Minimize(   &node->m_box.m_minBounds,
  49.                             &node->m_left->m_box.m_minBounds,
  50.                             &node->m_right->m_box.m_minBounds);
  51.         D3DXVec4Maximize(   &node->m_box.m_maxBounds,
  52.                             &node->m_left->m_box.m_maxBounds,
  53.                             &node->m_right->m_box.m_maxBounds);
  54.  
  55.         //this is an intermediate
  56.         node->m_box.m_minBounds.w = -1.0f;
  57.     }
  58.  
  59.     return node;
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top