将地图转换为顶点索引不起作用

时间:2014-04-04 13:51:21

标签: c++ directx directx-11

我正在尝试将具有xyz值的数组转换为要渲染的顶点和索引。

但不幸的是,我看不到任何东西。

这是我宣传一切的部分:

    ....

    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);
    ....

感谢您的阅读,以及不良英语的借口:(

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试创建一个40 X 40网格/地形,首先需要计算多少顶点和索引。

对于X n网格地图,如果要将其绘制为三角形列表,则具有(n X n X 2 X 3)个索引,其中n X n是地图中的单元格数,并且每个单元格都已制作由2个三角形组成,每个三角形由3个指数组成。和(n + 1)X(n + 1)个顶点。例如,如下所示的2 X 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;
    }
}