-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
44 lines (39 loc) · 1.6 KB
/
Camera.cpp
File metadata and controls
44 lines (39 loc) · 1.6 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
#include "Camera.h"
Camera::Camera(int height, int width, glm::vec3 position) {
cameraPos = position;
this->height = height;
this->width = width;
this->Orientation = glm::normalize(glm::vec3(0.0f, 0.0f, 0.0f) - cameraPos);
}
void Camera::Matrix(float FOVdeg, float nearPlane, float farPlane, Shader &shader, const char *uniform) {
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
view = glm::lookAt(cameraPos, cameraPos + Orientation, Up);
projection = glm::perspective(glm::radians(FOVdeg), (float) width / (float) height, nearPlane, farPlane);
glUniformMatrix4fv(glGetUniformLocation(shader.ID, uniform), 1, GL_FALSE, glm::value_ptr(projection * view));
}
void Camera::Inputs(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
cameraPos += speed * Orientation;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
cameraPos -= speed * Orientation;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
cameraPos -= glm::normalize(glm::cross(Orientation, Up)) * speed;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
cameraPos += glm::normalize(glm::cross(Orientation, Up)) * speed;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
cameraPos += speed * Up;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) {
cameraPos -= speed * Up;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
speed = 0.1f;
} else if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE) {
speed = 0.01f;
}
}