Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Terrain.h"
- Terrain::Terrain()
- {
- m_device = nullptr;
- m_deviceContext = nullptr;
- m_VertexBuffer = nullptr;
- m_IndexBuffer = nullptr;
- }
- Terrain::Terrain( ID3D11Device* device, ID3D11DeviceContext* deviceContext )
- {
- m_device = device;
- m_deviceContext = deviceContext;
- m_VertexBuffer = nullptr;
- m_IndexBuffer = nullptr;
- LoadHeightMap();
- Smooth();
- BuildTerrainBuffers();
- }
- Terrain::~Terrain()
- {
- SAFE_RELEASE( m_VertexBuffer );
- SAFE_RELEASE( m_IndexBuffer );
- }
- void Terrain::LoadHeightMap()
- {
- m_MapInfo.Filename = L"Map/map.raw";
- m_MapInfo.HeightScale = 25.0f;
- m_MapInfo.HeightMapWidth = 257; //640;
- m_MapInfo.HeightMapHeight = 257; //480;
- // A height for each vertex
- std::vector<unsigned char> in ( m_MapInfo.HeightMapWidth * m_MapInfo.HeightMapHeight );
- // Open the file
- std::ifstream inFile;
- inFile.open( m_MapInfo.Filename.c_str(), std::ios_base::binary );
- if( inFile )
- {
- // Read the RAW bytes
- inFile.read((char*)&in[0], (std::streamsize)in.size() );
- inFile.close();
- }
- // Copy data to a float array and scale it
- m_HeightMap.resize(m_MapInfo.HeightMapHeight * m_MapInfo.HeightMapWidth, 0 );
- for( UINT i = 0; i < m_MapInfo.HeightMapHeight * m_MapInfo.HeightMapWidth; ++i )
- {
- m_HeightMap[i] = (in[i] / 255.0f) * m_MapInfo.HeightScale;
- }
- }
- void Terrain::CreateGrid( float width, float depth, UINT m, UINT n, MeshData& meshData )
- {
- UINT vertexCount = m*n;
- UINT faceCount = (m-1)*(n-1)*2;
- //////////////////
- // Create vertices
- //////////////////
- float halfWidth = 0.5f * width;
- float halfDepth = 0.5f * depth;
- float dx = width / (n-1);
- float dz = depth / (m-1);
- float du = 1.0f / (n-1);
- float dv = 1.0f / (m-1);
- meshData.Vertices.resize( vertexCount );
- for( UINT i = 0; i < m; ++i )
- {
- float z = halfDepth - i*dz;
- for( UINT j = 0; j < n; ++j )
- {
- float x = -halfWidth + j*dx;
- // Position
- meshData.Vertices[i*n+j].pos[0] = x;
- meshData.Vertices[i*n+j].pos[1] = m_HeightMap[i*n+j];
- meshData.Vertices[i*n+j].pos[2] = z;
- // Normal (stämmer inte efter heightmapning)
- meshData.Vertices[i*n+j].norm[0] = 0.0f;
- meshData.Vertices[i*n+j].norm[1] = 1.0f;
- meshData.Vertices[i*n+j].norm[2] = 0.0f;
- // UV
- //meshData.Vertices[i*n+j].uv[0] = j*du;
- //meshData.Vertices[i*n+j].uv[1] = i*dv;
- meshData.Vertices[i*n+j].uv[0] = j*du; //0.0f;
- meshData.Vertices[i*n+j].uv[1] = i*dv; //0.0f;
- }
- }
- /////////////////
- // Create indices
- /////////////////
- meshData.Indices.resize( faceCount * 3 ); // 3 indicies per face
- // Iterate over each quad and compute indices
- UINT k = 0;
- for( UINT i = 0; i < m-1; ++i )
- {
- for( UINT j = 0; j < n-1; ++j )
- {
- meshData.Indices[k] = i*n+j;
- meshData.Indices[k+1] = i*n+j+1;
- meshData.Indices[k+2] = (i+1)*n+j;
- meshData.Indices[k+3] = (i+1)*n+j;
- meshData.Indices[k+4] = i*n+j+1;
- meshData.Indices[k+5] = (i+1)*n+j+1;
- k += 6; // next quad
- }
- }
- m_MeshData = meshData;
- }
- void Terrain::BuildTerrainBuffers()
- {
- MeshData grid;
- CreateGrid( (float)m_MapInfo.HeightMapWidth, (float)m_MapInfo.HeightMapHeight, 257, 257, grid );
- m_GridIndexCount = grid.Indices.size();
- D3D11_BUFFER_DESC vbd;
- vbd.Usage = D3D11_USAGE_IMMUTABLE;
- vbd.ByteWidth = sizeof(Vertex) * grid.Vertices.size();
- vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
- vbd.CPUAccessFlags = 0;
- vbd.MiscFlags = 0;
- D3D11_SUBRESOURCE_DATA vinitData;
- vinitData.pSysMem = &grid.Vertices[0];
- m_device->CreateBuffer( &vbd, &vinitData, &m_VertexBuffer );
- // Pack the indices of all the meshes into one index buffer
- D3D11_BUFFER_DESC ibd;
- ibd.Usage = D3D11_USAGE_IMMUTABLE;
- ibd.ByteWidth = sizeof(UINT) * m_GridIndexCount;
- ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
- ibd.CPUAccessFlags = 0;
- ibd.MiscFlags = 0;
- D3D11_SUBRESOURCE_DATA iinitData;
- iinitData.pSysMem = &grid.Indices[0];
- m_device->CreateBuffer( &ibd, &iinitData, &m_IndexBuffer );
- }
- bool Terrain::InBounds( int i, int j )
- {
- return i >= 0 && i < (int)m_MapInfo.HeightMapHeight &&
- j >= 0 && j < (int)m_MapInfo.HeightMapWidth;
- }
- float Terrain::Average( int i, int j )
- {
- float avg = 0.0f;
- float num = 0.0f;
- for( int m = i-1; m <= i+1; m++ )
- {
- for( int n = j-1; n <= j+1; n++ )
- {
- if( InBounds( m, n ) )
- {
- avg += m_HeightMap[ m * m_MapInfo.HeightMapWidth + n ];
- num += 1.0f;
- }
- }
- }
- return avg / num;
- }
- void Terrain::Smooth()
- {
- std::vector<float> dest( m_HeightMap.size() );
- for( UINT i = 0; i < m_MapInfo.HeightMapHeight; i++ )
- {
- for( UINT j = 0; j < m_MapInfo.HeightMapWidth; j++ )
- {
- dest[ i * m_MapInfo.HeightMapWidth + j ] = Average( i, j );
- }
- }
- m_HeightMap = dest;
- }
- float Terrain::GetHeight( float x, float z )
- {
- //Test1
- //return m_HeightMap[ z * m_MapInfo.HeightMapHeight + x];
- //return Average( m_HeightMap[z], m_HeightMap[x] ) + 10;
- //// Test 2
- //float test = 0.0;
- //if( InBounds( x, z ) )
- //{
- // int iter = (int)( x * 257 + z );
- // test = m_HeightMap[iter];
- //}
- //return test;
- // test 3
- float y = 100.0f;
- for( int i = 0; i < m_MeshData.Vertices.size(); i++ )
- {
- if( ((int)m_MeshData.Vertices[i].pos[0] == (int)x) && ((int)m_MeshData.Vertices[i].pos[2] == (int)z))
- {
- y = m_MeshData.Vertices[i].pos[1];
- break;
- }
- }
- return y;
- }
- HRESULT Terrain::Draw()
- {
- // Set topology
- m_deviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
- // Set vertices
- UINT32 vertexSize = sizeof( Vertex );
- UINT32 offset = 0;
- ID3D11Buffer* buffersToSet[] = { m_VertexBuffer };
- m_deviceContext->IASetVertexBuffers( 0, 1, buffersToSet, &vertexSize, &offset );
- m_deviceContext->IASetIndexBuffer( m_IndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
- m_deviceContext->DrawIndexed( m_GridIndexCount, 0, 0 );
- return S_OK;
- }
- void Terrain::GetVertexBuffer( ID3D11Buffer*& buffer )
- {
- buffer = m_VertexBuffer;
- }
- void Terrain::GetIndexBuffer( ID3D11Buffer*& buffer )
- {
- buffer = m_IndexBuffer;
- }
- void Terrain::GetVertexAndIndexBuffer( ID3D11Buffer*& vertexBuffer, ID3D11Buffer*& indexBuffer )
- {
- vertexBuffer = m_VertexBuffer;
- indexBuffer = m_IndexBuffer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement