4#include "math/Matrix4.h"
10namespace XXSharpKmyMath{
12 private value class Matrix4{
15 float m00, m01, m02, m03;
16 float m10, m11, m12, m13;
17 float m20, m21, m22, m23;
18 float m30, m31, m32, m33;
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)
46 static Matrix4 perspectiveFOV(
float fov,
float asp,
float znear,
float zfar)
48 auto n = kmyMath::Matrix4::perspectiveFOV(fov * 2, asp, znear, zfar);
49 return fromNativeMatrix4(n);
52 static Matrix4 ortho(
float left,
float right,
float top,
float bottom,
float znear,
float zfar)
54 auto n = kmyMath::Matrix4::ortho(left, right, top, bottom, znear, zfar);
55 return fromNativeMatrix4(n);
58 static Matrix4 translate(
float x,
float y,
float z)
60 return Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1);
63 static Matrix4 scale(
float x,
float y,
float z)
65 return Matrix4(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
68 static Matrix4 rotateX(
float r)
79 static Matrix4 rotateY(
float r)
90 static Matrix4 rotateZ(
float r)
101 static Matrix4 identity()
103 return Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
106 static Matrix4 inverse(Matrix4 m)
108 kmyMath::Matrix4 nm = m.toNativeMatrix4();
109 nm = kmyMath::Matrix4::inverse(nm);
110 Matrix4 ret = fromNativeMatrix4(nm);
114 static Matrix4 operator*(Matrix4 m1, Matrix4 m2)
118 kmyMath::Matrix4 nm1 = m1.toNativeMatrix4();
119 kmyMath::Matrix4 nm2 = m2.toNativeMatrix4();
121 kmyMath::Matrix4 res = nm1 * nm2;
123 return fromNativeMatrix4(res);
126 static Matrix4 lookat( Vector3 eye, Vector3 target, Vector3 upvec )
130 Vector3 zaxis, xaxis, yaxis;
132 zaxis = Vector3::normalize(target - eye);
133 xaxis = Vector3::normalize(Vector3::crossProduct(zaxis, upvec));
134 yaxis = Vector3::crossProduct(xaxis, zaxis);
159 static Vector4 operator*(Matrix4 m, Vector3 v)
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;
169 static Vector4 operator*(Matrix4 m, Vector4 v)
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;
179 kmyMath::Matrix4 toNativeMatrix4()
204 static Matrix4 fromNativeMatrix4(
const kmyMath::Matrix4 &m)
229 Vector3 translation()
231 return Vector3(m30, m31, m32);
237 sqrt(m00 * m00 + m01 * m01 + m02 * m02),
238 sqrt(m10 * m10 + m11 * m11 + m12 * m12),
239 sqrt(m20 * m20 + m21 * m21 + m22 * m22)
243 Matrix4 scaleIdentity()
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);
250 if (x == 0.0f || y == 0.0f || z == 0.0f) {
251 PRINTF(
"Matrix4 scale reset error");
255 Matrix4 r = Matrix4(*
this);
271 Matrix4 translateIdentity()
273 Matrix4 r = Matrix4(*
this);
274 r.m30 = r.m31 = r.m32 = 0.0f;
278 Vector3 getEularZXY()
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);
288 kmyMath::Matrix4 nm = toNativeMatrix4();
289 auto v = nm.getScale();
290 return Vector3(v.x, v.y, v.z);
295 return Vector3(m20, m21, m22);
300 return -Vector3(m20, m21, m22);
305 return Vector3(m00, m01, m02);
310 return -Vector3(m00, m01, m02);
315 return Vector3(m10, m11, m12);
320 return -Vector3(m10, m11, m12);