DirectX11 Mapping / Unmapping对渲染几何体没有影响

时间:2014-06-05 16:43:58

标签: c++ directx

我有一个简单的渲染程序我刚刚制作,但它拒绝绘制任何东西,但我设置的初始顶点。即使我调用Map()和Unmap(),几何也不会改变。我觉得它与Map()和Unmap()有关,但我不确定。现在,我的初始顶点数据由一个三角形组成。然后我用一组由两个三角形组成的新顶点映射顶点缓冲区,但它们没有被渲染。即使我在draw函数中传入6进行顶点计数,也只渲染一个三角形。这是我的设置代码:

VertexData vertexData[] = 
{
    {0.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
    {0.0f,1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f},
    {1.0f,1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f}
};

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

bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VertexData)*256;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

D3D11_SUBRESOURCE_DATA vertexBufferData;
vertexBufferData.pSysMem = vertexData;
vertexBufferData.SysMemPitch = 0;
vertexBufferData.SysMemSlicePitch = 0;

_device->CreateBuffer(&bd, &vertexBufferData, &_renderBuffer);

映射功能:

数据是一个包含六个VertexData结构的向量,用于六个顶点

D3D11_MAPPED_SUBRESOURCE ms;
ZeroMemory(&ms,sizeof(D3D11_MAPPED_SUBRESOURCE));
_deviceContext->Map(_renderBuffer,NULL,D3D11_MAP_WRITE_DISCARD,NULL,&ms);
memcpy(ms.pData,&data[0],sizeof(VertexData)*data.size());
_deviceContext->Unmap(_renderBuffer,NULL);

这是我的渲染代码:

_deviceContext->ClearRenderTargetView(_backBuffer,D3DXCOLOR(0.0f,0.0f,0.0f,1.0f));
_deviceContext->Draw(6,0);
_swapChain->Present(0,0);

编辑:禁用背面剔除,但三角形仍然没有出现。

映射功能

void Render::CopyDataToBuffers(std::vector<VertexData> data)
{
    D3D11_MAPPED_SUBRESOURCE ms;
    ZeroMemory(&ms,sizeof(D3D11_MAPPED_SUBRESOURCE));

    _deviceContext->Map(_renderBuffer,NULL,D3D11_MAP_WRITE_DISCARD,NULL,&ms);
    memcpy(ms.pData,&data[0],sizeof(VertexData)*data.size());
    _deviceContext->Unmap(_renderBuffer,NULL);
}

调用映射功能

std::vector<VertexData> vertexDataVec;
vertexDataVec.push_back(vertexData[0]);
vertexDataVec.push_back(vertexData[1]);
vertexDataVec.push_back(vertexData[2]);
vertexDataVec.push_back(vertexData[3]);
vertexDataVec.push_back(vertexData[4]);
vertexDataVec.push_back(vertexData[5]);

Render::GetRender().CopyDataToBuffers(vertexDataVec);

1 个答案:

答案 0 :(得分:0)

要解决此问题,我只需创建 ID3D11RasterizerState 并禁用剔除。 结构如下:

ID3D11RasterizerState* rasterizerState = NULL;

D3D11_RASTERIZER_DESC rd =
{
        D3D11_FILL_SOLID,
        D3D11_CULL_NONE,
        TRUE,
        1,
        10.0F,
        1.0F,
        TRUE
};

_device->CreateRasterizerState(&rd,&rasterizerState);

_deviceContext->RSSetState(rasterizerState);