]> git.tdb.fi Git - libs/gl.git/blob - source/matrix.h
Use libmspmath to provide vector and matrix operations
[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 &, const Vector3 &);
31         void rotate(double a, double x, double y, double z) { rotate(Angle::from_radians(a), Vector3(x, y, z)); }
32         void rotate(double a, const Vector3 &x) { rotate(Angle::from_radians(a), x); }
33         void rotate_deg(double a, double x, double y, double z) { rotate(Angle::from_degrees(a), Vector3(x, y, z)); }
34         void rotate_deg(double a, const Vector3 & x) { rotate(Angle::from_degrees(a), x); }
35         void scale(double s) { scale(Vector3(s, s, s)); }
36         void scale(double x, double y, double z) { scale(Vector3(x, y, z)); }
37         void scale(const Vector3 &);
38
39         Matrix operator*(const Matrix &) const;
40         Matrix &operator*=(const Matrix &);
41         Vector4 operator*(const Vector4 &) const;
42         Vector3 operator*(const Vector3 &) const;
43         double operator[](unsigned) const;
44
45         static Matrix translation(double x, double y, double z) { return translation(Vector3(x, y, z)); }
46         static Matrix translation(const Vector3 &);
47         static Matrix rotation(const Angle &a, const Vector3 &);
48         static Matrix rotation(double a, double x, double y, double z) { return rotation(Angle::from_radians(a), Vector3(x, y, z)); }
49         static Matrix rotation(double a, const Vector3 &x) { return rotation(Angle::from_radians(a), x); }
50         static Matrix rotation_deg(double a, double x, double y, double z) { return rotation(Angle::from_degrees(a), Vector3(x, y, z)); }
51         static Matrix rotation_deg(double a, const Vector3 &x) { return rotation(Angle::from_degrees(a), x); }
52         static Matrix scaling(double s) { return scaling(Vector3(s, s, s)); }
53         static Matrix scaling(double x, double y, double z) { return scaling(Vector3(x, y, z)); }
54         static Matrix scaling(const Vector3 &);
55
56         static Matrix ortho(double, double, double, double, double, double);
57         static Matrix ortho_centered(double, double);
58         static Matrix ortho_bottomleft(double, double);
59         static Matrix ortho_topleft(double, double);
60         static Matrix frustum(double, double, double, double, double, double);
61         static Matrix frustum_centered(double, double, double, double);
62         static Matrix perspective(double, double, double, double);
63 };
64
65 class MatrixStack
66 {
67 public:
68         class Push
69         {
70         private:
71                 MatrixStack &stack;
72
73         public:
74                 Push(MatrixStack &s): stack(s) { stack.push(); }
75                 ~Push() { stack.pop(); }
76         };
77
78 private:
79         GLenum mode;
80         std::vector<Matrix> matrices;
81
82         static GLenum current_mode;
83
84         MatrixStack(const MatrixStack &);
85         MatrixStack &operator=(const MatrixStack &);
86         MatrixStack(GLenum);
87 public:
88         MatrixStack();
89
90         const Matrix &top() const;
91         void load(const Matrix &);
92         void multiply(const Matrix &);
93         void push();
94         void pop();
95 private:
96         virtual void update();
97
98 public:
99         MatrixStack &operator=(const Matrix &);
100         MatrixStack &operator*=(const Matrix &);
101
102         static MatrixStack &modelview();
103         static MatrixStack &projection();
104 };
105
106 } // namespace GL
107 } // namespace Msp
108
109 #endif