Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XMFLOAT3 HeightMapApplication::getVertexNormal(INT xPos, INT yPos) const
- {
- XMFLOAT3 averagedFloat3(0.0f, 0.0f, 0.0f);
- XMVECTOR tempA, tempB;
- XMVECTOR* pVectors = nullptr;
- INT pVecSize = 0;
- auto toIndex = [](INT x, INT y, INT width)
- {
- return (x + y * width);
- };
- enum EGridPos
- {
- TopLeft,
- TopRight,
- BottomLeft,
- BottomRight,
- LeftBoundry,
- RightBoundry,
- TopBoundry,
- BottomBoundry,
- Central
- };
- EGridPos gridPos = Central;
- //determine grid state
- if (xPos == 0 || xPos == m_HeightMapWidth - 1 || yPos == 0 || yPos == m_HeightMapWidth - 1)
- {
- return averagedFloat3;
- }
- switch (gridPos)
- {
- case TopLeft:
- {
- pVecSize = 3;
- pVectors = new XMVECTOR[pVecSize];
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[0] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos + 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[1] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos + 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[2] = tempA - tempB;
- }
- break;
- case TopRight:
- {
- pVecSize = 3;
- pVectors = new XMVECTOR[pVecSize];
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[0] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos + 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[1] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos - 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[2] = tempA - tempB;
- }
- break;
- case BottomLeft:
- {
- pVecSize = 3;
- pVectors = new XMVECTOR[pVecSize];
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[0] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos - 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[1] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos - 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[2] = tempA - tempB;
- }
- break;
- case BottomRight:
- {
- pVecSize = 3;
- pVectors = new XMVECTOR[pVecSize];
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[0] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos - 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[1] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos - 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[2] = tempA - tempB;
- }
- break;
- case Central:
- {
- pVecSize = 8;
- pVectors = new XMVECTOR[pVecSize];
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[0] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos + 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[1] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos + 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[2] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[3] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos - 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[4] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos - 1, yPos + 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[5] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[6] = tempA - tempB;
- tempA = XMLoadFloat3(&m_pHeightMap[toIndex(xPos + 1, yPos - 1, m_HeightMapWidth)]);
- tempB = XMLoadFloat3(&m_pHeightMap[toIndex(xPos, yPos, m_HeightMapWidth)]);
- pVectors[7] = tempA - tempB;
- }
- break;
- default: break;
- }
- XMVECTOR averagedVec;
- switch (pVecSize)
- {
- default:break;
- case 3:
- averagedVec += XMVector3Cross(pVectors[0], pVectors[1]);
- averagedVec += XMVector3Cross(pVectors[0], pVectors[2]);
- averagedVec /= 2.0f;
- break;
- case 5:
- break;
- case 8:
- for (int i = 0; i < pVecSize / 2; i += 2)
- {
- averagedVec += XMVector3Cross(pVectors[i], pVectors[i + 1]);
- }
- averagedVec /= (float)(pVecSize / 2);
- break;
- }
- XMStoreFloat3(&averagedFloat3, averagedVec);
- delete[] pVectors;
- return averagedFloat3;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement