]> git.tdb.fi Git - libs/gl.git/blob - source/matrix.h
Drop Id tags and copyright notices from files
[libs/gl.git] / source / matrix.h
1 #ifndef MSP_GL_MATRIX_H_
2 #define MSP_GL_MATRIX_H_
3
4 #include <list>
5 #include "gl.h"
6 #include "vector.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Matrix
12 {
13 private:
14         enum Flags
15         {
16                 IDENTITY  = 0,
17                 TRANSLATE = 1,
18                 ROTATE    = 2,
19                 SCALE     = 4,
20                 ARBITARY  = 8
21         };
22
23         double matrix[16];
24         unsigned flags;
25
26 public:
27         Matrix();
28         Matrix(const float *);
29         Matrix(const double *);
30 private:
31         void check_flags();
32
33 public:
34         const double *data() const { return matrix; }
35
36         void multiply(const Matrix &);
37         void translate(double, double, double);
38         void translate(const Vector3 &t) { translate(t.x, t.y, t.z); }
39         void rotate(double, double, double, double);
40         void rotate(double a, const Vector3 &x) { rotate(a, x.x, x.y, x.z); }
41         void rotate_deg(double, double, double, double);
42         void rotate_deg(double a, const Vector3 & x) { rotate_deg(a, x.x, x.y, x.z); }
43         void scale(double);
44         void scale(double, double, double);
45
46         Matrix operator*(const Matrix &) const;
47         Matrix &operator*=(const Matrix &);
48         Vector4 operator*(const Vector4 &) const;
49         double operator[](unsigned) const;
50
51         static Matrix translation(double, double, double);
52         static Matrix translation(const Vector3 &t) { return translation(t.x, t.y, t.z); }
53         static Matrix rotation(double, double, double, double);
54         static Matrix rotation(double a, const Vector3 &x) { return rotation(a, x.x, x.y, x.z); }
55         static Matrix rotation_deg(double, double, double, double);
56         static Matrix rotation_deg(double a, const Vector3 &x) { return rotation_deg(a, x.x, x.y, x.z); }
57         static Matrix scaling(double);
58         static Matrix scaling(double, double, double);
59
60         static Matrix ortho(double, double, double, double, double, double);
61         static Matrix ortho_centered(double, double);
62         static Matrix ortho_bottomleft(double, double);
63         static Matrix ortho_topleft(double, double);
64         static Matrix frustum(double, double, double, double, double, double);
65         static Matrix frustum_centered(double, double, double, double);
66         static Matrix perspective(double, double, double, double);
67 };
68
69 class MatrixStack
70 {
71 public:
72         class Push
73         {
74         private:
75                 MatrixStack &stack;
76
77         public:
78                 Push(MatrixStack &s): stack(s) { stack.push(); }
79                 ~Push() { stack.pop(); }
80         };
81
82 private:
83         GLenum mode;
84         std::list<Matrix> matrices;
85
86         static GLenum current_mode;
87
88         MatrixStack(const MatrixStack &);
89         MatrixStack &operator=(const MatrixStack &);
90         MatrixStack(GLenum);
91 public:
92         MatrixStack();
93
94         const Matrix &top() const;
95         void load(const Matrix &);
96         void multiply(const Matrix &);
97         void push();
98         void pop();
99 private:
100         virtual void update();
101
102 public:
103         MatrixStack &operator=(const Matrix &);
104         MatrixStack &operator*=(const Matrix &);
105
106         static MatrixStack &modelview();
107         static MatrixStack &projection();
108 };
109
110
111 /* The stuff below is deprecated and preserved (for now) only for compatibility
112 with existing applications */
113
114 enum MatrixMode
115 {
116         MODELVIEW = GL_MODELVIEW,
117         PROJECTION = GL_PROJECTION,
118         TEXTURE = GL_TEXTURE
119 };
120
121 void matrix_mode(MatrixMode);
122 void load_identity();
123 void load_matrix(const float *);
124 void load_matrix(const double *);
125 void mult_matrix(const float *);
126 void mult_matrix(const double *);
127 void push_matrix();
128 void pop_matrix();
129
130 /// RAII object - pushes matrix when constructed and pops when destroyed
131 struct PushMatrix
132 {
133         PushMatrix() { push_matrix(); }
134         ~PushMatrix() { pop_matrix(); }
135 };
136
137 void translate(float, float, float);
138 void rotate(float, float, float, float);
139 void scale(float, float, float);
140 void scale_uniform(float);
141
142 } // namespace GL
143 } // namespace Msp
144
145 #endif