]> git.tdb.fi Git - libs/gl.git/blob - source/matrix.h
ce21b46f1192aab039ddbdd53b391a3e4e21b43c
[libs/gl.git] / source / matrix.h
1 #ifndef MSP_GL_MATRIX_H_
2 #define MSP_GL_MATRIX_H_
3
4 #include <vector>
5 #include <msp/geometry/angle.h>
6 #include <msp/linal/squarematrix.h>
7 #include "gl.h"
8 #include "vector.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class Matrix: public LinAl::SquareMatrix<double, 4>
14 {
15 private:
16         typedef LinAl::SquareMatrix<double, 4> Base;
17         typedef Geometry::Angle<double> Angle;
18
19 public:
20         Matrix();
21         Matrix(const float *);
22         Matrix(const double *);
23         Matrix(const LinAl::Matrix<double, 4, 4> &);
24
25         const double *data() const { return &Base::operator()(0, 0); }
26
27         void multiply(const Matrix &);
28         void translate(double x, double y, double z) { translate(Vector3(x, y, z)); }
29         void translate(const Vector3 &);
30         void rotate(const Angle &a, double x, double y, double z) { rotate(a, Vector3(x, y, z)); }
31         void rotate(const Angle &, const Vector3 &);
32         void rotate(double a, double x, double y, double z) { rotate(Angle::from_radians(a), Vector3(x, y, z)); }
33         void rotate(double a, const Vector3 &x) { rotate(Angle::from_radians(a), x); }
34         void rotate_deg(double a, double x, double y, double z) { rotate(Angle::from_degrees(a), Vector3(x, y, z)); }
35         void rotate_deg(double a, const Vector3 & x) { rotate(Angle::from_degrees(a), x); }
36         void scale(double s) { scale(Vector3(s, s, s)); }
37         void scale(double x, double y, double z) { scale(Vector3(x, y, z)); }
38         void scale(const Vector3 &);
39
40         Matrix operator*(const Matrix &) const;
41         Matrix &operator*=(const Matrix &);
42         Vector4 operator*(const Vector4 &) const;
43         Vector3 operator*(const Vector3 &) const;
44         double operator[](unsigned) const;
45
46         static Matrix translation(double x, double y, double z) { return translation(Vector3(x, y, z)); }
47         static Matrix translation(const Vector3 &);
48         static Matrix rotation(const Angle &a, double x, double y, double z) { return rotation(a, Vector3(x, y, z)); }
49         static Matrix rotation(const Angle &, const Vector3 &);
50         static Matrix rotation(double a, double x, double y, double z) { return rotation(Angle::from_radians(a), Vector3(x, y, z)); }
51         static Matrix rotation(double a, const Vector3 &x) { return rotation(Angle::from_radians(a), x); }
52         static Matrix rotation_deg(double a, double x, double y, double z) { return rotation(Angle::from_degrees(a), Vector3(x, y, z)); }
53         static Matrix rotation_deg(double a, const Vector3 &x) { return rotation(Angle::from_degrees(a), x); }
54         static Matrix scaling(double s) { return scaling(Vector3(s, s, s)); }
55         static Matrix scaling(double x, double y, double z) { return scaling(Vector3(x, y, z)); }
56         static Matrix scaling(const Vector3 &);
57
58         static Matrix ortho(double, double, double, double, double, double);
59         static Matrix ortho_centered(double, double);
60         static Matrix ortho_bottomleft(double, double);
61         static Matrix ortho_topleft(double, double);
62         static Matrix frustum(double, double, double, double, double, double);
63         static Matrix frustum_centered(double, double, double, double);
64         static Matrix perspective(const Angle &, double, double, double);
65 };
66
67 class MatrixStack
68 {
69 public:
70         class Push
71         {
72         private:
73                 MatrixStack &stack;
74
75         public:
76                 Push(MatrixStack &s): stack(s) { stack.push(); }
77                 ~Push() { stack.pop(); }
78         };
79
80 private:
81         GLenum mode;
82         std::vector<Matrix> matrices;
83
84         static GLenum current_mode;
85
86         MatrixStack(const MatrixStack &);
87         MatrixStack &operator=(const MatrixStack &);
88         MatrixStack(GLenum);
89 public:
90         MatrixStack();
91
92         const Matrix &top() const;
93         void load(const Matrix &);
94         void multiply(const Matrix &);
95         void push();
96         void pop();
97 private:
98         virtual void update();
99
100 public:
101         MatrixStack &operator=(const Matrix &);
102         MatrixStack &operator*=(const Matrix &);
103
104         static MatrixStack &modelview();
105         static MatrixStack &projection();
106 };
107
108 } // namespace GL
109 } // namespace Msp
110
111 #endif