移动相机

时间:2014-04-27 12:29:18

标签: c++ camera projection direct3d

我创建了一个LookAt相机的相机。我确实通过同时改变位置和目标位置来移动它。我还为每个轴添加了一个旋转值,这就是get Matrix的样子:

Camera::Camera() : m_pos(XMFLOAT3(0.0f, 0.0f, 0.0f)), m_target(XMFLOAT3(0.0f, 0.0f, 0.0f)), m_upVec(XMFLOAT3(0.0f, 1.0f, 0.0f)), m_rotation(XMFLOAT3(0.0f, 0.0f, 0.0f))
{
}

Camera::Camera(XMFLOAT3 position, XMFLOAT3 target) : m_pos(position), m_target(target), m_upVec(XMFLOAT3(0.0f, 1.0f, 0.0f))
{

}
Camera::~Camera()
{
}

XMMATRIX Camera::getViewMatrix()
{
    XMMATRIX lookAt = XMMatrixLookAtLH(XMLoadFloat3(&m_pos), XMLoadFloat3(&m_target), XMLoadFloat3(&m_upVec));
    //XMMATRIX translation = XMMatrixTranslation(m_pos.x, m_pos.y, m_pos.z); //tryed adding this
    XMMATRIX rotationX = XMMatrixRotationX(m_rotation.x);
    XMMATRIX rotationY = XMMatrixRotationY(m_rotation.y);
    XMMATRIX rotationZ = XMMatrixRotationZ(m_rotation.z);
    return lookAt * rotationX * rotationY * rotationZ;
}

void Camera::setPos(XMFLOAT3 pos, XMFLOAT3 target)
{
    m_target = target;
    m_pos = pos;
}

void Camera::setRotation(XMFLOAT3 rot)
{
    m_rotation = rot;
}

XMFLOAT3 Camera::getPosition()
{
    return m_pos;
}

XMFLOAT3 Camera::getRotation()
{
    return m_rotation;
}

XMFLOAT3 Camera::getTarget()
{
    return m_target;
}

我正在尝试根据摄像机的位置移动摄像机,但它始终沿着轴移动。不在"它的坐标系"。

以下是移动和旋转的示例:

if (m_keyBoardKeys[DIK_UP] & 0x80)
{
    m_camera->setPos(XMFLOAT3(m_camera->getPosition().x,
                              m_camera->getPosition().y - movementspeed*delta, m_camera->getPosition().z),
                     XMFLOAT3(m_camera->getTarget().x, m_camera->getTarget().y - movementspeed*delta,
                              m_camera->getTarget().z));
}

//check if pressed
if (m_keyBoardKeys[DIK_C] & 0x80)
{
    m_camera->setRotation(XMFLOAT3(m_camera->getRotation().x,
                                   m_camera->getRotation().y + movementspeed*delta/4, m_camera->getRotation().z));
}

此矩阵在每个渲染周期都会添加到着色器中,并且正确相乘。

PS_Input VS_Main(VS_Input vertex)
{
    PS_Input vsOut = (PS_Input)0;
    float4 worldPos = mul(vertex.pos, worldMatrix); //rotation and position of the current rendered object
    vsOut.pos = mul(worldPos, viewMatrix); // camera matrix
    vsOut.pos = mul(vsOut.pos, projMatrix); // perspective projection matrix

    vsOut.tex0 = vertex.tex0;
    vsOut.norm = mul(vertex.norm, (float3x3)worldMatrix);
    vsOut.norm = normalize(vsOut.norm);

    float3 lightPos = float3(0.0f, 500.0f, 50.0f);
        vsOut.lightVec = normalize(lightPos - worldPos);

    vsOut.viewVec = normalize(cameraPos - worldPos);

    return vsOut;
}

问题是:如何让相机移动到它的空间而不是在全球空间中,但仍然按它的轴旋转?

1 个答案:

答案 0 :(得分:2)

您需要根据旋转创建旋转矩阵,将目标转换为旋转矩阵,并将新矢量添加到眼睛位置。

以下是我如何操作的示例,这将根据传入的增量值向前或向后移动相机

XMVECTOR Pos = XMVectorSet( this->pos.x, this->pos.y, this->pos.z, this->pos.w );
XMVECTOR At = XMVectorSet(  this->targetMagnitude.x , this->targetMagnitude.y, this->targetMagnitude.z, this->targetMagnitude.w );

XMMATRIX RotationMatrix( XMMatrixRotationRollPitchYaw( this->RadianPitch , this->RadianYaw, this->RadianRoll ));

At = XMVector3TransformCoord( At, RotationMatrix );
At = XMVector4Normalize(At);
Pos += At * delta;
XMStoreFloat4(&this->pos, Pos);