forked from SurajSharma90/OpenGL-C---Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.h
124 lines (98 loc) · 2.58 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
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
122
123
124
#pragma once
#include<iostream>
#include<glew.h>
#include<glfw3.h>
#include<glm.hpp>
#include<vec3.hpp>
#include<mat4x4.hpp>
#include<gtc\matrix_transform.hpp>
enum direction {FORWARD = 0, BACKWARD, LEFT, RIGHT};
class Camera
{
private:
glm::mat4 ViewMatrix;
GLfloat movementSpeed;
GLfloat sensitivity;
glm::vec3 worldUp;
glm::vec3 position;
glm::vec3 front;
glm::vec3 right;
glm::vec3 up;
GLfloat pitch;
GLfloat yaw;
GLfloat roll;
void updateCameraVectors()
{
this->front.x = cos(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
this->front.y = sin(glm::radians(this->pitch));
this->front.z = sin(glm::radians(this->yaw)) * cos(glm::radians(this->pitch));
this->front = glm::normalize(this->front);
this->right = glm::normalize(glm::cross(this->front, this->worldUp));
this->up = glm::normalize(glm::cross(this->right, this->front));
}
public:
Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 worldUp)
{
this->ViewMatrix = glm::mat4(1.f);
this->movementSpeed = 3.f;
this->sensitivity = 5.f;
this->worldUp = worldUp;
this->position = position;
this->right = glm::vec3(0.f);
this->up = worldUp;
this->pitch = 0.f;
this->yaw = -90.f;
this->roll = 0.f;
this->updateCameraVectors();
}
~Camera() {}
//Accessors
const glm::mat4 getViewMatrix()
{
this->updateCameraVectors();
this->ViewMatrix = glm::lookAt(this->position, this->position + this->front, this->up);
return this->ViewMatrix;
}
const glm::vec3 getPosition() const
{
return this->position;
}
//Functions
void move(const float& dt, const int direction)
{
//Update position vector
switch (direction)
{
case FORWARD:
this->position += this->front * this->movementSpeed * dt;
break;
case BACKWARD:
this->position -= this->front * this->movementSpeed * dt;
break;
case LEFT:
this->position -= this->right * this->movementSpeed * dt;
break;
case RIGHT:
this->position += this->right * this->movementSpeed * dt;
break;
default:
break;
}
}
void updateMouseInput(const float& dt, const double& offsetX, const double& offsetY)
{
//Update pitch yaw and roll
this->pitch += static_cast<GLfloat>(offsetY) * this->sensitivity * dt;
this->yaw += static_cast<GLfloat>(offsetX) * this->sensitivity * dt;
if (this->pitch > 80.f)
this->pitch = 80.f;
else if (this->pitch < -80.f)
this->pitch = -80.f;
if (this->yaw > 360.f || this->yaw < -360.f)
this->yaw = 0.f;
}
void updateInput(const float& dt, const int direction, const double& offsetX, const double& offsetY)
{
this->updateMouseInput(dt, offsetX, offsetY);
}
};