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