C++ 为什么我的方块会这样做(索引缓冲区问题)?
为什么我的方块会这样做(索引缓冲区问题) 我已经设置了以下顶点和索引:C++ 为什么我的方块会这样做(索引缓冲区问题)?,c++,directx,buffer,indices,direct3d11,C++,Directx,Buffer,Indices,Direct3d11,为什么我的方块会这样做(索引缓冲区问题) 我已经设置了以下顶点和索引: 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,
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
}
传递给着色器设置的常量缓冲区如下所示:
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
}
我的输入布局如下所示:
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
}
我的绘图部分如下所示:
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
}
我已将透视图和相机设置为:
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
}
额外信息
如果我将iAssetIndexBuffer第二个参数更改为DXGI_格式_R16_UINT,它将绘制以下图形:
iSetIndexBuffer的第二个参数应该是
DXGI\u格式\u R16\u UINT
。短裤是16位的,不是32位的。我不是DirectX专家,但我认为您需要将D3D11\u PRIMITIVE\u TOPOLOGY\u TRIANGLELIST
传递给iSetPrimitiveTopology
三角形列表解决了问题。虽然现在我很困惑为什么这条带子没用。。。不管怎样,如果成功了,就成功了:谢谢你,伙计!三角形带是一种不同的格式-您可以将正方形格式化为三角形带,但索引将不同。例如,见。无论如何,没问题:)你知道它是什么:背面剔除-我颠倒了最后索引的顺序,它开始工作。布亚卡山