diff options
Diffstat (limited to 'src/math/mat.c')
-rw-r--r-- | src/math/mat.c | 216 |
1 files changed, 108 insertions, 108 deletions
diff --git a/src/math/mat.c b/src/math/mat.c index c254c02..e3c6ce3 100644 --- a/src/math/mat.c +++ b/src/math/mat.c @@ -31,7 +31,7 @@ sharedMat2 = *p;\ p = &sharedMat2;\ }while(0) -void mat4_tostring(Mat4* m, char str[]) { +void internal_mat4_tostring(Mat4* m, char str[]) { ssrM_zero(str, sizeof(str)); for (int r = 0; r < 4; ++r) { for (int c = 0; c < 4; ++c) { @@ -43,28 +43,28 @@ void mat4_tostring(Mat4* m, char str[]) { } } -void mat4_print(Mat4* m) { - mat4_tostring(m, printbuffer); +void internal_mat4_print(Mat4* m) { + internal_mat4_tostring(m, printbuffer); printf("\n%s\n", printbuffer); } -void mat4_zero(Mat4* out) { +void internal_mat4_zero(Mat4* out) { ssr_assert(out); ssrM_zero(out, sizeof(Mat4)); } -void mat4_setidentity(Mat4* out) { +void internal_mat4_setidentity(Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = 1; out->e11 = 1; out->e22 = 1; out->e33 = 1; } -void mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out) { +void internal_mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = 2 / (r - l); out->e03 = -(r + l) / (r - l); out->e11 = 2 / (t - b); @@ -74,9 +74,9 @@ void mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* o out->e33 = 1; } -void mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out) { +void internal_mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = (2.f * n) / (r - l); out->e02 = (r + l) / (r - l); out->e11 = 2.f * n / (t - b); @@ -86,12 +86,12 @@ void mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out->e32 = -1; } -void mat4_setperspective(float _fov, float aspect, float near, float far, Mat4* out) { +void internal_mat4_setperspective(float _fov, float aspect, float near, float far, Mat4* out) { float fov = _fov * PI / 180.f; float tanf = tan(fov * 0.5); ssr_assert(fov > 0 && aspect > 0); ssr_assert(near > 0 && far > 0 && near != far); - mat4_setfrustum( + internal_mat4_setfrustum( -near*tanf*aspect, near*tanf*aspect, -near*tanf, @@ -108,10 +108,10 @@ static float _mul(float* r, float* c) { #define mul(r, c) _mul(&MAT(m1,r,0), &MAT(m2,0,c)) -void mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out) { +void internal_mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out) { ssr_assert(m1 && m2 && out); - if (mat4_isidentity(m1)) { if(m2 != out) *out = *m2; return; } - if (mat4_isidentity(m2)) { if(m1 != out) *out = *m1; return; } + if (internal_mat4_isidentity(m1)) { if(m2 != out) *out = *m2; return; } + if (internal_mat4_isidentity(m2)) { if(m1 != out) *out = *m1; return; } if (m1 == out) shrmat(m1); if (m2 == out) shrmat2(m2); @@ -121,44 +121,44 @@ void mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out) { out->e30 = mul(3, 0); out->e31 = mul(3, 1); out->e32 = mul(3, 2); out->e33 = mul(3, 3); } -void mat4_setscale(float kx, float ky, float kz, Mat4* out) { +void internal_mat4_setscale(float kx, float ky, float kz, Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = kx; out->e11 = ky; out->e22 = kz; out->e33 = 1; } -void mat4_setposition(float x, float y, float z, Mat4* out) { +void internal_mat4_setposition(float x, float y, float z, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); out->e03 = x; out->e13 = y; out->e23 = z; } -void mat4_setrotatez(float angle, Mat4* out) { +void internal_mat4_setrotatez(float angle, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angle = radians(angle); float s = sin(angle), c = cos(angle); out->e00 = c; out->e01 = -s; out->e10 = s; out->e11 = c; } -void mat4_setrotatex(float angle, Mat4* out) { +void internal_mat4_setrotatex(float angle, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angle = radians(angle); float s = sin(angle), c = cos(angle); out->e11 = c; out->e12 = -s; out->e21 = s; out->e22 = c; } -void mat4_setrotatey(float angle, Mat4* out) { +void internal_mat4_setrotatey(float angle, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angle = radians(angle); float s = sin(angle), c = cos(angle); out->e00 = c; out->e02 = s; @@ -166,9 +166,9 @@ void mat4_setrotatey(float angle, Mat4* out) { } /*https://www.geometrictools.com/Documentation/EulerAngles.pdf*/ -void mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out) { +void internal_mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angleX = radians(angleX); angleY = radians(angleY); angleZ = radians(angleZ); float sx = sin(angleX), cx = cos(angleX); float sy = sin(angleY), cy = cos(angleY); @@ -178,7 +178,7 @@ void mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out) { out->e20 = -cz * sy + cy * sx * sz; out->e21 = cy * cz*sx + sy * sz; out->e22 = cx * cy; } -void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { +void internal_mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { ssr_assert(ax && out); float a = radians(angle); @@ -187,8 +187,8 @@ void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { Vec3 axis = *ax; Vec3 temp; - vec3_normalize(&axis, &axis); - vec3_scale(&axis, 1 - c, &temp); + internal_vec3_normalize(&axis, &axis); + internal_vec3_scale(&axis, 1 - c, &temp); /* rotation matrix 推导过程 https://zhuanlan.zhihu.com/p/56587491 @@ -198,7 +198,7 @@ void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { 0, 0, 0, 1 */ - mat4_setidentity(out); + internal_mat4_setidentity(out); out->m[0][0] = c + temp.x * axis.x; out->m[0][1] = 0 + temp.x * axis.y + s * axis.z; out->m[0][2] = 0 + temp.x * axis.z - s * axis.y; @@ -213,9 +213,9 @@ void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { } -void mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out) { +void internal_mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out) { ssr_assert(x && y && z); - mat4_setidentity(out); + internal_mat4_setidentity(out); Vec4 asix = { x->x, x->y, x->z, 0 }; Vec4 asiy = { y->x, y->y, y->z, 0 }; Vec4 asiz = { z->x, z->y, z->z, 0 }; @@ -224,7 +224,7 @@ void mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out) { out->colums[2] = asiz; } -bool mat4_isidentity(Mat4* m) { +bool internal_mat4_isidentity(Mat4* m) { ssr_assert(m); //return memcmp(m, &mat4identity, sizeof(Mat4)) == 0; return @@ -234,95 +234,95 @@ bool mat4_isidentity(Mat4* m) { compare(m->pos.x, 0 ) && compare(m->pos.y, 0 ) && compare(m->pos.z, 0 ) &&compare( m->pos.w, 1); } -bool mat4_isorthogonal(Mat4* m) { +bool internal_mat4_isorthogonal(Mat4* m) { ssr_assert(m); Mat4 trans = {0}, res = { 0 }; - mat4_transpose(m, &trans); - mat4_multiply(m, &trans, &res); - return mat4_isidentity(&res); + internal_mat4_transpose(m, &trans); + internal_mat4_multiply(m, &trans, &res); + return internal_mat4_isidentity(&res); } /* ** 以z轴为准进行正交化,分为施密特正交化和叉乘正交化,施密特过程更加普遍,叉乘适用于三维空间,两种方法实际上等价 ** 如果用叉乘的方法,只需要关注yz,x通过叉乘得到 */ -void mat4_orthogonalize(Mat4* in, Mat4* out) { +void internal_mat4_orthogonalize(Mat4* in, Mat4* out) { ssr_assert(in && out); if (in == out) { shrmat(in); } - mat4_setidentity(out); + internal_mat4_setidentity(out); Vec4 z = in->basis.z; - vec3_normalize(&z, &z); + internal_vec3_normalize(&z, &z); Vec4 y = in->basis.y; Vec4 x = {0}; - vec3_cross(&y, &z, &x); - vec3_normalize(&x, &x); - vec3_cross(&z, &x, &y); + internal_vec3_cross(&y, &z, &x); + internal_vec3_normalize(&x, &x); + internal_vec3_cross(&z, &x, &y); out->basis.x = x; out->basis.y = y; out->basis.z = z; /* - mat4_setidentity(out); + internal_mat4_setidentity(out); Vec4 x = in->basis.x; Vec4 y = in->basis.y; Vec4 z = in->basis.z; Vec3 temp, temp2; - vec3_normalize(&z, &z); + internal_vec3_normalize(&z, &z); out->basis.z = z; - float dot = vec3_dot(&y, &z); - vec3_scale(&z, dot, &temp); - vec3_minus(&y, &temp, &y); - vec3_normalize(&y, &y); + float dot = internal_vec3_dot(&y, &z); + internal_vec3_scale(&z, dot, &temp); + internal_vec3_minus(&y, &temp, &y); + internal_vec3_normalize(&y, &y); out->basis.y = y; - vec3_cross(&y, &z, &out->basis.x); + internal_vec3_cross(&y, &z, &out->basis.x); */ /*针对右手系调整basis.x的方向*/ /*https://math.stackexchange.com/questions/1847465/why-to-use-gram-schmidt-process-to-orthonormalise-a-basis-instead-of-cross-produ*/ /*由于需要针对右手系,这里不这样计算,因为可能要对结果进行翻转 - dot = vec3_dot(&x, &z); - vec3_scale(&z, dot, &temp); - vec3_minus(&x, &temp, &temp2); - dot = vec3_dot(&x, &y); - vec3_scale(&y, dot, &temp); - vec3_minus(&temp2, &temp, &x); - vec3_normalize(&x, &x); + dot = internal_vec3_dot(&x, &z); + internal_vec3_scale(&z, dot, &temp); + internal_vec3_minus(&x, &temp, &temp2); + dot = internal_vec3_dot(&x, &y); + internal_vec3_scale(&y, dot, &temp); + internal_vec3_minus(&temp2, &temp, &x); + internal_vec3_normalize(&x, &x); out->basis.x = x; */ } -bool mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out) { +bool internal_mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out) { ssr_assert(view && up && out); /*正交化*/ - float mag = vec3_magnitude(view); + float mag = internal_vec3_magnitude(view); if (mag < EPSILON) return 0; Vec3 z; - vec3_scale(view, 1.f / mag, &z); + internal_vec3_scale(view, 1.f / mag, &z); Vec3 x; - vec3_cross(up, &z, &x); - mag = vec3_magnitude(&x); + internal_vec3_cross(up, &z, &x); + mag = internal_vec3_magnitude(&x); if (mag < EPSILON) return 0; - vec3_scale(&x, 1.f / mag, &x); + internal_vec3_scale(&x, 1.f / mag, &x); Vec3 y; - vec3_cross(&z, &x, &y); - mag = vec3_magnitude(&y); + internal_vec3_cross(&z, &x, &y); + mag = internal_vec3_magnitude(&y); if (!compare(mag, 1)) return 0; - mat4_setorthonormalbias(&x, &y, &z, out); /*xyz正交*/ + internal_mat4_setorthonormalbias(&x, &y, &z, out); /*xyz正交*/ return 1; } -void mat4_mulvec4(Mat4* mat, Vec4* v, Vec4* out) { +void internal_mat4_mulvec4(Mat4* mat, Vec4* v, Vec4* out) { ssr_assert(mat && v && out); if (v == out) { sharedVec4 = *v; @@ -337,7 +337,7 @@ void mat4_mulvec4(Mat4* mat, Vec4* v, Vec4* out) { /* ** mat3 apply to vec3 */ -void mat4_mulvec3(Mat4* mat, Vec3* v, Vec3* out) { +void internal_mat4_mulvec3(Mat4* mat, Vec3* v, Vec3* out) { ssr_assert(mat && v && out); if (v == out) { sharedVec3 = *v; @@ -350,7 +350,7 @@ void mat4_mulvec3(Mat4* mat, Vec3* v, Vec3* out) { #define trans(r, c) out->e##r##c = m->e##c##r -void mat4_transpose(Mat4* m, Mat4* out) { +void internal_mat4_transpose(Mat4* m, Mat4* out) { ssr_assert(m && out); if (m == out) shrmat(m); @@ -362,10 +362,10 @@ void mat4_transpose(Mat4* m, Mat4* out) { /* ** 使用高斯消元法计算任意矩阵的逆矩阵。针对不含投影的3D变换矩阵,应该使用 -** mat4_invertgeneral3d() +** internal_mat4_invertgeneral3d() ** 更快一些 */ -bool mat4_invertfull(Mat4* m, Mat4* out) { +bool internal_mat4_invertfull(Mat4* m, Mat4* out) { ssr_assert(m && out); #define _m(r, c) MAT(m, r, c) @@ -473,11 +473,11 @@ bool mat4_invertfull(Mat4* m, Mat4* out) { ** 乘上平移矩阵的逆矩阵,即 ** M^-1 = (T(RS))^-1 = (RS)^-1 * T^-1 */ -bool mat4_invertgeneral3d(Mat4* in, Mat4* out) { +bool internal_mat4_invertgeneral3d(Mat4* in, Mat4* out) { ssr_assert(in && out); if (in == out) shrmat(in); - mat4_setidentity(out); + internal_mat4_setidentity(out); /*计算左上角3x3矩阵的行列式*/ float pos = 0, neg = 0, t; @@ -527,32 +527,32 @@ bool mat4_invertgeneral3d(Mat4* in, Mat4* out) { return 1; } -void mat4_invertpos(Mat4* in, Mat4* out) { +void internal_mat4_invertpos(Mat4* in, Mat4* out) { } -void mat4_invertscale(Mat4* in, Mat4* out) { +void internal_mat4_invertscale(Mat4* in, Mat4* out) { } -void mat4_invertrot(Mat4* in, Mat4* out) { +void internal_mat4_invertrot(Mat4* in, Mat4* out) { ssr_assert(in && out); - mat4_transpose(in, out); + internal_mat4_transpose(in, out); } -void mat4_settr(Vec3* pos, Quat* rot, Mat4* out) { +void internal_mat4_settr(Vec3* pos, Quat* rot, Mat4* out) { ssr_assert(pos && rot && out); - mat4_zero(out); - quat_tomat4(rot, out); + internal_mat4_zero(out); + internal_quat_tomat4(rot, out); out->e03 = pos->x; out->e13 = pos->y; out->e23 = pos->z; } -void mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out) { +void internal_mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out) { ssr_assert(pos && rot && scale && out); - mat4_zero(out); - quat_tomat4(rot, out); /*pos*rot*scale的顺序*/ + internal_mat4_zero(out); + internal_quat_tomat4(rot, out); /*pos*rot*scale的顺序*/ out->e00 *= scale->x; out->e01 *= scale->y; out->e02 *= scale->z; out->e10 *= scale->x; out->e11 *= scale->y; out->e12 *= scale->z; out->e20 *= scale->x; out->e21 *= scale->y; out->e22 *= scale->z; @@ -561,16 +561,16 @@ void mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out) { out->e23 = pos->z; } -void mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out) { +void internal_mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out) { ssr_assert(pos && rot && out); - mat4_zero(out); - quat_invert(rot, rot); - quat_tomat4(rot, out); + internal_mat4_zero(out); + internal_quat_invert(rot, rot); + internal_quat_tomat4(rot, out); Vec3 reverse = { -pos->x, -pos->y, -pos->z}; - mat4_translate(out, &reverse, out); /* (TR)^-1 = R^-1*T^-1所以这里是右乘*/ + internal_mat4_translate(out, &reverse, out); /* (TR)^-1 = R^-1*T^-1所以这里是右乘*/ } -void mat4_scale(Mat4* m, Vec3* scale, Mat4* out) { +void internal_mat4_scale(Mat4* m, Vec3* scale, Mat4* out) { ssr_assert(m && scale && out); if (out != m) { *out = *m; @@ -598,7 +598,7 @@ void mat4_scale(Mat4* m, Vec3* scale, Mat4* out) { out->e32 *= scale->z; } -void mat4_translate(Mat4* m, Vec3* pos, Mat4* out) { +void internal_mat4_translate(Mat4* m, Vec3* pos, Mat4* out) { ssr_assert(m && pos && out); if (out != m) { *out = *m; @@ -616,14 +616,14 @@ void mat4_translate(Mat4* m, Vec3* pos, Mat4* out) { out->e33 = out->e30 * pos->x + out->e31 * pos->y + out->e32 * pos->z + out->e33; } -void mat4_rotate(Mat4* m, float angle, Vec3* ax, Mat4* out) { +void internal_mat4_rotate(Mat4* m, float angle, Vec3* ax, Mat4* out) { ssr_assert(m && ax && out); Mat4 rot; - mat4_setaxisangle(ax, angle, &rot); - mat4_multiply(m, &rot, out); + internal_mat4_setaxisangle(ax, angle, &rot); + internal_mat4_multiply(m, &rot, out); } -void mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale) { +void internal_mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale) { ssr_assert(src && pos && quat && scale); Vec3* x = &src->colums[0]; @@ -633,11 +633,11 @@ void mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale) { *pos = *w; - quat_setlookrotation(z, y, quat); + internal_quat_setlookrotation(z, y, quat); - scale->x = vec3_magnitude(x); - scale->y = vec3_magnitude(y); - scale->z = vec3_magnitude(z); + scale->x = internal_vec3_magnitude(x); + scale->y = internal_vec3_magnitude(y); + scale->z = internal_vec3_magnitude(z); } static void MakePositive(Euler* euler) {/*弧度制欧拉角*/ @@ -665,7 +665,7 @@ static void SanitizeEuler(Euler* e) {/*弧度制欧拉角*/ } /*from unity src*/ -bool mat4_toeuler(Mat4* in, Euler* out) { +bool internal_mat4_toeuler(Mat4* in, Euler* out) { ssr_assert(in && out); // from http://www.geometrictools.com/Documentation/EulerAngles.pdf // YXZ order @@ -676,9 +676,9 @@ bool mat4_toeuler(Mat4* in, Euler* out) { out->x = asin(-MAT(in, 1, 2)); out->y = atan2(MAT(in, 0, 2), MAT(in, 2, 2)); out->z = atan2(MAT(in, 1, 0), MAT(in, 1, 1)); - //euler_rad2deg(out, out); + //internal_euler_rad2deg(out, out); SanitizeEuler(out); - euler_rad2deg(out, out); + internal_euler_rad2deg(out, out); return 1; } else @@ -687,9 +687,9 @@ bool mat4_toeuler(Mat4* in, Euler* out) { out->x = PI * 0.5F; out->y = atan2(MAT(in, 0, 1), MAT(in, 0, 0)); out->z = 0.0F; - //euler_rad2deg(out, out); + //internal_euler_rad2deg(out, out); SanitizeEuler(out); - euler_rad2deg(out, out); + internal_euler_rad2deg(out, out); return 0; } } @@ -699,16 +699,16 @@ bool mat4_toeuler(Mat4* in, Euler* out) { out->x = -PI * 0.5F; out->y = atan2(-MAT(in, 0, 1), MAT(in, 0, 0)); out->z = 0.0F; - //euler_rad2deg(out, out); + //internal_euler_rad2deg(out, out); SanitizeEuler(out); - euler_rad2deg(out, out); + internal_euler_rad2deg(out, out); return 0; } } /*from unity source*/ /*https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/*/ -void mat4_toquat(Mat4* in, Quat* out) { +void internal_mat4_toquat(Mat4* in, Quat* out) { // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes // article "Quaternionf Calculus and Fast Animation". float fTrace = MAT(in, 0, 0) + MAT(in, 1, 1) + MAT(in, 2, 2); @@ -745,7 +745,7 @@ void mat4_toquat(Mat4* in, Quat* out) { *apkQuat[j] = (MAT(in, j, i) + MAT(in, i, j))*fRoot; *apkQuat[k] = (MAT(in, k, i) + MAT(in, i, k))*fRoot; } - quat_normalize(out, out); + internal_quat_normalize(out, out); } void mat3_multvec3(Mat3* m, Vec3* v, Vec3* out) { |