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 inline void cgm_wcons(cgm_vec4 *v, float x, float y, float z, float w)
17 static inline void cgm_wadd(cgm_vec4 *a, const cgm_vec4 *b)
25 static inline void cgm_wsub(cgm_vec4 *a, const cgm_vec4 *b)
33 static inline void cgm_wmul(cgm_vec4 *a, const cgm_vec4 *b)
41 static inline void cgm_wscale(cgm_vec4 *v, float s)
49 static inline void cgm_wmul_m4v4(cgm_vec4 *v, const float *m)
51 float x = v->x * m[0] + v->y * m[4] + v->z * m[8] + v->w * m[12];
52 float y = v->x * m[1] + v->y * m[5] + v->z * m[9] + v->w * m[13];
53 float z = v->x * m[2] + v->y * m[6] + v->z * m[10] + v->w * m[14];
54 v->w = v->x * m[3] + v->y * m[7] + v->z * m[11] + v->w * m[15];
60 static inline void cgm_wmul_v4m4(cgm_vec4 *v, const float *m)
62 float x = v->x * m[0] + v->y * m[1] + v->z * m[2] + v->w * m[3];
63 float y = v->x * m[4] + v->y * m[5] + v->z * m[6] + v->w * m[7];
64 float z = v->x * m[8] + v->y * m[9] + v->z * m[10] + v->w * m[11];
65 v->w = v->x * m[12] + v->y * m[13] + v->z * m[14] + v->w * m[15];
71 static inline void cgm_wmul_m34v4(cgm_vec4 *v, const float *m)
73 float x = v->x * m[0] + v->y * m[4] + v->z * m[8] + v->w * m[12];
74 float y = v->x * m[1] + v->y * m[5] + v->z * m[9] + v->w * m[13];
75 v->z = v->x * m[2] + v->y * m[6] + v->z * m[10] + v->w * m[14];
80 static inline void cgm_wmul_v4m43(cgm_vec4 *v, const float *m)
82 float x = v->x * m[0] + v->y * m[1] + v->z * m[2] + v->w * m[3];
83 float y = v->x * m[4] + v->y * m[5] + v->z * m[6] + v->w * m[7];
84 v->z = v->x * m[8] + v->y * m[9] + v->z * m[10] + v->w * m[11];
89 static inline void cgm_wmul_m3v4(cgm_vec4 *v, const float *m)
91 float x = v->x * m[0] + v->y * m[4] + v->z * m[8];
92 float y = v->x * m[1] + v->y * m[5] + v->z * m[9];
93 v->z = v->x * m[2] + v->y * m[6] + v->z * m[10];
98 static inline void cgm_wmul_v4m3(cgm_vec4 *v, const float *m)
100 float x = v->x * m[0] + v->y * m[1] + v->z * m[2];
101 float y = v->x * m[4] + v->y * m[5] + v->z * m[6];
102 v->z = v->x * m[8] + v->y * m[9] + v->z * m[10];
107 static inline float cgm_wdot(const cgm_vec4 *a, const cgm_vec4 *b)
109 return a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w;
112 static inline float cgm_wlength(const cgm_vec4 *v)
114 return sqrt(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
117 static inline float cgm_wlength_sq(const cgm_vec4 *v)
119 return v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w;
122 static inline float cgm_wdist(const cgm_vec4 *a, const cgm_vec4 *b)
124 float dx = a->x - b->x;
125 float dy = a->y - b->y;
126 float dz = a->z - b->z;
127 float dw = a->w - b->w;
128 return sqrt(dx * dx + dy * dy + dz * dz + dw * dw);
131 static inline float cgm_wdist_sq(const cgm_vec4 *a, const cgm_vec4 *b)
133 float dx = a->x - b->x;
134 float dy = a->y - b->y;
135 float dz = a->z - b->z;
136 float dw = a->w - b->w;
137 return dx * dx + dy * dy + dz * dz + dw * dw;
140 static inline void cgm_wnormalize(cgm_vec4 *v)
142 float len = cgm_wlength(v);
144 float s = 1.0f / len;
152 static inline void cgm_wlerp(cgm_vec4 *res, const cgm_vec4 *a, const cgm_vec4 *b, float t)
154 res->x = a->x + (b->x - a->x) * t;
155 res->y = a->y + (b->y - a->y) * t;
156 res->z = a->z + (b->z - a->z) * t;
157 res->w = a->w + (b->w - a->w) * t;