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
|
#include "camera.h"
static void cameraUpdateVectors(struct Camera *camera);
Mat4
cameraGetViewMatrix(struct Camera camera)
{
Vec3 target = linearVec3Add(camera.position, camera.front);
return linearLookAt(camera.position, target, camera.up);
}
void
cameraProcessKeyboard(
struct Camera *camera,
enum CameraMovement direction,
float deltaTime)
{
float speed = camera->movementSpeed * deltaTime;
Vec3 tmp;
switch (direction) {
case CAMDIR_FORWARD:
// pos = pos + front * speed
tmp = linearVec3ScalarMulp(camera->front, speed);
camera->position = linearVec3Add(camera->position, tmp);
break;
case CAMDIR_BACKWARD:
// pos = pos + front * (-speed)
tmp = linearVec3ScalarMulp(camera->front, -speed);
camera->position = linearVec3Add(camera->position, tmp);
break;
case CAMDIR_LEFT:
// pos = pos + unit|(up x front)| * (speed)
tmp = linearVec3CrossProduct(camera->front, camera->up);
tmp = linearVec3Normalize(tmp);
tmp = linearVec3ScalarMulp(tmp, -speed);
camera->position = linearVec3Add(camera->position, tmp);
break;
case CAMDIR_RIGHT:
// pos = pos + unit|(up x front)| * (-speed)
tmp = linearVec3CrossProduct(camera->front, camera->up);
tmp = linearVec3Normalize(tmp);
tmp = linearVec3ScalarMulp(tmp, speed);
camera->position = linearVec3Add(camera->position, tmp);
break;
default:
break;
}
}
void
cameraProcessMouseMovement(struct Camera *camera, float xoffset, float yoffset)
{
camera->yaw += camera->mouseSensivity * xoffset;
camera->pitch += camera->mouseSensivity * yoffset;
if (camera->pitch > 89.0f)
camera->pitch = 89.0f;
else if (camera->pitch < -89.0f)
camera->pitch = - 89.0f;
cameraUpdateVectors(camera);
}
void
cameraUpdateVectors(struct Camera *camera)
{
Vec3 tmp;
float rpitch = M_PI / 180 * camera->pitch;
float ryaw = M_PI / 180 * camera->yaw;
tmp = linearVec3(
cosf(ryaw) * cosf(-rpitch),
sinf(-rpitch),
sinf(ryaw) * cosf(-rpitch));
camera->front = linearVec3Normalize(tmp);
}
|