-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
96 lines (85 loc) · 2.71 KB
/
Camera.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "Camera.h"
Camera::Camera() = default;
Camera::Camera(const Camera& camera)
{
this->m_camera_pos = camera.m_camera_pos;
this->m_world_up = camera.m_world_up;
this->m_yaw = camera.m_yaw;
this->m_pitch = camera.m_pitch;
this->m_movement_speed = camera.m_movement_speed;
this->m_mouse_sensitive = camera.m_mouse_sensitive;
}
Camera::Camera(DirectX::XMVECTOR init_camera_pos, DirectX::XMVECTOR init_world_up, float init_yaw, float init_pitch, float movement_speed, float mouse_sensitive)
{
this->m_camera_pos = init_camera_pos;
this->m_world_up = init_world_up;
this->m_yaw = init_yaw;
this->m_pitch = init_pitch;
this->m_movement_speed = movement_speed;
this->m_mouse_sensitive = mouse_sensitive;
}
Camera::~Camera()
{
}
void Camera::KeyControl(int key, float delta_time)
{
if (key == 'W')
{
m_camera_pos = DirectX::XMVectorAdd(m_camera_pos, DirectX::XMVectorScale(m_camera_front, delta_time * this->m_movement_speed));
}
if (key == 'S')
{
m_camera_pos = DirectX::XMVectorSubtract(m_camera_pos, DirectX::XMVectorScale(m_camera_front, delta_time * this->m_movement_speed));
}
if (key == 'A')
{
m_camera_pos = DirectX::XMVectorAdd(m_camera_pos, DirectX::XMVectorScale(m_camera_right, delta_time * this->m_movement_speed));
}
if (key == 'D')
{
m_camera_pos = DirectX::XMVectorSubtract(m_camera_pos, DirectX::XMVectorScale(m_camera_right, delta_time * this->m_movement_speed));
}
if (key == VK_SPACE)
{
m_camera_pos = DirectX::XMVectorAdd(m_camera_pos, DirectX::XMVectorScale(m_camera_up, delta_time * this->m_movement_speed));
}
if (key == VK_LSHIFT)
{
m_camera_pos = DirectX::XMVectorSubtract(m_camera_pos, DirectX::XMVectorScale(m_camera_up, delta_time * this->m_movement_speed));
}
}
void Camera::MouseControl(float x, float y)
{
x *= m_mouse_sensitive;
y *= m_mouse_sensitive;
m_yaw -= x;
m_pitch -= y;
if (m_pitch > 89.0f)
{
m_pitch = 89.0f;
}
if (m_pitch < -89.0f)
{
m_pitch = -89.0f;
}
}
void Camera::UpdateViewMatrix()
{
DirectX::XMVECTOR front{
cos(DirectX::XMConvertToRadians(m_yaw)) * cos(DirectX::XMConvertToRadians(m_pitch)),
sin(DirectX::XMConvertToRadians(m_pitch)),
sin(DirectX::XMConvertToRadians(m_yaw)) * cos(DirectX::XMConvertToRadians(m_pitch))};
front = DirectX::XMVector3Normalize(front);
m_camera_front = front;
m_camera_right = DirectX::XMVector3Normalize(DirectX::XMVector3Cross(m_camera_front, m_world_up));
m_camera_up = DirectX::XMVector3Normalize(DirectX::XMVector3Cross(m_camera_right, m_camera_front));
}
DirectX::XMMATRIX Camera::GetViewMatrix()
{
return DirectX::XMMatrixLookAtLH(m_camera_pos, DirectX::XMVectorAdd(m_camera_pos, m_camera_front), m_camera_up);
}
DirectX::XMVECTOR Camera::GetCameraPosition()
{
return m_camera_pos;
}
// Path: GameWindow.cpp