Skip to content

Commit a299ce7

Browse files
authored
Merge pull request #20 from Pilzschaf/tutorial24-fpscamera
FPS Camera
2 parents 036b63f + 8e99723 commit a299ce7

4 files changed

Lines changed: 94 additions & 10 deletions

File tree

camera.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22

33
#include "libs/glm/glm.hpp"
44
#include "libs/glm/ext/matrix_transform.hpp"
5-
#include "libs/glm/gtc/matrix_transform.hpp"
65

76
class Camera {
87
public:
98

109
Camera(float fov, float width, float height) {
1110
projection = glm::perspective(fov/2.0f, width / height, 0.1f, 1000.0f);
1211
view = glm::mat4(1.0f);
12+
position = glm::vec3(0.0f);
1313
update();
1414
}
1515

1616
glm::mat4 getViewProj() {
1717
return viewProj;
1818
}
1919

20-
void update() {
20+
virtual void update() {
2121
viewProj = projection * view;
2222
}
2323

24-
void translate(glm::vec3 v) {
24+
virtual void translate(glm::vec3 v) {
25+
position += v;
2526
view = glm::translate(view, v*-1.0f);
2627
}
2728

28-
private:
29+
protected:
30+
glm::vec3 position;
2931
glm::mat4 projection;
3032
glm::mat4 view;
3133
glm::mat4 viewProj;

floating_camera.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "fps_camera.h"
2+
3+
class FloatingCamera : public FPSCamera {
4+
public:
5+
FloatingCamera(float fov, float width, float height) : FPSCamera(fov, width, height) {}
6+
7+
void moveUp(float amount) {
8+
translate(up * amount);
9+
}
10+
};

fps_camera.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "camera.h"
2+
3+
class FPSCamera : public Camera {
4+
public:
5+
FPSCamera(float fov, float width, float height) : Camera(fov, width, height) {
6+
up = glm::vec3(0.0f, 1.0f, 0.0f);
7+
yaw = -90.0f;
8+
pitch = 0.0f;
9+
onMouseMoved(0.0f, 0.0f);
10+
update();
11+
}
12+
13+
void onMouseMoved(float xRel, float yRel) {
14+
yaw += xRel * mouseSensitivity;
15+
pitch -= yRel * mouseSensitivity;
16+
if(pitch > 89.0f)
17+
pitch = 89.0f;
18+
if(pitch < -89.0f)
19+
pitch = -89.0f;
20+
21+
glm::vec3 front;
22+
front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
23+
front.y = sin(glm::radians(pitch));
24+
front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
25+
lookAt = glm::normalize(front);
26+
update();
27+
}
28+
29+
void update() override {
30+
view = glm::lookAt(position, position+lookAt, up);
31+
viewProj = projection * view;
32+
}
33+
34+
void moveFront(float amount) {
35+
translate(glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f)*lookAt) * amount);
36+
update();
37+
}
38+
39+
void moveSideways(float amount) {
40+
translate(glm::normalize(glm::cross(lookAt, up)) * amount);
41+
update();
42+
}
43+
44+
protected:
45+
float yaw;
46+
float pitch;
47+
glm::vec3 lookAt;
48+
const float mouseSensitivity = 0.3f;
49+
glm::vec3 up;
50+
};

main.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "vertex_buffer.h"
2222
#include "index_buffer.h"
2323
#include "shader.h"
24-
#include "camera.h"
24+
#include "floating_camera.h"
2525

2626
void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) {
2727
std::cout << "[OpenGL Error] " << message << std::endl;
@@ -107,7 +107,7 @@ int main(int argc, char** argv) {
107107
glm::mat4 model = glm::mat4(1.0f);
108108
model = glm::scale(model, glm::vec3(1.2f));
109109

110-
Camera camera(90.0f, 800.0f, 600.0f);
110+
FloatingCamera camera(90.0f, 800.0f, 600.0f);
111111
camera.translate(glm::vec3(0.0f, 0.0f, 5.0f));
112112
camera.update();
113113

@@ -122,9 +122,13 @@ int main(int argc, char** argv) {
122122
bool buttonS = false;
123123
bool buttonA = false;
124124
bool buttonD = false;
125+
bool buttonSpace = false;
126+
bool buttonShift = false;
125127

128+
float cameraSpeed = 6.0f;
126129
float time = 0.0f;
127130
bool close = false;
131+
SDL_SetRelativeMouseMode(SDL_TRUE);
128132
while(!close) {
129133
SDL_Event event;
130134
while(SDL_PollEvent(&event)) {
@@ -144,6 +148,12 @@ int main(int argc, char** argv) {
144148
case SDLK_d:
145149
buttonD = true;
146150
break;
151+
case SDLK_SPACE:
152+
buttonSpace = true;
153+
break;
154+
case SDLK_LSHIFT:
155+
buttonShift = true;
156+
break;
147157
}
148158
} else if(event.type == SDL_KEYUP) {
149159
switch(event.key.keysym.sym) {
@@ -159,7 +169,15 @@ int main(int argc, char** argv) {
159169
case SDLK_d:
160170
buttonD = false;
161171
break;
172+
case SDLK_SPACE:
173+
buttonSpace = false;
174+
break;
175+
case SDLK_LSHIFT:
176+
buttonShift = false;
177+
break;
162178
}
179+
} else if(event.type == SDL_MOUSEMOTION) {
180+
camera.onMouseMoved(event.motion.xrel, event.motion.yrel);
163181
}
164182
}
165183

@@ -168,14 +186,18 @@ int main(int argc, char** argv) {
168186
time += delta;
169187

170188
if(buttonW) {
171-
camera.translate(glm::vec3(0.0f, 0.0f, -2.0f * delta));
189+
camera.moveFront(delta * cameraSpeed);
172190
}
173191
if(buttonS) {
174-
camera.translate(glm::vec3(0.0f, 0.0f, 2.0f * delta));
192+
camera.moveFront(-delta * cameraSpeed);
175193
}if(buttonA) {
176-
camera.translate(glm::vec3(-2.0f * delta, 0.0f, 0.0f));
194+
camera.moveSideways(-delta * cameraSpeed);
177195
}if(buttonD) {
178-
camera.translate(glm::vec3(2.0f * delta, 0.0f, 0.0f));
196+
camera.moveSideways(delta * cameraSpeed);
197+
}if(buttonSpace) {
198+
camera.moveUp(delta * cameraSpeed);
199+
}if(buttonShift) {
200+
camera.moveUp(-delta * cameraSpeed);
179201
}
180202

181203
camera.update();

0 commit comments

Comments
 (0)