-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
34 lines (31 loc) · 1.15 KB
/
camera.cpp
File metadata and controls
34 lines (31 loc) · 1.15 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
#include "camera.hpp"
#include "display.hpp"
#include <cmath>
#include <GL/glew.h>
#include "glm/glm.hpp"
#include "glm/gtx/transform.hpp"
void set_view_matrices(Camera const& camera, unsigned int program) {
unsigned int location;
glUseProgram(program);
auto xlook = -sin(camera.yaw);
auto ylook = sin(camera.pitch);
auto zlook = -cos(camera.yaw);
// View
glm::mat4 view = glm::lookAt(
glm::vec3(camera.position.x, camera.position.y, camera.position.z),
glm::vec3(camera.position.x + xlook, camera.position.y + ylook,
camera.position.z + zlook), glm::vec3(0, 1, 0));
location = glGetUniformLocation(program, "view");
glUniformMatrix4fv(location, 1, GL_FALSE, &view[0][0]);
// Perspective Matrix
location = glGetUniformLocation(program, "perspective");
constexpr float fov = 90.0f;
auto perspective = glm::perspective(fov/180.0f*3.1415f,
static_cast<float>(width/height), 0.1f, 10000.0f);
glUniformMatrix4fv(location, 1, GL_FALSE, &perspective[0][0]);
}
void Camera::clamp() {
constexpr auto HALF_PI = 3.1415 / 2.0;
pitch = fmin(pitch, HALF_PI);
pitch = fmax(pitch, -HALF_PI);
}