繁体   English   中英

移动相机

[英]Moving the camera

我创建了一个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;
}

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

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

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

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM