-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
109 lines (90 loc) · 2.06 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
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "Camera.h"
void Camera::lookAt(glm::vec3 _pos, glm::vec3 _front, glm::vec3 _up)
{
m_position = _pos;
m_front = glm::normalize(_front);
m_up = _up;
m_vMatrix = glm::lookAt(m_position, m_position + m_front, m_up);
}
void Camera::update()
{
m_vMatrix = glm::lookAt(m_position, m_position + m_front, m_up);
}
glm::mat4 Camera::getViewMatrix()
{
return m_vMatrix;
}
glm::mat4 Camera::getProjectMatrix() {
return m_pMatrx;
}
void Camera::move(CAMERA_MOVE _mode)
{
switch (_mode)
{
case CAMERA_MOVE::MOVE_LEFT:
m_position -= glm::normalize(glm::cross(m_front, m_up)) * m_speed;
break;
case CAMERA_MOVE::MOVE_RIGHT:
m_position += glm::normalize(
glm::cross(m_front, m_up)) * m_speed;
break;
case CAMERA_MOVE::MOVE_FRONT:
m_position += m_speed * m_front;
break;
case CAMERA_MOVE::MOVE_BACK:
m_position -= m_speed * m_front;
break;
default:
break;
}
update();
}
void Camera::pitch(float _yOffset)
{
m_pitch += _yOffset * m_sensitivity;
if (m_pitch >= 89.0f)
{
m_pitch = 89.0f;
}
if (m_pitch <= -89.0f)
{
m_pitch = -89.0f;
}
m_front.y = sin(glm::radians(m_pitch));
m_front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(m_front);
update();
}
void Camera::yaw(float _xOffset)
{
m_yaw += _xOffset * m_sensitivity;
m_front.y = sin(glm::radians(m_pitch));
m_front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(m_front);
update();
}
void Camera::setSentitivity(float _s)
{
m_sensitivity = _s;
}
void Camera::setPerpective(float angle, float ratio, float near, float far) {
m_pMatrx = glm::perspective(glm::radians(angle), ratio, near, far);
}
void Camera::onMouseMove(double _xpos, double _ypos)
{
if (m_firstMove)
{
m_xpos = _xpos;
m_ypos = _ypos;
m_firstMove = false;
return;
}
float _xOffset = _xpos - m_xpos;
float _yOffset = -(_ypos - m_ypos);
m_xpos = _xpos;
m_ypos = _ypos;
pitch(_yOffset);
yaw(_xOffset);
}