diff options
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/mat.c | 216 | ||||
-rw-r--r-- | src/math/math.h | 204 | ||||
-rw-r--r-- | src/math/quat.c | 120 | ||||
-rw-r--r-- | src/math/vec2.c | 14 | ||||
-rw-r--r-- | src/math/vec3.c | 66 | ||||
-rw-r--r-- | src/math/vec4.c | 16 |
6 files changed, 318 insertions, 318 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) { diff --git a/src/math/math.h b/src/math/math.h index a69dabe..8f59137 100644 --- a/src/math/math.h +++ b/src/math/math.h @@ -133,17 +133,17 @@ typedef union { /* Vec */ /************************************************************************/ -void vec2_scale(Vec2* v, float k, Vec2* out); -void vec2_plus(Vec2* v1, Vec2* v2, Vec2* out); -void vec2_offset(Vec2* v, float offset, Vec2* out); -void vec2_rotate(Vec2* v, float angle, Vec2* out); +void internal_vec2_scale(Vec2* v, float k, Vec2* out); +void internal_vec2_plus(Vec2* v1, Vec2* v2, Vec2* out); +void internal_vec2_offset(Vec2* v, float offset, Vec2* out); +void internal_vec2_rotate(Vec2* v, float angle, Vec2* out); -float vec2_dot(Vec2* v1, Vec2* v2); +float internal_vec2_dot(Vec2* v1, Vec2* v2); -void vec2_tostring(Vec2* v, char buf[]); -void vec2_print(Vec2* v); +void internal_vec2_tostring(Vec2* v, char buf[]); +void internal_vec2_print(Vec2* v); -#define vec3_xy(v) (v->xy) +#define internal_vec3_xy(v) (v->xy) extern Vec3 vec3forward; /*(0,0,1)*/ extern Vec3 vec3up; /*(0,1,0)*/ @@ -156,41 +156,41 @@ extern Vec4 vec4zero; /*(0,0,0)*/ #define zerovec3 {0, 0, 0} #define zerovec4 {0, 0, 0, 0} -void vec3_tostring(Vec3* v, char buf[]); -void vec3_print(Vec3* v); +void internal_vec3_tostring(Vec3* v, char buf[]); +void internal_vec3_print(Vec3* v); -Vec3 vec3_make(float x, float y, float z); -float vec3_intersection(Vec3* v1, Vec3* v2); /*夹角*/ -void vec3_projection(Vec3* v1, Vec3* v2, Vec3* out);/*v1在v2上的投影*/ -void vec3_scale(Vec3* v, float k, Vec3* out); -void vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out); -void vec3_plus(Vec3* v1, Vec3* v2, Vec3* out); -void vec3_offset(Vec3* v, float offset, Vec3* out); -void vec3_normalize(Vec3* v, Vec3* out); -void vec3_vec4(float w, Vec4* out); +Vec3 internal_vec3_make(float x, float y, float z); +float internal_vec3_intersection(Vec3* v1, Vec3* v2); /*夹角*/ +void internal_vec3_projection(Vec3* v1, Vec3* v2, Vec3* out);/*v1在v2上的投影*/ +void internal_vec3_scale(Vec3* v, float k, Vec3* out); +void internal_vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out); +void internal_vec3_plus(Vec3* v1, Vec3* v2, Vec3* out); +void internal_vec3_offset(Vec3* v, float offset, Vec3* out); +void internal_vec3_normalize(Vec3* v, Vec3* out); +void internal_vec3_vec4(float w, Vec4* out); -void vec3_minus(Vec3* v1, Vec3* v2, Vec3* out); -float vec3_dot(Vec3* v1, Vec3* v2); -void vec3_cross(Vec3* v1, Vec3* v2, Vec3* out); -void vec3_multiply(Vec3* v1, Vec3* v2, Quat* out);// 向量的乘法,st=sxt-s*t,结果是一个四元数 +void internal_vec3_minus(Vec3* v1, Vec3* v2, Vec3* out); +float internal_vec3_dot(Vec3* v1, Vec3* v2); +void internal_vec3_cross(Vec3* v1, Vec3* v2, Vec3* out); +void internal_vec3_multiply(Vec3* v1, Vec3* v2, Quat* out);// 向量的乘法,st=sxt-s*t,结果是一个四元数 -float vec3_magnitude(Vec3* v1); -float vec3_magnitude2(Vec3* v1); +float internal_vec3_magnitude(Vec3* v1); +float internal_vec3_magnitude2(Vec3* v1); -void vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out); -void vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out); +void internal_vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out); +void internal_vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out); -void vec4_dividew(Vec4* v, Vec3* out); -void vec4_dividewnoz(Vec4* v, Vec4* out); +void internal_vec4_dividew(Vec4* v, Vec3* out); +void internal_vec4_dividewnoz(Vec4* v, Vec4* out); -void vec4_tostring(Vec4* v, char buf[]); -void vec4_print(Vec4* v); +void internal_vec4_tostring(Vec4* v, char buf[]); +void internal_vec4_print(Vec4* v); -void vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out); +void internal_vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out); -void vec4_scale(Vec4* v, float t, Vec4* out); +void internal_vec4_scale(Vec4* v, float t, Vec4* out); -void vec4_add(Vec4* v1, Vec4* v2, Vec4* out); +void internal_vec4_add(Vec4* v1, Vec4* v2, Vec4* out); /************************************************************************/ @@ -199,57 +199,57 @@ void vec4_add(Vec4* v1, Vec4* v2, Vec4* out); extern Mat4 mat4identity; -void mat4_tostring(Mat4* m, char str[]); -void mat4_print(Mat4* m); +void internal_mat4_tostring(Mat4* m, char str[]); +void internal_mat4_print(Mat4* m); -void mat4_zero(Mat4* out); -void mat4_setidentity(Mat4* out); -void mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out); -void mat4_setperspective(float fov, float aspect, float near, float far, Mat4* out); -void mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out); -void mat4_setscale(float kx, float ky, float kz, Mat4* out); -void mat4_setposition(float x, float y, float z, Mat4* out); -void mat4_setrotatez(float angle, Mat4* out); -void mat4_setrotatex(float angle, Mat4* out); -void mat4_setrotatey(float angle, Mat4* out); -void mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out);/*RyRxRz*/ -void mat4_setaxisangle(Vec3* axis, float angle, Mat4* out); +void internal_mat4_zero(Mat4* out); +void internal_mat4_setidentity(Mat4* out); +void internal_mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out); +void internal_mat4_setperspective(float fov, float aspect, float near, float far, Mat4* out); +void internal_mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out); +void internal_mat4_setscale(float kx, float ky, float kz, Mat4* out); +void internal_mat4_setposition(float x, float y, float z, Mat4* out); +void internal_mat4_setrotatez(float angle, Mat4* out); +void internal_mat4_setrotatex(float angle, Mat4* out); +void internal_mat4_setrotatey(float angle, Mat4* out); +void internal_mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out);/*RyRxRz*/ +void internal_mat4_setaxisangle(Vec3* axis, float angle, Mat4* out); -bool mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out); -void mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out); /*正交的三个轴*/ +bool internal_mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out); +void internal_mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out); /*正交的三个轴*/ -void mat4_orthogonalize(Mat4* in, Mat4* out); /*解决矩阵蠕变,对左上角3x3矩阵进行正交化,结果是右手系的正交矩阵*/ -bool mat4_isorthogonal(Mat4* m); /*判断是不是正交矩阵*/ -bool mat4_isidentity(Mat4* m); +void internal_mat4_orthogonalize(Mat4* in, Mat4* out); /*解决矩阵蠕变,对左上角3x3矩阵进行正交化,结果是右手系的正交矩阵*/ +bool internal_mat4_isorthogonal(Mat4* m); /*判断是不是正交矩阵*/ +bool internal_mat4_isidentity(Mat4* m); -void mat4_settr(Vec3* pos, Quat* rot, Mat4* out); /*用旋转和平移初始化mat4*/ -void mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out); -void mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out); +void internal_mat4_settr(Vec3* pos, Quat* rot, Mat4* out); /*用旋转和平移初始化mat4*/ +void internal_mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out); +void internal_mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out); -void mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out); /* m1的行乘m2的列,意义是用m1变换m2 */ +void internal_mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out); /* m1的行乘m2的列,意义是用m1变换m2 */ -void mat4_transpose(Mat4* m, Mat4* out); +void internal_mat4_transpose(Mat4* m, Mat4* out); -void mat4_scale(Mat4* m, Vec3* scale, Mat4* out);/* 后乘post-multiply scale */ -void mat4_translate(Mat4* m, Vec3* pos, Mat4* out); /* 后乘post-multiply translate */ -void mat4_rotate(Mat4*m, float angle, Vec3* rot, Mat4* out);/*后乘绕任意轴向量旋转矩阵*/ +void internal_mat4_scale(Mat4* m, Vec3* scale, Mat4* out);/* 后乘post-multiply scale */ +void internal_mat4_translate(Mat4* m, Vec3* pos, Mat4* out); /* 后乘post-multiply translate */ +void internal_mat4_rotate(Mat4*m, float angle, Vec3* rot, Mat4* out);/*后乘绕任意轴向量旋转矩阵*/ -bool mat4_invertfull(Mat4* in, Mat4* out); /* 并不是所有矩阵都能求逆 */ -bool mat4_invertgeneral3d(Mat4* in, Mat4* out); /* 对scale rotate translate求逆 */ -void mat4_invertscale(Mat4* scale, Mat4* out); /* 对缩放矩阵求逆 */ -void mat4_invertrot(Mat4* rot, Mat4* out); /* 对旋转矩阵求逆 */ -void mat4_invertpos(Mat4* pos, Mat4* out); /* 对平移矩阵求逆 */ +bool internal_mat4_invertfull(Mat4* in, Mat4* out); /* 并不是所有矩阵都能求逆 */ +bool internal_mat4_invertgeneral3d(Mat4* in, Mat4* out); /* 对scale rotate translate求逆 */ +void internal_mat4_invertscale(Mat4* scale, Mat4* out); /* 对缩放矩阵求逆 */ +void internal_mat4_invertrot(Mat4* rot, Mat4* out); /* 对旋转矩阵求逆 */ +void internal_mat4_invertpos(Mat4* pos, Mat4* out); /* 对平移矩阵求逆 */ -void mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale); /*分解trs矩阵*/ +void internal_mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale); /*分解trs矩阵*/ -void mat4_mulvec4(Mat4* m, Vec4* v, Vec4* out); -void mat4_mulvec3(Mat4* m, Vec3* v, Vec3* out); +void internal_mat4_mulvec4(Mat4* m, Vec4* v, Vec4* out); +void internal_mat4_mulvec3(Mat4* m, Vec3* v, Vec3* out); -bool mat4_toeuler(Mat4* in, Euler* out); /* 计算YXZ旋转矩阵的欧拉角 */ -void mat4_toquat(Mat4* in, Quat* out); /*in是正交矩阵*/ +bool internal_mat4_toeuler(Mat4* in, Euler* out); /* 计算YXZ旋转矩阵的欧拉角 */ +void internal_mat4_toquat(Mat4* in, Quat* out); /*in是正交矩阵*/ #define ROWMAT(A, ...)\ -Mat4 A={__VA_ARGS__};mat4_transpose(&A, &A); +Mat4 A={__VA_ARGS__};internal_mat4_transpose(&A, &A); void mat3_multvec3(Mat3* m, Vec3* v, Vec3* out); void mat23_applytovec3(Mat23* m, Vec3* v, Vec2* out); @@ -259,47 +259,47 @@ void mat43_applytovec3(Mat43* m, Vec3* v, Vec4* out); /* Quat */ /************************************************************************/ -void quat_tostring(Quat* q, char str[]); -void quat_print(Quat* q); +void internal_quat_tostring(Quat* q, char str[]); +void internal_quat_print(Quat* q); -Quat quat_make(float rx, float ry, float rz); +Quat internal_quat_make(float rx, float ry, float rz); -void euler_toquat(Euler* e, Quat* out); -void euler_deg2rad(Euler* in, Euler* out); -void euler_rad2deg(Euler* in, Euler* out); +void internal_euler_toquat(Euler* e, Quat* out); +void internal_euler_deg2rad(Euler* in, Euler* out); +void internal_euler_rad2deg(Euler* in, Euler* out); -void euler_tostring(Euler* v, char buf[]); -void euler_print(Euler* v); +void internal_euler_tostring(Euler* v, char buf[]); +void internal_euler_print(Euler* v); -void quat_fromaxisangle(Vec3* axis, float angle, Quat* out); /*轴角转四元数*/ -void quat_fromeuler(Euler* euler, Quat* out); /*按照zxy顺序*/ +void internal_quat_fromaxisangle(Vec3* axis, float angle, Quat* out); /*轴角转四元数*/ +void internal_quat_fromeuler(Euler* euler, Quat* out); /*按照zxy顺序*/ -void quat_tomat4(Quat* q, Mat4* out); -void quat_toeuler(Quat*q, Euler* out); +void internal_quat_tomat4(Quat* q, Mat4* out); +void internal_quat_toeuler(Quat*q, Euler* out); -void quat_normalize(Quat* q, Quat* out); /*解决蠕变,保持四元数合法*/ +void internal_quat_normalize(Quat* q, Quat* out); /*解决蠕变,保持四元数合法*/ -void quat_scale(Quat* q, float scale, Quat* out); -void quat_rotate(); +void internal_quat_scale(Quat* q, float scale, Quat* out); +void internal_quat_rotate(); -void quat_minus(Quat* q1, Quat* q2, Quat* out); -void quat_slerp(Quat* start, Quat* end, float t, Quat* out); -void quat_lerp(Quat* start, Quat* end, float t, Quat* out); -void quat_translate(Quat* q, Vec4* v, Vec4* out); -void quat_invert(Quat* q, Quat* out); -float quat_dot(Quat* q1, Quat* q2); -void quat_multiply(Quat* q1, Quat* q2, Quat* out); -void quat_devide(Quat* q, float k, Quat* out); -void quat_negtive(Quat* in, Quat* out); -bool quat_isidentity(Quat* q); +void internal_quat_minus(Quat* q1, Quat* q2, Quat* out); +void internal_quat_slerp(Quat* start, Quat* end, float t, Quat* out); +void internal_quat_lerp(Quat* start, Quat* end, float t, Quat* out); +void internal_quat_translate(Quat* q, Vec4* v, Vec4* out); +void internal_quat_invert(Quat* q, Quat* out); +float internal_quat_dot(Quat* q1, Quat* q2); +void internal_quat_multiply(Quat* q1, Quat* q2, Quat* out); +void internal_quat_devide(Quat* q, float k, Quat* out); +void internal_quat_negtive(Quat* in, Quat* out); +bool internal_quat_isidentity(Quat* q); -void quat_applytovec3(Quat* q, Vec3* v, Vec3* out); /*用四元数直接旋转向量*/ +void internal_quat_applytovec3(Quat* q, Vec3* v, Vec3* out); /*用四元数直接旋转向量*/ -void quat_conjugate(Quat* in, Quat* out); +void internal_quat_conjugate(Quat* in, Quat* out); -bool quat_setlookrotation(Vec3* view, Vec3* up, Quat* out); +bool internal_quat_setlookrotation(Vec3* view, Vec3* up, Quat* out); -float quat_magnitude(Quat* q); -float quat_magnitude2(Quat* q); +float internal_quat_magnitude(Quat* q); +float internal_quat_magnitude2(Quat* q); #endif
\ No newline at end of file diff --git a/src/math/quat.c b/src/math/quat.c index 8f7274d..07215d2 100644 --- a/src/math/quat.c +++ b/src/math/quat.c @@ -35,59 +35,59 @@ sharedEuler = *v;\ v = &sharedEuler;\ }while(0) -void quat_tostring(Quat* v, char buf[]) { +void internal_quat_tostring(Quat* v, char buf[]) { ssr_assert(v); sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z); } -void quat_print(Quat* q) { +void internal_quat_print(Quat* q) { ssr_assert(q); - quat_tostring(q, printbuffer); + internal_quat_tostring(q, printbuffer); printf("\n%s\n", printbuffer); } -void euler_tostring(Euler* v, char buf[]) { +void internal_euler_tostring(Euler* v, char buf[]) { sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z); } -void euler_print(Euler* v) { - euler_tostring(v, printbuffer); +void internal_euler_print(Euler* v) { + internal_euler_tostring(v, printbuffer); printf("\n%s\n", printbuffer); } -void euler_deg2rad(Euler* in, Euler* out) { +void internal_euler_deg2rad(Euler* in, Euler* out) { ssr_assert(in && out); out->x = radians(in->x); out->y = radians(in->y); out->z = radians(in->z); } -void euler_rad2deg(Euler* in, Euler* out) { +void internal_euler_rad2deg(Euler* in, Euler* out) { ssr_assert(in && out); out->x = degree(in->x); out->y = degree(in->y); out->z = degree(in->z); } -Quat quat_make(float rx, float ry, float rz) { +Quat internal_quat_make(float rx, float ry, float rz) { Quat rot; Euler euler = {rx, ry, rz}; - quat_fromeuler(&euler, &rot); + internal_quat_fromeuler(&euler, &rot); return rot; } -void euler_toquat(Euler* euler, Quat* out) { +void internal_euler_toquat(Euler* euler, Quat* out) { ssr_assert(euler && out); - quat_fromeuler(euler, out); + internal_quat_fromeuler(euler, out); } /*这里精度不对*/ -bool quat_isidentity(Quat* q) { - return compare(quat_magnitude(q), 1.f); +bool internal_quat_isidentity(Quat* q) { + return compare(internal_quat_magnitude(q), 1.f); } -void quat_fromaxisangle(Vec3* axis, float angle, Quat* out) { - ssr_assert(compare(vec3_magnitude2(axis), 1.f)); +void internal_quat_fromaxisangle(Vec3* axis, float angle, Quat* out) { + ssr_assert(compare(internal_vec3_magnitude2(axis), 1.f)); angle = radians(angle); float half = angle * 0.5; float s = sin(half); @@ -97,10 +97,10 @@ void quat_fromaxisangle(Vec3* axis, float angle, Quat* out) { out->z = s * axis->z; } -void quat_fromeuler(Euler* el, Quat* out) { +void internal_quat_fromeuler(Euler* el, Quat* out) { ssr_assert(el && out); Euler euler; - euler_deg2rad(el, &euler); + internal_euler_deg2rad(el, &euler); float cx = cos(euler.x * 0.5f); float sx = sin(euler.x * 0.5f); float cy = cos(euler.y * 0.5f); @@ -111,15 +111,15 @@ void quat_fromeuler(Euler* el, Quat* out) { Quat qy = {0, sy, 0,cy}; Quat qz = {0, 0,sz,cz}; /*按ZXY顺序*/ - quat_multiply(&qx, &qz, &qx); - quat_multiply(&qy, &qx, out); - ssr_assert(quat_isidentity(out)); + internal_quat_multiply(&qx, &qz, &qx); + internal_quat_multiply(&qy, &qx, out); + ssr_assert(internal_quat_isidentity(out)); } /* ** 四元数直接对向量进行旋转和先转成矩阵形式再旋转计算量一样 */ -void quat_applytovec3(Quat* q, Vec3* v, Vec3* out) { +void internal_quat_applytovec3(Quat* q, Vec3* v, Vec3* out) { ssr_assert(q && v && out); if (v == out) { shrvec3(v); @@ -145,13 +145,13 @@ void quat_applytovec3(Quat* q, Vec3* v, Vec3* out) { out->z = (xz - wy) * v->x + (yz + wx) * v->y + (1.0f - (xx + yy)) * v->z; } -void quat_tomat4(Quat* q, Mat4* out) { +void internal_quat_tomat4(Quat* q, Mat4* out) { ssr_assert(q && out); - ssr_assert(quat_isidentity(q)); + ssr_assert(internal_quat_isidentity(q)); - mat4_setidentity(out); + internal_mat4_setidentity(out); - float x = q->x * 2.0F; /*从quat_applytovec3能得到矩阵形式*/ + float x = q->x * 2.0F; /*从internal_quat_applytovec3能得到矩阵形式*/ float y = q->y * 2.0F; float z = q->z * 2.0F; float xx = q->x * x; @@ -164,7 +164,7 @@ void quat_tomat4(Quat* q, Mat4* out) { float wy = q->w * y; float wz = q->w * z; - /*和mat4_setaxisangle实际上结果一样*/ + /*和internal_mat4_setaxisangle实际上结果一样*/ out->l[0] = 1.0f - (yy + zz); out->l[1] = xy + wz; out->l[2] = xz - wy; @@ -178,20 +178,20 @@ void quat_tomat4(Quat* q, Mat4* out) { out->l[10] = 1.0f - (xx + yy); } -void quat_toeuler(Quat* q, Euler* out) { +void internal_quat_toeuler(Quat* q, Euler* out) { ssr_assert(q && out); Mat4 mat; - quat_tomat4(q, &mat); /*计算变换矩阵*/ - mat4_toeuler(&mat, out); /*mat是按照RyRxRz顺序(z->y->x)的旋转矩阵*/ + internal_quat_tomat4(q, &mat); /*计算变换矩阵*/ + internal_mat4_toeuler(&mat, out); /*mat是按照RyRxRz顺序(z->y->x)的旋转矩阵*/ } -void quat_normalize(Quat* q, Quat* out) { +void internal_quat_normalize(Quat* q, Quat* out) { ssr_assert(q && out); - float mag = quat_magnitude(q); - quat_scale(q, 1.f/mag, out); + float mag = internal_quat_magnitude(q); + internal_quat_scale(q, 1.f/mag, out); } -void quat_scale(Quat* q, float scale, Quat* out) { +void internal_quat_scale(Quat* q, float scale, Quat* out) { ssr_assert(q && out); out->x = q->x * scale; out->y = q->y * scale; @@ -199,7 +199,7 @@ void quat_scale(Quat* q, float scale, Quat* out) { out->w = q->w * scale; } -void quat_negtive(Quat* in, Quat* out) { +void internal_quat_negtive(Quat* in, Quat* out) { ssr_assert(in && out); out->w = -in->w; out->x = -in->x; @@ -207,7 +207,7 @@ void quat_negtive(Quat* in, Quat* out) { out->z = -in->z; } -void quat_devide(Quat* q, float k, Quat* out) { +void internal_quat_devide(Quat* q, float k, Quat* out) { ssr_assert(q && out); k = 1 / k; out->w = q->w * k; @@ -216,31 +216,31 @@ void quat_devide(Quat* q, float k, Quat* out) { out->z = q->z * k; } -void quat_invert(Quat* q, Quat* out) { +void internal_quat_invert(Quat* q, Quat* out) { ssr_assert(q && out); - float mag2 = quat_magnitude2(q); - quat_conjugate(q, out); - quat_devide(out, mag2, out); + float mag2 = internal_quat_magnitude2(q); + internal_quat_conjugate(q, out); + internal_quat_devide(out, mag2, out); /*实际上如果是单位四元数,共轭就是逆*/ } -bool quat_setlookrotation(Vec3* view, Vec3* up, Quat* out) { +bool internal_quat_setlookrotation(Vec3* view, Vec3* up, Quat* out) { ssr_assert(view && up && out); Mat4 m; - mat4_setlookrotation(view, up, &m); /*先以view为准构建正交矩阵*/ - mat4_toquat(&m, out); + internal_mat4_setlookrotation(view, up, &m); /*先以view为准构建正交矩阵*/ + internal_mat4_toquat(&m, out); return 1; } /*q1 * q2^-1*/ -void quat_minus(Quat* q1, Quat* q2, Quat* out) { +void internal_quat_minus(Quat* q1, Quat* q2, Quat* out) { ssr_assert(q1 && q2 && out); - Quat q2i; quat_invert(q2, &q2i); - quat_multiply(q1, &q2i, out); + Quat q2i; internal_quat_invert(q2, &q2i); + internal_quat_multiply(q1, &q2i, out); } /*q1*q2*/ -void quat_multiply(Quat* q1, Quat* q2, Quat* out) { +void internal_quat_multiply(Quat* q1, Quat* q2, Quat* out) { ssr_assert(q1 && q2 && out); float w1 = q1->w, x1 = q1->x, y1 = q1->y, z1 = q1->z, @@ -254,7 +254,7 @@ void quat_multiply(Quat* q1, Quat* q2, Quat* out) { /* ** 共轭的几何意义是颠倒旋转轴方向,代表了和q相反的角位移 */ -void quat_conjugate(Quat* in, Quat* out) { +void internal_quat_conjugate(Quat* in, Quat* out) { ssr_assert(in && out); out->w = in->w; out->x = -in->x; @@ -262,25 +262,25 @@ void quat_conjugate(Quat* in, Quat* out) { out->z = -in->z; } -float quat_dot(Quat* q1, Quat* q2) { +float internal_quat_dot(Quat* q1, Quat* q2) { ssr_assert(q1 && q2); return (q1->x*q2->x + q1->y*q2->y + q1->z*q2->z + q1->w*q2->w); } -float quat_magnitude(Quat* q) { +float internal_quat_magnitude(Quat* q) { ssr_assert(q); - return sqrt(quat_dot(q, q)); + return sqrt(internal_quat_dot(q, q)); } -float quat_magnitude2(Quat* q) { +float internal_quat_magnitude2(Quat* q) { ssr_assert(q); - return quat_dot(q, q); + return internal_quat_dot(q, q); } -void quat_slerp(Quat* q1, Quat* q2, float t, Quat* out) { +void internal_quat_slerp(Quat* q1, Quat* q2, float t, Quat* out) { ssr_assert(q1 && q2 && out); - ssr_assert(quat_isidentity(q1) && quat_isidentity(q2)); /*适用于单位四元数*/ - float dot = quat_dot(q1, q2); + ssr_assert(internal_quat_isidentity(q1) && internal_quat_isidentity(q2)); /*适用于单位四元数*/ + float dot = internal_quat_dot(q1, q2); Quat temp; if (dot < 0.0f) { dot = -dot; @@ -302,14 +302,14 @@ void quat_slerp(Quat* q1, Quat* q2, float t, Quat* out) { out->z = (q1->z*sinaomt + temp.z*sinat)*sinadiv; out->w = (q1->w*sinaomt + temp.w*sinat)*sinadiv; } else { /*小范围时使用lerp*/ - quat_lerp(q1, &temp, t, out); + internal_quat_lerp(q1, &temp, t, out); } } -void quat_lerp(Quat* q1, Quat* q2, float t, Quat* out) { +void internal_quat_lerp(Quat* q1, Quat* q2, float t, Quat* out) { ssr_assert(q1 && q2 && out); float t2 = 1 - t; - if (quat_dot(q1, q2) < 0.f) { /*点乘不能是负的,翻转一个四元数的符号,使得落回360°*/ + if (internal_quat_dot(q1, q2) < 0.f) { /*点乘不能是负的,翻转一个四元数的符号,使得落回360°*/ out->x = q1->x * t2 - t * q2->x; out->y = q1->y * t2 - t * q2->y; out->z = q1->z * t2 - t * q2->z; @@ -320,5 +320,5 @@ void quat_lerp(Quat* q1, Quat* q2, float t, Quat* out) { out->z = q1->z * t2 + t * q2->z; out->w = q1->w * t2 + t * q2->w; } - quat_normalize(out, out); + internal_quat_normalize(out, out); } diff --git a/src/math/vec2.c b/src/math/vec2.c index 152f965..dc964e7 100644 --- a/src/math/vec2.c +++ b/src/math/vec2.c @@ -4,35 +4,35 @@ Vec2 vec2zero = { 0, 0}; -void vec2_scale(Vec2* v, float k, Vec2* out) { +void internal_vec2_scale(Vec2* v, float k, Vec2* out) { ssr_assert(v && out); out->x = v->x * k; out->y = v->y * k; } -void vec2_plus(Vec2* v1, Vec2* v2, Vec2* out) { +void internal_vec2_plus(Vec2* v1, Vec2* v2, Vec2* out) { ssr_assert(v1 && v2 && out); out->x = v1->x + v2->x; out->y = v1->y + v2->y; } -void vec2_offset(Vec2* v, float offset, Vec2* out) { +void internal_vec2_offset(Vec2* v, float offset, Vec2* out) { ssr_assert(v && out); out->x = v->x + offset; out->y = v->y + offset; } -float vec2_dot(Vec2* v1, Vec2* v2) { +float internal_vec2_dot(Vec2* v1, Vec2* v2) { ssr_assert(v1 && v2); float d = v1->x * v2->x + v1->y * v2->y; return d; } -void vec2_tostring(Vec2* v, char buf[]) { +void internal_vec2_tostring(Vec2* v, char buf[]) { sprintf(buf, "%8.3f %8.3f", v->x, v->y); } -void vec2_print(Vec2* v) { - vec2_tostring(v, printbuffer); +void internal_vec2_print(Vec2* v) { + internal_vec2_tostring(v, printbuffer); printf("%s", printbuffer); }
\ No newline at end of file diff --git a/src/math/vec3.c b/src/math/vec3.c index df56f3f..e9c12a4 100644 --- a/src/math/vec3.c +++ b/src/math/vec3.c @@ -9,58 +9,58 @@ Vec3 vec3up = {0, 1, 0}; Vec3 vec3left = {1, 0, 0}; Vec3 vec3zero = {0, 0, 0}; -void vec3_cross(Vec3* v1, Vec3* v2, Vec3* out) { +void internal_vec3_cross(Vec3* v1, Vec3* v2, Vec3* out) { ssr_assert(v1 && v2 && out); out->x = v1->y*v2->z - v1->z*v2->y; out->y = v1->z*v2->x - v1->x*v2->z; out->z = v1->x*v2->y - v1->y*v2->x; } -Vec3 vec3_make(float x, float y, float z) { +Vec3 internal_vec3_make(float x, float y, float z) { Vec3 v = {x, y, z}; return v; } -void vec3_scale(Vec3* v, float k, Vec3* out) { +void internal_vec3_scale(Vec3* v, float k, Vec3* out) { ssr_assert(v && out); out->x = v->x * k; out->y = v->y * k; out->z = v->z * k; } -void vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out) { +void internal_vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out) { ssr_assert(v && scalar && out); out->x = v->x * scalar->x; out->y = v->y * scalar->y; out->z = v->z * scalar->z; } -void vec3_plus(Vec3* v1, Vec3* v2, Vec3* out) { +void internal_vec3_plus(Vec3* v1, Vec3* v2, Vec3* out) { ssr_assert(v1 && v2 && out); out->x = v1->x + v2->x; out->y = v1->y + v2->y; out->z = v1->z + v2->z; } -void vec3_offset(Vec3* v, float offset, Vec3* out) { +void internal_vec3_offset(Vec3* v, float offset, Vec3* out) { ssr_assert(v && out); out->x = v->x + offset; out->y = v->y + offset; out->z = v->z + offset; } -float vec3_magnitude(Vec3* v) { +float internal_vec3_magnitude(Vec3* v) { ssr_assert(v); - float l2 = vec3_magnitude2(v); + float l2 = internal_vec3_magnitude2(v); return sqrt(l2); } -float vec3_magnitude2(Vec3* v) { +float internal_vec3_magnitude2(Vec3* v) { ssr_assert(v); return v->x*v->x + v->y*v->y + v->z*v->z; } -void vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { +void internal_vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { ssr_assert(v1 && v2 && out); float t2 = 1 - t; out->x = v1->x * t2 + v2->x * t; @@ -68,49 +68,49 @@ void vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { out->z = v1->z * t2 + v2->z * t; } -void vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { +void internal_vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { ssr_assert(v1 && v2 && out); - float mag1 = vec3_magnitude(v1); - float mag2 = vec3_magnitude(v2); + float mag1 = internal_vec3_magnitude(v1); + float mag2 = internal_vec3_magnitude(v2); if (mag1 < EPSILON || mag2 < EPSILON) { - vec3_lerp(v1, v2, t, out); + internal_vec3_lerp(v1, v2, t, out); return; } float lerplen = lerp(mag1, mag2, t); /*分为长度上的lerp和角度上的slerp*/ - float dot = vec3_dot(v1, v2) / (mag1 * mag2); + float dot = internal_vec3_dot(v1, v2) / (mag1 * mag2); if (dot > 1.f - EPSILON) { /*几乎是同向*/ - vec3_lerp(v1, v2, t, out); + internal_vec3_lerp(v1, v2, t, out); return; } else if (dot < -1 + EPSILON) { /*几乎是反向*/ } else { - Vec3 axis; vec3_cross(v1, v2, &axis); vec3_normalize(&axis, &axis); - Vec3 v1n; vec3_normalize(v1, &v1n); + Vec3 axis; internal_vec3_cross(v1, v2, &axis); internal_vec3_normalize(&axis, &axis); + Vec3 v1n; internal_vec3_normalize(v1, &v1n); float angle = acos(dot) * t; - //Mat4 m; mat4_setaxisangle(&axis, angle, &m); - //mat4_mulvec4(&m, &v1n, &v1n); - Quat q; quat_fromaxisangle(&axis, angle, &q); - quat_applytovec3(&q, &v1n, &v1n); - vec3_scale(&v1n, lerplen, out); + //Mat4 m; internal_mat4_setaxisangle(&axis, angle, &m); + //internal_mat4_mulvec4(&m, &v1n, &v1n); + Quat q; internal_quat_fromaxisangle(&axis, angle, &q); + internal_quat_applytovec3(&q, &v1n, &v1n); + internal_vec3_scale(&v1n, lerplen, out); } } -float vec3_dot(Vec3* v1, Vec3* v2) { +float internal_vec3_dot(Vec3* v1, Vec3* v2) { ssr_assert(v1 && v2); return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z; } -void vec3_multiply(Vec3* v1, Vec3* v2, Quat* out) { +void internal_vec3_multiply(Vec3* v1, Vec3* v2, Quat* out) { ssr_assert(v1 && v2 && out); - vec3_cross(v1, v2, out); - out->w = -vec3_dot(v1, v2); + internal_vec3_cross(v1, v2, out); + out->w = -internal_vec3_dot(v1, v2); } -void vec3_normalize(Vec3* v, Vec3* out) { +void internal_vec3_normalize(Vec3* v, Vec3* out) { ssr_assert(v && out); //float mag = rsqrt(v->x * v->x + v->y * v->y + v->z * v->z); - float mag = vec3_magnitude(v); + float mag = internal_vec3_magnitude(v); if (compare(mag, 0)) { return; } @@ -120,16 +120,16 @@ void vec3_normalize(Vec3* v, Vec3* out) { out->z = v->z * mag; } -void vec3_tostring(Vec3* v, char buf[]) { +void internal_vec3_tostring(Vec3* v, char buf[]) { sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z); } -void vec3_print(Vec3* v) { - vec3_tostring(v, printbuffer); +void internal_vec3_print(Vec3* v) { + internal_vec3_tostring(v, printbuffer); printf("\n%s\n", printbuffer); } -void vec3_minus(Vec3* v1, Vec3* v2, Vec3* out) { +void internal_vec3_minus(Vec3* v1, Vec3* v2, Vec3* out) { ssr_assert(v1 && v2 && out); out->x = v1->x - v2->x; out->y = v1->y - v2->y; diff --git a/src/math/vec4.c b/src/math/vec4.c index 3d6df46..24df08f 100644 --- a/src/math/vec4.c +++ b/src/math/vec4.c @@ -8,7 +8,7 @@ Vec4 vec4zero = { 0, 0, 0,0 }; static Vec4 sharedmat4; -void vec4_dividew(Vec4* v, Vec3* out) { +void internal_vec4_dividew(Vec4* v, Vec3* out) { ssr_assert(out && v); float w = 1.f / v->w; out->x = v->x * w ; @@ -16,7 +16,7 @@ void vec4_dividew(Vec4* v, Vec3* out) { out->z = v->z * w; } -void vec4_dividewnoz(Vec4* v, Vec4* out) { +void internal_vec4_dividewnoz(Vec4* v, Vec4* out) { ssr_assert(out && v); float w = 1.f / v->w; out->x = v->x * w; @@ -25,30 +25,30 @@ void vec4_dividewnoz(Vec4* v, Vec4* out) { out->w = v->w; } -void vec4_tostring(Vec4* v, char buf[]) { +void internal_vec4_tostring(Vec4* v, char buf[]) { sprintf(buf, "%8.3f %8.3f %8.3f %8.3f", v->x, v->y, v->z, v->w); } -void vec4_print(Vec4* v) { - vec4_tostring(v, printbuffer); +void internal_vec4_print(Vec4* v) { + internal_vec4_tostring(v, printbuffer); printf("\n%s\n", printbuffer); } -void vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out) { +void internal_vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out) { out->x = lerp(a->x, b->x, t); out->y = lerp(a->y, b->y, t); out->z = lerp(a->z, b->z, t); out->w = lerp(a->w, b->w, t); } -void vec4_scale(Vec4* v, float t, Vec4* out) { +void internal_vec4_scale(Vec4* v, float t, Vec4* out) { out->x = v->x * t; out->y = v->y * t; out->z = v->z * t; out->w = v->w * t; } -void vec4_add(Vec4* v1, Vec4* v2, Vec4* out) { +void internal_vec4_add(Vec4* v1, Vec4* v2, Vec4* out) { out->x = v1->x + v2->x; out->y = v1->y + v2->y; out->z = v1->z + v2->z; |