1 /* gph-cmath - C graphics math library
2 * Copyright (C) 2018 John Tsiombikas <nuclear@member.fsf.org>
4 * This program is free software. Feel free to use, modify, and/or redistribute
5 * it under the terms of the MIT/X11 license. See LICENSE for details.
6 * If you intend to redistribute parts of the code without the LICENSE file
7 * replace this paragraph with the full contents of the LICENSE file.
9 static CGM_INLINE void cgm_wcons(cgm_vec4 *v, float x, float y, float z, float w)
17 static CGM_INLINE cgm_vec4 cgm_wvec(float x, float y, float z, float w)
27 static CGM_INLINE void cgm_wadd(cgm_vec4 *a, const cgm_vec4 *b)
35 static CGM_INLINE void cgm_wsub(cgm_vec4 *a, const cgm_vec4 *b)
43 static CGM_INLINE void cgm_wmul(cgm_vec4 *a, const cgm_vec4 *b)
51 static CGM_INLINE void cgm_wscale(cgm_vec4 *v, float s)
59 static CGM_INLINE void cgm_wmul_m4v4(cgm_vec4 *v, const float *m)
61 float x = v->x * m[0] + v->y * m[4] + v->z * m[8] + v->w * m[12];
62 float y = v->x * m[1] + v->y * m[5] + v->z * m[9] + v->w * m[13];
63 float z = v->x * m[2] + v->y * m[6] + v->z * m[10] + v->w * m[14];
64 v->w = v->x * m[3] + v->y * m[7] + v->z * m[11] + v->w * m[15];
70 static CGM_INLINE void cgm_wmul_v4m4(cgm_vec4 *v, const float *m)
72 float x = v->x * m[0] + v->y * m[1] + v->z * m[2] + v->w * m[3];
73 float y = v->x * m[4] + v->y * m[5] + v->z * m[6] + v->w * m[7];
74 float z = v->x * m[8] + v->y * m[9] + v->z * m[10] + v->w * m[11];
75 v->w = v->x * m[12] + v->y * m[13] + v->z * m[14] + v->w * m[15];
81 static CGM_INLINE void cgm_wmul_m34v4(cgm_vec4 *v, const float *m)
83 float x = v->x * m[0] + v->y * m[4] + v->z * m[8] + v->w * m[12];
84 float y = v->x * m[1] + v->y * m[5] + v->z * m[9] + v->w * m[13];
85 v->z = v->x * m[2] + v->y * m[6] + v->z * m[10] + v->w * m[14];
90 static CGM_INLINE void cgm_wmul_v4m43(cgm_vec4 *v, const float *m)
92 float x = v->x * m[0] + v->y * m[1] + v->z * m[2] + v->w * m[3];
93 float y = v->x * m[4] + v->y * m[5] + v->z * m[6] + v->w * m[7];
94 v->z = v->x * m[8] + v->y * m[9] + v->z * m[10] + v->w * m[11];
99 static CGM_INLINE void cgm_wmul_m3v4(cgm_vec4 *v, const float *m)
101 float x = v->x * m[0] + v->y * m[4] + v->z * m[8];
102 float y = v->x * m[1] + v->y * m[5] + v->z * m[9];
103 v->z = v->x * m[2] + v->y * m[6] + v->z * m[10];
108 static CGM_INLINE void cgm_wmul_v4m3(cgm_vec4 *v, const float *m)
110 float x = v->x * m[0] + v->y * m[1] + v->z * m[2];
111 float y = v->x * m[4] + v->y * m[5] + v->z * m[6];
112 v->z = v->x * m[8] + v->y * m[9] + v->z * m[10];
117 static CGM_INLINE float cgm_wdot(const cgm_vec4 *a, const cgm_vec4 *b)
119 return a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w;
122 static CGM_INLINE float cgm_wlength(const cgm_vec4 *v)
124 return sqrt(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
127 static CGM_INLINE float cgm_wlength_sq(const cgm_vec4 *v)
129 return v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w;
132 static CGM_INLINE float cgm_wdist(const cgm_vec4 *a, const cgm_vec4 *b)
134 float dx = a->x - b->x;
135 float dy = a->y - b->y;
136 float dz = a->z - b->z;
137 float dw = a->w - b->w;
138 return sqrt(dx * dx + dy * dy + dz * dz + dw * dw);
141 static CGM_INLINE float cgm_wdist_sq(const cgm_vec4 *a, const cgm_vec4 *b)
143 float dx = a->x - b->x;
144 float dy = a->y - b->y;
145 float dz = a->z - b->z;
146 float dw = a->w - b->w;
147 return dx * dx + dy * dy + dz * dz + dw * dw;
150 static CGM_INLINE void cgm_wnormalize(cgm_vec4 *v)
152 float len = cgm_wlength(v);
154 float s = 1.0f / len;
162 static CGM_INLINE void cgm_wlerp(cgm_vec4 *res, const cgm_vec4 *a, const cgm_vec4 *b, float t)
164 res->x = a->x + (b->x - a->x) * t;
165 res->y = a->y + (b->y - a->y) * t;
166 res->z = a->z + (b->z - a->z) * t;
167 res->w = a->w + (b->w - a->w) * t;