]> git.tdb.fi Git - libs/gl.git/blob - source/render/camera.h
Use default member initializers for simple types
[libs/gl.git] / source / render / camera.h
1 #ifndef MSP_GL_CAMERA_H_
2 #define MSP_GL_CAMERA_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include "placeable.h"
6 #include "programdata.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Camera: public Placeable
12 {
13 public:
14         class Loader: public DataFile::ObjectLoader<Camera>
15         {
16         public:
17                 Loader(Camera &);
18
19         private:
20                 void aspect_ratio(float);
21                 void depth_clip(float, float);
22                 void field_of_view(float);
23                 void look_at(float, float, float);
24                 void look_direction(float, float, float);
25                 void orthographic(float, float);
26                 void position(float, float, float);
27                 void up_direction(float, float, float);
28         };
29
30 private:
31         Geometry::Angle<float> fov = Geometry::Angle<float>::from_turns(0.125f);
32         float height = 0.0f;
33         float aspect = 4.0f/3.0f;
34         // Some compilers have "near" and "far" keywords
35         float clip_near = 0.1f;
36         float clip_far = 10.0f;
37         float frustum_x = 0.0f;
38         float frustum_y = 0.0f;
39         Geometry::Angle<float> rotate;
40         Vector3 position = { 0.0f, 0.0f, 0.0f };
41         Vector3 look_dir = { 0.0f, 0.0f, -1.0f };
42         Vector3 up_dir = { 0.0f, 1.0f, 0.0f };
43         Matrix view_matrix;
44         Matrix proj_matrix;
45         ProgramData shdata;
46
47 public:
48         Camera();
49
50         void set_field_of_view(const Geometry::Angle<float> &);
51         void set_orthographic(float, float);
52         void set_aspect_ratio(float);
53         void set_depth_clip(float, float);
54         void set_frustum_axis(float, float);
55         void set_frustum_rotation(const Geometry::Angle<float> &);
56         const Geometry::Angle<float> &get_field_of_view() const { return fov; }
57         bool is_orthographic() const { return fov==Geometry::Angle<float>::zero(); }
58         float get_orthographic_width() const { return height*aspect; }
59         float get_orthographic_height() const { return height; }
60         float get_aspect_ratio() const { return aspect; }
61         float get_near_clip() const { return clip_near; }
62         float get_far_clip() const { return clip_far; }
63         const Geometry::Angle<float> &get_frustum_rotation() const { return rotate; }
64
65         void set_position(const Vector3 &);
66         void set_look_direction(const Vector3 &);
67         void look_at(const Vector3 &);
68         void set_up_direction(const Vector3 &);
69         const Vector3 &get_position() const { return position; }
70         const Vector3 &get_look_direction() const { return look_dir; }
71         const Vector3 &get_up_direction() const { return up_dir; }
72
73         virtual void set_matrix(const Matrix &m) { set_object_matrix(m); }
74
75         /** Sets the position and orientation of the camera from an object matrix. */
76         void set_object_matrix(const Matrix &);
77
78         /** Returns the view matrix, used to transform coordinates from world space
79         to eye space. */
80         const Matrix &get_view_matrix() const { return view_matrix; }
81
82         /** Returns the object matrix, used to transform coordinates from eye space
83         to world space. */
84         const Matrix &get_object_matrix() const { return matrix; }
85
86         /** Returns the projection matrix. */
87         const Matrix &get_projection_matrix() const { return proj_matrix; }
88
89         Vector3 project(const Vector4 &) const;
90         Vector3 project(const Vector3 &) const;
91         Vector4 unproject(const Vector4 &) const;
92         Vector3 unproject(const Vector3 &) const;
93
94         const ProgramData &get_shader_data() const { return shdata; }
95
96 private:
97         void update_projection_matrix();
98         void update_object_matrix();
99
100 public:
101         void set_debug_name(const std::string &);
102 };
103
104 } // namespace GL
105 } // namespcae Msp
106
107 #endif