-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
85 lines (67 loc) · 1.86 KB
/
Camera.cpp
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
#include "Camera.h"
static camera_t camera;
void init_camera(vec3_t position, vec3_t direction)
{
camera.position = position;
camera.direction = direction;
camera.forward_velocity = vec3_new(0, 0, 0);
camera.yaw = 0.0;
camera.pitch = 0.0;
};
vec3_t get_camera_position(void)
{
return camera.position;
}
vec3_t get_camera_direction(void)
{
return camera.direction;
}
vec3_t get_camera_forward_velocity(void)
{
return camera.forward_velocity;
}
float get_camera_yaw(void)
{
return camera.yaw;
}
float get_camera_pitch(void)
{
return camera.pitch;
}
void update_camera_position(vec3_t position)
{
camera.position = position;
}
void update_camera_direction(vec3_t direction)
{
camera.direction = direction;
}
void update_camera_forward_velocity(vec3_t forward_velocity)
{
camera.forward_velocity = forward_velocity;
}
void rotate_camera_yaw(float angle)
{
camera.yaw += angle;
}
void rotate_camera_pitch(float angle)
{
camera.pitch += angle;
}
vec3_t get_camera_lookat_target(void)
{
// Initialize the target looking at the positive z-axis
vec3_t target = { 0, 0, 1 };
mat4_t camera_yaw_rotation = mat4_make_rotation_y(camera.yaw);
mat4_t camera_pitch_rotation = mat4_make_rotation_x(camera.pitch);
// Create camera rotation matrix based on yaw and pitch
mat4_t camera_rotation = mat4_identity();
camera_rotation = mat4_mul_mat4(camera_pitch_rotation, camera_rotation);
camera_rotation = mat4_mul_mat4(camera_yaw_rotation, camera_rotation);
// Update camera direction based on the rotation
vec4_t camera_direction = mat4_mul_vec4(camera_rotation, vec4_from_vec3(target));
camera.direction = vec3_from_vec4(camera_direction);
// Offset the camera position in the direction where the camera is pointing at
target = vec3_add(camera.position, camera.direction);
return target;
}