]> git.tdb.fi Git - libs/game.git/blob - source/game/camera.h
f6df873c1120586c04e6ce6fb3cfb80d55e15159
[libs/game.git] / source / game / camera.h
1 #ifndef MSP_GAME_CAMERA_H_
2 #define MSP_GAME_CAMERA_H_
3
4 #include <msp/geometry/angle.h>
5 #include <msp/linal/vector.h>
6 #include "component.h"
7
8 namespace Msp::Game {
9
10 enum class CameraScaling
11 {
12         ORIGINAL_SIZE,
13         SCALE_TO_FIT,
14         SCALE_TO_COVER,
15         STRETCH_TO_FIT
16 };
17
18 struct CameraSetup
19 {
20         Geometry::Angle<float> field_of_view_y = Geometry::Angle<float>::from_degrees(60);
21         LinAl::Vector<float, 2> size = { 16.0f/9.0f, 1.0f };
22         float near_clip = 0.1f;
23         float far_clip = 100.0f;
24         CameraScaling scaling = CameraScaling::SCALE_TO_FIT;
25         std::string sequence_name;
26 };
27
28 struct CameraData
29 {
30         Geometry::Angle<float> fov_y;
31         LinAl::Vector<float, 2> size;
32         float height;
33         float near_clip;
34         float far_clip;
35
36         bool is_orthographic() const { return fov_y==Geometry::Angle<float>::zero(); }
37         Geometry::Angle<float> get_fov_horizontal() const { return Geometry::atan(tan(fov_y/2.0f)*get_aspect())*2.0f; }
38         float get_aspect() const { return size.x/size.y; }
39 };
40
41 class Camera: public BufferedComponent<CameraData>
42 {
43 public:
44         using Setup = CameraSetup;
45
46 private:
47         const Setup &setup;
48
49 public:
50         Camera(Handle<Entity>, const Setup &);
51
52         void set_field_of_view(Geometry::Angle<float>, float);
53         void set_size(const LinAl::Vector<float, 2> &);
54         bool is_orthographic() const { return read().is_orthographic(); }
55         Geometry::Angle<float> get_fov_vertical() const { return read().fov_y; }
56         Geometry::Angle<float> get_fov_horizontal() const { return read().get_fov_horizontal(); }
57         const LinAl::Vector<float, 2> &get_size() const { return read().size; }
58         float get_aspect() const { return read().get_aspect(); }
59         float get_near_clip() const { return read().near_clip; }
60         float get_far_clip() const { return read().far_clip; }
61         CameraScaling get_scaling() const { return setup.scaling; }
62         const std::string &get_sequence_name() const { return setup.sequence_name; }
63 };
64
65 } // namespace Msp::Game
66
67 #endif