顶点不渲染 - DirectX

时间:2016-11-22 19:01:38

标签: c++ directx

我正在学习DirectX并尝试确保在继续创建索引缓冲区之前理解函数正在做什么(这就是我使用重复顶点而不是特定索引的原因)。我想渲染一个正方形,但我只有一个三角形显示。我确信这是我对绕线顺序的误解或在传递到着色器时抵消了值,但我无法找到问题。下面是当前相关代码和运行时的结果。

void RenderFrame(void) {

// Clear BackBuffer to a color
devContext->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));

// select which vertex buffer to display
UINT stride = sizeof(Vertex);
UINT offset = 0;
devContext->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);

// select which primtive type we are using
devContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELST);

// draw the vertex buffer to the back buffer
devContext->Draw(6, 0);

// swap buffers
swapchain->Present(0, 0);
}

void ParseGraphics() {

// Create a triangle with the Vertex Struct
Vertex square[] =
{
    { 0.2f, 0.5f, 0.5f, D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f) }, //top-right
    { 0.2f, -0.5f, 0.5f, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f) }, //bottom-right
    { -0.2f, -0.5f, 0.5f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) }, //bottom-left
    { 0.2f, -0.5f, 0.5f, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f) }, //bottom-right
    { -0.2f, -0.5f, 0.5f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) }, //bottom-left
    { -0.2f, 0.5f, 0.5f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) }, //top-left


};

// Create Vertex Buffer
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));

bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(Vertex) * 6;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

dev->CreateBuffer(&bd, NULL, &pVBuffer);

// copy vertices into buffer
D3D11_MAPPED_SUBRESOURCE msr;
devContext->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &msr);
memcpy(msr.pData, square, sizeof(square));
devContext->Unmap(pVBuffer, NULL);

}

void BuildPipeline() {
// Load and Compile Shaders
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);

// Create shaders from the data in the Blobs Buffer
dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);

// Apply Shaders to the device context
devContext->VSSetShader(pVS, 0, 0);
devContext->PSSetShader(pPS, 0, 0);

// Define the layout of the input given to the shaders
D3D11_INPUT_ELEMENT_DESC ied[] =
{
    {"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},

};

dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devContext->IASetInputLayout(pLayout);

}

DirectX render

2 个答案:

答案 0 :(得分:1)

{“POSITION”,0,DXGI_FORMAT_R32G32B32 A32 _FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0}

应为DXGI_FORMAT_R32G32B32_FLOAT,因为该职位只有三名成员。 AlignedByteOffset可以设置为12(4x3)。

答案 1 :(得分:0)

对于拓扑,您指定D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,因此顶点数据应形成triangle strip。但是,您提供的数据指定了两个单独的三角形(即三角形列表),因此您应该使用

devContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 

基本错误似乎在Draw()调用中:您指定要绘制总共3个顶点,而实际上您想从缓冲区中绘制所有6个顶点。即。

devContext->Draw(6, 0);

Color元素描述的字节偏移也是错误的。该位置需要3个单精度浮点数(3 * 4 = 12个字节),后面紧跟颜色数据。

{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},