./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 27
0.0%
Branches:
0 / 36
0.0%
Functions:
0 / 6
0.0%

libs/render/src/eu/render/camera.cc
Line Branch Exec Source
1 #include "eu/render/camera.h"
2
3 #include <cmath>
4
5 namespace eu::render
6 {
7
8 // todo(Gustav): this bit is weird, build a matrix directly and extract the in vector from it is probably better
9
10 ✗ m4 create_view_from_world_mat(const v3& pos, const CameraVectors& cv)
11 {
12
0/4
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 10 not taken.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 10 not taken.
✗ const auto trans = m4::from_translation(-pos);
13
0/4
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 11 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 11 not taken.
✗ const auto rot = m4::from(Q::look_in_direction(cv.front, kk::up));
14
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 12 not taken.
✗ return trans * rot;
15 }
16
17 ✗ CompiledCamera compile(const Camera& cam, const Size& window_size)
18 {
19 // todo(Gustav): use the near and far from the camera struct
20 ✗ const float aspect = static_cast<float>(window_size.width) / static_cast<float>(window_size.height);
21
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 9 not taken.
✗ const auto clip_from_view = m4::create_perspective(cam.fov, aspect, cam.near, cam.far);
22
23 return CompiledCamera{
24 .clip_from_view = clip_from_view,
25 ✗ .view_from_world = cam.world_from_view.get_inverted(),
26
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 9 not taken.
✗ .position = cam.world_from_view.get_translation(),
27
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 9 not taken.
✗ .in = cam.world_from_view.get_transformed_vec(kk::in)
28
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 9 not taken.
✗ };
29 }
30
31 ✗ CompiledCamera compile(const OrthoCamera& cam, const Size& window_size)
32 {
33 ✗ const float aspect = static_cast<float>(window_size.width) / static_cast<float>(window_size.height);
34
35 ✗ const float width = cam.size * aspect;
36 ✗ const float height = cam.size;
37
38
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 7 not taken.
✗ const auto clip_from_view = m4::create_ortho_lrud(-width/2.0f, width/2.0f, height/2.0f, -height/2.0f, cam.near, cam.far);
39
40
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 7 not taken.
✗ const auto camera_space = create_vectors(cam.rotation);
41
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 7 not taken.
✗ const auto view_from_world = create_view_from_world_mat(cam.position, camera_space);
42
43 return CompiledCamera{
44 .clip_from_view = clip_from_view,
45 .view_from_world = view_from_world,
46 .position = cam.position,
47 .in = camera_space.front
48 ✗ };
49 }
50
51 /** Convert from -1..1 to 0..1
52 */
53 ✗ float r01_from_r11(float f)
54 {
55 ✗ return (f + 1.0f) / 2.0f;
56 }
57
58 ✗ v2 screen_from_clip(const v2& resolution, const v4& clip_pos)
59 {
60
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 11 not taken.
✗ const auto ndc = clip_pos.to_vec3_persp_div();
61
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 10 not taken.
✗ return v2{r01_from_r11(ndc.x), r01_from_r11(ndc.y)} * resolution;
62 }
63
64 ✗ v2 screen_from_world(const CompiledCamera& cam, const v3& world_pos, const v2& resolution)
65 {
66
0/6
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 10 not taken.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 9 not taken.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 9 not taken.
✗ const auto clip_pos = cam.clip_from_view * cam.view_from_world * v4(world_pos, 1.0f);
67
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 11 not taken.
✗ return screen_from_clip(resolution, clip_pos);
68 }
69
70 } // namespace eu::render
71