]> git.tdb.fi Git - libs/gl.git/blob - source/render/camera.h
Check the flat qualifier from the correct member
[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 "camera_backend.h"
6 #include "placeable.h"
7 #include "programdata.h"
8
9 namespace Msp {
10 namespace GL {
11
12 /**
13 Represents a point of view in 3D space.
14
15 A Camera provides two matrices.  The view matrix is the inverse of the camera's
16 model matrix and transforms coordinates from world space to eye space (the
17 camera's object space).  The projection matrix transforms coordinates from eye
18 space to clip space. 
19
20 Orientation of the Camera is determined by look direction and up direction.
21 Look direction corresponds to the negative Z axis direction in eye space.  The
22 YZ plane of eye space is aligned to the plane formed by the look and up
23 directions.  Setting the up direction to the opposite of gravity direction is
24 an easy way to keep the camera upright.
25 */
26 class Camera: public CameraBackend, public Placeable
27 {
28 public:
29         class Loader: public DataFile::ObjectLoader<Camera>
30         {
31         public:
32                 Loader(Camera &);
33
34         private:
35                 void aspect_ratio(float);
36                 void depth_clip(float, float);
37                 void field_of_view(float);
38                 void look_at(float, float, float);
39                 void look_direction(float, float, float);
40                 void orthographic(float, float);
41                 void position(float, float, float);
42                 void up_direction(float, float, float);
43         };
44
45 private:
46         Geometry::Angle<float> fov = Geometry::Angle<float>::from_turns(0.125f);
47         float height = 0.0f;
48         float aspect = 4.0f/3.0f;
49         // Some compilers have "near" and "far" keywords
50         float clip_near = 0.1f;
51         float clip_far = 10.0f;
52         float frustum_x = 0.0f;
53         float frustum_y = 0.0f;
54         Geometry::Angle<float> rotate;
55         Vector3 position = { 0.0f, 0.0f, 0.0f };
56         Vector3 look_dir = { 0.0f, 0.0f, -1.0f };
57         Vector3 up_dir = { 0.0f, 1.0f, 0.0f };
58         Matrix view_matrix;
59         Matrix proj_matrix;
60         ProgramData shdata;
61
62 public:
63         Camera();
64
65         void copy_parameters(const Camera &);
66
67         /** Sets the camera projection to perspective, characterised by the vertical
68         field of view.  Horizontal FoV is computed with the aspect ratio. */
69         void set_field_of_view(const Geometry::Angle<float> &);
70
71         /** Sets the camera projection to orthogonal, characterized by the size of
72         the projection region. */
73         void set_orthographic(float, float);
74
75         void set_aspect_ratio(float);
76         void set_depth_clip(float, float);
77
78         /** Sets the direction of the frustum axis, which corresponds to the center
79         of the screen.  The offset is expressed in terms of the neutral frustum such
80         that -1 is the left or bottom edge and 1 is the right or top edge. */
81         void set_frustum_axis(float, float);
82
83         /** Apply a rotation to the view frustum after projection.  This can be used
84         with rotated displayes without affecting the camera's orientation. */
85         void set_frustum_rotation(const Geometry::Angle<float> &);
86
87         const Geometry::Angle<float> &get_field_of_view() const { return fov; }
88         bool is_orthographic() const { return fov==Geometry::Angle<float>::zero(); }
89         float get_orthographic_width() const { return height*aspect; }
90         float get_orthographic_height() const { return height; }
91         float get_aspect_ratio() const { return aspect; }
92         float get_near_clip() const { return clip_near; }
93         float get_far_clip() const { return clip_far; }
94         const Geometry::Angle<float> &get_frustum_rotation() const { return rotate; }
95
96         void set_position(const Vector3 &);
97         void set_look_direction(const Vector3 &);
98         void look_at(const Vector3 &);
99         void set_up_direction(const Vector3 &);
100         const Vector3 &get_position() const { return position; }
101         const Vector3 &get_look_direction() const { return look_dir; }
102         const Vector3 &get_up_direction() const { return up_dir; }
103
104         virtual void set_matrix(const Matrix &m) { set_object_matrix(m); }
105
106         /** Sets the position and orientation of the camera from an object matrix. */
107         void set_object_matrix(const Matrix &);
108
109         /** Returns the view matrix, used to transform coordinates from world space
110         to eye space. */
111         const Matrix &get_view_matrix() const { return view_matrix; }
112
113         /** Returns the object matrix, used to transform coordinates from eye space
114         to world space. */
115         const Matrix &get_object_matrix() const { return matrix; }
116
117         /** Returns the projection matrix. */
118         const Matrix &get_projection_matrix() const { return proj_matrix; }
119
120         Vector3 project(const Vector4 &) const;
121         Vector3 project(const Vector3 &) const;
122         Vector4 unproject(const Vector4 &) const;
123         Vector3 unproject(const Vector3 &) const;
124
125         /** Returns a ProgramData object containing the camera matrices. */
126         const ProgramData &get_shader_data() const { return shdata; }
127
128 private:
129         void update_projection_matrix();
130         void update_object_matrix();
131
132 public:
133         void set_debug_name(const std::string &);
134 };
135
136 } // namespace GL
137 } // namespcae Msp
138
139 #endif