Repository navigation
Expand file tree
/
Copy pathCamera.cpp
More file actions
48 lines (37 loc) · 2.08 KB
/
Copy pathCamera.cpp
File metadata and controls
48 lines (37 loc) · 2.08 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
#include "Camera.h"
#include <SDL2/SDL.h>
void Camera::resize(int width, int height) {
float half_width = 0.5f * width;
float half_height = 0.5f * height;
// Distance to the viewing plane
float d = half_width / tanf(0.5f * fov);
// Initialize viewing pyramid vectors
top_left_corner = Vector3(-half_width, half_height, d);
x_axis = Vector3(1.0f, 0.0f, 0.0f);
y_axis = Vector3(0.0f, -1.0f, 0.0f);
}
void Camera::update(float delta, const unsigned char * keys) {
// Move Camera around
const float MOVEMENT_SPEED = 10.0f;
const float ROTATION_SPEED = 3.0f;
Vector3 right = rotation * Vector3(1.0f, 0.0f, 0.0f);
Vector3 forward = rotation * Vector3(0.0f, 0.0f, 1.0f);
if (keys[SDL_SCANCODE_W]) position += forward * MOVEMENT_SPEED * delta;
if (keys[SDL_SCANCODE_A]) position -= right * MOVEMENT_SPEED * delta;
if (keys[SDL_SCANCODE_S]) position -= forward * MOVEMENT_SPEED * delta;
if (keys[SDL_SCANCODE_D]) position += right * MOVEMENT_SPEED * delta;
if (keys[SDL_SCANCODE_LSHIFT]) position.y -= MOVEMENT_SPEED * delta;
if (keys[SDL_SCANCODE_SPACE]) position.y += MOVEMENT_SPEED * delta;
if (keys[SDL_SCANCODE_UP]) rotation = Quaternion::axis_angle(right, -ROTATION_SPEED * delta) * rotation;
if (keys[SDL_SCANCODE_DOWN]) rotation = Quaternion::axis_angle(right, +ROTATION_SPEED * delta) * rotation;
if (keys[SDL_SCANCODE_LEFT]) rotation = Quaternion::axis_angle(Vector3(0.0f, 1.0f, 0.0f), -ROTATION_SPEED * delta) * rotation;
if (keys[SDL_SCANCODE_RIGHT]) rotation = Quaternion::axis_angle(Vector3(0.0f, 1.0f, 0.0f), +ROTATION_SPEED * delta) * rotation;
if (keys[SDL_SCANCODE_F]) {
printf("camera.position = Vector3(%ff, %ff, %ff);\n", position.x, position.y, position.z);
printf("camera.rotation = Quaternion(%ff, %ff, %ff, %ff);\n", rotation.x, rotation.y, rotation.z, rotation.w);
}
// Transform view pyramid according to rotation
rotated_top_left_corner = SIMD_Vector3(rotation * top_left_corner);
rotated_x_axis = SIMD_Vector3(rotation * x_axis);
rotated_y_axis = SIMD_Vector3(rotation * y_axis);
}