c++ - DirectX 11 D3DXMatrixTranslation 继续运行?

标签 c++ visual-c++ directx directx-11

我想将移动对象绑定(bind)到按下按钮。当我按下按钮时,该对象迅速消失并出现,就好像第一个翻译一直在运行一样。然后,当我松开按钮时,它会迅速消失,并在不触摸按钮的情况下结束。当我按下/释放按钮时在两者之间“弹跳”。

D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;
bool result;


// Generate the view matrix based on the camera's position.
m_Camera->Render();

// Get the world, view, and projection matrices from the camera and d3d objects.
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);

// Move the world matrix by the rotation value so that the object will move.
if(m_Input->IsAPressed() == true) {
D3DXMatrixTranslation(&worldMatrix, 1.0f*rotation, 0.0f, 0.0f);
}
else {
    D3DXMatrixTranslation(&worldMatrix, 0.1f*rotation, 0.0f, 0.0f);
}

// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
m_Model->Render(m_Direct3D->GetDeviceContext());

// Render the model using the light shader.
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, 
                               m_Model->GetTexture(), m_Light->GetDirection(), m_Light->GetDiffuseColor());


if(!result)
{
    return false;
}

// Present the rendered scene to the screen.
m_Direct3D->EndScene();

我对 DX11 还是个新手,但我已经四处看看了。我正在紧张地想弄清楚发生了什么。

最佳答案

这就是您的代码所做的。如果按下按钮,则设置一个世界矩阵,如果没有,则设置另一个。您需要做的是将世界矩阵乘以新生成的平移矩阵。请注意,这种乘法每秒会发生约 60 次,因此您每次只需要移动非常小的距离。

你的代码应该是这样的

if (m_Input->IsAPressed() == true) {
   D3DXMATRIX translation;
   D3DXMatrixTranslation(&translation, 0.05f, 0.0f, 0.0f);
   worldMatrix *= translation;
}

你可能需要做

m_Direct3D->SetWorldMatrix(worldMatrix);

或类似的东西。我认为我不熟悉您为 m_Camera 和 m_Direct3D 使用的类。

关于c++ - DirectX 11 D3DXMatrixTranslation 继续运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8885057/

相关文章:

c++ - qt-creator 和 msvc 无法添加(静态)库

c++ - 如何处理不同的包围体类型?

c++ - 使用Direct 3D时,代码中应该处理什么,HLSL中应该处理什么?

c++ - 从特征类继承

c++ - 多线程妄想症

c++ - 使用MSVC在Windows上编译报错Botan库

c++ - MFC:如何绘制带有换行和垂直居中的文本?

c++ - 为什么我的光线追踪器出现如此严重的边缘扭曲?

c++ - 通过具有可变参数列表的函数计算欧氏距离

c++ - VS2012使用3D模型的困境