-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.hpp
121 lines (107 loc) · 3.6 KB
/
Camera.hpp
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
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once
#include <cstdio>
#include <cmath>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include "incgraphics.h"
#include "Transformation.hpp"
struct Camera {
bool has_changed = true;
Camera()
{}
void update(float ratio) {
/* cameraPos = cameraTarget + glm::vec3(0, std::cos(glm::radians(angle)), std::sin(glm::radians(angle))); */
cameraPos = cameraTarget + glm::vec3(0, std::cos(glm::radians(angle)), std::sin(glm::radians(angle)));
cameraDirection = glm::normalize(cameraPos - cameraTarget);
cameraRight = glm::normalize(glm::cross(up, cameraDirection));
cameraUp = glm::cross(cameraDirection, cameraRight);
view = glm::lookAt(
cameraPos,
cameraTarget,
up
);
projection = glm::infinitePerspective(
glm::radians(fov),
ratio,
0.1f
);
/* * glm::ortho( */
/* -ratio, ratio, */
/* -ratio, ratio, */
/* std::cos(glm::radians(fov)), 0.f */
/* ); */
}
glm::vec3 cameraPos;
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection;
glm::vec3 up = glm::vec3(0, 0, 1);
glm::vec3 cameraRight;
glm::vec3 cameraUp;
glm::mat4 view;
glm::mat4 projection;
float fov = 75.;
float angle = 60.f;
/* glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, 0.0f); */
/* glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); */
float cameraSpeedKeys = 0.05f; // adjust accordingly
float cameraSpeedMouse = 0.02f; // adjust accordingly
void move_up(float movespeed) {
cameraTarget.y = std::fmax(cameraTarget.y - movespeed, -1.5); /* * cameraFront; */
has_changed = true;
}
void move_down(float movespeed) {
cameraTarget.y = std::fmin(cameraTarget.y + movespeed, 2); /* * cameraFront; */
has_changed = true;
}
void move_left(float movespeed) {
cameraTarget.x = std::fmin(cameraTarget.x + movespeed, 3); /* glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed; */
has_changed = true;
}
void move_right(float movespeed) {
cameraTarget.x = std::fmax(cameraTarget.x - movespeed, -3); /* glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed; */
has_changed = true;
}
void keyboard(GLFWwindow *w, float ratio) {
/* static float accel = 1.01; */
if (glfwGetKey(w, GLFW_KEY_UP) == GLFW_PRESS) {
move_up(cameraSpeedKeys);
// * cameraFront;
} else if (glfwGetKey(w, GLFW_KEY_DOWN) == GLFW_PRESS) {
move_down(cameraSpeedKeys);
// * cameraFront;
} else if (glfwGetKey(w, GLFW_KEY_LEFT) == GLFW_PRESS) {
move_left(cameraSpeedKeys);
} else if (glfwGetKey(w, GLFW_KEY_RIGHT) == GLFW_PRESS) {
move_right(cameraSpeedKeys);
} else if (glfwGetKey(w, GLFW_KEY_MINUS) == GLFW_PRESS) {
fov = std::fmin<double>(fov + 1, 89);
has_changed = true;
} else if (glfwGetKey(w, GLFW_KEY_EQUAL) == GLFW_PRESS) {
fov = std::fmax<double>(fov - 1, 1);
has_changed = true;
} else if (glfwGetKey(w, GLFW_KEY_LEFT_BRACKET) == GLFW_PRESS) {
angle = std::fmin<double>(angle + 1, 89);
has_changed = true;
} else if (glfwGetKey(w, GLFW_KEY_RIGHT_BRACKET) == GLFW_PRESS) {
angle = std::fmax<double>(angle - 1, 10);
has_changed = true;
}
update(ratio);
}
void mouse(double x, double y) {
double border = .02;
if(x < border) {
move_left(cameraSpeedMouse);
} else if(x > 1.-border) {
move_right(cameraSpeedMouse);
}
if(y < border) {
move_up(cameraSpeedMouse);
} else if(y > 1.-border) {
move_down(cameraSpeedMouse);
}
}
decltype(auto) get_matrix() {
return projection * view;
}
};