-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.h
88 lines (78 loc) · 2.18 KB
/
Camera.h
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
#include<vector>
#define GLEW_STATIC
#include<GL/glew.h>
#include<glm/glm.hpp>
#include<glm/gtc/matrix_transform.hpp>
enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT
};
const GLfloat YAW = -90.0f;
const GLfloat PITCH = 0.0f;
const GLfloat ZOOM = 45.0f;
const GLfloat SPEED = 6.0f;
const GLfloat SENSITIVITY = 0.2f;
class Camera {
public:
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
GLfloat yaw = YAW, GLfloat pitch = PITCH) :front(glm::vec3(0.0f, 0.0f, -1.0f)), zoom(ZOOM) ,movementSpeed(SPEED),mouseSensitivity(SENSITIVITY){
this->position = position;
this->worldUp = up;
this->yaw = yaw;
this->pitch = pitch;
this->updateCameraVectors();
}
glm::mat4 GetViewMatrix() {
return glm::lookAt(this->position, this->position + this->front, this->up);
}
GLfloat GetZoom() {
return this->zoom;
}
glm::vec3 GetPosition() {
return this->position;
}
void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime) {
GLfloat velocity = this->movementSpeed * deltaTime;
if (direction == FORWARD) {
this->position += this->front * velocity;
}
if (direction == BACKWARD) {
this->position -= this->front * velocity;
}
if (direction == LEFT) {
this->position -= this->right * velocity;
}
if (direction == RIGHT) {
this->position += this->right * velocity;
}
}
void ProcessMouseMovement(GLfloat xOffset, GLfloat yOffset) {
xOffset *= this->mouseSensitivity;
yOffset *= this->mouseSensitivity;
this->yaw += xOffset;
this->pitch += yOffset;
this->updateCameraVectors();
}
private:
GLfloat yaw;
GLfloat pitch;
GLfloat zoom;
GLfloat movementSpeed;
GLfloat mouseSensitivity;
glm::vec3 position;
glm::vec3 front;
glm::vec3 right;
glm::vec3 up;
glm::vec3 worldUp;
void updateCameraVectors() {
glm::vec3 front;
front.x = cos(glm::radians(this->pitch)) * cos(glm::radians(this->yaw));
front.y = sin(glm::radians(this->pitch));
front.z = cos(glm::radians(this->pitch)) * sin(glm::radians(this->yaw));
this->front = glm::normalize(front);
this->right = glm::normalize(glm::cross(this->front, this->worldUp));
this->up = glm::normalize(glm::cross(this->right, this->front));
}
};