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