aboutsummaryrefslogtreecommitdiff
path: root/samples/dual_layer/D3D12/src/Camera.cpp
blob: fee0fe3073f2b4d0ef96866a8dd1219caf45f257 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Copyright (c) 2008-2018, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/

#include "Camera.h"

Camera::Camera()
{
    InitProjectionMatrix(1280, 720);

    mPosition = { -5.0f, -5.0f, 3.0f };
    mViewVector = XMVector3Normalize({ -4.0f, -4.0f, -2.0f });
    UpdateViewMatrix();
}

void Camera::InitProjectionMatrix(uint32_t width, uint32_t height, float zNear, float zFar)
{
    mMatProj = XMMatrixPerspectiveFovRH(XMConvertToRadians(60.0f), (float)width / (float)height, zNear, zFar);
    UpdateViewMatrix();
}

void Camera::MoveForward(float dt)
{
    mPosition += mViewVector * dt;
    UpdateViewMatrix();
}

void Camera::MoveRight(float dt)
{
    mPosition += XMMatrixTranspose(mMatView).r[0] * dt;
    UpdateViewMatrix();
}

void Camera::AdvanceAngles(float yaw, float pitch)
{
    XMMATRIX matBasisVecs = XMMatrixTranspose(mMatView);

    XMMATRIX matRot = XMMatrixRotationY(yaw);// *XMMatrixRotationX(pitch);

    mViewVector = XMVector3Rotate(mViewVector, XMQuaternionRotationAxis(matBasisVecs.r[1], yaw));
    mViewVector = XMVector3Rotate(mViewVector, XMQuaternionRotationAxis(matBasisVecs.r[0], pitch));
    UpdateViewMatrix();
}

void Camera::UpdateViewMatrix()
{
    mMatView = XMMatrixLookAtRH(mPosition, mPosition + mViewVector, { 0.0f, 0.0f, 1.0f });
    mMatViewProj = XMMatrixMultiply(mMatView, mMatProj);
}