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