summaryrefslogtreecommitdiff
path: root/source/LoG/math/matrix44.cpp
blob: 4514feb07ddffc0227c4d6ee814998f49ff25286 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <memory>

#include "matrix44.h"

const Matrix44 Matrix44::Identity;

// | e0 e4 e8  e12 |
// | e1 e5 e9  e13 |
// | e2 e6 e10 e14 |
// | e3 e7 e11 e15 |

Matrix44::Matrix44()
{
	SetIdentity();
}

Matrix44::Matrix44(const Matrix44& m)
{
	memcpy(&e, &m.e, 16 * sizeof(float));
}

Matrix44::~Matrix44()
{
}

void Matrix44::operator = (const Matrix44& m)
{
	memcpy(&e, &m.e, 16 * sizeof(float));
}

void Matrix44::SetOrtho(float l, float r, float b, float t, float n, float f)
{
	SetIdentity();
	float w = r - l;
	float h = t - b;
	float z = f - n;
	e[0] = 2 / w;
	e[5] = 2 / h;
	e[10] = -2 / z;
	e[12] = -(r + l) / w;
	e[13] = -(t + b) / h;
	e[14] = -(f + n) / z;
	e[15] = 1;
}

//                 | e0 e4 e8  e12 |
//                 | e1 e5 e9  e13 |
//                 | e2 e6 e10 e14 |
//                 | e3 e7 e11 e15 |
// | e0 e4 e8  e12 |
// | e1 e5 e9  e13 |
// | e2 e6 e10 e14 |
// | e3 e7 e11 e15 |

Matrix44 Matrix44::operator * (const Matrix44 & m) const
{
	Matrix44 t;

	t.e[0] = (e[0] * m.e[0]) + (e[4] * m.e[1]) + (e[8] * m.e[2]) + (e[12] * m.e[3]);
	t.e[4] = (e[0] * m.e[4]) + (e[4] * m.e[5]) + (e[8] * m.e[6]) + (e[12] * m.e[7]);
	t.e[8] = (e[0] * m.e[8]) + (e[4] * m.e[9]) + (e[8] * m.e[10]) + (e[12] * m.e[11]);
	t.e[12] = (e[0] * m.e[12]) + (e[4] * m.e[13]) + (e[8] * m.e[14]) + (e[12] * m.e[15]);

	t.e[1] = (e[1] * m.e[0]) + (e[5] * m.e[1]) + (e[9] * m.e[2]) + (e[13] * m.e[3]);
	t.e[5] = (e[1] * m.e[4]) + (e[5] * m.e[5]) + (e[9] * m.e[6]) + (e[13] * m.e[7]);
	t.e[9] = (e[1] * m.e[8]) + (e[5] * m.e[9]) + (e[9] * m.e[10]) + (e[13] * m.e[11]);
	t.e[13] = (e[1] * m.e[12]) + (e[5] * m.e[13]) + (e[9] * m.e[14]) + (e[13] * m.e[15]);

	t.e[2] = (e[2] * m.e[0]) + (e[6] * m.e[1]) + (e[10] * m.e[2]) + (e[14] * m.e[3]);
	t.e[6] = (e[2] * m.e[4]) + (e[6] * m.e[5]) + (e[10] * m.e[6]) + (e[14] * m.e[7]);
	t.e[10] = (e[2] * m.e[8]) + (e[6] * m.e[9]) + (e[10] * m.e[10]) + (e[14] * m.e[11]);
	t.e[14] = (e[2] * m.e[12]) + (e[6] * m.e[13]) + (e[10] * m.e[14]) + (e[14] * m.e[15]);

	t.e[3] = (e[3] * m.e[0]) + (e[7] * m.e[1]) + (e[11] * m.e[2]) + (e[15] * m.e[3]);
	t.e[7] = (e[3] * m.e[4]) + (e[7] * m.e[5]) + (e[11] * m.e[6]) + (e[15] * m.e[7]);
	t.e[11] = (e[3] * m.e[8]) + (e[7] * m.e[9]) + (e[11] * m.e[10]) + (e[15] * m.e[11]);
	t.e[15] = (e[3] * m.e[12]) + (e[7] * m.e[13]) + (e[11] * m.e[14]) + (e[15] * m.e[15]);

	return t;
}

