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