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