forked from apurvsinghdark/GraphicProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.h
134 lines (105 loc) · 2.94 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
125
126
127
128
129
130
131
132
133
134
#pragma once
#include<iostream>
#include<glew.h>
#include<glfw3.h>
#include<glm.hpp>
#include<vec3.hpp>
#include<mat4x4.hpp>
#include<gtc/matrix_transform.hpp>
#include<gtc/type_ptr.hpp>
enum direction { FORWARD = 0, BACKWARD, LEFT, RIGHT };
class Camera
{
private:
glm::mat4 ViewMatrix;
GLfloat movementSpeed;
GLfloat sensitivity;
glm::vec3 position;
glm::vec3 worldUp;
glm::vec3 front;
glm::vec3 right;
glm::vec3 up;
GLfloat pitch;
GLfloat yaw;
GLfloat roll;
void UpdateCameraVectors();
public:
Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 worldUp);
~Camera();
glm::mat4 GetViewMatrix();
const glm::vec3 GetPosition() const;
void UpdateInput(const float& dt, const int direction, const double& offsetX, const double& offsetY);
void UpdateMouseInput(const float& dt, const double& offsetX, const double& offsetY);
void Move(const float& dt, const int direction);
};
void Camera::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(front);
this->right = glm::normalize(glm::cross(this->front, this->worldUp));
this->up = glm::normalize(glm::cross(this->right, this->front));
}
Camera::Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 worldUp)
{
this->ViewMatrix = glm::mat4(1.0f);
this->movementSpeed = 3.0f;
this->sensitivity = 3.0f;
this->worldUp = worldUp;
this->position = position;
this->up = worldUp;
this->right = glm::vec3(1.0f);
this->pitch = 0.0f;
this->yaw = -90.0f;
this->roll = 0.0f;
this->UpdateCameraVectors();
}
Camera::~Camera()
{
}
glm::mat4 Camera::GetViewMatrix()
{
this->ViewMatrix = glm::lookAt(this->position, this->position + this->front, this->worldUp);
this->UpdateCameraVectors();
return this->ViewMatrix;
}
const glm::vec3 Camera::GetPosition() const
{
return this->position;
}
void Camera::UpdateInput(const float& dt, const int direction, const double& offsetX, const double& offsetY)
{
this->UpdateMouseInput(dt, offsetX, offsetY);
}
void Camera::UpdateMouseInput(const float& dt, const double& offsetX, const double& offsetY)
{
this->pitch += static_cast<GLfloat>(offsetY) * this->sensitivity * dt;
this->yaw += static_cast<GLfloat>(offsetX) * this->sensitivity * dt;
if (this->pitch > 89.0f)
this->pitch = 89.0f;
if (this->pitch < -89.0f)
this->pitch = -89.0f;
if (this->yaw > 360.0f || this->yaw < -360.0f)
this->yaw = 0.0f;
}
void Camera::Move(const float& dt, const int direction)
{
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;
}
}