-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
81 lines (66 loc) · 2.07 KB
/
camera.cpp
File metadata and controls
81 lines (66 loc) · 2.07 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
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
#include "camera.h"
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
Camera::Camera(glm::vec3 position)
: position(position),
front(glm::vec3(0.0f, 0.0f, -1.0f)),
worldUp(glm::vec3(0.0f, 1.0f, 0.0f)),
yaw(-90.0f),
pitch(0.0f),
movementSpeed(2.5f),
mouseSensitivity(0.1f)
{
updateCameraVectors();
}
glm::mat4 Camera::getViewMatrix() const
{
return glm::lookAt(position, position + front, up);
}
void Camera::processInput(GLFWwindow* window, float deltaTime)
{
float velocity = movementSpeed * deltaTime;
// W - Move forward
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
position += front * velocity;
// S - Move backward
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
position -= front * velocity;
// A - Move left
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
position -= right * velocity;
// D - Move right
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
position += right * velocity;
// SPACE - Move up
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
position += worldUp * velocity;
// CTRL - Move down
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)
position -= worldUp * velocity;
}
void Camera::processMouseMovement(float xoffset, float yoffset, bool constrainPitch)
{
xoffset *= mouseSensitivity;
yoffset *= mouseSensitivity;
yaw += xoffset;
pitch += yoffset;
if (constrainPitch)
{
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
}
updateCameraVectors();
}
void Camera::updateCameraVectors()
{
glm::vec3 newFront;
newFront.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
newFront.y = sin(glm::radians(pitch));
newFront.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(newFront);
right = glm::normalize(glm::cross(front, worldUp));
up = glm::normalize(glm::cross(right, front));
}