Logo
Matrix4.h
1#pragma once
2
3#include "Types.h"
4#include "math/Matrix4.h"
5#include "math/Math.h"
6#include "Vector3.h"
7#include "Vector2.h"
8#include "Vector4.h"
9
10namespace SharpKmyMath{
11
12 public value class Matrix4{
13 public:
14
15 float m00, m01, m02, m03;
16 float m10, m11, m12, m13;
17 float m20, m21, m22, m23;
18 float m30, m31, m32, m33;
19
20 Matrix4(float _m00, float _m01, float _m02, float _m03,
21 float _m10, float _m11, float _m12, float _m13,
22 float _m20, float _m21, float _m22, float _m23,
23 float _m30, float _m31, float _m32, float _m33)
24 {
25 m00 = _m00;
26 m01 = _m01;
27 m02 = _m02;
28 m03 = _m03;
29
30 m10 = _m10;
31 m11 = _m11;
32 m12 = _m12;
33 m13 = _m13;
34
35 m20 = _m20;
36 m21 = _m21;
37 m22 = _m22;
38 m23 = _m23;
39
40 m30 = _m30;
41 m31 = _m31;
42 m32 = _m32;
43 m33 = _m33;
44 }
45
46 static Matrix4 perspectiveFOV(float fov, float asp, float znear, float zfar)
47 {
48 auto n = kmyMath::Matrix4::perspectiveFOV(fov * 2, asp, znear, zfar);
49 return fromNativeMatrix4(n);
50 }
51
52 static Matrix4 ortho(float left, float right, float top, float bottom, float znear, float zfar)
53 {
54 auto n = kmyMath::Matrix4::ortho(left, right, top, bottom, znear, zfar);
55 return fromNativeMatrix4(n);
56 }
57
58 static Matrix4 translate(float x, float y, float z)
59 {
60 return Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1);
61 }
62
63 static Matrix4 scale(float x, float y, float z)
64 {
65 return Matrix4(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
66 }
67
68 static Matrix4 rotateX(float r)
69 {
70 Matrix4 retval;
71 retval = identity();
72 retval.m11 = cos(r);
73 retval.m12 = sin(r);
74 retval.m21 = -sin(r);
75 retval.m22 = cos(r);
76 return retval;
77 }
78
79 static Matrix4 rotateY(float r)
80 {
81 Matrix4 retval;
82 retval = identity();
83 retval.m00 = cos(r);
84 retval.m02 = -sin(r);
85 retval.m20 = sin(r);
86 retval.m22 = cos(r);
87 return retval;
88 }
89
90 static Matrix4 rotateZ(float r)
91 {
92 Matrix4 retval;
93 retval = identity();
94 retval.m00 = cos(r);
95 retval.m01 = sin(r);
96 retval.m10 = -sin(r);
97 retval.m11 = cos(r);
98 return retval;
99 }
100
102 {
103 return Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
104 }
105
107 {
108 kmyMath::Matrix4 nm = m.toNativeMatrix4();
109 nm = kmyMath::Matrix4::inverse(nm);
110 Matrix4 ret = fromNativeMatrix4(nm);
111 return ret;
112 }
113
115 {
116 Matrix4 ret;
117
118 kmyMath::Matrix4 nm1 = m1.toNativeMatrix4();
119 kmyMath::Matrix4 nm2 = m2.toNativeMatrix4();
120
121 kmyMath::Matrix4 res = nm1 * nm2;
122
123 return fromNativeMatrix4(res);
124 }
125
126 static Matrix4 lookat( Vector3 eye, Vector3 target, Vector3 upvec )
127 {
128 Matrix4 ret;
129
130 Vector3 zaxis, xaxis, yaxis;
131
132 zaxis = Vector3::normalize(target - eye);
133 xaxis = Vector3::normalize(Vector3::crossProduct(zaxis, upvec));
134 yaxis = Vector3::crossProduct(xaxis, zaxis);
135
136 ret.m00 = xaxis.x;
137 ret.m01 = xaxis.y;
138 ret.m02 = xaxis.z;
139 ret.m03 = 0;
140
141 ret.m10 = yaxis.x;
142 ret.m11 = yaxis.y;
143 ret.m12 = yaxis.z;
144 ret.m13 = 0;
145
146 ret.m20 = -zaxis.x;
147 ret.m21 = -zaxis.y;
148 ret.m22 = -zaxis.z;
149 ret.m23 = 0;
150
151 ret.m30 = eye.x;
152 ret.m31 = eye.y;
153 ret.m32 = eye.z;
154 ret.m33 = 1;
155
156 return ret;
157 }
158
160 {
161 Vector4 result;
162 result.x = m.m00 * v.x + m.m10 * v.y + m.m20 * v.z + m.m30;
163 result.y = m.m01 * v.x + m.m11 * v.y + m.m21 * v.z + m.m31;
164 result.z = m.m02 * v.x + m.m12 * v.y + m.m22 * v.z + m.m32;
165 result.w = m.m03 * v.x + m.m13 * v.y + m.m23 * v.z + m.m33;
166 return result;
167 }
168
170 {
171 Vector4 result;
172 result.x = m.m00 * v.x + m.m10 * v.y + m.m20 * v.z + m.m30 * v.w;
173 result.y = m.m01 * v.x + m.m11 * v.y + m.m21 * v.z + m.m31 * v.w;
174 result.z = m.m02 * v.x + m.m12 * v.y + m.m22 * v.z + m.m32 * v.w;
175 result.w = m.m03 * v.x + m.m13 * v.y + m.m23 * v.z + m.m33 * v.w;
176 return result;
177 }
178
179 kmyMath::Matrix4 toNativeMatrix4()
180 {
181 kmyMath::Matrix4 m;
182 m.m00 = m00;
183 m.m01 = m01;
184 m.m02 = m02;
185 m.m03 = m03;
186
187 m.m10 = m10;
188 m.m11 = m11;
189 m.m12 = m12;
190 m.m13 = m13;
191
192 m.m20 = m20;
193 m.m21 = m21;
194 m.m22 = m22;
195 m.m23 = m23;
196
197 m.m30 = m30;
198 m.m31 = m31;
199 m.m32 = m32;
200 m.m33 = m33;
201 return m;
202 }
203
204 static Matrix4 fromNativeMatrix4(const kmyMath::Matrix4 &m)
205 {
206 Matrix4 ret;
207 ret.m00 = m.m00;
208 ret.m01 = m.m01;
209 ret.m02 = m.m02;
210 ret.m03 = m.m03;
211
212 ret.m10 = m.m10;
213 ret.m11 = m.m11;
214 ret.m12 = m.m12;
215 ret.m13 = m.m13;
216
217 ret.m20 = m.m20;
218 ret.m21 = m.m21;
219 ret.m22 = m.m22;
220 ret.m23 = m.m23;
221
222 ret.m30 = m.m30;
223 ret.m31 = m.m31;
224 ret.m32 = m.m32;
225 ret.m33 = m.m33;
226 return ret;
227 }
228
230 {
231 return Vector3(m30, m31, m32);
232 }
233
235 {
236 return Vector3(
237 sqrt(m00 * m00 + m01 * m01 + m02 * m02),
238 sqrt(m10 * m10 + m11 * m11 + m12 * m12),
239 sqrt(m20 * m20 + m21 * m21 + m22 * m22)
240 );
241 }
242
244 {
245 auto x = sqrt(m00 * m00 + m01 * m01 + m02 * m02);
246 auto y = sqrt(m10 * m10 + m11 * m11 + m12 * m12);
247 auto z = sqrt(m20 * m20 + m21 * m21 + m22 * m22);
248
249 //サイズが0なら無理
250 if (x == 0.0f || y == 0.0f || z == 0.0f) {
251 PRINTF("Matrix4 scale reset error");
252 return identity();
253 }
254
255 Matrix4 r = Matrix4(*this);
256 r.m00 /= x;
257 r.m01 /= x;
258 r.m02 /= x;
259
260 r.m10 /= y;
261 r.m11 /= y;
262 r.m12 /= y;
263
264 r.m20 /= z;
265 r.m21 /= z;
266 r.m22 /= z;
267
268 return r;
269 }
270
272 {
273 Matrix4 r = Matrix4(*this);
274 r.m30 = r.m31 = r.m32 = 0.0f;
275 return r;
276 }
277
279 {
280 Matrix4 r = Matrix4(*this);
281 kmyMath::Matrix4 nm = r.toNativeMatrix4();
282 kmyMath::Vector3 v = nm.getEularZXY();
283 return Vector3(v.x, v.y, v.z);
284 }
285
287 {
288 kmyMath::Matrix4 nm = toNativeMatrix4();
289 auto v = nm.getScale();
290 return Vector3(v.x, v.y, v.z);
291 }
292
294 {
295 return Vector3(m20, m21, m22);
296 }
297
299 {
300 return -Vector3(m20, m21, m22);
301 }
302
304 {
305 return Vector3(m00, m01, m02);
306 }
307
309 {
310 return -Vector3(m00, m01, m02);
311 }
312
314 {
315 return Vector3(m10, m11, m12);
316 }
317
319 {
320 return -Vector3(m10, m11, m12);
321 }
322 };
323
324
325
326}
Definition: Matrix4.h:12
Vector3 getScale()
Definition: Matrix4.h:286
Vector3 translation()
Definition: Matrix4.h:229
Vector3 getEularZXY()
Definition: Matrix4.h:278
static Matrix4 ortho(float left, float right, float top, float bottom, float znear, float zfar)
Definition: Matrix4.h:52
float m31
Definition: Matrix4.h:18
static Matrix4 rotateX(float r)
Definition: Matrix4.h:68
static Vector4 operator*(Matrix4 m, Vector4 v)
Definition: Matrix4.h:169
float m02
Definition: Matrix4.h:15
static Matrix4 inverse(Matrix4 m)
Definition: Matrix4.h:106
float m10
Definition: Matrix4.h:16
float m30
Definition: Matrix4.h:18
static Matrix4 operator*(Matrix4 m1, Matrix4 m2)
Definition: Matrix4.h:114
float m12
Definition: Matrix4.h:16
float m22
Definition: Matrix4.h:17
static Matrix4 rotateZ(float r)
Definition: Matrix4.h:90
float m01
Definition: Matrix4.h:15
float m33
Definition: Matrix4.h:18
float m00
Definition: Matrix4.h:15
static Vector4 operator*(Matrix4 m, Vector3 v)
Definition: Matrix4.h:159
static Matrix4 scale(float x, float y, float z)
Definition: Matrix4.h:63
Vector3 left()
Definition: Matrix4.h:303
Vector3 up()
Definition: Matrix4.h:313
static Matrix4 fromNativeMatrix4(const kmyMath::Matrix4 &m)
Definition: Matrix4.h:204
float m23
Definition: Matrix4.h:17
Vector3 down()
Definition: Matrix4.h:318
float m20
Definition: Matrix4.h:17
Matrix4(float _m00, float _m01, float _m02, float _m03, float _m10, float _m11, float _m12, float _m13, float _m20, float _m21, float _m22, float _m23, float _m30, float _m31, float _m32, float _m33)
Definition: Matrix4.h:20
float m32
Definition: Matrix4.h:18
Vector3 front()
Definition: Matrix4.h:293
static Matrix4 perspectiveFOV(float fov, float asp, float znear, float zfar)
Definition: Matrix4.h:46
static Matrix4 identity()
Definition: Matrix4.h:101
static Matrix4 translate(float x, float y, float z)
Definition: Matrix4.h:58
kmyMath::Matrix4 toNativeMatrix4()
Definition: Matrix4.h:179
Matrix4 scaleIdentity()
Definition: Matrix4.h:243
float m13
Definition: Matrix4.h:16
float m03
Definition: Matrix4.h:15
static Matrix4 lookat(Vector3 eye, Vector3 target, Vector3 upvec)
Definition: Matrix4.h:126
Vector3 right()
Definition: Matrix4.h:308
Vector3 scale()
Definition: Matrix4.h:234
Vector3 back()
Definition: Matrix4.h:298
float m21
Definition: Matrix4.h:17
Matrix4 translateIdentity()
Definition: Matrix4.h:271
static Matrix4 rotateY(float r)
Definition: Matrix4.h:79
float m11
Definition: Matrix4.h:16
Definition: RefCapture.h:3
Definition: Vector3.h:7
float y
Definition: Vector3.h:10
float z
Definition: Vector3.h:10
static Vector3 crossProduct(Vector3 v1, Vector3 v2)
Definition: Vector3.h:98
float x
Definition: Vector3.h:10
static Vector3 normalize(Vector3 v)
Definition: Vector3.h:29
Definition: Vector4.h:6
float x
Definition: Vector4.h:9
float w
Definition: Vector4.h:9
float y
Definition: Vector4.h:9
float z
Definition: Vector4.h:9