added scr_lvled, a bunch of libraries, and improved framework code
[raydungeon] / libs / cgmath / cgmvec3.inl
1 /* gph-cmath - C graphics math library
2  * Copyright (C) 2018 John Tsiombikas <nuclear@member.fsf.org>
3  *
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.
8  */
9 static CGM_INLINE void cgm_vcons(cgm_vec3 *v, float x, float y, float z)
10 {
11         v->x = x;
12         v->y = y;
13         v->z = z;
14 }
15
16 static CGM_INLINE cgm_vec3 cgm_vvec(float x, float y, float z)
17 {
18         cgm_vec3 v;
19         v.x = x;
20         v.y = y;
21         v.z = z;
22         return v;
23 }
24
25 static CGM_INLINE void cgm_vadd(cgm_vec3 *a, const cgm_vec3 *b)
26 {
27         a->x += b->x;
28         a->y += b->y;
29         a->z += b->z;
30 }
31
32 static CGM_INLINE void cgm_vadd_scaled(cgm_vec3 *a, const cgm_vec3 *b, float s)
33 {
34         a->x += b->x * s;
35         a->y += b->y * s;
36         a->z += b->z * s;
37 }
38
39 static CGM_INLINE void cgm_vsub(cgm_vec3 *a, const cgm_vec3 *b)
40 {
41         a->x -= b->x;
42         a->y -= b->y;
43         a->z -= b->z;
44 }
45
46 static CGM_INLINE void cgm_vsub_scaled(cgm_vec3 *a, const cgm_vec3 *b, float s)
47 {
48         a->x -= b->x * s;
49         a->y -= b->y * s;
50         a->z -= b->z * s;
51 }
52
53 static CGM_INLINE void cgm_vmul(cgm_vec3 *a, const cgm_vec3 *b)
54 {
55         a->x *= b->x;
56         a->y *= b->y;
57         a->z *= b->z;
58 }
59
60 static CGM_INLINE void cgm_vscale(cgm_vec3 *v, float s)
61 {
62         v->x *= s;
63         v->y *= s;
64         v->z *= s;
65 }
66
67 static CGM_INLINE void cgm_vmul_m4v3(cgm_vec3 *v, const float *m)
68 {
69         float x = v->x * m[0] + v->y * m[4] + v->z * m[8] + m[12];
70         float y = v->x * m[1] + v->y * m[5] + v->z * m[9] + m[13];
71         v->z = v->x * m[2] + v->y * m[6] + v->z * m[10] + m[14];
72         v->x = x;
73         v->y = y;
74 }
75
76 static CGM_INLINE void cgm_vmul_v3m4(cgm_vec3 *v, const float *m)
77 {
78         float x = v->x * m[0] + v->y * m[1] + v->z * m[2] + m[3];
79         float y = v->x * m[4] + v->y * m[5] + v->z * m[6] + m[7];
80         v->z = v->x * m[8] + v->y * m[9] + v->z * m[10] + m[11];
81         v->x = x;
82         v->y = y;
83 }
84
85 static CGM_INLINE void cgm_vmul_m3v3(cgm_vec3 *v, const float *m)
86 {
87         float x = v->x * m[0] + v->y * m[4] + v->z * m[8];
88         float y = v->x * m[1] + v->y * m[5] + v->z * m[9];
89         v->z = v->x * m[2] + v->y * m[6] + v->z * m[10];
90         v->x = x;
91         v->y = y;
92 }
93
94 static CGM_INLINE void cgm_vmul_v3m3(cgm_vec3 *v, const float *m)
95 {
96         float x = v->x * m[0] + v->y * m[1] + v->z * m[2];
97         float y = v->x * m[4] + v->y * m[5] + v->z * m[6];
98         v->z = v->x * m[8] + v->y * m[9] + v->z * m[10];
99         v->x = x;
100         v->y = y;
101 }
102
103 static CGM_INLINE float cgm_vdot(const cgm_vec3 *a, const cgm_vec3 *b)
104 {
105         return a->x * b->x + a->y * b->y + a->z * b->z;
106 }
107
108 static CGM_INLINE void cgm_vcross(cgm_vec3 *res, const cgm_vec3 *a, const cgm_vec3 *b)
109 {
110         res->x = a->y * b->z - a->z * b->y;
111         res->y = a->z * b->x - a->x * b->z;
112         res->z = a->x * b->y - a->y * b->x;
113 }
114
115 static CGM_INLINE float cgm_vlength(const cgm_vec3 *v)
116 {
117         return sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
118 }
119
120 static CGM_INLINE float cgm_vlength_sq(const cgm_vec3 *v)
121 {
122         return v->x * v->x + v->y * v->y + v->z * v->z;
123 }
124
125 static CGM_INLINE float cgm_vdist(const cgm_vec3 *a, const cgm_vec3 *b)
126 {
127         float dx = a->x - b->x;
128         float dy = a->y - b->y;
129         float dz = a->z - b->z;
130         return sqrt(dx * dx + dy * dy + dz * dz);
131 }
132
133 static CGM_INLINE float cgm_vdist_sq(const cgm_vec3 *a, const cgm_vec3 *b)
134 {
135         float dx = a->x - b->x;
136         float dy = a->y - b->y;
137         float dz = a->z - b->z;
138         return dx * dx + dy * dy + dz * dz;
139 }
140
141 static CGM_INLINE void cgm_vnormalize(cgm_vec3 *v)
142 {
143         float len = cgm_vlength(v);
144         if(len != 0.0f) {
145                 float s = 1.0f / len;
146                 v->x *= s;
147                 v->y *= s;
148                 v->z *= s;
149         }
150 }
151
152 static CGM_INLINE void cgm_vreflect(cgm_vec3 *v, const cgm_vec3 *n)
153 {
154         float ndotv2 = cgm_vdot(v, n) * 2.0f;
155         v->x -= n->x * ndotv2;
156         v->y -= n->y * ndotv2;
157         v->z -= n->z * ndotv2;
158 }
159
160 static CGM_INLINE void cgm_vrefract(cgm_vec3 *v, const cgm_vec3 *n, float ior)
161 {
162         float ndotv = cgm_vdot(v, n);
163         float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
164         if(k < 0.0f) {
165                 cgm_vreflect(v, n);     /* TIR */
166         } else {
167                 float sqrt_k = sqrt(k);
168                 v->x = ior * v->x - (ior * ndotv + sqrt_k) * n->x;
169                 v->y = ior * v->y - (ior * ndotv + sqrt_k) * n->y;
170                 v->z = ior * v->z - (ior * ndotv + sqrt_k) * n->z;
171         }
172 }
173
174 static CGM_INLINE void cgm_vrotate_quat(cgm_vec3 *v, const cgm_quat *q)
175 {
176         cgm_quat vq, inv_q = *q, tmp_q = *q;
177
178         cgm_qcons(&vq, v->x, v->y, v->z, 0.0f);
179         cgm_qinvert(&inv_q);
180         cgm_qmul(&tmp_q, &vq);
181         cgm_qmul(&tmp_q, &inv_q);
182         cgm_vcons(v, tmp_q.x, tmp_q.y, tmp_q.z);
183 }
184
185 static CGM_INLINE void cgm_vrotate_axis(cgm_vec3 *v, int axis, float angle)
186 {
187         float m[16];
188         cgm_mrotation_axis(m, axis, angle);
189         cgm_vmul_m3v3(v, m);
190 }
191
192 static CGM_INLINE void cgm_vrotate(cgm_vec3 *v, float angle, float x, float y, float z)
193 {
194         float m[16];
195         cgm_mrotation(m, angle, x, y, z);
196         cgm_vmul_m3v3(v, m);
197 }
198
199 static CGM_INLINE void cgm_vrotate_euler(cgm_vec3 *v, float a, float b, float c, enum cgm_euler_mode mode)
200 {
201         float m[16];
202         cgm_mrotation_euler(m, a, b, c, mode);
203         cgm_vmul_m3v3(v, m);
204 }
205
206 static CGM_INLINE void cgm_vlerp(cgm_vec3 *res, const cgm_vec3 *a, const cgm_vec3 *b, float t)
207 {
208         res->x = a->x + (b->x - a->x) * t;
209         res->y = a->y + (b->y - a->y) * t;
210         res->z = a->z + (b->z - a->z) * t;
211 }