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