我在使用Direct3D时遇到了一些问题。即顶点缓冲区及其ByteWidth成员。
我想绘制两个四边形,所以我创建了我的顶点缓冲区,如下所示:
struct Vertex
{
XMFLOAT3 pos;
XMFLOAT3 normal;
XMFLOAT2 texCoord;
};
....
void GameWindow::CreateVerticesAndBuffer()
{
Vertex vertices[] =
{
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT3(0.0f, 0.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) }
};
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
desc.CPUAccessFlags = 0;
desc.Usage = D3D11_USAGE_DEFAULT; //Will not ever change after creation (??)
desc.MiscFlags = 0;
desc.ByteWidth = sizeof(Vertex) * 8;
desc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA data;
ZeroMemory(&data, sizeof(data));
data.pSysMem = vertices;
HR(device->CreateBuffer(
&desc,
&data,
&vertexBuffer));
UINT stride = sizeof(Vertex);
UINT offset = 0;
deviceContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
}
此代码会产生一些奇怪的结果,如here所示。由于某种原因,背面是镜像的。
但是,如果我改变了
desc.ByteWidth = sizeof(Vertex) * 8
到
desc.ByteWidth = sizeof(Vertex) * 9
有没有人知道为什么会这样?
编辑:这是我的CreateInputLayout:
D3D11_INPUT_ELEMENT_DESC inputDesc[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEX", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
hRes = device->CreateInputLayout(
inputDesc,
ARRAYSIZE(inputDesc),
vertexShaderSource->GetBufferPointer(),
vertexShaderSource->GetBufferSize(),
&vertexInputLayout);
答案 0 :(得分:3)
您已为TEX成员指定了DXGI_FORMAT_R32G32B32_FLOAT
(float3)。将其更改为DXGI_FORMAT_R32G32_FLOAT
,它应该可以正常工作。