-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.h
235 lines (205 loc) · 6.17 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef CAMERA_H
#define CAMERA_H
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include<iostream>
#include <vector>
using namespace std;
// 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 float YAW = 90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
// An abstract camera class that processes input and calculates the corresponding Euler Angles, Vectors and Matrices for use in OpenGL
class Camera
{
public:
// Camera Attributes
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;
// Euler Angles
float Yaw;
float Pitch;
// Camera options
float MovementSpeed;
float MouseSensitivity;
float Zoom;
// 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), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
// Constructor with scalar values
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = glm::vec3(posX, posY, posZ);
WorldUp = glm::vec3(upX, upY, upZ);
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
// specially for double window
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
void ChangePos(glm::vec3 Pos)
{
Position = Pos;
}
// Returns the view matrix calculated using Euler Angles and the LookAt Matrix
glm::mat4 GetViewMatrix()
{
return glm::lookAt(Position, Position + Front, Up);
}
// 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, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
}
// Processes input received from a mouse input system. Expects the offset value in both the x and y direction.
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
Yaw += xoffset;
Pitch += yoffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
}
// Update Front, Right and Up Vectors using the updated Euler angles
updateCameraVectors();
}
// Processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis
void ProcessMouseScroll(float yoffset)
{
if (Zoom >= 1.0f && Zoom <= 45.0f)
Zoom -= yoffset;
if (Zoom <= 1.0f)
Zoom = 1.0f;
if (Zoom >= 45.0f)
Zoom = 45.0f;
}
// Calculates the front vector from the Camera's (updated) Euler Angles
void updateCameraVectors()
{
// Calculate the new Front vector
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
// Also re-calculate the Right and Up vector
Right = glm::normalize(glm::cross(Front, WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
Up = glm::normalize(glm::cross(Right, Front));
}
};
class Two_Camera
{
public:
glm::vec3 dis; // ÊÓ²î
Camera camera1;
Camera camera2;
Two_Camera(glm::vec3 dis_ = glm::vec3(2.0f, 0.0f, 0.0f), Camera camera1_ = Camera(glm::vec3(3.5f, -2.0f, -2.0f)))
{
dis = dis_;
//std::cout << dis << glm::vec3(0.0f, 0.0f, 3.0f) + dis << endl;
camera1 = camera1_;
camera2 = Camera(glm::vec3(3.5f, -2.0f, -2.0f) + dis);
}
glm::mat4 GetViewMatrix()
{
return camera1.GetViewMatrix();
}
void ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = camera1.MovementSpeed * deltaTime;
if (direction == FORWARD)
{
camera1.Position += camera1.Front * velocity;
camera2.Position = camera1.Position + dis;
}
if (direction == BACKWARD)
{
camera1.Position -= camera1.Front * velocity;
camera2.Position = camera1.Position + dis;
}
if (direction == LEFT)
{
camera1.Position -= camera1.Right * velocity;
camera2.Position = camera1.Position + dis;
}
if (direction == RIGHT)
{
camera1.Position += camera1.Right * velocity;
camera2.Position = camera1.Position + dis;
}
}
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
{
xoffset *= camera1.MouseSensitivity;
yoffset *= camera1.MouseSensitivity;
camera1.Yaw += xoffset;
camera2.Yaw += xoffset;
camera1.Pitch += yoffset;
camera2.Pitch += yoffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (camera1.Pitch > 89.0f) {
camera1.Pitch = 89.0f;
camera2.Pitch = 89.0f;
}
if (camera1.Pitch < -89.0f) {
camera1.Pitch = -89.0f;
camera2.Pitch = -89.0f;
}
}
// Update Front, Right and Up Vectors using the updated Euler angles
camera1.updateCameraVectors();
camera2.updateCameraVectors();
}
void ProcessMouseScroll(float yoffset)
{
if (camera1.Zoom >= 1.0f && camera1.Zoom <= 45.0f) {
camera1.Zoom -= yoffset;
camera2.Zoom -= yoffset;
}
if (camera1.Zoom <= 1.0f) {
camera1.Zoom = 1.0f;
camera2.Zoom = 1.0f;
}
if (camera1.Zoom >= 45.0f) {
camera1.Zoom = 45.0f;
camera2.Zoom = 45.0f;
}
}
};
#endif