Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "VoxelGeneration.h"
- VoxelGeneration::VoxelGeneration(HWND hwnd, ID3D11Device* device)
- {
- bool result;
- _voxelVertexBuffer = 0;
- _meshVertexBuffer = 0;
- _meshIndexBuffer = 0;
- result = Initialize(device);
- if (result) {
- MessageBox(hwnd, L"Failed to Initialize VoxelGeneration", L"Error", MB_OK);
- }
- }
- bool VoxelGeneration::Initialize(ID3D11Device* device)
- {
- // Method Variables
- std::vector<D3DXVECTOR3> vertices;
- // Set the basic voxel Params
- _boxSize = 16.0f;
- _cellSize = 1.0f;
- _boxCenter = D3DXVECTOR3(0, 0, 0);
- _voxelDepth = 1;
- InitializeVoxels();
- InitializeBuffers(device);
- /**
- Create first 4
- Check if object is in any
- subdivide found collison cubes
- check new voxel size == _cellSize
- repeat from step 2 until voxelSize == _cellSize
- Generate structure
- */
- return true;
- }
- bool VoxelGeneration::InitializeVoxels()
- {
- GRIDCELL baseBox;
- vector<GRIDCELL> potentialVoxels0;
- vector<GRIDCELL> potentialVoxels1;
- D3DXVECTOR3 center = _boxCenter;
- // Create the Base Voxel
- baseBox.p[0] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
- baseBox.p[1] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2));
- baseBox.p[2] = D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2));
- baseBox.p[3] = D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
- baseBox.val[0] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
- baseBox.val[1] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2)));
- baseBox.val[2] = Function(D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), (_boxSize / 2)));
- baseBox.val[3] = Function(D3DXVECTOR3((_boxSize / 2), _boxCenter.y - (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
- baseBox.p[4] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
- baseBox.p[5] = D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), (_boxSize / 2));
- baseBox.p[6] = D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), (_boxSize / 2));
- baseBox.p[7] = D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2));
- baseBox.val[4] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
- baseBox.val[5] = Function(D3DXVECTOR3(_boxCenter.x - (_boxSize / 2), (_boxSize / 2), (_boxSize / 2)));
- baseBox.val[6] = Function(D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), (_boxSize / 2)));
- baseBox.val[7] = Function(D3DXVECTOR3((_boxSize / 2), (_boxSize / 2), _boxCenter.z - (_boxSize / 2)));
- voxels.push_back(baseBox);
- // Loop to decrease size of voxels creating less total voxels. stops when voxels are at desired size
- for (int size = (int)_boxSize / 2; size > (int)_cellSize; size /= 2)
- {
- // Set size every iteration just incase the above for statement doesn't set it.
- //size = (_boxSize / (2^(int)_voxelDepth)) + 3;
- for (GRIDCELL voxel : voxels)
- {
- // Get the center of a previous valid voxel
- center = ((voxel.p[6] + voxel.p[0])/2);
- // Generate 4 sub voxels
- GRIDCELL *tempVoxels = GenerateVoxels(center, size);
- // Check Validity of the sub voxels
- for (int i = 0; i < 4; i++) {
- //for (GRIDCELL tempVoxel : tVoxels) {
- if (CheckVoxelValidity(tempVoxels[i])) {
- potentialVoxels1.push_back(tempVoxels[i]);
- }
- }
- }
- // replace voxels with potentialVoxels; clear potentialVoxels
- voxels.clear();
- voxels = move(potentialVoxels1);
- if (!potentialVoxels1.empty())
- potentialVoxels1.clear();
- }
- // Delete Temp Variables
- //std::vector<GRIDCELL>().swap(potentialVoxels);
- delete center;
- return true;
- }
- bool VoxelGeneration::InitializeBuffers(ID3D11Device* device)
- {
- VertexPosition* voxelVertices;
- D3D11_BUFFER_DESC voxelVertexBufferDesc;
- D3D11_SUBRESOURCE_DATA voxelVertexData;
- HRESULT result;
- _voxelVertexCount = voxels.size() * 24;
- voxelVertices = new VertexPosition[_voxelVertexCount];
- if (!voxelVertices)
- return false;
- // For every voxel create vertex pairs for the Line Lists
- int i = 0;
- for (unsigned int voxelIndex = 0; voxelIndex < voxels.size(); voxelIndex++)
- {
- voxelVertices[i++].position = voxels[voxelIndex].p[0]; // 0
- voxelVertices[i++].position = voxels[voxelIndex].p[1]; // 1
- voxelVertices[i++].position = voxels[voxelIndex].p[1]; // 1
- voxelVertices[i++].position = voxels[voxelIndex].p[2]; // 2
- voxelVertices[i++].position = voxels[voxelIndex].p[2]; // 2
- voxelVertices[i++].position = voxels[voxelIndex].p[3]; // 3
- voxelVertices[i++].position = voxels[voxelIndex].p[3]; // 3
- voxelVertices[i++].position = voxels[voxelIndex].p[0]; // 0
- voxelVertices[i++].position = voxels[voxelIndex].p[4]; // 4
- voxelVertices[i++].position = voxels[voxelIndex].p[5]; // 5
- voxelVertices[i++].position = voxels[voxelIndex].p[5]; // 5
- voxelVertices[i++].position = voxels[voxelIndex].p[6]; // 6
- voxelVertices[i++].position = voxels[voxelIndex].p[6]; // 6
- voxelVertices[i++].position = voxels[voxelIndex].p[7]; // 7
- voxelVertices[i++].position = voxels[voxelIndex].p[7]; // 7
- voxelVertices[i++].position = voxels[voxelIndex].p[4]; // 4
- voxelVertices[i++].position = voxels[voxelIndex].p[0]; // 0
- voxelVertices[i++].position = voxels[voxelIndex].p[4]; // 4
- voxelVertices[i++].position = voxels[voxelIndex].p[1]; // 1
- voxelVertices[i++].position = voxels[voxelIndex].p[5]; // 5
- voxelVertices[i++].position = voxels[voxelIndex].p[2]; // 2
- voxelVertices[i++].position = voxels[voxelIndex].p[6]; // 6
- voxelVertices[i++].position = voxels[voxelIndex].p[3]; // 3
- voxelVertices[i++].position = voxels[voxelIndex].p[7]; // 7
- }
- // Set up the description of the static voxel vertex buffer
- voxelVertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- voxelVertexBufferDesc.ByteWidth = sizeof(VertexPosition) * _voxelVertexCount;
- voxelVertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
- voxelVertexBufferDesc.CPUAccessFlags = 0;
- voxelVertexBufferDesc.MiscFlags = 0;
- voxelVertexBufferDesc.StructureByteStride = 0;
- // Give the subresource structure a pointer to the vertex data.
- voxelVertexData.pSysMem = voxelVertices;
- voxelVertexData.SysMemPitch = 0;
- voxelVertexData.SysMemSlicePitch = 0;
- // Create the vertex beffer
- result = device->CreateBuffer(&voxelVertexBufferDesc, &voxelVertexData, &_voxelVertexBuffer);
- if (FAILED(result))
- return false;
- // Release the arrays and vectors now that the buffers are created on RAM
- delete[] voxelVertices;
- voxelVertices = 0;
- std::vector<GRIDCELL>().swap(voxels);
- return true;
- }
- GRIDCELL* VoxelGeneration::GenerateVoxels(D3DXVECTOR3 center, int size)
- {
- GRIDCELL* tVoxels = new GRIDCELL[8];
- D3DXVECTOR3 tVertices[27];
- // Create new Voxel size and first vertex position.
- D3DXVECTOR3 start = D3DXVECTOR3(center.x - size, center.y - size, center.z - size);
- // Generate vertices for the 4 voxels
- int i = 0;
- for (int y = 0; y < 3; y++) {
- for (int x = 0; x < 3; x++) {
- for (int z = 0; z < 3; z++) {
- tVertices[i++] = D3DXVECTOR3(start.x + (size * x) , start.y + (size * y), start.z + (size * z));
- }
- }
- }
- // Generate the voxels
- i = 0;
- for (int y = 0; y < 2; y++) {
- for (int x = 0; x < 2; x++) {
- for (int z = 0; z < 2; z++) {
- tVoxels[i].p[0] = tVertices[(z + 0) + (x + 0) * 3 + (y + 0) * 9];
- tVoxels[i].p[1] = tVertices[(z + 1) + (x + 0) * 3 + (y + 0) * 9];
- tVoxels[i].p[2] = tVertices[(z + 1) + (x + 1) * 3 + (y + 0) * 9];
- tVoxels[i].p[3] = tVertices[(z + 0) + (x + 1) * 3 + (y + 0) * 9];
- tVoxels[i].val[0] = Function(tVertices[(z + 0) + (x + 0) * 3 + (y + 0) * 9]);
- tVoxels[i].val[1] = Function(tVertices[(z + 1) + (x + 0) * 3 + (y + 0) * 9]);
- tVoxels[i].val[2] = Function(tVertices[(z + 1) + (x + 1) * 3 + (y + 0) * 9]);
- tVoxels[i].val[3] = Function(tVertices[(z + 0) + (x + 1) * 3 + (y + 0) * 9]);
- tVoxels[i].p[4] = tVertices[(z + 0) + (x + 0) * 3 + (y + 1) * 9];
- tVoxels[i].p[5] = tVertices[(z + 1) + (x + 0) * 3 + (y + 1) * 9];
- tVoxels[i].p[6] = tVertices[(z + 1) + (x + 1) * 3 + (y + 1) * 9];
- tVoxels[i].p[7] = tVertices[(z + 0) + (x + 1) * 3 + (y + 1) * 9];
- tVoxels[i].val[4] = Function(tVertices[(z + 0) + (x + 0) * 3 + (y + 1) * 9]);
- tVoxels[i].val[5] = Function(tVertices[(z + 1) + (x + 0) * 3 + (y + 1) * 9]);
- tVoxels[i].val[6] = Function(tVertices[(z + 1) + (x + 1) * 3 + (y + 1) * 9]);
- tVoxels[i].val[7] = Function(tVertices[(z + 0) + (x + 1) * 3 + (y + 1) * 9]);
- i++;
- }
- }
- }
- return tVoxels;
- }
- float VoxelGeneration::Function(D3DXVECTOR3 pos)
- {
- return pos.x * pos.x + pos.y * pos.y + pos.z * pos.z - 190.0f;
- }
- int VoxelGeneration::GetVertexCount()
- {
- return _voxelVertexCount;
- }
- void VoxelGeneration::Shutdown()
- {
- if (_voxelVertexBuffer)
- {
- _voxelVertexBuffer->Release();
- _voxelVertexBuffer = 0;
- }
- }
- bool VoxelGeneration::RenderVoxels(ID3D11DeviceContext* deviceContext)
- {
- unsigned int stride;
- unsigned int offset;
- stride = sizeof(VertexPosition);
- offset = 0;
- // Set the vertex buffer to active in the input assembler so it can be rendered.
- deviceContext->IASetVertexBuffers(0, 1, &_voxelVertexBuffer, &stride, &offset);
- // Set the type of primitive that should be rendered from this vertex buffer. Line list.
- deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement