为什么我的方块会这样做(索引缓冲区问题)?
我设置了以下顶点和索引:
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 它会绘制: