Advertisement
Guest User

VoxelGeneration.cpp

a guest
Oct 13th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.57 KB | None | 0 0
  1. #include "VoxelGeneration.h"
  2.  
  3.  
  4. VoxelGeneration::VoxelGeneration(HWND hwnd, ID3D11Device* device)
  5. {
  6.     bool result;
  7.     _voxelVertexBuffer = 0;
  8.     _meshVertexBuffer  = 0;
  9.     _meshIndexBuffer   = 0;
  10.  
  11.     result = Initialize(device);
  12.  
  13.     if (result) {
  14.         MessageBox(hwnd, L"Failed to Initialize VoxelGeneration", L"Error", MB_OK);
  15.     }
  16. }
  17.  
  18. bool VoxelGeneration::Initialize(ID3D11Device* device)
  19. {
  20.     // Method Variables
  21.     std::vector<D3DXVECTOR3> vertices;
  22.  
  23.     // Set the basic voxel Params
  24.     _boxSize   = 16.0f;
  25.     _cellSize  = 1.0f;
  26.     _boxCenter = D3DXVECTOR3(0, 0, 0);
  27.     _voxelDepth = 1;
  28.  
  29.     InitializeVoxels();
  30.     InitializeBuffers(device);
  31.  
  32.     /**
  33.      Create first 4
  34.      Check if object is in any
  35.      subdivide found collison cubes
  36.      check new voxel size == _cellSize
  37.      repeat from step 2 until voxelSize == _cellSize
  38.      Generate structure
  39.      */
  40.  
  41.     return true;
  42. }
  43.  
  44. bool VoxelGeneration::InitializeVoxels()
  45. {
  46.     GRIDCELL baseBox;
  47.     vector<GRIDCELL> potentialVoxels0;
  48.     vector<GRIDCELL> potentialVoxels1;
  49.     D3DXVECTOR3 center = _boxCenter;
  50.  
  51.  
  52.     // Create the Base Voxel
  53.     baseBox.p[0] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
  54.     baseBox.p[1] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2));
  55.     baseBox.p[2] = D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2));
  56.     baseBox.p[3] = D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
  57.  
  58.     baseBox.val[0] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
  59.     baseBox.val[1] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2)));
  60.     baseBox.val[2] = Function(D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2)));
  61.     baseBox.val[3] = Function(D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
  62.  
  63.     baseBox.p[4] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
  64.     baseBox.p[5] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), (_boxSize / 2));
  65.     baseBox.p[6] = D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), (_boxSize / 2));
  66.     baseBox.p[7] = D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
  67.  
  68.     baseBox.val[4] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
  69.     baseBox.val[5] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), (_boxSize / 2)));
  70.     baseBox.val[6] = Function(D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), (_boxSize / 2)));
  71.     baseBox.val[7] = Function(D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
  72.  
  73.     voxels.push_back(baseBox);
  74.  
  75.     // Loop to decrease size of voxels creating less total voxels. stops when voxels are at desired size
  76.     for (int size = (int)_boxSize / 2; size > (int)_cellSize; size /= 2)
  77.     {
  78.         // Set size every iteration just incase the above for statement doesn't set it.
  79.         //size = (_boxSize / (2^(int)_voxelDepth)) + 3;
  80.         for (GRIDCELL voxel : voxels)
  81.         {
  82.             // Get the center of a previous valid voxel
  83.             center = ((voxel.p[6] + voxel.p[0])/2);
  84.             // Generate 4 sub voxels
  85.  
  86.             GRIDCELL *tempVoxels = GenerateVoxels(center, size);
  87.  
  88.             // Check Validity of the sub voxels
  89.             for (int i = 0; i < 4; i++) {
  90.             //for (GRIDCELL tempVoxel : tVoxels) {
  91.                 if (CheckVoxelValidity(tempVoxels[i])) {
  92.                     potentialVoxels1.push_back(tempVoxels[i]);
  93.                 }
  94.             }
  95.         }
  96.         // replace voxels with potentialVoxels; clear potentialVoxels
  97.         voxels.clear();
  98.         voxels = move(potentialVoxels1);
  99.         if (!potentialVoxels1.empty())
  100.             potentialVoxels1.clear();
  101.     }
  102.  
  103.     // Delete Temp Variables
  104.     //std::vector<GRIDCELL>().swap(potentialVoxels);
  105.     delete center;
  106.  
  107.     return true;
  108. }
  109.  
  110. bool VoxelGeneration::InitializeBuffers(ID3D11Device* device)
  111. {
  112.     VertexPosition* voxelVertices;
  113.     D3D11_BUFFER_DESC voxelVertexBufferDesc;
  114.     D3D11_SUBRESOURCE_DATA voxelVertexData;
  115.     HRESULT result;
  116.  
  117.     _voxelVertexCount = voxels.size() * 24;
  118.  
  119.     voxelVertices = new VertexPosition[_voxelVertexCount];
  120.     if (!voxelVertices)
  121.         return false;
  122.  
  123.     // For every voxel create vertex pairs for the Line Lists
  124.     int i = 0;
  125.     for (unsigned int voxelIndex = 0; voxelIndex < voxels.size(); voxelIndex++)
  126.     {
  127.         voxelVertices[i++].position = voxels[voxelIndex].p[0];  // 0
  128.         voxelVertices[i++].position = voxels[voxelIndex].p[1];  // 1
  129.  
  130.         voxelVertices[i++].position = voxels[voxelIndex].p[1];  // 1
  131.         voxelVertices[i++].position = voxels[voxelIndex].p[2];  // 2
  132.  
  133.         voxelVertices[i++].position = voxels[voxelIndex].p[2];  // 2
  134.         voxelVertices[i++].position = voxels[voxelIndex].p[3];  // 3
  135.  
  136.         voxelVertices[i++].position = voxels[voxelIndex].p[3];  // 3
  137.         voxelVertices[i++].position = voxels[voxelIndex].p[0];  // 0
  138.  
  139.         voxelVertices[i++].position = voxels[voxelIndex].p[4];  // 4
  140.         voxelVertices[i++].position = voxels[voxelIndex].p[5];  // 5
  141.  
  142.         voxelVertices[i++].position = voxels[voxelIndex].p[5];  // 5
  143.         voxelVertices[i++].position = voxels[voxelIndex].p[6];  // 6
  144.  
  145.         voxelVertices[i++].position = voxels[voxelIndex].p[6];  // 6
  146.         voxelVertices[i++].position = voxels[voxelIndex].p[7];  // 7
  147.  
  148.         voxelVertices[i++].position = voxels[voxelIndex].p[7];  // 7
  149.         voxelVertices[i++].position = voxels[voxelIndex].p[4];  // 4
  150.  
  151.         voxelVertices[i++].position = voxels[voxelIndex].p[0];  // 0
  152.         voxelVertices[i++].position = voxels[voxelIndex].p[4];  // 4
  153.  
  154.         voxelVertices[i++].position = voxels[voxelIndex].p[1];  // 1
  155.         voxelVertices[i++].position = voxels[voxelIndex].p[5];  // 5
  156.  
  157.         voxelVertices[i++].position = voxels[voxelIndex].p[2];  // 2
  158.         voxelVertices[i++].position = voxels[voxelIndex].p[6];  // 6
  159.  
  160.         voxelVertices[i++].position = voxels[voxelIndex].p[3];  // 3
  161.         voxelVertices[i++].position = voxels[voxelIndex].p[7];  // 7
  162.     }
  163.  
  164.     // Set up the description of the static voxel vertex buffer
  165.     voxelVertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  166.     voxelVertexBufferDesc.ByteWidth = sizeof(VertexPosition) * _voxelVertexCount;
  167.     voxelVertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  168.     voxelVertexBufferDesc.CPUAccessFlags = 0;
  169.     voxelVertexBufferDesc.MiscFlags = 0;
  170.     voxelVertexBufferDesc.StructureByteStride = 0;
  171.  
  172.     // Give the subresource structure a pointer to the vertex data.
  173.     voxelVertexData.pSysMem = voxelVertices;
  174.     voxelVertexData.SysMemPitch = 0;
  175.     voxelVertexData.SysMemSlicePitch = 0;
  176.  
  177.     // Create the vertex beffer
  178.     result = device->CreateBuffer(&voxelVertexBufferDesc, &voxelVertexData, &_voxelVertexBuffer);
  179.     if (FAILED(result))
  180.         return false;
  181.  
  182.  
  183.     // Release the arrays and vectors now that the buffers are created on RAM
  184.     delete[] voxelVertices;
  185.     voxelVertices = 0;
  186.  
  187.     std::vector<GRIDCELL>().swap(voxels);
  188.  
  189.     return true;
  190. }
  191.  
  192. GRIDCELL* VoxelGeneration::GenerateVoxels(D3DXVECTOR3 center, int size)
  193. {
  194.     GRIDCELL* tVoxels = new GRIDCELL[8];
  195.  
  196.             D3DXVECTOR3 tVertices[27];
  197.  
  198.             // Create new Voxel size and first vertex position.
  199.             D3DXVECTOR3 start = D3DXVECTOR3(center.x - size, center.y - size, center.z - size);
  200.  
  201.             // Generate vertices for the 4 voxels
  202.             int i = 0;
  203.             for (int y = 0; y < 3; y++) {
  204.                 for (int x = 0; x < 3; x++) {
  205.                     for (int z = 0; z < 3; z++) {
  206.                         tVertices[i++] = D3DXVECTOR3(start.x + (size * x) , start.y + (size * y), start.z + (size * z));
  207.                     }
  208.                 }
  209.             }
  210.  
  211.             // Generate the voxels
  212.             i = 0;
  213.             for (int y = 0; y < 2; y++) {
  214.                 for (int x = 0; x < 2; x++) {
  215.                     for (int z = 0; z < 2; z++) {
  216.                         tVoxels[i].p[0] = tVertices[(z + 0) + (x + 0) * 3 + (y + 0) * 9];
  217.                         tVoxels[i].p[1] = tVertices[(z + 1) + (x + 0) * 3 + (y + 0) * 9];
  218.                         tVoxels[i].p[2] = tVertices[(z + 1) + (x + 1) * 3 + (y + 0) * 9];
  219.                         tVoxels[i].p[3] = tVertices[(z + 0) + (x + 1) * 3 + (y + 0) * 9];
  220.  
  221.                         tVoxels[i].val[0] = Function(tVertices[(z + 0) + (x + 0) * 3 + (y + 0) * 9]);
  222.                         tVoxels[i].val[1] = Function(tVertices[(z + 1) + (x + 0) * 3 + (y + 0) * 9]);
  223.                         tVoxels[i].val[2] = Function(tVertices[(z + 1) + (x + 1) * 3 + (y + 0) * 9]);
  224.                         tVoxels[i].val[3] = Function(tVertices[(z + 0) + (x + 1) * 3 + (y + 0) * 9]);
  225.  
  226.                         tVoxels[i].p[4] = tVertices[(z + 0) + (x + 0) * 3 + (y + 1) * 9];
  227.                         tVoxels[i].p[5] = tVertices[(z + 1) + (x + 0) * 3 + (y + 1) * 9];
  228.                         tVoxels[i].p[6] = tVertices[(z + 1) + (x + 1) * 3 + (y + 1) * 9];
  229.                         tVoxels[i].p[7] = tVertices[(z + 0) + (x + 1) * 3 + (y + 1) * 9];
  230.  
  231.                         tVoxels[i].val[4] = Function(tVertices[(z + 0) + (x + 0) * 3 + (y + 1) * 9]);
  232.                         tVoxels[i].val[5] = Function(tVertices[(z + 1) + (x + 0) * 3 + (y + 1) * 9]);
  233.                         tVoxels[i].val[6] = Function(tVertices[(z + 1) + (x + 1) * 3 + (y + 1) * 9]);
  234.                         tVoxels[i].val[7] = Function(tVertices[(z + 0) + (x + 1) * 3 + (y + 1) * 9]);
  235.                         i++;
  236.                     }
  237.                 }
  238.             }
  239.     return tVoxels;
  240. }
  241.  
  242.  
  243. float VoxelGeneration::Function(D3DXVECTOR3 pos)
  244. {
  245.     return pos.x * pos.x + pos.y * pos.y + pos.z * pos.z - 190.0f;
  246. }
  247.  
  248. int VoxelGeneration::GetVertexCount()
  249. {
  250.     return _voxelVertexCount;
  251. }
  252.  
  253. void VoxelGeneration::Shutdown()
  254. {
  255.     if (_voxelVertexBuffer)
  256.     {
  257.         _voxelVertexBuffer->Release();
  258.         _voxelVertexBuffer = 0;
  259.     }
  260. }
  261.  
  262. bool VoxelGeneration::RenderVoxels(ID3D11DeviceContext* deviceContext)
  263. {
  264.     unsigned int stride;
  265.     unsigned int offset;
  266.  
  267.     stride = sizeof(VertexPosition);
  268.     offset = 0;
  269.  
  270.     // Set the vertex buffer to active in the input assembler so it can be rendered.
  271.     deviceContext->IASetVertexBuffers(0, 1, &_voxelVertexBuffer, &stride, &offset);
  272.  
  273.     // Set the type of primitive that should be rendered from this vertex buffer. Line list.
  274.     deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
  275.  
  276.     return true;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement