]> git.tdb.fi Git - libs/gl.git/blob - source/render/camera.h
Store simpler states by value in PipelineState
[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         void copy_parameters(const Camera &);
65
66         /** Sets the camera projection to perspective, characterised by the vertical
67         field of view.  Horizontal FoV is computed with the aspect ratio. */
68         void set_field_of_view(const Geometry::Angle<float> &);
69
70         /** Sets the camera projection to orthogonal, characterized by the size of
71         the projection region. */
72         void set_orthographic(float, float);
73
74         void set_aspect_ratio(float);
75         void set_depth_clip(float, float);
76
77         /** Sets the direction of the frustum axis, which corresponds to the center
78         of the screen.  The offset is expressed in terms of the neutral frustum such
79         that -1 is the left or bottom edge and 1 is the right or top edge. */
80         void set_frustum_axis(float, float);
81
82         /** Apply a rotation to the view frustum after projection.  This can be used
83         with rotated displayes without affecting the camera's orientation. */
84         void set_frustum_rotation(const Geometry::Angle<float> &);
85
86         const Geometry::Angle<float> &get_field_of_view() const { return fov; }
87         bool is_orthographic() const { return fov==Geometry::Angle<float>::zero(); }
88         float get_orthographic_width() const { return height*aspect; }
89         float get_orthographic_height() const { return height; }
90         float get_aspect_ratio() const { return aspect; }
91         float get_near_clip() const { return clip_near; }
92         float get_far_clip() const { return clip_far; }
93         const Geometry::Angle<float> &get_frustum_rotation() const { return rotate; }
94
95         void set_position(const Vector3 &);
96         void set_look_direction(const Vector3 &);
97         void look_at(const Vector3 &);
98         void set_up_direction(const Vector3 &);
99         const Vector3 &get_position() const { return position; }
100         const Vector3 &get_look_direction() const { return look_dir; }
101         const Vector3 &get_up_direction() const { return up_dir; }
102
103         virtual void set_matrix(const Matrix &m) { set_object_matrix(m); }
104
105         /** Sets the position and orientation of the camera from an object matrix. */
106         void set_object_matrix(const Matrix &);
107
108         /** Returns the view matrix, used to transform coordinates from world space
109         to eye space. */
110         const Matrix &get_view_matrix() const { return view_matrix; }
111
112         /** Returns the object matrix, used to transform coordinates from eye space
113         to world space. */
114         const Matrix &get_object_matrix() const { return matrix; }
115
116         /** Returns the projection matrix. */
117         const Matrix &get_projection_matrix() const { return proj_matrix; }
118
119         Vector3 project(const Vector4 &) const;
120         Vector3 project(const Vector3 &) const;
121         Vector4 unproject(const Vector4 &) const;
122         Vector3 unproject(const Vector3 &) const;
123
124         /** Returns a ProgramData object containing the camera matrices. */
125         const ProgramData &get_shader_data() const { return shdata; }
126
127 private:
128         void update_projection_matrix();
129         void update_object_matrix();
130
131 public:
132         void set_debug_name(const std::string &);
133 };
134
135 } // namespace GL
136 } // namespcae Msp
137
138 #endif