-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.h
More file actions
171 lines (142 loc) · 4.13 KB
/
Camera.h
File metadata and controls
171 lines (142 loc) · 4.13 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once
// Std. Includes
#include <vector>
// GL Includes
#define GLEW_STATIC
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods
enum Camera_Movement
{
FORWARD,
BACKWARD,
LEFT,
RIGHT
};
// Default camera values
const GLfloat YAW = -90.0f;
const GLfloat PITCH = 0.0f;
const GLfloat SPEED = 6.0f;
const GLfloat SENSITIVTY = 0.25f;
const GLfloat ZOOM = 45.0f;
class Camera
{
public:
// Constructor with vectors
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) : m_front(glm::vec3(0.0f, 0.0f, -1.0f)), m_movementSpeed(SPEED), m_mouseSensitivity(SENSITIVTY), m_zoom(ZOOM)
{
m_position = position;
m_worldUp = up;
m_yaw = yaw;
m_pitch = pitch;
updateCameraVectors();
}
// Constructor with scalar values
Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch) : m_front(glm::vec3(0.0f, 0.0f, -1.0f)), m_movementSpeed(SPEED), m_mouseSensitivity(SENSITIVTY), m_zoom(ZOOM)
{
m_position = glm::vec3(posX, posY, posZ);
m_worldUp = glm::vec3(upX, upY, upZ);
m_yaw = yaw;
m_pitch = pitch;
updateCameraVectors();
}
// get position
glm::vec3 getPosition() { return m_position; }
// Returns the view matrix calculated using Eular Angles and the LookAt Matrix
glm::mat4 GetViewMatrix()
{
return glm::lookAt(m_position, m_position + m_front, m_up);
}
// set camera position
void setCameraPosition(const glm::vec3 &pos) {
m_position = pos;
}
// Processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems)
void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime)
{
GLfloat velocity = m_movementSpeed * deltaTime;
if (direction == FORWARD)
{
m_position += m_front * velocity;
}
if (direction == BACKWARD)
{
m_position -= m_front * velocity;
}
if (direction == LEFT)
{
m_position -= m_right * velocity;
}
if (direction == RIGHT)
{
m_position += m_right * velocity;
}
}
// Processes input received from a mouse input system. Expects the offset value in both the x and y direction.
void ProcessMouseMovement(GLfloat xOffset, GLfloat yOffset, GLboolean constrainPitch = true)
{
xOffset *= m_mouseSensitivity;
yOffset *= m_mouseSensitivity;
m_yaw += xOffset;
m_pitch += yOffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (m_pitch > 89.0f)
{
m_pitch = 89.0f;
}
if (m_pitch < -89.0f)
{
m_pitch = -89.0f;
}
}
// Update Front, Right and Up Vectors using the updated Eular angles
this->updateCameraVectors();
}
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
void ProcessMouseScroll(GLfloat yOffset)
{
if (m_zoom >= 1.0f && m_zoom <= 45.0f)
{
m_zoom -= yOffset;
}
if (m_zoom <= 1.0f)
{
m_zoom = 1.0f;
}
if (m_zoom >= 45.0f)
{
m_zoom = 45.0f;
}
}
GLfloat GetZoom() { return m_zoom; }
private:
// Camera Attributes
glm::vec3 m_position;
glm::vec3 m_front;
glm::vec3 m_up;
glm::vec3 m_right;
glm::vec3 m_worldUp;
// Eular Angles
GLfloat m_yaw;
GLfloat m_pitch;
// Camera options
GLfloat m_movementSpeed;
GLfloat m_mouseSensitivity;
GLfloat m_zoom;
// Calculate the front vector from the Camera's (updated) Eular Angles
void updateCameraVectors()
{
// Calculate the new Front vector
glm::vec3 front;
front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
front.y = sin(glm::radians(m_pitch));
front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(front);
// re-calculate the Right and Up vector (Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.)
m_right = glm::normalize(glm::cross(m_front, m_worldUp));
m_up = glm::normalize(glm::cross(m_right, m_front));
}
};