0

为什么我的方块会这样做(索引缓冲区问题)?

在此处输入图像描述

我设置了以下顶点和索引:

struct VERTEX
{
    float X, Y, Z;
    float R, G, B;
};

const unsigned short SquareVertices::indices[ 6 ] = {
    0, 1, 2,
    0, 2, 3
};

const VERTEX SquareVertices::vertices[ 4 ] = {
    -1.0f, -1.0f, 1.0f,     1.0f, 0.0f, 0.0f,
    -1.0f, 1.0f, 1.0f,      0.0f, 1.0f, 0.0f,
    1.0f, 1.0f, 1.0f,       0.0f, 0.0f, 1.0f,
    1.0f, -1.0f, 1.0f,      0.0f, 1.0f, 0.0f
};

我已经完成了这样的初始化:

void Player::Initialize( )
{
    // Create vertex buffer
    D3D11_BUFFER_DESC bd = { 0 };
    bd.ByteWidth = sizeof( VERTEX )* ARRAYSIZE( Player::vertices );
    bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;

    D3D11_SUBRESOURCE_DATA srd = { Player::vertices, 0, 0 };

    d3dDevice->CreateBuffer( &bd, &srd, &vertexbuffer );

    // Create the index buffer
    D3D11_BUFFER_DESC ibd = { 0 };
    ibd.ByteWidth = sizeof( short )* ARRAYSIZE( Player::indices );
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;

    D3D11_SUBRESOURCE_DATA isrd = { Player::indices, 0, 0 };

    d3dDevice->CreateBuffer( &ibd, &isrd, &indexbuffer );
}

传递给着色器设置的常量缓冲区如下所示:

void Game::SetUpConstantBuffer()
{
    D3D11_BUFFER_DESC bd = { 0 };

    bd.Usage = D3D11_USAGE_DEFAULT;
    bd.ByteWidth = 64;
    bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

    d3dDevice->CreateBuffer( &bd, nullptr, &constantbuffer ); 
    d3dDeviceContext->VSSetConstantBuffers( 0, 1, constantbuffer.GetAddressOf( ) );
}

我的输入布局如下所示:

// initialize input layout
D3D11_INPUT_ELEMENT_DESC ied[ ] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};

我的绘图部分如下所示:

// set the vertex buffer
            UINT stride = sizeof( VERTEX );
            UINT offset = 0;
                d3dDeviceContext->IASetVertexBuffers( 0, 1, player->vertexbuffer.GetAddressOf( ), &stride, &offset );
                d3dDeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP ); 
                d3dDeviceContext->IASetIndexBuffer( player->indexbuffer.Get( ), DXGI_FORMAT_R32_UINT, 0 );
                d3dDeviceContext->UpdateSubresource( constantbuffer.Get( ), 0, 0, &matFinal[ 0 ], 0, 0 ); // set the new values for the constant buffer
                d3dDeviceContext->DrawIndexed( ARRAYSIZE( player->indices ), 0, 0 ); // draw 3 vertices, starting from vertex 0

我已经像这样设置了我的视角和相机:

void Game::SetUpViewTransformations( )
{
    XMVECTOR vecCamPosition = XMVectorSet( 0.0f, 0.0f, -10.0f, 0 );
    XMVECTOR vecCamLookAt = XMVectorSet( 0, 0, 0, 0 );
    XMVECTOR vecCamUp = XMVectorSet( 0, 1, 0, 0 );

    matView = XMMatrixLookAtLH( vecCamPosition, vecCamLookAt, vecCamUp );
}

void Game::SetUpMatProjection( )
{
    CoreWindow^ window = CoreWindow::GetForCurrentThread( );    // get the window pointer

    matProjection = XMMatrixPerspectiveFovLH(
        XMConvertToRadians( 45 ),                                      // the field of view
        ( FLOAT )window->Bounds.Width / ( FLOAT )window->Bounds.Height,  // aspect ratio
        1,                                                           // the near view-plane
        100 );                                                        // the far view-plan
}

额外信息

如果我将 IASetIndexBuffer 2nd 参数更改为 DXGI_FORMAT_R16_UINT 它会绘制:

在此处输入图像描述

4

1 回答 1

0

你的第二个参数IASetIndexBuffer 应该DXGI_FORMAT_R16_UINT. 短裤是 16 位的,而不是 32 位的。我不是 DirectX 专家,但我认为您需要D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST通过IASetPrimitiveTopology.

于 2014-01-06T02:07:57.293 回答