-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamera.cpp
More file actions
55 lines (45 loc) · 1.56 KB
/
Camera.cpp
File metadata and controls
55 lines (45 loc) · 1.56 KB
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
#include "Camera.h"
Camera::Camera(DirectX::XMFLOAT3 initPosition /*= DirectX::XMFLOAT3(0,0,0)*/, DirectX::XMFLOAT3 initRotation /*= DirectX::XMFLOAT3(0,0,0)*/, float aspectRatio /*= 1920.f / 1080.f*/, float fieldOfView /*= 90.f*/, float nearClip /*= 0.01f*/, float farClip /*= 10.f*/, float movementSpeed /*= 5.f*/, float mouseLookSpeed /*= 1.f */)
{
transform.SetPosition(initPosition.x, initPosition.y, initPosition.z);
transform.SetRotation(initRotation.x, initRotation.y, initRotation.z);
fov = fieldOfView;
this->nearClip = nearClip;
this->farClip = farClip;
this->mouseLookSpeed = mouseLookSpeed;
this->movementSpeed = movementSpeed;
mousePos = {};
UpdateViewMatrix();
UpdateProjectionMatrix(aspectRatio);
}
void Camera::UpdateProjectionMatrix(float aspectRatio)
{
DirectX::XMStoreFloat4x4
(
&projMatrix,
DirectX::XMMatrixPerspectiveFovLH(DirectX::XM_PIDIV4, aspectRatio, nearClip, 100.0f)
);
}
void Camera::UpdateViewMatrix()
{
DirectX::XMVECTOR forward = DirectX::XMVectorSet(0,0,1,0);
DirectX::XMVECTOR rotQuat = DirectX::XMQuaternionRotationRollPitchYawFromVector
(
DirectX::XMLoadFloat3
(
&transform.GetPitchYawRoll()
)
);
forward = DirectX::XMVector3Rotate(forward, rotQuat);
DirectX::XMMATRIX lookToMatrix = DirectX::XMMatrixLookToLH
(
DirectX::XMLoadFloat3(&transform.GetPosition()),
forward,
DirectX::XMVectorSet(0, 1, 0, 0)
);
DirectX::XMStoreFloat4x4(&viewMatrix, lookToMatrix);
}
void Camera::DestroySelf()
{
// @todo. Also consider making a class wrapper so that not the camera class inherits the Player interface
}