尝试使用XMMatrixTranslation函数修改立方体位置。附上的是代码......
//旋转并修改原点周围立方体的位置 g_World = XMMatrixRotationY(t);
g_World = XMMatrixMultiply(g_World, XMMatrixTranslation(0, yDirection, 0));
// Modify the color
g_vMeshColor.x = ( sinf( t * 1.0f ) + 1.0f ) * 0.5f;
g_vMeshColor.y = ( cosf( t * 3.0f ) + 1.0f ) * 0.5f;
g_vMeshColor.z = ( sinf( t * 5.0f ) + 1.0f ) * 0.5f;
//
// Clear the back buffer
//
float ClearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f }; // red, green, blue, alpha
g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, ClearColor );
//
// Clear the depth buffer to 1.0 (max depth)
//
g_pImmediateContext->ClearDepthStencilView( g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );
//
// Update variables that change once per frame
//
CBChangesEveryFrame cb;
cb.mWorld = XMMatrixTranspose( g_World );
cb.vMeshColor = g_vMeshColor;
g_pImmediateContext->UpdateSubresource( g_pCBChangesEveryFrame, 0, NULL, &cb, 0, 0 );
//
// Render the cube
//
g_pImmediateContext->VSSetShader( g_pVertexShader, NULL, 0 );
g_pImmediateContext->VSSetConstantBuffers( 0, 1, &g_pCBNeverChanges );
g_pImmediateContext->VSSetConstantBuffers( 1, 1, &g_pCBChangeOnResize );
g_pImmediateContext->VSSetConstantBuffers( 2, 1, &g_pCBChangesEveryFrame );
g_pImmediateContext->PSSetShader( g_pPixelShader, NULL, 0 );
g_pImmediateContext->PSSetConstantBuffers( 2, 1, &g_pCBChangesEveryFrame );
g_pImmediateContext->PSSetShaderResources( 0, 1, &g_pTextureRV );
g_pImmediateContext->PSSetSamplers( 0, 1, &g_pSamplerLinear );
g_pImmediateContext->DrawIndexed( 36, 0, 0 );
//
// Present our back buffer to our front buffer
//
g_pSwapChain->Present( 0, 0 );
当我按下向下按钮时,立方体的位置应该在负y方向上改变。当我按下键时,立方体消失了。
谢谢,
斯科特