2014-04-04 22 views
0

我试图用xyz值将数组转换为要渲染的顶点和索引。将地图转换为顶点索引不起作用

但不幸的是,我什么也看不见。

这是我initiliaze部分的一切:

.... 

    WORD size = 40; 
    XMFLOAT3* m_heightMap = new XMFLOAT3[size * size]; 

    for (WORD i = 0; i < size; i++) 
    { 
     for (WORD j = 0; j < size; j++) 
     { 
      WORD index = size * i + j; 
      m_heightMap[index].x = (FLOAT)i; 
      m_heightMap[index].y = (FLOAT)0; 
      m_heightMap[index].z = (FLOAT)j; 
     } 
    } 

    WORD vicount = (size - 1) * (size - 1) * 12; 
    SimpleVertex* vertices = new SimpleVertex[vicount]; 
    WORD* indices = new WORD[vicount]; 

    WORD index = 0; 

    // Load the vertex and index array with the terrain data. 
    for (WORD j = 0; j<(size - 1); j++) 
    { 
     for (WORD i = 0; i<(size - 1); i++) 
     { 
      WORD index1 = (size * j) + i;   // Bottom left. 
      WORD index2 = (size * j) + (i + 1);  // Bottom right. 
      WORD index3 = (size * (j + 1)) + i;  // Upper left. 
      WORD index4 = (size * (j + 1)) + (i + 1); // Upper right. 

      // Upper left. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index3].x, m_heightMap[index3].y, m_heightMap[index3].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Upper right. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Upper right. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Bottom left. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Bottom left. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Upper left. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index3].x, m_heightMap[index3].y, m_heightMap[index3].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Bottom left. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Upper right. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Upper right. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Bottom right. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index2].x, m_heightMap[index2].y, m_heightMap[index2].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Bottom right. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index2].x, m_heightMap[index2].y, m_heightMap[index2].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 

      // Bottom left. 
      vertices[index].Pos = XMFLOAT3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z); 
      vertices[index].Color = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); 
      indices[index] = index; 
      index++; 
     } 
    } 

    D3D11_BUFFER_DESC bd; 
    ZeroMemory(&bd, sizeof(bd)); 

    // Create vertex buffer 
    bd.Usage = D3D11_USAGE_DEFAULT; 
    bd.ByteWidth = sizeof(SimpleVertex)* vicount; 
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; 
    bd.CPUAccessFlags = 0; 
    D3D11_SUBRESOURCE_DATA InitData; 
    ZeroMemory(&InitData, sizeof(InitData)); 
    InitData.pSysMem = vertices; 
    hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer); 
    if (FAILED(hr)) 
     return hr; 

    // Set vertex buffer 
    UINT stride = sizeof(SimpleVertex); 
    UINT offset = 0; 
    g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); 

    // Create index buffer 
    bd.Usage = D3D11_USAGE_DEFAULT; 
    bd.ByteWidth = sizeof(WORD) * vicount; 
    bd.BindFlags = D3D11_BIND_INDEX_BUFFER; 
    bd.CPUAccessFlags = 0; 
    InitData.pSysMem = indices; 
    hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pIndexBuffer); 
    if (FAILED(hr)) 
     return hr; 

    // Set index buffer 
    g_pImmediateContext->IASetIndexBuffer(g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); 

    // Set primitive topology 
    g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 

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

    // Initialize the world matrix 
    g_World = XMMatrixIdentity(); 

    // Initialize the view matrix 
    XMVECTOR Eye = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f); 
    XMVECTOR At = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); 
    XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); 
    g_View = XMMatrixLookAtLH(Eye, At, Up); 

    // Initialize the projection matrix 
    g_Projection = XMMatrixPerspectiveFovLH(XM_PIDIV2, (FLOAT)width/(FLOAT)height, 0.01f, 100.0f); 
    .... 

谢谢您的阅读,和借口英语不好:(

+0

侧面说明 - 您需要解决这些内存泄漏如果该函数返回一个失败。最好使用std :: vector而不是所有对new []的调用。 – PaulMcKenzie

+0

我建议尝试渲染更简单的东西,然后展开该代码以渲染地形,每次更改内容时检查渲染。我怀疑任何人都会通过所有的代码进行测试。 –

+0

@PaulMcKenzie谢谢,我会解决这个问题! – user3476584

回答

0

看来你想创建一个40 X 40格/地形,首先你需要计算多少顶点和索引

对于一个X n网格图,如果你想把它绘制成三角形列表,你有(n X n X 2 X 3)个索引,其中n X n是地图中单元格的数量,每个单元格由2个三角形组成,每个三角形都是由3个指数组成。和(n + 1)个X(n + 1)个顶点。例如,下面的2×2网格需要16个索引和9个顶点。

------------- 
|  |  | 
|  |  | 
------------- 
|  |  | 
|  |  | 
------------- 

建立了索引缓存

------------- 
|  |  | 
|  |  | 
------------- 
|  |(i,j)| 
|  |  | 
-------------(i+1, j+1) 

// Fill the index array 
for (DWORD i = 0; i < numCellsperRow; ++i) 
{ 
    for (DWORD j = 0; j < numCellsperCol; ++j) 
    { 
     indices[k]  =  i * numVertexperCol + j;   // 0 
     indices[k + 1] =  i * numVertexperCol + (j + 1); // 1 
     indices[k + 2] = (i + 1) * numVertexperCol + j;   // 2 


     indices[k + 3] = (i + 1) * numVertexperCol + j;   // 3 
     indices[k + 4] =  i * numVertexperCol + (j + 1); // 4 
     indices[k + 5] = (i + 1) * numVertexperCol + (j + 1); // 5 

     // next quad 
     k += 6; 
    } 
}