首先是一些相关的全局变量的声明。
XMVECTOR DefaultForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
XMVECTOR DefaultRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
XMVECTOR camForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
XMVECTOR camRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
XMMATRIX camRotationMatrix;
XMMATRIX groundWorld;
float moveLeftRight = 0.0f;
float moveBackForward = 0.0f;
float camYaw = 0.0f;
float camPitch = 0.0f;
首先是摄像机的前向量和右向量。
以及摄像机的旋转矩阵。
下面的两个浮点数是用来控制在相应的向量上的移动。
最后两个浮点数是用来计算摄像机与x y轴的夹角的。
首先我们需要把交换链的windowed成员设置为false 因为我们要全屏
接下来是一个更新camera的操作。
void UpdateCamera()
{
camRotationMatrix = XMMatrixRotationRollPitchYaw(camPitch, camYaw, 0);
camTarget = XMVector3TransformCoord(DefaultForward, camRotationMatrix );
camTarget = XMVector3Normalize(camTarget);
XMMATRIX RotateYTempMatrix;
RotateYTempMatrix = XMMatrixRotationY(camYaw);
camRight = XMVector3TransformCoord(DefaultRight, RotateYTempMatrix);
camUp = XMVector3TransformCoord(camUp, RotateYTempMatrix);
camForward = XMVector3TransformCoord(DefaultForward, RotateYTempMatrix);
camPosition += moveLeftRight*camRight;
camPosition += moveBackForward*camForward;
moveLeftRight = 0.0f;
moveBackForward = 0.0f;
camTarget = camPosition + camTarget;
camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );
}
首先是计算摄像机沿着x和y轴的旋转角度
然后得到摄像机的朝向 向量。
通过计算摄像机沿着Y轴的旋转角度 得到摄像机当前的右向量 上向量 和前向量
更新摄像机的位置,然后重置唯一变量。同时设置相机矩阵。
剩下的操作就是更新场景中一些其他的变换矩阵