added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / n3dmath2 / n3dmath2_mat.hpp
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This file is part of the n3dmath2 library.
5
6 The n3dmath2 library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 The n3dmath2 library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with the n3dmath2 library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef _N3DMATH2_MAT_HPP_
22 #define _N3DMATH2_MAT_HPP_
23
24 #include "n3dmath2.hpp"
25
26 class Matrix3x3 {
27 private:
28         scalar_t m[3][3];
29 public:
30         
31         static Matrix3x3 identity_matrix;
32
33         Matrix3x3();
34         Matrix3x3(      scalar_t m11, scalar_t m12, scalar_t m13,
35                                 scalar_t m21, scalar_t m22, scalar_t m23,
36                                 scalar_t m31, scalar_t m32, scalar_t m33);
37         
38         Matrix3x3(const Matrix4x4 &mat4x4);
39         
40         // binary operations matrix (op) matrix
41         friend Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2);
42         friend Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2);
43         friend Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2);
44         
45         friend void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2);
46         friend void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2);
47         friend void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2);
48         
49         // binary operations matrix (op) scalar and scalar (op) matrix
50         friend Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar);
51         friend Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat);
52         
53         friend void operator *=(Matrix3x3 &mat, scalar_t scalar);
54         
55         inline scalar_t *operator [](int index);
56         inline const scalar_t *operator [](int index) const;
57         
58         inline void reset_identity();
59         
60         void translate(const Vector2 &trans);
61         void set_translation(const Vector2 &trans);
62         
63         void rotate(scalar_t angle);                                            // 2d rotation
64         void rotate(const Vector3 &euler_angles);                       // 3d rotation with euler angles
65         void rotate(const Vector3 &axis, scalar_t angle);       // 3d axis/angle rotation
66         void set_rotation(scalar_t angle);
67         void set_rotation(const Vector3 &euler_angles);
68         void set_rotation(const Vector3 &axis, scalar_t angle);
69         
70         void scale(const Vector3 &scale_vec);
71         void set_scaling(const Vector3 &scale_vec);
72         
73         void set_column_vector(const Vector3 &vec, unsigned int col_index);
74         void set_row_vector(const Vector3 &vec, unsigned int row_index);
75         Vector3 get_column_vector(unsigned int col_index) const;
76         Vector3 get_row_vector(unsigned int row_index) const;
77
78         void transpose();
79         Matrix3x3 transposed() const;   
80         scalar_t determinant() const;
81         Matrix3x3 inverse() const;
82         
83         friend std::ostream &operator <<(std::ostream &out, const Matrix3x3 &mat);
84 };
85
86
87 class Matrix4x4 {
88 private:
89         scalar_t m[4][4];
90         mutable float *glmatrix;
91 public:
92         
93         static Matrix4x4 identity_matrix;
94
95         Matrix4x4();
96         Matrix4x4(      scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
97                                 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
98                                 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
99                                 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44);
100         
101         Matrix4x4(const Matrix3x3 &mat3x3);
102         ~Matrix4x4();
103         
104         // binary operations matrix (op) matrix
105         friend Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2);
106         friend Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2);
107         friend Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2);
108         
109         friend void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2);
110         friend void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2);
111         friend void operator *=(Matrix4x4 &m1, const Matrix4x4 &m2);
112         
113         // binary operations matrix (op) scalar and scalar (op) matrix
114         friend Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar);
115         friend Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat);
116         
117         friend void operator *=(Matrix4x4 &mat, scalar_t scalar);
118         
119         inline scalar_t *operator [](int index);
120         inline const scalar_t *operator [](int index) const;
121         
122         inline void reset_identity();
123         
124         void translate(const Vector3 &trans);
125         void set_translation(const Vector3 &trans);
126         
127         void rotate(const Vector3 &euler_angles);                       // 3d rotation with euler angles
128         void rotate(const Vector3 &axis, scalar_t angle);       // 3d axis/angle rotation
129         void set_rotation(const Vector3 &euler_angles);
130         void set_rotation(const Vector3 &axis, scalar_t angle);
131         
132         void scale(const Vector4 &scale_vec);
133         void set_scaling(const Vector4 &scale_vec);
134         
135         void set_column_vector(const Vector4 &vec, unsigned int col_index);
136         void set_row_vector(const Vector4 &vec, unsigned int row_index);
137         Vector4 get_column_vector(unsigned int col_index) const;
138         Vector4 get_row_vector(unsigned int row_index) const;
139         
140         void transpose();
141         Matrix4x4 transposed() const;
142         scalar_t determinant() const;
143         Matrix4x4 adjoint() const;
144         Matrix4x4 inverse() const;
145         
146         const scalar_t *opengl_matrix() const;
147                 
148         friend std::ostream &operator <<(std::ostream &out, const Matrix4x4 &mat);
149 };
150
151 #include "n3dmath2_mat.inl"
152
153 #endif  // _N3DMATH2_MAT_HPP_