我正在学习directx编程 . 昨天我成功制作了一个立方体 . 我正在尝试制作一个旋转的立方体 . 事实上,我成功了,但我不确定我是否使用了正确的方法 .
下面的代码是常用的方式吗?我调用函数来转换顶点位置并在每个帧上创建新的顶点缓冲区 . 有没有其他方法不会在每个帧上创建顶点缓冲区?是否有更有效的方式来证明性能?
void BoxApp::makeVertex()
{
Vertex vertices[] =
{
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), (const XMFLOAT4)Colors::White },
{ XMFLOAT3(-1.0f, +1.0f, -1.0f), (const XMFLOAT4)Colors::Black },
{ XMFLOAT3(+1.0f, +1.0f, -1.0f), (const XMFLOAT4)Colors::Red },
{ XMFLOAT3(+1.0f, -1.0f, -1.0f), (const XMFLOAT4)Colors::Green },
{ XMFLOAT3(-1.0f, -1.0f, +1.0f), (const XMFLOAT4)Colors::Blue },
{ XMFLOAT3(-1.0f, +1.0f, +1.0f), (const XMFLOAT4)Colors::Yellow },
{ XMFLOAT3(+1.0f, +1.0f, +1.0f), (const XMFLOAT4)Colors::Cyan },
{ XMFLOAT3(+1.0f, -1.0f, +1.0f), (const XMFLOAT4)Colors::Magenta }
};
rotating_angle += 0.01;
for (int i = 0; i < 8; i++) {
XMStoreFloat3(&vertices[i].pos, XMVector3Transform(XMLoadFloat3(&vertices[i].pos),
XMMatrixTranslation(0.0f, 1.0f, 1.0f) *
XMMatrixRotationX(rotating_angle) *
XMMatrixTranslation(0.0f, -1.0f, -1.0f)
));
}
D3D11_BUFFER_DESC vbd;
... #Set BUFFER_DESC
D3D11_SUBRESOURCE_DATA vinitData;
... #Set SUBRESURCE
md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVB);
}
1 回答
将顶点位置设置为局部坐标,并将变换矩阵从局部坐标转换为世界坐标 . 使用常量缓冲区将其发送到顶点着色器 .