From: John Tsiombikas Date: Thu, 26 Dec 2019 09:42:37 +0000 (+0200) Subject: updated cgmath X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=commitdiff_plain;h=0d5155d4bb78c145268a4b1d2a327fc5e14eb0ef updated cgmath --- diff --git a/libs/cgmath/cgmath.h b/libs/cgmath/cgmath.h index 8e6871d..5dc7e13 100644 --- a/libs/cgmath/cgmath.h +++ b/libs/cgmath/cgmath.h @@ -31,6 +31,10 @@ #endif typedef struct { + float x, y; +} cgm_vec2; + +typedef struct { float x, y, z; } cgm_vec3; @@ -91,6 +95,8 @@ static inline void cgm_vrotate_euler(cgm_vec3 *v, float a, float b, float c, enu static inline void cgm_vlerp(cgm_vec3 *res, const cgm_vec3 *a, const cgm_vec3 *b, float t); +#define cgm_velem(vptr, idx) ((&(vptr)->x)[idx]) + /* --- operations on cgm_vec4 --- */ static inline void cgm_wcons(cgm_vec4 *v, float x, float y, float z, float w); @@ -106,6 +112,8 @@ static inline void cgm_wmul_v4m43(cgm_vec4 *v, const float *m); /* doesn't affec static inline void cgm_wmul_m3v4(cgm_vec4 *v, const float *m); /* (m still 16 floats) */ static inline void cgm_wmul_v4m3(cgm_vec4 *v, const float *m); /* (m still 16 floats) */ +static inline float cgm_wdot(const cgm_vec4 *a, const cgm_vec4 *b); + static inline float cgm_wlength(const cgm_vec4 *v); static inline float cgm_wlength_sq(const cgm_vec4 *v); static inline float cgm_wdist(const cgm_vec4 *a, const cgm_vec4 *b); @@ -114,6 +122,8 @@ static inline void cgm_wnormalize(cgm_vec4 *v); static inline void cgm_wlerp(cgm_vec4 *res, const cgm_vec4 *a, const cgm_vec4 *b, float t); +#define cgm_welem(vptr, idx) ((&(vptr)->x)[idx]) + /* --- operations on quaternions --- */ static inline void cgm_qcons(cgm_quat *q, float x, float y, float z, float w); @@ -128,12 +138,14 @@ static inline void cgm_qnormalize(cgm_quat *q); static inline void cgm_qconjugate(cgm_quat *q); static inline void cgm_qinvert(cgm_quat *q); -static inline void cgm_qrotation(cgm_quat *q, const cgm_vec3 *axis, float angle); -static inline void cgm_qrotate(cgm_quat *q, const cgm_vec3 *axis, float angle); +static inline void cgm_qrotation(cgm_quat *q, float angle, float x, float y, float z); +static inline void cgm_qrotate(cgm_quat *q, float angle, float x, float y, float z); static inline void cgm_qslerp(cgm_quat *res, const cgm_quat *a, const cgm_quat *b, float t); static inline void cgm_qlerp(cgm_quat *res, const cgm_quat *a, const cgm_quat *b, float t); +#define cgm_qelem(qptr, idx) ((&(qptr)->x)[idx]) + /* --- operations on matrices --- */ static inline void cgm_mcopy(float *dest, const float *src); static inline void cgm_mzero(float *m); @@ -214,6 +226,8 @@ static inline float cgm_rad_to_deg(float rad); static inline float cgm_smoothstep(float a, float b, float x); static inline float cgm_lerp(float a, float b, float t); static inline float cgm_bezier(float a, float b, float c, float d, float t); +static inline float cgm_bspline(float a, float b, float c, float d, float t); +static inline float cgm_spline(float a, float b, float c, float d, float t); static inline void cgm_discrand(cgm_vec3 *v, float rad); static inline void cgm_sphrand(cgm_vec3 *v, float rad); diff --git a/libs/cgmath/cgmmisc.inl b/libs/cgmath/cgmmisc.inl index 6cfa37d..e4360f6 100644 --- a/libs/cgmath/cgmmisc.inl +++ b/libs/cgmath/cgmmisc.inl @@ -43,6 +43,42 @@ static inline float cgm_bezier(float a, float b, float c, float d, float t) return (a * omt3) + (b * f * omt) + (c * f * t) + (d * t3); } +static inline float cgm_bspline(float a, float b, float c, float d, float t) +{ + static const float mat[] = { + -1, 3, -3, 1, + 3, -6, 0, 4, + -3, 3, 3, 1, + 1, 0, 0, 0 + }; + cgm_vec4 tmp, qfact; + float tsq = t * t; + + cgm_wcons(&qfact, tsq * t, tsq, t, 1.0f); + cgm_wcons(&tmp, a, b, c, d); + cgm_wmul_m4v4(&tmp, mat); + cgm_wscale(&tmp, 1.0f / 6.0f); + return cgm_wdot(&tmp, &qfact); +} + +static inline float cgm_spline(float a, float b, float c, float d, float t) +{ + static const float mat[] = { + -1, 2, -1, 0, + 3, -5, 0, 2, + -3, 4, 1, 0, + 1, -1, 0, 0 + }; + cgm_vec4 tmp, qfact; + float tsq = t * t; + + cgm_wcons(&qfact, tsq * t, tsq, t, 1.0f); + cgm_wcons(&tmp, a, b, c, d); + cgm_wmul_m4v4(&tmp, mat); + cgm_wscale(&tmp, 1.0f / 6.0f); + return cgm_wdot(&tmp, &qfact); +} + static inline void cgm_discrand(cgm_vec3 *pt, float rad) { float theta = 2.0f * M_PI * (float)rand() / RAND_MAX; diff --git a/libs/cgmath/cgmquat.inl b/libs/cgmath/cgmquat.inl index 4b29fd2..743d818 100644 --- a/libs/cgmath/cgmquat.inl +++ b/libs/cgmath/cgmquat.inl @@ -98,20 +98,20 @@ static inline void cgm_qinvert(cgm_quat *q) } } -static inline void cgm_qrotation(cgm_quat *q, const cgm_vec3 *axis, float angle) +static inline void cgm_qrotation(cgm_quat *q, float angle, float x, float y, float z) { float hangle = angle * 0.5f; float sin_ha = sin(hangle); q->w = cos(hangle); - q->x = axis->x * sin_ha; - q->y = axis->y * sin_ha; - q->z = axis->z * sin_ha; + q->x = x * sin_ha; + q->y = y * sin_ha; + q->z = z * sin_ha; } -static inline void cgm_qrotate(cgm_quat *q, const cgm_vec3 *axis, float angle) +static inline void cgm_qrotate(cgm_quat *q, float angle, float x, float y, float z) { cgm_quat qrot; - cgm_qrotation(&qrot, axis, angle); + cgm_qrotation(&qrot, angle, x, y, z); cgm_qmul(q, &qrot); } diff --git a/libs/cgmath/cgmvec3.inl b/libs/cgmath/cgmvec3.inl index 88211c4..3747029 100644 --- a/libs/cgmath/cgmvec3.inl +++ b/libs/cgmath/cgmvec3.inl @@ -155,8 +155,8 @@ static inline void cgm_vrotate_quat(cgm_vec3 *v, const cgm_quat *q) cgm_qcons(&vq, v->x, v->y, v->z, 0.0f); cgm_qinvert(&inv_q); cgm_qmul(&tmp_q, &vq); - cgm_qmul(&vq, &inv_q); - cgm_vcons(v, vq.x, vq.y, vq.z); + cgm_qmul(&tmp_q, &inv_q); + cgm_vcons(v, tmp_q.x, tmp_q.y, tmp_q.z); } static inline void cgm_vrotate_axis(cgm_vec3 *v, int axis, float angle) diff --git a/libs/cgmath/cgmvec4.inl b/libs/cgmath/cgmvec4.inl index 1c143c2..b68856c 100644 --- a/libs/cgmath/cgmvec4.inl +++ b/libs/cgmath/cgmvec4.inl @@ -104,6 +104,11 @@ static inline void cgm_wmul_v4m3(cgm_vec4 *v, const float *m) v->y = y; } +static inline float cgm_wdot(const cgm_vec4 *a, const cgm_vec4 *b) +{ + return a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w; +} + static inline float cgm_wlength(const cgm_vec4 *v) { return sqrt(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);