-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCamera.cpp
82 lines (61 loc) · 1.89 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
#include "Camera.h"
Camera::Camera(const glm::vec3 &position, const glm::vec3 &up, float yaw, float pitch)
: m_front(glm::vec3(0.0f, 0.0f, -1.0f)), m_movementSpeed(SPEED), m_mouseSensitivity(SENSITIVTY)
{
m_pos = position;
m_worldUp = up;
m_yaw = yaw;
m_pitch = pitch;
updateCameraVectors();
}
Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch)
: m_front(glm::vec3(0.0f, 0.0f, -1.0f)), m_movementSpeed(SPEED), m_mouseSensitivity(SENSITIVTY)
{
m_pos = glm::vec3(posX, posY, posZ);
m_worldUp = glm::vec3(upX, upY, upZ);
m_yaw = yaw;
m_pitch = pitch;
updateCameraVectors();
}
glm::mat4 Camera::getViewMatrix()
{
return glm::lookAt(m_pos, m_pos + m_front, m_up);
}
void Camera::processKeyboard(CameraMovement direction, float deltaTime)
{
float velocity = m_movementSpeed*deltaTime;
if (direction & CameraMovement::eForward)
m_pos += m_front*velocity;
if (direction & CameraMovement::eBackward )
m_pos -= m_front*velocity;
if (direction & CameraMovement::eLeft)
m_pos -= m_right*velocity;
if (direction & CameraMovement::eRight)
m_pos += m_right*velocity;
}
void Camera::processMouseMovement(float xoffset, float yoffset, bool constrainm_pitch) {
xoffset *= m_mouseSensitivity;
yoffset *= m_mouseSensitivity;
m_yaw += xoffset;
m_pitch += yoffset;
if (constrainm_pitch) {
if (m_pitch > 89.0f)
m_pitch = 89.0f;
if (m_pitch < -89.0f)
m_pitch = 89.0f;
}
updateCameraVectors();
return;
}
void Camera::updateCameraVectors() {
m_front.x = cos(glm::radians(m_yaw))*cos(glm::radians(m_pitch));
m_front.y = sin(glm::radians(m_pitch));
m_front.z = sin(glm::radians(m_yaw))*cos(glm::radians(m_pitch));
m_front = glm::normalize(m_front);
m_right = glm::normalize(glm::cross(m_front, m_worldUp));
m_up = glm::normalize(glm::cross(m_right, m_front));
}
glm::vec3 Camera::getPositon() const
{
return glm::vec3(m_pos);
}