forked from cricklet/Hatched
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
157 lines (127 loc) · 3.87 KB
/
Copy pathcamera.cpp
File metadata and controls
157 lines (127 loc) · 3.87 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
#include "camera.h"
#include "helper.h"
#include <iostream>
using namespace std;
void
Camera::SetupTransforms(const Uniforms &u) {
GLint viewTransUnif = u.get(VIEW_TRANS);
GLint projTransUnif = u.get(PROJ_TRANS);
GLint invViewTransUnif = u.get(INV_VIEW_TRANS);
GLint invProjTransUnif = u.get(INV_PROJ_TRANS);
auto set = [] (GLint unif, auto &mat) {
if (unif != -1) {
glUniformMatrix4fv(unif, 1, GL_FALSE, glm::value_ptr(mat));
}
};
set(viewTransUnif, this->viewTrans);
set(projTransUnif, this->projTrans);
auto invView = glm::inverse(this->viewTrans);
auto invProj = glm::inverse(this->projTrans);
set(invViewTransUnif, invView);
set(invProjTransUnif, invProj);
checkErrors();
}
RotationCamera::RotationCamera() {
this->location = glm::vec3(1.2,0,0);
this->origin = glm::vec3(0,0,0);
this->up = glm::vec3(0,0,1);
this->projTrans = glm::perspective(
(float) (M_PI / 3), // fov y
WIDTH / (float) HEIGHT, // aspect
0.1f, // near
100.0f //far
);
}
void
RotationCamera::HandleEvent(SDL_Event event) {
if (event.type != SDL_MOUSEMOTION) return;
if (event.button.button != SDL_BUTTON_LEFT) return;
float dx = event.motion.xrel;
float dy = event.motion.yrel;
// Determine axis & angle to rotate camera
float vx = dx / (float) HEIGHT;
float vy = - dy / (float) WIDTH;
float theta = 4.0 * (fabs(vx) + fabs(vy));
glm::vec3 towards = this->origin - this->location;
glm::vec3 right = glm::cross(towards, this->up);
glm::vec3 vector = (right * vx) + (this->up * vy);
glm::vec3 axis = glm::cross(towards, vector);
axis = glm::normalize(axis);
this->location = glm::vec3(glm::rotate(theta, axis) * glm::vec4(this->location, 1.0));
}
void
RotationCamera::Think(float dt) {
this->viewTrans = glm::lookAt(
this->location, // location of camera
this->origin, // look at
this->up // camera up vector
);
}
FPSCamera::FPSCamera() {
this->location = glm::vec3(0,0,1.5);
this->pitch = - M_PI / 2;
this->yaw = 0;
this->projTrans = glm::perspective(
(float) (M_PI / 3), // fov y
WIDTH / (float) HEIGHT, // aspect
0.1f, // near
100.0f //far
);
}
static void handleMouse(SDL_Event event, float &pitch, float &yaw) {
if (event.type != SDL_MOUSEMOTION) return;
if (event.button.button != SDL_BUTTON_LEFT) return;
float dx = event.motion.xrel;
float dy = event.motion.yrel;
pitch += dy / 400.0f;
yaw += dx / 400.0f;
}
static glm::vec3 computeForward(float pitch, float yaw) {
return glm::vec3(
sin(pitch) * cos (yaw),
sin(pitch) * sin(yaw),
cos(pitch)
);
}
static glm::vec3 computeRight(glm::vec3 forward, glm::vec3 up) {
return glm::cross(forward, up);
}
static void handleKeys(SDL_Event event, bool &w, bool &a, bool &s, bool &d) {
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_w: w = true; break;
case SDLK_a: a = true; break;
case SDLK_s: s = true; break;
case SDLK_d: d = true; break;
}
}
if (event.type == SDL_KEYUP) {
switch (event.key.keysym.sym) {
case SDLK_w: w = false; break;
case SDLK_a: a = false; break;
case SDLK_s: s = false; break;
case SDLK_d: d = false; break;
}
}
}
void
FPSCamera::HandleEvent(SDL_Event event) {
handleMouse(event, this->pitch, this->yaw);
handleKeys(event, this->w, this->a, this->s, this->d);
}
void FPSCamera::Think(float dt) {
glm::vec3 forward = computeForward(this->pitch, this->yaw);
glm::vec3 right = computeRight(forward, glm::vec3(0,0,1));
glm::vec2 dir(0,0);
if (w) dir.y += 1;
if (a) dir.x -= 1;
if (s) dir.y -= 1;
if (d) dir.x += 1;
this->location += 3 * dt * forward * dir.y;
this->location += 4 * dt * right * dir.x;
this->viewTrans = glm::lookAt(
this->location, // location of camera
this->location + forward, // look at
glm::vec3(0,0,1) // camera up vector
);
}