Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1.  
  2. XMFLOAT3 HeightMapApplication::getVertexNormal(INT xPos, INT yPos) const
  3. {
  4.     XMFLOAT3 averagedFloat3(0.0f, 0.0f, 0.0f);
  5.     XMVECTOR tempA, tempB;
  6.  
  7.     XMVECTOR* pVectors = nullptr;
  8.     INT pVecSize = 0;
  9.  
  10.     auto toIndex = [](INT x, INT y, INT width)
  11.     {
  12.         return (x + y * width);
  13.     };
  14.  
  15.     enum EGridPos
  16.     {
  17.         TopLeft,
  18.         TopRight,
  19.         BottomLeft,
  20.         BottomRight,
  21.         LeftBoundry,
  22.         RightBoundry,
  23.         TopBoundry,
  24.         BottomBoundry,
  25.         Central
  26.     };
  27.  
  28.     EGridPos gridPos = Central;
  29.     //determine grid state
  30.     if (xPos == 0 || xPos == m_HeightMapWidth - 1 || yPos == 0 || yPos == m_HeightMapWidth - 1)
  31.     {
  32.         return averagedFloat3;
  33.     }
  34.  
  35.     switch (gridPos)
  36.     {
  37.  
  38.     case TopLeft:
  39.     {
  40.         pVecSize = 3;
  41.         pVectors = new XMVECTOR[pVecSize];
  42.  
  43.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
  44.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  45.         pVectors[0] = tempA - tempB;
  46.  
  47.  
  48.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos + 1, m_HeightMapWidth)]);
  49.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  50.         pVectors[1] = tempA - tempB;
  51.  
  52.  
  53.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos + 1, m_HeightMapWidth)]);
  54.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  55.         pVectors[2] = tempA - tempB;
  56.  
  57.     }
  58.     break;
  59.  
  60.     case TopRight:
  61.     {
  62.         pVecSize = 3;
  63.         pVectors = new XMVECTOR[pVecSize];
  64.  
  65.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos, m_HeightMapWidth)]);
  66.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  67.         pVectors[0] = tempA - tempB;
  68.  
  69.  
  70.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos + 1, m_HeightMapWidth)]);
  71.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  72.         pVectors[1] = tempA - tempB;
  73.  
  74.  
  75.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos - 1, m_HeightMapWidth)]);
  76.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  77.         pVectors[2] = tempA - tempB;
  78.     }
  79.     break;
  80.  
  81.     case BottomLeft:
  82.     {
  83.         pVecSize = 3;
  84.         pVectors = new XMVECTOR[pVecSize];
  85.  
  86.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
  87.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  88.         pVectors[0] = tempA - tempB;
  89.  
  90.  
  91.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos - 1, m_HeightMapWidth)]);
  92.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  93.         pVectors[1] = tempA - tempB;
  94.  
  95.  
  96.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos - 1, m_HeightMapWidth)]);
  97.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  98.         pVectors[2] = tempA - tempB;
  99.     }
  100.     break;
  101.  
  102.     case BottomRight:
  103.     {
  104.         pVecSize = 3;
  105.         pVectors = new XMVECTOR[pVecSize];
  106.  
  107.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos, m_HeightMapWidth)]);
  108.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  109.         pVectors[0] = tempA - tempB;
  110.  
  111.  
  112.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos - 1, m_HeightMapWidth)]);
  113.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  114.         pVectors[1] = tempA - tempB;
  115.  
  116.  
  117.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos - 1, m_HeightMapWidth)]);
  118.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  119.         pVectors[2] = tempA - tempB;
  120.     }
  121.     break;
  122.  
  123.  
  124.     case Central:
  125.     {
  126.         pVecSize = 8;
  127.         pVectors = new XMVECTOR[pVecSize];
  128.  
  129.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
  130.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  131.         pVectors[0] = tempA - tempB;
  132.  
  133.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos + 1, m_HeightMapWidth)]);
  134.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  135.         pVectors[1] = tempA - tempB;
  136.  
  137.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos + 1, m_HeightMapWidth)]);
  138.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  139.         pVectors[2] = tempA - tempB;
  140.  
  141.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos, m_HeightMapWidth)]);
  142.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  143.         pVectors[3] = tempA - tempB;
  144.  
  145.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos - 1, m_HeightMapWidth)]);
  146.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  147.         pVectors[4] = tempA - tempB;
  148.  
  149.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos + 1, m_HeightMapWidth)]);
  150.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  151.         pVectors[5] = tempA - tempB;
  152.  
  153.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
  154.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  155.         pVectors[6] = tempA - tempB;
  156.  
  157.         tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos - 1, m_HeightMapWidth)]);
  158.         tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
  159.         pVectors[7] = tempA - tempB;
  160.     }
  161.     break;
  162.  
  163.     default: break;
  164.  
  165.     }
  166.  
  167.     XMVECTOR averagedVec;
  168.  
  169.     switch (pVecSize)
  170.     {
  171.     default:break;
  172.     case 3:
  173.  
  174.         averagedVec += XMVector3Cross(pVectors[0], pVectors[1]);
  175.         averagedVec += XMVector3Cross(pVectors[0], pVectors[2]);
  176.         averagedVec /= 2.0f;
  177.         break;
  178.     case 5:
  179.         break;
  180.     case 8:
  181.         for (int i = 0; i < pVecSize / 2; i += 2)
  182.         {
  183.             averagedVec += XMVector3Cross(pVectors[i], pVectors[i + 1]);
  184.         }
  185.         averagedVec /= (float)(pVecSize / 2);
  186.         break;
  187.     }
  188.  
  189.     XMStoreFloat3(&averagedFloat3, averagedVec);
  190.  
  191.     delete[] pVectors;
  192.  
  193.     return averagedFloat3;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement