aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/math/matrix.h
blob: c92a293f942bb12308390a328487fd26cf7d3bc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef __JE_MATRIX_H__
#define __JE_MATRIX_H__

namespace JinEngine
{
	
	// Forward declarations.
	namespace Graphics
	{
		struct Vertex;
	}

	namespace Math
	{

		/// 
		/// This class is the basis for all transformations in LOVE. Althought not
		/// really needed for 2D, it contains 4x4 elements to be compatible with
		/// OpenGL without conversions.
		/// ҪתõOpenGL
		/// https://blog.csdn.net/candycat1992/article/details/8830894
		/// 
		class Matrix
		{
		private:

			/// 
			/// | e0 e4 e8  e12 |
			/// | e1 e5 e9  e13 |
			/// | e2 e6 e10 e14 |
			/// | e3 e7 e11 e15 |
			///
			float e[16];

		public:

			static const Matrix Identity;

			/// 
			/// Creates a new identity matrix.
			/// 
			Matrix();

			///
			/// Destructor.
			///
			~Matrix();

			void setOrtho(float _left, float _right, float _bottom, float _top, float _near, float _far);

			/// 
			/// Multiplies this Matrix with another Matrix, changing neither.
			/// @param m The Matrix to multiply with this Matrix.
			/// @return The combined matrix.
			/// 
			Matrix operator * (const Matrix & m) const;

			/// 
			/// Multiplies a Matrix into this Matrix.
			/// @param m The Matrix to combine into this Matrix.
			/// 
			void operator *= (const Matrix & m);

			/// 
			/// Gets a pointer to the 16 array elements.
			/// @return The array elements.
			/// 
			const float* getElements() const;

			/// 
			/// Resets this Matrix to the identity matrix.
			/// 
			void setIdentity();

			/// 
			/// Resets this Matrix to a translation.
			/// @param x Translation along x-axis.
			/// @param y Translation along y-axis.
			/// 
			void setTranslation(float x, float y);

			/// 
			/// Resets this Matrix to a rotation.
			/// @param r The angle in radians.
			/// 
			void setRotation(float r);

			/// 
			/// Resets this Matrix to a scale transformation.
			/// @param sx Scale factor along the x-axis.
			/// @param sy Scale factor along the y-axis.
			/// 
			void setScale(float sx, float sy);

			/// 
			/// Resets this Matrix to a shear transformation.
			/// @param kx Shear along x-axis.
			/// @param ky Shear along y-axis.
			/// 
			void setShear(float kx, float ky);

			/// 
			/// Creates a transformation with a certain position, orientation, scale
			/// and offset. Perfect for Drawables -- what a coincidence!
			///
			/// @param x The translation along the x-axis.
			/// @param y The translation along the y-axis.
			/// @param angle The rotation (rad) around the center with offset (ox,oy).
			/// @param sx Scale along x-axis.
			/// @param sy Scale along y-axis.
			/// @param ox The offset for rotation along the x-axis.
			/// @param oy The offset for rotation along the y-axis.
			/// @param kx Shear along x-axis
			/// @param ky Shear along y-axis
			/// 
			void setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy);

			/// 
			/// Multiplies this Matrix with a translation.
			/// @param x Translation along x-axis.
			/// @param y Translation along y-axis.
			/// 
			void translate(float x, float y);

			/// 
			/// Multiplies this Matrix with a rotation.
			/// @param r Angle in radians.
			/// 
			void rotate(float r);

			/// 
			/// Multiplies this Matrix with a scale transformation.
			/// @param sx Scale factor along the x-axis.
			/// @param sy Scale factor along the y-axis.
			/// 
			void scale(float sx, float sy);

			/// 
			/// Multiplies this Matrix with a shear transformation.
			/// @param kx Shear along the x-axis.
			/// @param ky Shear along the y-axis.
			/// 
			void shear(float kx, float ky);

			/// 
			/// Transforms an array of vertices by this Matrix. The sources and
			/// destination arrays may be the same.
			///
			/// @param dst Storage for the transformed vertices.
			/// @param src The source vertices.
			/// @param size The number of vertices.
			/// 
			void transform(Graphics::Vertex* dst, const Graphics::Vertex * src, int size) const;

		}; 

	} // namespace Math
} // namespace JinEngine

#endif // __JE_MATRIX_H__