-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.c
66 lines (52 loc) · 1.92 KB
/
Camera.c
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
#include "Camera.h"
#include <math.h>
void Camera_create(Camera* camera) {
camera->pos = (Vec3) { 0.0, 10.0, 0.0 };
camera->rot = (Vec3) { 0.0, 0.0, 0.0 };
camera->focusedBodyId = -1;
}
void Camera_accelForward(Camera* camera, double amount) {
camera->vel = vsum(camera->vel, vscl(Camera_forwardVector(camera), amount));
}
void Camera_accelRight(Camera* camera, double amount) {
camera->vel = vsum(camera->vel, vscl(Camera_rightVector(camera), amount));
}
void Camera_accelUp(Camera* camera, double amount) {
camera->vel = vsum(camera->vel, vscl(Camera_upVector(camera), amount));
}
void Camera_move(Camera* camera, double dt) {
camera->pos = vsum(camera->pos, vscl(camera->vel, dt));
}
Vec3 Camera_forwardVector(Camera* camera) {
double yaw = camera->rot.y;
double cosYaw = cos(yaw);
double sinYaw = sin(yaw);
Vec3 forward = vrotzx((Vec3) { 0.0, 0.0, -1.0 }, cosYaw, sinYaw);
return forward;
}
Vec3 Camera_rightVector(Camera* camera) {
double yaw = camera->rot.y;
double cosYaw = cos(yaw);
double sinYaw = sin(yaw);
Vec3 right = vrotzx((Vec3) { 1.0, 0.0, 0.0 }, cosYaw, sinYaw);
return right;
}
Vec3 Camera_upVector(Camera* camera) {
return (Vec3) { 0.0, 1.0, 0.0 };
}
Vec3 Camera_pixelToRay(Camera* camera, double x, double y, double width, double height, double fov) {
double sizeY = 2.0 * tan(fov / 2.0);
double sizeX = sizeY * width / height;
double nearX = (x / width - 0.5) * sizeX;
double nearY = -(y / height - 0.5) * sizeY;
double yaw = camera->rot.y;
double pitch = camera->rot.x;
double cosYaw = cos(yaw);
double sinYaw = sin(yaw);
double cosPitch = cos(pitch);
double sinPitch = sin(pitch);
Vec3 ray = vnorm((Vec3) { nearX, nearY, -1.0 });
ray = vrotyz(ray, cosPitch, sinPitch);
ray = vrotzx(ray, cosYaw, sinYaw);
return ray;
}