vec4.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*
  8. Macros:
  9. GLMS_VEC4_ONE_INIT
  10. GLMS_VEC4_BLACK_INIT
  11. GLMS_VEC4_ZERO_INIT
  12. GLMS_VEC4_ONE
  13. GLMS_VEC4_BLACK
  14. GLMS_VEC4_ZERO
  15. Functions:
  16. CGLM_INLINE vec4s glms_vec4(vec3s v3, float last);
  17. CGLM_INLINE vec3s glms_vec4_copy3(vec4s v);
  18. CGLM_INLINE vec4s glms_vec4_copy(vec4s v);
  19. CGLM_INLINE vec4s glms_vec4_ucopy(vec4s v);
  20. CGLM_INLINE void glms_vec4_pack(vec4s dst[], vec4 src[], size_t len);
  21. CGLM_INLINE void glms_vec4_unpack(vec4 dst[], vec4s src[], size_t len);
  22. CGLM_INLINE float glms_vec4_dot(vec4s a, vec4s b);
  23. CGLM_INLINE float glms_vec4_norm2(vec4s v);
  24. CGLM_INLINE float glms_vec4_norm(vec4s v);
  25. CGLM_INLINE float glms_vec4_norm_one(vec4s v);
  26. CGLM_INLINE float glms_vec4_norm_inf(vec4s v);
  27. CGLM_INLINE vec4s glms_vec4_add(vec4s a, vec4s b);
  28. CGLM_INLINE vec4s glms_vec4_adds(vec4s v, float s);
  29. CGLM_INLINE vec4s glms_vec4_sub(vec4s a, vec4s b);
  30. CGLM_INLINE vec4s glms_vec4_subs(vec4s v, float s);
  31. CGLM_INLINE vec4s glms_vec4_mul(vec4s a, vec4s b);
  32. CGLM_INLINE vec4s glms_vec4_scale(vec4s v, float s);
  33. CGLM_INLINE vec4s glms_vec4_scale_as(vec4s v, float s);
  34. CGLM_INLINE vec4s glms_vec4_div(vec4s a, vec4s b);
  35. CGLM_INLINE vec4s glms_vec4_divs(vec4s v, float s);
  36. CGLM_INLINE vec4s glms_vec4_addadd(vec4s a, vec4s b, vec4s dest);
  37. CGLM_INLINE vec4s glms_vec4_subadd(vec4s a, vec4s b, vec4s dest);
  38. CGLM_INLINE vec4s glms_vec4_muladd(vec4s a, vec4s b, vec4s dest);
  39. CGLM_INLINE vec4s glms_vec4_muladds(vec4s a, float s, vec4s dest);
  40. CGLM_INLINE vec4s glms_vec4_maxadd(vec4s a, vec4s b, vec4s dest);
  41. CGLM_INLINE vec4s glms_vec4_minadd(vec4s a, vec4s b, vec4s dest);
  42. CGLM_INLINE vec4s glms_vec4_subsub(vec4s a, vec4s b, vec4s dest);
  43. CGLM_INLINE vec4s glms_vec4_addsub(vec4s a, vec4s b, vec4s dest);
  44. CGLM_INLINE vec4s glms_vec4_mulsub(vec4s a, vec4s b, vec4s dest);
  45. CGLM_INLINE vec4s glms_vec4_mulsubs(vec4s a, float s, vec4s dest);
  46. CGLM_INLINE vec4s glms_vec4_maxsub(vec4s a, vec4s b, vec4s dest);
  47. CGLM_INLINE vec4s glms_vec4_minsub(vec4s a, vec4s b, vec4s dest);
  48. CGLM_INLINE vec4s glms_vec4_negate(vec4s v);
  49. CGLM_INLINE vec4s glms_vec4_normalize(vec4s v);
  50. CGLM_INLINE float glms_vec4_distance(vec4s a, vec4s b);
  51. CGLM_INLINE float glms_vec4_distance2(vec4s a, vec4s b);
  52. CGLM_INLINE vec4s glms_vec4_maxv(vec4s a, vec4s b);
  53. CGLM_INLINE vec4s glms_vec4_minv(vec4s a, vec4s b);
  54. CGLM_INLINE vec4s glms_vec4_clamp(vec4s v, float minVal, float maxVal);
  55. CGLM_INLINE vec4s glms_vec4_lerp(vec4s from, vec4s to, float t);
  56. CGLM_INLINE vec4s glms_vec4_lerpc(vec4s from, vec4s to, float t);
  57. CGLM_INLINE vec4s glms_vec4_mix(vec4s from, vec4s to, float t);
  58. CGLM_INLINE vec4s glms_vec4_mixc(vec4s from, vec4s to, float t);
  59. CGLM_INLINE vec4s glms_vec4_step(vec4s edge, vec4s x);
  60. CGLM_INLINE vec4s glms_vec4_smoothstep_uni(float edge0, float edge1, vec4s x);
  61. CGLM_INLINE vec4s glms_vec4_smoothstep(vec4s edge0, vec4s edge1, vec4s x);
  62. CGLM_INLINE vec4s glms_vec4_smoothinterp(vec4s from, vec4s to, float t);
  63. CGLM_INLINE vec4s glms_vec4_smoothinterpc(vec4s from, vec4s to, float t);
  64. CGLM_INLINE vec4s glms_vec4_cubic(float s);
  65. CGLM_INLINE vec4s glms_vec4_swizzle(vec4s v, int mask);
  66. CGLM_INLINE vec4s glms_vec4_make(float * restrict src);
  67. CGLM_INLINE vec4s glms_vec4_reflect(vec4s v, vec4s n);
  68. CGLM_INLINE bool glms_vec4_refract(vec4s v, vec4s n, float eta, vec4s *dest)
  69. Deprecated:
  70. glms_vec4_step_uni --> use glms_vec4_steps
  71. */
  72. #ifndef cglms_vec4s_h
  73. #define cglms_vec4s_h
  74. #include "../common.h"
  75. #include "../types-struct.h"
  76. #include "../util.h"
  77. #include "../vec4.h"
  78. #include "vec4-ext.h"
  79. /* DEPRECATED! */
  80. #define glms_vec4_step_uni(edge, x) glms_vec4_steps(edge, x)
  81. #define GLMS_VEC4_ONE_INIT {GLM_VEC4_ONE_INIT}
  82. #define GLMS_VEC4_BLACK_INIT {GLM_VEC4_BLACK_INIT}
  83. #define GLMS_VEC4_ZERO_INIT {GLM_VEC4_ZERO_INIT}
  84. #define GLMS_VEC4_ONE ((vec4s)GLM_VEC4_ONE_INIT)
  85. #define GLMS_VEC4_BLACK ((vec4s)GLM_VEC4_BLACK_INIT)
  86. #define GLMS_VEC4_ZERO ((vec4s)GLM_VEC4_ZERO_INIT)
  87. /*!
  88. * @brief init vec4 using vec3
  89. *
  90. * @param[in] v3 vector3
  91. * @param[in] last last item
  92. * @returns destination
  93. */
  94. CGLM_INLINE
  95. vec4s
  96. glms_vec4(vec3s v3, float last) {
  97. vec4s r;
  98. glm_vec4(v3.raw, last, r.raw);
  99. return r;
  100. }
  101. /*!
  102. * @brief copy first 3 members of [a] to [dest]
  103. *
  104. * @param[in] v source
  105. * @returns vec3
  106. */
  107. CGLM_INLINE
  108. vec3s
  109. glms_vec4_(copy3)(vec4s v) {
  110. vec3s r;
  111. glm_vec4_copy3(v.raw, r.raw);
  112. return r;
  113. }
  114. /*!
  115. * @brief copy all members of [a] to [dest]
  116. *
  117. * @param[in] v source
  118. * @returns destination
  119. */
  120. CGLM_INLINE
  121. vec4s
  122. glms_vec4_(copy)(vec4s v) {
  123. vec4s r;
  124. glm_vec4_copy(v.raw, r.raw);
  125. return r;
  126. }
  127. /*!
  128. * @brief copy all members of [a] to [dest]
  129. *
  130. * alignment is not required
  131. *
  132. * @param[in] v source
  133. * @returns destination
  134. */
  135. CGLM_INLINE
  136. vec4s
  137. glms_vec4_(ucopy)(vec4s v) {
  138. vec4s r;
  139. glm_vec4_ucopy(v.raw, r.raw);
  140. return r;
  141. }
  142. /*!
  143. * @brief pack an array of vec4 into an array of vec4s
  144. *
  145. * @param[out] dst array of vec4
  146. * @param[in] src array of vec4s
  147. * @param[in] len number of elements
  148. */
  149. CGLM_INLINE
  150. void
  151. glms_vec4_(pack)(vec4s dst[], vec4 src[], size_t len) {
  152. size_t i;
  153. for (i = 0; i < len; i++) {
  154. glm_vec4_copy(src[i], dst[i].raw);
  155. }
  156. }
  157. /*!
  158. * @brief unpack an array of vec4s into an array of vec4
  159. *
  160. * @param[out] dst array of vec4s
  161. * @param[in] src array of vec4
  162. * @param[in] len number of elements
  163. */
  164. CGLM_INLINE
  165. void
  166. glms_vec4_(unpack)(vec4 dst[], vec4s src[], size_t len) {
  167. size_t i;
  168. for (i = 0; i < len; i++) {
  169. glm_vec4_copy(src[i].raw, dst[i]);
  170. }
  171. }
  172. /*!
  173. * @brief make vector zero
  174. *
  175. * @returns zero vector
  176. */
  177. CGLM_INLINE
  178. vec4s
  179. glms_vec4_(zero)(void) {
  180. vec4s r;
  181. glm_vec4_zero(r.raw);
  182. return r;
  183. }
  184. /*!
  185. * @brief make vector one
  186. *
  187. * @returns one vector
  188. */
  189. CGLM_INLINE
  190. vec4s
  191. glms_vec4_(one)(void) {
  192. vec4s r;
  193. glm_vec4_one(r.raw);
  194. return r;
  195. }
  196. /*!
  197. * @brief vec4 dot product
  198. *
  199. * @param[in] a vector1
  200. * @param[in] b vector2
  201. *
  202. * @return dot product
  203. */
  204. CGLM_INLINE
  205. float
  206. glms_vec4_(dot)(vec4s a, vec4s b) {
  207. return glm_vec4_dot(a.raw, b.raw);
  208. }
  209. /*!
  210. * @brief norm * norm (magnitude) of vec
  211. *
  212. * we can use this func instead of calling norm * norm, because it would call
  213. * sqrtf function twice but with this func we can avoid func call, maybe this is
  214. * not good name for this func
  215. *
  216. * @param[in] v vec4
  217. *
  218. * @return norm * norm
  219. */
  220. CGLM_INLINE
  221. float
  222. glms_vec4_(norm2)(vec4s v) {
  223. return glm_vec4_norm2(v.raw);
  224. }
  225. /*!
  226. * @brief norm (magnitude) of vec4
  227. *
  228. * @param[in] v vector
  229. *
  230. * @return norm
  231. */
  232. CGLM_INLINE
  233. float
  234. glms_vec4_(norm)(vec4s v) {
  235. return glm_vec4_norm(v.raw);
  236. }
  237. /*!
  238. * @brief L1 norm of vec4
  239. * Also known as Manhattan Distance or Taxicab norm.
  240. * L1 Norm is the sum of the magnitudes of the vectors in a space.
  241. * It is calculated as the sum of the absolute values of the vector components.
  242. * In this norm, all the components of the vector are weighted equally.
  243. *
  244. * This computes:
  245. * R = |v[0]| + |v[1]| + |v[2]| + |v[3]|
  246. *
  247. * @param[in] v vector
  248. *
  249. * @return L1 norm
  250. */
  251. CGLM_INLINE
  252. float
  253. glms_vec4_(norm_one)(vec4s v) {
  254. return glm_vec4_norm_one(v.raw);
  255. }
  256. /*!
  257. * @brief Infinity norm of vec4
  258. * Also known as Maximum norm.
  259. * Infinity Norm is the largest magnitude among each element of a vector.
  260. * It is calculated as the maximum of the absolute values of the vector components.
  261. *
  262. * This computes:
  263. * inf norm = max(|v[0]|, |v[1]|, |v[2]|, |v[3]|)
  264. *
  265. * @param[in] v vector
  266. *
  267. * @return Infinity norm
  268. */
  269. CGLM_INLINE
  270. float
  271. glms_vec4_(norm_inf)(vec4s v) {
  272. return glm_vec4_norm_inf(v.raw);
  273. }
  274. /*!
  275. * @brief add b vector to a vector store result in dest
  276. *
  277. * @param[in] a vector1
  278. * @param[in] b vector2
  279. * @returns destination vector
  280. */
  281. CGLM_INLINE
  282. vec4s
  283. glms_vec4_(add)(vec4s a, vec4s b) {
  284. vec4s r;
  285. glm_vec4_add(a.raw, b.raw, r.raw);
  286. return r;
  287. }
  288. /*!
  289. * @brief add scalar to v vector store result in dest (d = v + vec(s))
  290. *
  291. * @param[in] v vector
  292. * @param[in] s scalar
  293. * @returns destination vector
  294. */
  295. CGLM_INLINE
  296. vec4s
  297. glms_vec4_(adds)(vec4s v, float s) {
  298. vec4s r;
  299. glm_vec4_adds(v.raw, s, r.raw);
  300. return r;
  301. }
  302. /*!
  303. * @brief subtract b vector from a vector store result in dest (d = a - b)
  304. *
  305. * @param[in] a vector1
  306. * @param[in] b vector2
  307. * @returns destination vector
  308. */
  309. CGLM_INLINE
  310. vec4s
  311. glms_vec4_(sub)(vec4s a, vec4s b) {
  312. vec4s r;
  313. glm_vec4_sub(a.raw, b.raw, r.raw);
  314. return r;
  315. }
  316. /*!
  317. * @brief subtract scalar from v vector store result in dest (d = v - vec(s))
  318. *
  319. * @param[in] v vector
  320. * @param[in] s scalar
  321. * @returns destination vector
  322. */
  323. CGLM_INLINE
  324. vec4s
  325. glms_vec4_(subs)(vec4s v, float s) {
  326. vec4s r;
  327. glm_vec4_subs(v.raw, s, r.raw);
  328. return r;
  329. }
  330. /*!
  331. * @brief multiply two vectors (component-wise multiplication)
  332. *
  333. * @param a vector1
  334. * @param b vector2
  335. * @returns dest = (a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3])
  336. */
  337. CGLM_INLINE
  338. vec4s
  339. glms_vec4_(mul)(vec4s a, vec4s b) {
  340. vec4s r;
  341. glm_vec4_mul(a.raw, b.raw, r.raw);
  342. return r;
  343. }
  344. /*!
  345. * @brief multiply/scale vec4 vector with scalar: result = v * s
  346. *
  347. * @param[in] v vector
  348. * @param[in] s scalar
  349. * @returns destination vector
  350. */
  351. CGLM_INLINE
  352. vec4s
  353. glms_vec4_(scale)(vec4s v, float s) {
  354. vec4s r;
  355. glm_vec4_scale(v.raw, s, r.raw);
  356. return r;
  357. }
  358. /*!
  359. * @brief make vec4 vector scale as specified: result = unit(v) * s
  360. *
  361. * @param[in] v vector
  362. * @param[in] s scalar
  363. * @returns destination vector
  364. */
  365. CGLM_INLINE
  366. vec4s
  367. glms_vec4_(scale_as)(vec4s v, float s) {
  368. vec4s r;
  369. glm_vec4_scale_as(v.raw, s, r.raw);
  370. return r;
  371. }
  372. /*!
  373. * @brief div vector with another component-wise division: d = a / b
  374. *
  375. * @param[in] a vector 1
  376. * @param[in] b vector 2
  377. * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2], a[3]/b[3])
  378. */
  379. CGLM_INLINE
  380. vec4s
  381. glms_vec4_(div)(vec4s a, vec4s b) {
  382. vec4s r;
  383. glm_vec4_div(a.raw, b.raw, r.raw);
  384. return r;
  385. }
  386. /*!
  387. * @brief div vec4 vector with scalar: d = v / s
  388. *
  389. * @param[in] v vector
  390. * @param[in] s scalar
  391. * @returns destination vector
  392. */
  393. CGLM_INLINE
  394. vec4s
  395. glms_vec4_(divs)(vec4s v, float s) {
  396. vec4s r;
  397. glm_vec4_divs(v.raw, s, r.raw);
  398. return r;
  399. }
  400. /*!
  401. * @brief add two vectors and add result to sum
  402. *
  403. * it applies += operator so dest must be initialized
  404. *
  405. * @param[in] a vector 1
  406. * @param[in] b vector 2
  407. * @returns dest += (a + b)
  408. */
  409. CGLM_INLINE
  410. vec4s
  411. glms_vec4_(addadd)(vec4s a, vec4s b, vec4s dest) {
  412. glm_vec4_addadd(a.raw, b.raw, dest.raw);
  413. return dest;
  414. }
  415. /*!
  416. * @brief sub two vectors and add result to dest
  417. *
  418. * it applies += operator so dest must be initialized
  419. *
  420. * @param[in] a vector 1
  421. * @param[in] b vector 2
  422. * @returns dest += (a - b)
  423. */
  424. CGLM_INLINE
  425. vec4s
  426. glms_vec4_(subadd)(vec4s a, vec4s b, vec4s dest) {
  427. glm_vec4_subadd(a.raw, b.raw, dest.raw);
  428. return dest;
  429. }
  430. /*!
  431. * @brief mul two vectors and add result to dest
  432. *
  433. * it applies += operator so dest must be initialized
  434. *
  435. * @param[in] a vector 1
  436. * @param[in] b vector 2
  437. * @returns dest += (a * b)
  438. */
  439. CGLM_INLINE
  440. vec4s
  441. glms_vec4_(muladd)(vec4s a, vec4s b, vec4s dest) {
  442. glm_vec4_muladd(a.raw, b.raw, dest.raw);
  443. return dest;
  444. }
  445. /*!
  446. * @brief mul vector with scalar and add result to sum
  447. *
  448. * it applies += operator so dest must be initialized
  449. *
  450. * @param[in] a vector
  451. * @param[in] s scalar
  452. * @returns dest += (a * b)
  453. */
  454. CGLM_INLINE
  455. vec4s
  456. glms_vec4_(muladds)(vec4s a, float s, vec4s dest) {
  457. glm_vec4_muladds(a.raw, s, dest.raw);
  458. return dest;
  459. }
  460. /*!
  461. * @brief add max of two vectors to result/dest
  462. *
  463. * it applies += operator so dest must be initialized
  464. *
  465. * @param[in] a vector 1
  466. * @param[in] b vector 2
  467. * @returns dest += max(a, b)
  468. */
  469. CGLM_INLINE
  470. vec4s
  471. glms_vec4_(maxadd)(vec4s a, vec4s b, vec4s dest) {
  472. glm_vec4_maxadd(a.raw, b.raw, dest.raw);
  473. return dest;
  474. }
  475. /*!
  476. * @brief add min of two vectors to result/dest
  477. *
  478. * it applies += operator so dest must be initialized
  479. *
  480. * @param[in] a vector 1
  481. * @param[in] b vector 2
  482. * @returns dest += min(a, b)
  483. */
  484. CGLM_INLINE
  485. vec4s
  486. glms_vec4_(minadd)(vec4s a, vec4s b, vec4s dest) {
  487. glm_vec4_minadd(a.raw, b.raw, dest.raw);
  488. return dest;
  489. }
  490. /*!
  491. * @brief sub two vectors and sub result to dest
  492. *
  493. * it applies -= operator so dest must be initialized
  494. *
  495. * @param[in] a vector 1
  496. * @param[in] b vector 2
  497. * @returns dest -= (a + b)
  498. */
  499. CGLM_INLINE
  500. vec4s
  501. glms_vec4_(subsub)(vec4s a, vec4s b, vec4s dest) {
  502. glm_vec4_subsub(a.raw, b.raw, dest.raw);
  503. return dest;
  504. }
  505. /*!
  506. * @brief add two vectors and sub result to dest
  507. *
  508. * it applies -= operator so dest must be initialized
  509. *
  510. * @param[in] a vector 1
  511. * @param[in] b vector 2
  512. * @returns dest -= (a + b)
  513. */
  514. CGLM_INLINE
  515. vec4s
  516. glms_vec4_(addsub)(vec4s a, vec4s b, vec4s dest) {
  517. glm_vec4_addsub(a.raw, b.raw, dest.raw);
  518. return dest;
  519. }
  520. /*!
  521. * @brief mul two vectors and sub result to dest
  522. *
  523. * it applies -= operator so dest must be initialized
  524. *
  525. * @param[in] a vector 1
  526. * @param[in] b vector 2
  527. * @returns dest -= (a * b)
  528. */
  529. CGLM_INLINE
  530. vec4s
  531. glms_vec4_(mulsub)(vec4s a, vec4s b, vec4s dest) {
  532. glm_vec4_mulsub(a.raw, b.raw, dest.raw);
  533. return dest;
  534. }
  535. /*!
  536. * @brief mul vector with scalar and sub result to dest
  537. *
  538. * it applies -= operator so dest must be initialized
  539. *
  540. * @param[in] a vector
  541. * @param[in] s scalar
  542. * @returns dest -= (a * b)
  543. */
  544. CGLM_INLINE
  545. vec4s
  546. glms_vec4_(mulsubs)(vec4s a, float s, vec4s dest) {
  547. glm_vec4_mulsubs(a.raw, s, dest.raw);
  548. return dest;
  549. }
  550. /*!
  551. * @brief sub max of two vectors to dest
  552. *
  553. * it applies -= operator so dest must be initialized
  554. *
  555. * @param[in] a vector 1
  556. * @param[in] b vector 2
  557. * @returns dest -= max(a, b)
  558. */
  559. CGLM_INLINE
  560. vec4s
  561. glms_vec4_(maxsub)(vec4s a, vec4s b, vec4s dest) {
  562. glm_vec4_maxsub(a.raw, b.raw, dest.raw);
  563. return dest;
  564. }
  565. /*!
  566. * @brief sub min of two vectors to dest
  567. *
  568. * it applies -= operator so dest must be initialized
  569. *
  570. * @param[in] a vector 1
  571. * @param[in] b vector 2
  572. * @returns dest -= min(a, b)
  573. */
  574. CGLM_INLINE
  575. vec4s
  576. glms_vec4_(minsub)(vec4s a, vec4s b, vec4s dest) {
  577. glm_vec4_minsub(a.raw, b.raw, dest.raw);
  578. return dest;
  579. }
  580. /*!
  581. * @brief negate vector components and store result in dest
  582. *
  583. * @param[in] v vector
  584. * @returns result vector
  585. */
  586. CGLM_INLINE
  587. vec4s
  588. glms_vec4_(negate)(vec4s v) {
  589. glm_vec4_negate(v.raw);
  590. return v;
  591. }
  592. /*!
  593. * @brief normalize vec4 and store result in same vec
  594. *
  595. * @param[in] v vector
  596. * @returns normalized vector
  597. */
  598. CGLM_INLINE
  599. vec4s
  600. glms_vec4_(normalize)(vec4s v) {
  601. glm_vec4_normalize(v.raw);
  602. return v;
  603. }
  604. /**
  605. * @brief distance between two vectors
  606. *
  607. * @param[in] a vector1
  608. * @param[in] b vector2
  609. * @return returns distance
  610. */
  611. CGLM_INLINE
  612. float
  613. glms_vec4_(distance)(vec4s a, vec4s b) {
  614. return glm_vec4_distance(a.raw, b.raw);
  615. }
  616. /**
  617. * @brief squared distance between two vectors
  618. *
  619. * @param[in] a vector1
  620. * @param[in] b vector2
  621. * @return returns squared distance
  622. */
  623. CGLM_INLINE
  624. float
  625. glms_vec4_(distance2)(vec4s a, vec4s b) {
  626. return glm_vec4_distance2(a.raw, b.raw);
  627. }
  628. /*!
  629. * @brief max values of vectors
  630. *
  631. * @param[in] a vector1
  632. * @param[in] b vector2
  633. * @returns destination
  634. */
  635. CGLM_INLINE
  636. vec4s
  637. glms_vec4_(maxv)(vec4s a, vec4s b) {
  638. vec4s r;
  639. glm_vec4_maxv(a.raw, b.raw, r.raw);
  640. return r;
  641. }
  642. /*!
  643. * @brief min values of vectors
  644. *
  645. * @param[in] a vector1
  646. * @param[in] b vector2
  647. * @returns destination
  648. */
  649. CGLM_INLINE
  650. vec4s
  651. glms_vec4_(minv)(vec4s a, vec4s b) {
  652. vec4s r;
  653. glm_vec4_minv(a.raw, b.raw, r.raw);
  654. return r;
  655. }
  656. /*!
  657. * @brief clamp vector's individual members between min and max values
  658. *
  659. * @param[in] v vector
  660. * @param[in] minVal minimum value
  661. * @param[in] maxVal maximum value
  662. * @returns clamped vector
  663. */
  664. CGLM_INLINE
  665. vec4s
  666. glms_vec4_(clamp)(vec4s v, float minVal, float maxVal) {
  667. glm_vec4_clamp(v.raw, minVal, maxVal);
  668. return v;
  669. }
  670. /*!
  671. * @brief linear interpolation between two vectors
  672. *
  673. * formula: from + s * (to - from)
  674. *
  675. * @param[in] from from value
  676. * @param[in] to to value
  677. * @param[in] t interpolant (amount)
  678. * @returns destination
  679. */
  680. CGLM_INLINE
  681. vec4s
  682. glms_vec4_(lerp)(vec4s from, vec4s to, float t) {
  683. vec4s r;
  684. glm_vec4_lerp(from.raw, to.raw, t, r.raw);
  685. return r;
  686. }
  687. /*!
  688. * @brief linear interpolation between two vectors (clamped)
  689. *
  690. * formula: from + s * (to - from)
  691. *
  692. * @param[in] from from value
  693. * @param[in] to to value
  694. * @param[in] t interpolant (amount) clamped between 0 and 1
  695. * @returns destination
  696. */
  697. CGLM_INLINE
  698. vec4s
  699. glms_vec4_(lerpc)(vec4s from, vec4s to, float t) {
  700. vec4s r;
  701. glm_vec4_lerpc(from.raw, to.raw, t, r.raw);
  702. return r;
  703. }
  704. /*!
  705. * @brief linear interpolation between two vectors
  706. *
  707. * formula: from + s * (to - from)
  708. *
  709. * @param[in] from from value
  710. * @param[in] to to value
  711. * @param[in] t interpolant (amount)
  712. * @returns destination
  713. */
  714. CGLM_INLINE
  715. vec4s
  716. glms_vec4_(mix)(vec4s from, vec4s to, float t) {
  717. vec4s r;
  718. glm_vec4_mix(from.raw, to.raw, t, r.raw);
  719. return r;
  720. }
  721. /*!
  722. * @brief linear interpolation between two vectors (clamped)
  723. *
  724. * formula: from + s * (to - from)
  725. *
  726. * @param[in] from from value
  727. * @param[in] to to value
  728. * @param[in] t interpolant (amount) clamped between 0 and 1
  729. * @returns destination
  730. */
  731. CGLM_INLINE
  732. vec4s
  733. glms_vec4_(mixc)(vec4s from, vec4s to, float t) {
  734. vec4s r;
  735. glm_vec4_mixc(from.raw, to.raw, t, r.raw);
  736. return r;
  737. }
  738. /*!
  739. * @brief threshold function
  740. *
  741. * @param[in] edge threshold
  742. * @param[in] x value to test against threshold
  743. * @returns 0.0 if x < edge, else 1.0
  744. */
  745. CGLM_INLINE
  746. vec4s
  747. glms_vec4_(step)(vec4s edge, vec4s x) {
  748. vec4s r;
  749. glm_vec4_step(edge.raw, x.raw, r.raw);
  750. return r;
  751. }
  752. /*!
  753. * @brief threshold function with a smooth transition (unidimensional)
  754. *
  755. * @param[in] edge0 low threshold
  756. * @param[in] edge1 high threshold
  757. * @param[in] x value to test against threshold
  758. * @returns destination
  759. */
  760. CGLM_INLINE
  761. vec4s
  762. glms_vec4_(smoothstep_uni)(float edge0, float edge1, vec4s x) {
  763. vec4s r;
  764. glm_vec4_smoothstep_uni(edge0, edge1, x.raw, r.raw);
  765. return r;
  766. }
  767. /*!
  768. * @brief threshold function with a smooth transition
  769. *
  770. * @param[in] edge0 low threshold
  771. * @param[in] edge1 high threshold
  772. * @param[in] x value to test against threshold
  773. * @returns destination
  774. */
  775. CGLM_INLINE
  776. vec4s
  777. glms_vec4_(smoothstep)(vec4s edge0, vec4s edge1, vec4s x) {
  778. vec4s r;
  779. glm_vec4_smoothstep(edge0.raw, edge1.raw, x.raw, r.raw);
  780. return r;
  781. }
  782. /*!
  783. * @brief smooth Hermite interpolation between two vectors
  784. *
  785. * formula: from + s * (to - from)
  786. *
  787. * @param[in] from from value
  788. * @param[in] to to value
  789. * @param[in] t interpolant (amount)
  790. * @returns destination
  791. */
  792. CGLM_INLINE
  793. vec4s
  794. glms_vec4_(smoothinterp)(vec4s from, vec4s to, float t) {
  795. vec4s r;
  796. glm_vec4_smoothinterp(from.raw, to.raw, t, r.raw);
  797. return r;
  798. }
  799. /*!
  800. * @brief smooth Hermite interpolation between two vectors (clamped)
  801. *
  802. * formula: from + s * (to - from)
  803. *
  804. * @param[in] from from value
  805. * @param[in] to to value
  806. * @param[in] t interpolant (amount) clamped between 0 and 1
  807. * @returns destination
  808. */
  809. CGLM_INLINE
  810. vec4s
  811. glms_vec4_(smoothinterpc)(vec4s from, vec4s to, float t) {
  812. vec4s r;
  813. glm_vec4_smoothinterpc(from.raw, to.raw, t, r.raw);
  814. return r;
  815. }
  816. /*!
  817. * @brief helper to fill vec4 as [S^3, S^2, S, 1]
  818. *
  819. * @param[in] s parameter
  820. * @returns destination
  821. */
  822. CGLM_INLINE
  823. vec4s
  824. glms_vec4_(cubic)(float s) {
  825. vec4s r;
  826. glm_vec4_cubic(s, r.raw);
  827. return r;
  828. }
  829. /*!
  830. * @brief swizzle vector components
  831. *
  832. * you can use existing masks e.g. GLM_XXXX, GLM_WZYX
  833. *
  834. * @param[in] v source
  835. * @param[in] mask mask
  836. * @returns swizzled vector
  837. */
  838. CGLM_INLINE
  839. vec4s
  840. glms_vec4_(swizzle)(vec4s v, int mask) {
  841. vec4s dest;
  842. glm_vec4_swizzle(v.raw, mask, dest.raw);
  843. return dest;
  844. }
  845. /*!
  846. * @brief Create four dimensional vector from pointer
  847. *
  848. * @param[in] src pointer to an array of floats
  849. * @returns constructed 4D vector from raw pointer
  850. */
  851. CGLM_INLINE
  852. vec4s
  853. glms_vec4_(make)(const float * __restrict src) {
  854. vec4s dest;
  855. glm_vec4_make(src, dest.raw);
  856. return dest;
  857. }
  858. /*!
  859. * @brief reflection vector using an incident ray and a surface normal
  860. *
  861. * @param[in] v incident vector
  862. * @param[in] n normalized normal vector
  863. * @returns reflection result
  864. */
  865. CGLM_INLINE
  866. vec4s
  867. glms_vec4_(reflect)(vec4s v, vec4s n) {
  868. vec4s dest;
  869. glm_vec4_reflect(v.raw, n.raw, dest.raw);
  870. return dest;
  871. }
  872. /*!
  873. * @brief computes refraction vector for an incident vector and a surface normal.
  874. *
  875. * calculates the refraction vector based on Snell's law. If total internal reflection
  876. * occurs (angle too great given eta), dest is set to zero and returns false.
  877. * Otherwise, computes refraction vector, stores it in dest, and returns true.
  878. *
  879. * this implementation does not explicitly preserve the 'w' component of the
  880. * incident vector 'I' in the output 'dest', users requiring the preservation of
  881. * the 'w' component should manually adjust 'dest' after calling this function.
  882. *
  883. * @param[in] v normalized incident vector
  884. * @param[in] n normalized normal vector
  885. * @param[in] eta ratio of indices of refraction (incident/transmitted)
  886. * @param[out] dest refraction vector if refraction occurs; zero vector otherwise
  887. *
  888. * @returns true if refraction occurs; false if total internal reflection occurs.
  889. */
  890. CGLM_INLINE
  891. bool
  892. glms_vec4_(refract)(vec4s v, vec4s n, float eta, vec4s * __restrict dest) {
  893. return glm_vec4_refract(v.raw, n.raw, eta, dest->raw);
  894. }
  895. #endif /* cglms_vec4s_h */