void Matrix44::operator *= (const Matrix44 & m)
{
	Matrix44 t = (*this) * m;
	memcpy((void*)this->e, (void*)t.e, sizeof(float) * 16);
}

const float * Matrix44::GetElements() const
{
	return e;
}

void Matrix44::SetIdentity()
{
	memset(e, 0, sizeof(float) * 16);
	e[0] = e[5] = e[10] = e[15] = 1;
}

void Matrix44::SetTranslation(float x, float y)
{
	SetIdentity();
	e[12] = x;
	e[13] = y;
}

void Matrix44::SetRotation(float rad)
{
	SetIdentity();
	float c = cos(rad), s = sin(rad);
	e[0] = c; e[4] = -s;
	e[1] = s; e[5] = c;
}

void Matrix44::SetScale(float sx, float sy)
{
	SetIdentity();
	e[0] = sx;
	e[5] = sy;
}

void Matrix44::SetShear(float kx, float ky)
{
	SetIdentity();
	e[1] = ky;
	e[4] = kx;
}

void Matrix44::SetTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy)
{
	memset(e, 0, sizeof(float) * 16); // zero out matrix
	float c = cos(angle), s = sin(angle);
	// matrix multiplication carried out on paper:
	// |1     x| |c -s    | |sx       | |1     -ox|
	// |  1   y| |s  c    | |   sy    | |  1   -oy|
	// |    1  | |     1  | |      1  | |    1    |
	// |      1| |       1| |        1| |       1 |
	//   move      rotate      scale      origin
	e[10] = e[15] = 1.0f;
	e[0] = c * sx; // = a
	e[1] = s * sx; // = b
	e[4] = -s * sy; // = c
	e[5] = c * sy; // = d
	e[12] = x - ox * e[0] - oy * e[4];
	e[13] = y - ox * e[1] - oy * e[5];
}

void Matrix44::Translate(float x, float y)
{
	Matrix44 t;
	t.SetTranslation(x, y);
	this->operator *=(t);
}

void Matrix44::Rotate(float rad)
{
	Matrix44 t;
	t.SetRotation(rad);
	this->operator *=(t);
}

void Matrix44::Scale(float sx, float sy)
{
	Matrix44 t;
	t.SetScale(sx, sy);
	this->operator *=(t);
}

void Matrix44::Shear(float kx, float ky)
{
	Matrix44 t;
	t.SetShear(kx, ky);
	this->operator *=(t);
}

void Matrix44::Transform(float x, float y, float angle, float sx, float sy, float ox, float oy)
{
	Matrix44 t;
	t.SetTransformation(x, y, angle, sx, sy, ox, oy);
	this->operator *=(t);
}

void Matrix44::Ortho(float left, float right, float bottom, float top, float near, float far)
{
	Matrix44 t;
	t.SetOrtho(left, right, bottom, top, near, far);
	this->operator *=(t);
}

//                 | x |
//                 | y |
//                 | 0 |
//                 | 1 |
// | e0 e4 e8  e12 |
// | e1 e5 e9  e13 |
// | e2 e6 e10 e14 |
// | e3 e7 e11 e15 |

//void Matrix44::transform(Graphics::Vertex* dst, const Graphics::Vertex* src, int size) const
//{
//	for (int i = 0; i<size; ++i)
//	{
//		// Store in temp variables in case src = dst
//		float x = (e[0] * src[i].xy.x()) + (e[4] * src[i].xy.y()) + (0) + (e[12]);
//		float y = (e[1] * src[i].xy.x()) + (e[5] * src[i].xy.y()) + (0) + (e[13]);

//		dst[i].xy.Set(x, y);
//	}
//}

void Matrix44::Frustum(float l, float r, float t, float b, float near, float far)
{


}

void Matrix44::Perspective(float fov, float aspect, float near, float far)
{

}