2

I'm having some issues with Direct3D. Namely the vertex buffer and its ByteWidth member.

I want to draw two quads, so I create my vertex buffer like so:

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

This code produces some weird results as seen here. The back face is mirrored for some reason.

But, if I change

desc.ByteWidth = sizeof(Vertex) * 8

to

desc.ByteWidth = sizeof(Vertex) * 9

It is drawn correctly.

Does anyone have any idea why this happens?

EDIT: Here is my 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);
Semicolon
  • 163
  • 2
  • 16

1 Answers1

3

You have specified DXGI_FORMAT_R32G32B32_FLOAT (float3) for your TEX member. Change it to DXGI_FORMAT_R32G32_FLOAT and it should work.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43