2016-05-08 98 views
0

我正在嘗試使用directx 11渲染地形並將高度貼圖應用於它。 我加載高度圖,然後將其複製到一個整數矢量,然後對於每個頂點位置,我將該頂點的Y位置分配給heightmap值,但地形完全被破壞和扭曲。當我移除Y軸上的計算時,我得到一個平坦的網格,沒有問題。在directx中渲染地形11

bool cGrid::readRawFile(std::string fileName, int m, int n) 
{ 
    // A height for each vertex 
    std::vector<BYTE> in(m*n); 
    std::ifstream inFile(fileName.c_str(), std::ios_base::binary); 
    if (!inFile) 
     return false; 
    inFile.read(
     (char*)&in[0], // buffer 
     in.size());// number of bytes to read into buffer 
    inFile.close(); 
    // copy BYTE vector to int vector 
    m_heightmap.resize(n*m); 
    for (int i = 0; i < in.size(); i++) 
     m_heightmap[i] = in[i]; 
    return true; 
} 

for (size_t i = 0; i<m_Mesh.m_Vertices.size(); ++i) 
    { 
     XMFLOAT3 p = m_Mesh.m_Vertices[i].Position; 
     p.y = (float)m_heightmap[i]*0.5f; 
     m_Mesh.m_Vertices[i].Position = p; 
    } 

這裏的問題是

https://www.youtube.com/watch?v=lnlIz3DjebM&feature=youtu.beenter image description here

enter image description here

HRESULT cGrid::CreateGrid(float width, float depth, UINT n, UINT m) 
{ 
    HRESULT hr; 
    int vertexCount = m*n; 
    UINT faceCount = (m - 1)*(n - 1) * 2; // each quad consists of two triangles 

    float halfWidth = 0.5f*width; 
    float halfDepth = 0.5f*depth; 

    // project the grid into xz plane 
    float dx = width/(n - 1); 
    float dz = depth/(m - 1); 

    float du = 1.0f/(n - 1); // texture co-ordinates 
    float dv = 1.0f/(m - 1); 


    m_Mesh.m_Vertices.resize(vertexCount); 

    // build the vertices of the grid, including the normals and the tangent, 
    //you can build then the bitanget by cross product for normal maps -_- 

    for (UINT i = 0; i < m; ++i) 
    { 
     float z = halfDepth - i*dz; // reset for the next cell 
     for (UINT j = 0; j < n; ++j) 
     { 
      float x = -halfWidth + j*dx; 
      float y = (float)m_heightmap[j + i*m]; 
      m_Mesh.m_Vertices[i*n + j].Position = XMFLOAT3(x, y, z); 
//   m_Mesh.m_Vertices[i*n + j].Normal = XMFLOAT3(0.0f, 1.0f, 0.0f); 
//   m_Mesh.m_Vertices[i*n + j].TangentU = XMFLOAT3(1.0f, 0.0f, 0.0f); 

      // Stretch texture over grid. 
      m_Mesh.m_Vertices[i*n + j].TextureCords.x = j*du; 
      m_Mesh.m_Vertices[i*n + j].TextureCords.y = i*dv; 
     } 
    } 

    m_Mesh.m_Indices.resize(faceCount * 3); // 3 indices 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) 
     { 
      m_Mesh.m_Indices[k] = i*n + j; 
      m_Mesh.m_Indices[k + 1] = i*n + j + 1; 
      m_Mesh.m_Indices[k + 2] = (i + 1)*n + j; 

      m_Mesh.m_Indices[k + 3] = (i + 1)*n + j; 
      m_Mesh.m_Indices[k + 4] = i*n + j + 1; 
      m_Mesh.m_Indices[k + 5] = (i + 1)*n + j + 1; 

      k += 6; // next quad 
     } 
    } 

    m_IndicesSize = m_Mesh.m_Indices.size(); 


    // Pack all the vertices into vertex buffer 
    D3D11_BUFFER_DESC vbd; 
    vbd.Usage = D3D11_USAGE_IMMUTABLE; 
    vbd.ByteWidth = sizeof(MeshVertex)* vertexCount; 
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER; 
    vbd.CPUAccessFlags = 0; 
    vbd.MiscFlags = 0; 
    D3D11_SUBRESOURCE_DATA vinitData; 
    vinitData.pSysMem = &(m_Mesh.m_Vertices[0]); 
    m_pGraphics->getDevice()->CreateBuffer(&vbd, &vinitData, &mVB); 

    // Pack the indices of all the meshes into one index buffer. 
    D3D11_BUFFER_DESC ibd; 
    ibd.Usage = D3D11_USAGE_DEFAULT; 
    ibd.ByteWidth = sizeof(UINT)* m_IndicesSize; 
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER; 
    ibd.CPUAccessFlags = 0; 
    ibd.MiscFlags = 0; 
    D3D11_SUBRESOURCE_DATA iinitData; 
    iinitData.pSysMem = &m_Mesh.m_Indices[0]; 
    m_pGraphics->getDevice()->CreateBuffer(&ibd, &iinitData, &mIB); 

    // Create the constant buffer 
    ibd.Usage = D3D11_USAGE_DEFAULT; 
    ibd.ByteWidth = sizeof(ConstantBuffer); 
    ibd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 
    ibd.CPUAccessFlags = 0; 
    hr = m_pGraphics->getDevice()->CreateBuffer(&ibd, nullptr, &m_pConstantBuffer); 
    if (FAILED(hr)) 
     return hr; 

    return hr; 
} 
+0

作爲建議的一件事,你可以看看一個很好的參考是可以在http://www.rastertek.com了DirectX 10和DirectX下找到地形撕心裂肺教程「歷史教程」下的11個地形教程。 –

+0

(續...)我還想補充一點,在系列2下,DirectX 11的地形教程有完整的版本。 –

回答

1

定義std::vector<BYTE> in(m*n)時,我會用的,而不是unsigned char的​​的視頻,因爲它不是一部分C標準庫,因此它是依賴於系統的。

還可以使用流延在這條線

in.size());// number of bytes to read into buffer 

實際參數類型的ifstream::read這是std::streamsize

像這樣:

(std::streamsize)in.size());// number of bytes to read into buffer 

既然你有一個8位的高度圖的工作,你或許應該不只是從RAW文件的值複製到你的高度圖是這樣的:

for (int i = 0; i < in.size(); i++) 
    m_heightmap[i] = in[i]; 

由於每個高度圖值由一個8位整數表示,因此您可以嘗試除高度圖值以及將其乘以一些比例修改值。如果你想測試你的方式來獲得一些好的價值,這將使它更方便。純粹是爲了視覺目的...

for (int i = 0; i < in.size(); i++) 
    m_heightmap[i] = (float)(in[i]/255.0f) * scaleModifier; 
+0

在應用您的評論之後,我只是得到了一個平面網格。根本沒有地形 –

+0

看上面的帖子中的附圖。現在我得到一輛這樣的火車。只是一些尖峯 –

+0

我附上的高度圖 –