~vladh/peony

peony/src/cameras.cpp -rw-r--r-- 3.8 KiB
7fc9922bVlad-Stefan Harbuz changes shaders namespace into class a month ago
                                                                                
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
  Peony Game Engine
  Copyright (C) 2020 Vlad-Stefan Harbuz <vlad@vladh.net>
  All rights reserved.
*/

#include "cameras.hpp"
#include "intrinsics.hpp"


namespace cameras {
  pny_internal void update_matrices_ortho(
    Camera *camera, uint32 window_width, uint32 window_height
  ) {
    if (window_width == 0 || window_height == 0) {
      return;
    }

    camera->view = glm::lookAt(
      camera->position,
      v3(0.0f, 0.0f, 0.0f),
      camera->up
    );

    camera->projection = glm::ortho(
      -10.0f, 10.0f, -10.0f, 10.0f,
      camera->near_clip_dist, camera->far_clip_dist
    );
  }


  pny_internal void update_matrices_perspective(
    Camera *camera, uint32 window_width, uint32 window_height
  ) {
    if (window_width == 0 || window_height == 0) {
      return;
    }

    camera->front = normalize(v3(
      cos(radians(camera->yaw)) * cos(radians(camera->pitch)),
      -sin(radians(camera->pitch)),
      sin(radians(camera->yaw)) * cos(radians(camera->pitch))
    ));

    camera->view = glm::lookAt(
      camera->position, camera->position + camera->front, camera->up
    );

    camera->projection = glm::perspective(
      radians(camera->horizontal_fov),
      (real32)window_width / (real32)window_height,
      camera->near_clip_dist, camera->far_clip_dist
    );
    // https://en.wikipedia.org/wiki/Field_of_view_in_video_games#Field_of_view_calculations
    camera->vertical_fov = (real32)degrees(2 * atan(
      tan(radians(camera->horizontal_fov) / 2) * window_height / window_width
    ));
  }
}


void cameras::update_matrices(
  Camera *camera, uint32 window_width, uint32 window_height
) {
  if (window_width == 0 || window_height == 0) {
    return;
  }

  if (camera->type == CameraType::perspective) {
    update_matrices_perspective(camera, window_width, window_height);
  } else if (camera->type == CameraType::ortho) {
    update_matrices_ortho(camera, window_width, window_height);
  }
}


void cameras::update_ui_matrices(
  Camera *camera, uint32 window_width, uint32 window_height
) {
  camera->ui_projection = glm::ortho(
    0.0f, (real32)window_width, 0.0f, (real32)window_height
  );
}


void cameras::move_front_back(Camera *camera, real32 sign, real64 dt) {
  camera->position += (sign * camera->speed * (real32)dt) * camera->front;
}


void cameras::move_left_right(Camera *camera, real32 sign, real64 dt) {
  v3 direction = normalize(cross(
    camera->front, camera->up
  ));
  camera->position += (sign * camera->speed * (real32)dt) * direction;
}


void cameras::move_up_down(Camera *camera, real32 sign, real64 dt) {
  camera->position += (sign * camera->speed * (real32)dt) * camera->up;
}


void cameras::update_mouse(Camera *camera, v2 mouse_offset) {
  camera->yaw += mouse_offset.x;
  camera->pitch += mouse_offset.y;

  if (camera->pitch > 89.0f) {
    camera->pitch = 89.0f;
  } else if (camera->pitch < -89.0f) {
    camera->pitch = -89.0f;
  }
}


void cameras::init(
  CamerasState *cameras_state,
  uint32 window_width,
  uint32 window_height
) {
  cameras_state->camera_main.type = CameraType::perspective;
  cameras_state->camera_main.yaw = -45.0f;
  cameras_state->camera_main.pitch = 0.0f;
  cameras_state->camera_main.position = v3(-7.0f, 3.0f, 7.0f);
  cameras_state->camera_main.front = v3(0.0f, 0.0f, 0.0f);
  cameras_state->camera_main.up = v3(0.0f, 1.0f, 0.0f);
  cameras_state->camera_main.speed = 5.0f;
  cameras_state->camera_main.horizontal_fov = 60.0f;
  cameras_state->camera_main.vertical_fov = 0.0f; // Filled in later
  cameras_state->camera_main.near_clip_dist = 0.1f;
  cameras_state->camera_main.far_clip_dist = 600.0f;
  cameras_state->camera_main.exposure = 1.0f;

  update_matrices(&cameras_state->camera_main, window_width, window_height);
  update_ui_matrices(&cameras_state->camera_main, window_width, window_height);

  cameras_state->camera_active = &cameras_state->camera_main;
}