generated from 32blit/32blit-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.hpp
42 lines (28 loc) · 935 Bytes
/
camera.hpp
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
#pragma once
#include "engine/engine.hpp"
#include "types/point.hpp"
#include "types/vec3.hpp"
class Camera final {
public:
void update() {
forward = look_at - pos;
forward.normalize();
right = blit::Vec3(-forward.z, 0.0f, forward.x);
right.normalize();
up = right.cross(forward);
}
blit::Point world_to_screen(blit::Vec3 world_pos, float &scale, float &z) const {
blit::Point screen_center(viewport.x + viewport.w / 2, viewport.y + viewport.h / 2);
auto dist = world_pos - pos;
float x = dist.dot(right);
float y = -dist.dot(up);
z = dist.dot(forward);
scale = focal_distance / z;
return blit::Point(blit::Vec2(x, y) * scale) + screen_center;
}
blit::Vec3 pos, look_at;
blit::Vec3 forward, right, up;
float focal_distance = 320.0f;
float near = 1.0f, far = 500.0f;
blit::Rect viewport;
};