fpumath.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. // FPU math lib
  6. #ifndef BX_FPU_MATH_H_HEADER_GUARD
  7. #define BX_FPU_MATH_H_HEADER_GUARD
  8. #include <math.h>
  9. #include <string.h>
  10. namespace bx
  11. {
  12. const float pi = 3.14159265358979323846f;
  13. const float piHalf = 1.57079632679489661923f;
  14. inline float toRad(float _deg)
  15. {
  16. return _deg * pi / 180.0f;;
  17. }
  18. inline float toDeg(float _rad)
  19. {
  20. return _rad * 180.0f / pi;
  21. }
  22. inline float fmin(float _a, float _b)
  23. {
  24. return _a < _b ? _a : _b;
  25. }
  26. inline float fmax(float _a, float _b)
  27. {
  28. return _a > _b ? _a : _b;
  29. }
  30. inline float fmin3(float _a, float _b, float _c)
  31. {
  32. return fmin(_a, fmin(_b, _c) );
  33. }
  34. inline float fmax3(float _a, float _b, float _c)
  35. {
  36. return fmax(_a, fmax(_b, _c) );
  37. }
  38. inline float fclamp(float _a, float _min, float _max)
  39. {
  40. return fmin(fmax(_a, _min), _max);
  41. }
  42. inline float fsaturate(float _a)
  43. {
  44. return fclamp(_a, 0.0f, 1.0f);
  45. }
  46. inline float flerp(float _a, float _b, float _t)
  47. {
  48. return _a + (_b - _a) * _t;
  49. }
  50. inline float fsign(float _a)
  51. {
  52. return _a < 0.0f ? -1.0f : 1.0f;
  53. }
  54. inline float fstep(float _edge, float _a)
  55. {
  56. return _a < _edge ? 0.0f : 1.0f;
  57. }
  58. inline float fpulse(float _a, float _start, float _end)
  59. {
  60. return fstep(_a, _start) - fstep(_a, _end);
  61. }
  62. inline float fabsolute(float _a)
  63. {
  64. return fabsf(_a);
  65. }
  66. inline float fsqrt(float _a)
  67. {
  68. return sqrtf(_a);
  69. }
  70. inline float ffract(float _a)
  71. {
  72. return _a - floorf(_a);
  73. }
  74. inline void vec3Move(float* __restrict _result, const float* __restrict _a)
  75. {
  76. _result[0] = _a[0];
  77. _result[1] = _a[1];
  78. _result[2] = _a[2];
  79. }
  80. inline void vec3Abs(float* __restrict _result, const float* __restrict _a)
  81. {
  82. _result[0] = fabsolute(_a[0]);
  83. _result[1] = fabsolute(_a[1]);
  84. _result[2] = fabsolute(_a[2]);
  85. }
  86. inline void vec3Neg(float* __restrict _result, const float* __restrict _a)
  87. {
  88. _result[0] = -_a[0];
  89. _result[1] = -_a[1];
  90. _result[2] = -_a[2];
  91. }
  92. inline void vec3Add(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  93. {
  94. _result[0] = _a[0] + _b[0];
  95. _result[1] = _a[1] + _b[1];
  96. _result[2] = _a[2] + _b[2];
  97. }
  98. inline void vec3Sub(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  99. {
  100. _result[0] = _a[0] - _b[0];
  101. _result[1] = _a[1] - _b[1];
  102. _result[2] = _a[2] - _b[2];
  103. }
  104. inline void vec3Mul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  105. {
  106. _result[0] = _a[0] * _b[0];
  107. _result[1] = _a[1] * _b[1];
  108. _result[2] = _a[2] * _b[2];
  109. }
  110. inline void vec3Mul(float* __restrict _result, const float* __restrict _a, float _b)
  111. {
  112. _result[0] = _a[0] * _b;
  113. _result[1] = _a[1] * _b;
  114. _result[2] = _a[2] * _b;
  115. }
  116. inline float vec3Dot(const float* __restrict _a, const float* __restrict _b)
  117. {
  118. return _a[0]*_b[0] + _a[1]*_b[1] + _a[2]*_b[2];
  119. }
  120. inline void vec3Cross(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  121. {
  122. _result[0] = _a[1]*_b[2] - _a[2]*_b[1];
  123. _result[1] = _a[2]*_b[0] - _a[0]*_b[2];
  124. _result[2] = _a[0]*_b[1] - _a[1]*_b[0];
  125. }
  126. inline float vec3Length(const float* _a)
  127. {
  128. return fsqrt(vec3Dot(_a, _a) );
  129. }
  130. inline float vec3Norm(float* __restrict _result, const float* __restrict _a)
  131. {
  132. const float len = vec3Length(_a);
  133. const float invLen = 1.0f/len;
  134. _result[0] = _a[0] * invLen;
  135. _result[1] = _a[1] * invLen;
  136. _result[2] = _a[2] * invLen;
  137. return len;
  138. }
  139. inline void mtxIdentity(float* _result)
  140. {
  141. memset(_result, 0, sizeof(float)*16);
  142. _result[0] = _result[5] = _result[10] = _result[15] = 1.0f;
  143. }
  144. inline void mtxTranslate(float* _result, float _tx, float _ty, float _tz)
  145. {
  146. mtxIdentity(_result);
  147. _result[12] = _tx;
  148. _result[13] = _ty;
  149. _result[14] = _tz;
  150. }
  151. inline void mtxScale(float* _result, float _sx, float _sy, float _sz)
  152. {
  153. memset(_result, 0, sizeof(float) * 16);
  154. _result[0] = _sx;
  155. _result[5] = _sy;
  156. _result[10] = _sz;
  157. _result[15] = 1.0f;
  158. }
  159. inline void mtxLookAt(float* __restrict _result, const float* __restrict _eye, const float* __restrict _at, const float* __restrict _up = NULL)
  160. {
  161. float tmp[4];
  162. vec3Sub(tmp, _at, _eye);
  163. float view[4];
  164. vec3Norm(view, tmp);
  165. float up[3] = { 0.0f, 1.0f, 0.0f };
  166. if (NULL != _up)
  167. {
  168. up[0] = _up[0];
  169. up[1] = _up[1];
  170. up[2] = _up[2];
  171. }
  172. vec3Cross(tmp, up, view);
  173. float right[4];
  174. vec3Norm(right, tmp);
  175. vec3Cross(up, view, right);
  176. memset(_result, 0, sizeof(float)*16);
  177. _result[ 0] = right[0];
  178. _result[ 1] = up[0];
  179. _result[ 2] = view[0];
  180. _result[ 4] = right[1];
  181. _result[ 5] = up[1];
  182. _result[ 6] = view[1];
  183. _result[ 8] = right[2];
  184. _result[ 9] = up[2];
  185. _result[10] = view[2];
  186. _result[12] = -vec3Dot(right, _eye);
  187. _result[13] = -vec3Dot(up, _eye);
  188. _result[14] = -vec3Dot(view, _eye);
  189. _result[15] = 1.0f;
  190. }
  191. inline void mtxProjXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, float _far, bool _oglNdc = false)
  192. {
  193. const float diff = _far-_near;
  194. const float aa = _oglNdc ? (_far+_near)/diff : _far/diff;
  195. const float bb = _oglNdc ? -(2.0f*_far*_near)/diff : -_near*aa;
  196. memset(_result, 0, sizeof(float)*16);
  197. _result[ 0] = _width;
  198. _result[ 5] = _height;
  199. _result[ 8] = _x;
  200. _result[ 9] = -_y;
  201. _result[10] = aa;
  202. _result[11] = 1.0f;
  203. _result[14] = bb;
  204. }
  205. inline void mtxProj(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc = false)
  206. {
  207. const float width = 2.0f / (_lt + _rt);
  208. const float height = 2.0f / (_ut + _dt);
  209. const float xx = (_lt - _rt) * width * 0.5f;
  210. const float yy = (_ut - _dt) * height * 0.5f;
  211. mtxProjXYWH(_result, xx, yy, width, height, _near, _far, _oglNdc);
  212. }
  213. inline void mtxProj(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc = false)
  214. {
  215. mtxProj(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _far, _oglNdc);
  216. }
  217. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc = false)
  218. {
  219. const float height = 1.0f/tanf(toRad(_fovy)*0.5f);
  220. const float width = height * 1.0f/_aspect;
  221. mtxProjXYWH(_result, 0.0f, 0.0f, width, height, _near, _far, _oglNdc);
  222. }
  223. inline void mtxOrtho(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far)
  224. {
  225. const float aa = 2.0f/(_right - _left);
  226. const float bb = 2.0f/(_top - _bottom);
  227. const float cc = 1.0f/(_far - _near);
  228. const float dd = (_left + _right)/(_left - _right);
  229. const float ee = (_top + _bottom)/(_bottom - _top);
  230. const float ff = _near / (_near - _far);
  231. memset(_result, 0, sizeof(float)*16);
  232. _result[0] = aa;
  233. _result[5] = bb;
  234. _result[10] = cc;
  235. _result[12] = dd;
  236. _result[13] = ee;
  237. _result[14] = ff;
  238. _result[15] = 1.0f;
  239. }
  240. inline void mtxRotateX(float* _result, float _ax)
  241. {
  242. const float sx = sinf(_ax);
  243. const float cx = cosf(_ax);
  244. memset(_result, 0, sizeof(float)*16);
  245. _result[ 0] = 1.0f;
  246. _result[ 5] = cx;
  247. _result[ 6] = -sx;
  248. _result[ 9] = sx;
  249. _result[10] = cx;
  250. _result[15] = 1.0f;
  251. }
  252. inline void mtxRotateY(float* _result, float _ay)
  253. {
  254. const float sy = sinf(_ay);
  255. const float cy = cosf(_ay);
  256. memset(_result, 0, sizeof(float)*16);
  257. _result[ 0] = cy;
  258. _result[ 2] = sy;
  259. _result[ 5] = 1.0f;
  260. _result[ 8] = -sy;
  261. _result[10] = cy;
  262. _result[15] = 1.0f;
  263. }
  264. inline void mtxRotateZ(float* _result, float _az)
  265. {
  266. const float sz = sinf(_az);
  267. const float cz = cosf(_az);
  268. memset(_result, 0, sizeof(float)*16);
  269. _result[ 0] = cz;
  270. _result[ 1] = -sz;
  271. _result[ 4] = sz;
  272. _result[ 5] = cz;
  273. _result[10] = 1.0f;
  274. _result[15] = 1.0f;
  275. }
  276. inline void mtxRotateXY(float* _result, float _ax, float _ay)
  277. {
  278. const float sx = sinf(_ax);
  279. const float cx = cosf(_ax);
  280. const float sy = sinf(_ay);
  281. const float cy = cosf(_ay);
  282. memset(_result, 0, sizeof(float)*16);
  283. _result[ 0] = cy;
  284. _result[ 2] = sy;
  285. _result[ 4] = sx*sy;
  286. _result[ 5] = cx;
  287. _result[ 6] = -sx*cy;
  288. _result[ 8] = -cx*sy;
  289. _result[ 9] = sx;
  290. _result[10] = cx*cy;
  291. _result[15] = 1.0f;
  292. }
  293. inline void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az)
  294. {
  295. const float sx = sinf(_ax);
  296. const float cx = cosf(_ax);
  297. const float sy = sinf(_ay);
  298. const float cy = cosf(_ay);
  299. const float sz = sinf(_az);
  300. const float cz = cosf(_az);
  301. memset(_result, 0, sizeof(float)*16);
  302. _result[ 0] = cy*cz;
  303. _result[ 1] = -cy*sz;
  304. _result[ 2] = sy;
  305. _result[ 4] = cz*sx*sy + cx*sz;
  306. _result[ 5] = cx*cz - sx*sy*sz;
  307. _result[ 6] = -cy*sx;
  308. _result[ 8] = -cx*cz*sy + sx*sz;
  309. _result[ 9] = cz*sx + cx*sy*sz;
  310. _result[10] = cx*cy;
  311. _result[15] = 1.0f;
  312. }
  313. inline void mtxRotateZYX(float* _result, float _ax, float _ay, float _az)
  314. {
  315. const float sx = sinf(_ax);
  316. const float cx = cosf(_ax);
  317. const float sy = sinf(_ay);
  318. const float cy = cosf(_ay);
  319. const float sz = sinf(_az);
  320. const float cz = cosf(_az);
  321. memset(_result, 0, sizeof(float)*16);
  322. _result[ 0] = cy*cz;
  323. _result[ 1] = cz*sx*sy-cx*sz;
  324. _result[ 2] = cx*cz*sy+sx*sz;
  325. _result[ 4] = cy*sz;
  326. _result[ 5] = cx*cz + sx*sy*sz;
  327. _result[ 6] = -cz*sx + cx*sy*sz;
  328. _result[ 8] = -sy;
  329. _result[ 9] = cy*sx;
  330. _result[10] = cx*cy;
  331. _result[15] = 1.0f;
  332. };
  333. inline void mtxSRT(float* _result, float _sx, float _sy, float _sz, float _ax, float _ay, float _az, float _tx, float _ty, float _tz)
  334. {
  335. const float sx = sinf(_ax);
  336. const float cx = cosf(_ax);
  337. const float sy = sinf(_ay);
  338. const float cy = cosf(_ay);
  339. const float sz = sinf(_az);
  340. const float cz = cosf(_az);
  341. const float sxsz = sx*sz;
  342. const float cycz = cy*cz;
  343. _result[ 0] = _sx * (cycz - sxsz*sy);
  344. _result[ 1] = _sx * -cx*sz;
  345. _result[ 2] = _sx * (cz*sy + cy*sxsz);
  346. _result[ 3] = 0.0f;
  347. _result[ 4] = _sy * (cz*sx*sy + cy*sz);
  348. _result[ 5] = _sy * cx*cz;
  349. _result[ 6] = _sy * (sy*sz -cycz*sx);
  350. _result[ 7] = 0.0f;
  351. _result[ 8] = _sz * -cx*sy;
  352. _result[ 9] = _sz * sx;
  353. _result[10] = _sz * cx*cy;
  354. _result[11] = 0.0f;
  355. _result[12] = _tx;
  356. _result[13] = _ty;
  357. _result[14] = _tz;
  358. _result[15] = 1.0f;
  359. }
  360. inline void vec3MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
  361. {
  362. _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _mat[12];
  363. _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _mat[13];
  364. _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _mat[14];
  365. }
  366. inline void vec3MulMtxH(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
  367. {
  368. float xx = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _mat[12];
  369. float yy = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _mat[13];
  370. float zz = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _mat[14];
  371. float ww = _vec[0] * _mat[ 3] + _vec[1] * _mat[7] + _vec[2] * _mat[11] + _mat[15];
  372. float invW = fsign(ww)/ww;
  373. _result[0] = xx*invW;
  374. _result[1] = yy*invW;
  375. _result[2] = zz*invW;
  376. }
  377. inline void vec4MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
  378. {
  379. _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _vec[3] * _mat[12];
  380. _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _vec[3] * _mat[13];
  381. _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _vec[3] * _mat[14];
  382. _result[3] = _vec[0] * _mat[ 3] + _vec[1] * _mat[7] + _vec[2] * _mat[11] + _vec[3] * _mat[15];
  383. }
  384. inline void mtxMul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  385. {
  386. vec4MulMtx(&_result[ 0], &_a[ 0], _b);
  387. vec4MulMtx(&_result[ 4], &_a[ 4], _b);
  388. vec4MulMtx(&_result[ 8], &_a[ 8], _b);
  389. vec4MulMtx(&_result[12], &_a[12], _b);
  390. }
  391. inline void mtxTranspose(float* __restrict _result, const float* __restrict _a)
  392. {
  393. _result[ 0] = _a[ 0];
  394. _result[ 4] = _a[ 1];
  395. _result[ 8] = _a[ 2];
  396. _result[12] = _a[ 3];
  397. _result[ 1] = _a[ 4];
  398. _result[ 5] = _a[ 5];
  399. _result[ 9] = _a[ 6];
  400. _result[13] = _a[ 7];
  401. _result[ 2] = _a[ 8];
  402. _result[ 6] = _a[ 9];
  403. _result[10] = _a[10];
  404. _result[14] = _a[11];
  405. _result[ 3] = _a[12];
  406. _result[ 7] = _a[13];
  407. _result[11] = _a[14];
  408. _result[15] = _a[15];
  409. }
  410. inline void mtx3Inverse(float* __restrict _result, const float* __restrict _a)
  411. {
  412. float xx = _a[0];
  413. float xy = _a[1];
  414. float xz = _a[2];
  415. float yx = _a[3];
  416. float yy = _a[4];
  417. float yz = _a[5];
  418. float zx = _a[6];
  419. float zy = _a[7];
  420. float zz = _a[8];
  421. float det = 0.0f;
  422. det += xx * (yy*zz - yz*zy);
  423. det -= xy * (yx*zz - yz*zx);
  424. det += xz * (yx*zy - yy*zx);
  425. float invDet = 1.0f/det;
  426. _result[0] = +(yy*zz - yz*zy) * invDet;
  427. _result[1] = -(xy*zz - xz*zy) * invDet;
  428. _result[2] = +(xy*yz - xz*yy) * invDet;
  429. _result[3] = -(yx*zz - yz*zx) * invDet;
  430. _result[4] = +(xx*zz - xz*zx) * invDet;
  431. _result[5] = -(xx*yz - xz*yx) * invDet;
  432. _result[6] = +(yx*zy - yy*zx) * invDet;
  433. _result[7] = -(xx*zy - xy*zx) * invDet;
  434. _result[8] = +(xx*yy - xy*yx) * invDet;
  435. }
  436. inline void mtxInverse(float* __restrict _result, const float* __restrict _a)
  437. {
  438. float xx = _a[ 0];
  439. float xy = _a[ 1];
  440. float xz = _a[ 2];
  441. float xw = _a[ 3];
  442. float yx = _a[ 4];
  443. float yy = _a[ 5];
  444. float yz = _a[ 6];
  445. float yw = _a[ 7];
  446. float zx = _a[ 8];
  447. float zy = _a[ 9];
  448. float zz = _a[10];
  449. float zw = _a[11];
  450. float wx = _a[12];
  451. float wy = _a[13];
  452. float wz = _a[14];
  453. float ww = _a[15];
  454. float det = 0.0f;
  455. det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) );
  456. det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) );
  457. det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) );
  458. det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) );
  459. float invDet = 1.0f/det;
  460. _result[ 0] = +(yy*(zz*ww - wz*zw) - yz*(zy*ww - wy*zw) + yw*(zy*wz - wy*zz) ) * invDet;
  461. _result[ 1] = -(xy*(zz*ww - wz*zw) - xz*(zy*ww - wy*zw) + xw*(zy*wz - wy*zz) ) * invDet;
  462. _result[ 2] = +(xy*(yz*ww - wz*yw) - xz*(yy*ww - wy*yw) + xw*(yy*wz - wy*yz) ) * invDet;
  463. _result[ 3] = -(xy*(yz*zw - zz*yw) - xz*(yy*zw - zy*yw) + xw*(yy*zz - zy*yz) ) * invDet;
  464. _result[ 4] = -(yx*(zz*ww - wz*zw) - yz*(zx*ww - wx*zw) + yw*(zx*wz - wx*zz) ) * invDet;
  465. _result[ 5] = +(xx*(zz*ww - wz*zw) - xz*(zx*ww - wx*zw) + xw*(zx*wz - wx*zz) ) * invDet;
  466. _result[ 6] = -(xx*(yz*ww - wz*yw) - xz*(yx*ww - wx*yw) + xw*(yx*wz - wx*yz) ) * invDet;
  467. _result[ 7] = +(xx*(yz*zw - zz*yw) - xz*(yx*zw - zx*yw) + xw*(yx*zz - zx*yz) ) * invDet;
  468. _result[ 8] = +(yx*(zy*ww - wy*zw) - yy*(zx*ww - wx*zw) + yw*(zx*wy - wx*zy) ) * invDet;
  469. _result[ 9] = -(xx*(zy*ww - wy*zw) - xy*(zx*ww - wx*zw) + xw*(zx*wy - wx*zy) ) * invDet;
  470. _result[10] = +(xx*(yy*ww - wy*yw) - xy*(yx*ww - wx*yw) + xw*(yx*wy - wx*yy) ) * invDet;
  471. _result[11] = -(xx*(yy*zw - zy*yw) - xy*(yx*zw - zx*yw) + xw*(yx*zy - zx*yy) ) * invDet;
  472. _result[12] = -(yx*(zy*wz - wy*zz) - yy*(zx*wz - wx*zz) + yz*(zx*wy - wx*zy) ) * invDet;
  473. _result[13] = +(xx*(zy*wz - wy*zz) - xy*(zx*wz - wx*zz) + xz*(zx*wy - wx*zy) ) * invDet;
  474. _result[14] = -(xx*(yy*wz - wy*yz) - xy*(yx*wz - wx*yz) + xz*(yx*wy - wx*yy) ) * invDet;
  475. _result[15] = +(xx*(yy*zz - zy*yz) - xy*(yx*zz - zx*yz) + xz*(yx*zy - zx*yy) ) * invDet;
  476. }
  477. /// Convert LH to RH projection matrix and vice versa.
  478. inline void mtxProjFlipHandedness(float* __restrict _dst, const float* __restrict _src)
  479. {
  480. _dst[ 0] = -_src[ 0];
  481. _dst[ 1] = -_src[ 1];
  482. _dst[ 2] = -_src[ 2];
  483. _dst[ 3] = -_src[ 3];
  484. _dst[ 4] = _src[ 4];
  485. _dst[ 5] = _src[ 5];
  486. _dst[ 6] = _src[ 6];
  487. _dst[ 7] = _src[ 7];
  488. _dst[ 8] = -_src[ 8];
  489. _dst[ 9] = -_src[ 9];
  490. _dst[10] = -_src[10];
  491. _dst[11] = -_src[11];
  492. _dst[12] = _src[12];
  493. _dst[13] = _src[13];
  494. _dst[14] = _src[14];
  495. _dst[15] = _src[15];
  496. }
  497. /// Convert LH to RH view matrix and vice versa.
  498. inline void mtxViewFlipHandedness(float* __restrict _dst, const float* __restrict _src)
  499. {
  500. _dst[ 0] = -_src[ 0];
  501. _dst[ 1] = _src[ 1];
  502. _dst[ 2] = -_src[ 2];
  503. _dst[ 3] = _src[ 3];
  504. _dst[ 4] = -_src[ 4];
  505. _dst[ 5] = _src[ 5];
  506. _dst[ 6] = -_src[ 6];
  507. _dst[ 7] = _src[ 7];
  508. _dst[ 8] = -_src[ 8];
  509. _dst[ 9] = _src[ 9];
  510. _dst[10] = -_src[10];
  511. _dst[11] = _src[11];
  512. _dst[12] = -_src[12];
  513. _dst[13] = _src[13];
  514. _dst[14] = -_src[14];
  515. _dst[15] = _src[15];
  516. }
  517. inline void calcNormal(float _result[3], float _va[3], float _vb[3], float _vc[3])
  518. {
  519. float ba[3];
  520. vec3Sub(ba, _vb, _va);
  521. float ca[3];
  522. vec3Sub(ca, _vc, _va);
  523. float baxca[3];
  524. vec3Cross(baxca, ba, ca);
  525. vec3Norm(_result, baxca);
  526. }
  527. inline void calcPlane(float _result[4], float _va[3], float _vb[3], float _vc[3])
  528. {
  529. float normal[3];
  530. calcNormal(normal, _va, _vb, _vc);
  531. _result[0] = normal[0];
  532. _result[1] = normal[1];
  533. _result[2] = normal[2];
  534. _result[3] = -vec3Dot(normal, _va);
  535. }
  536. inline void rgbToHsv(float _hsv[3], const float _rgb[3])
  537. {
  538. const float rr = _rgb[0];
  539. const float gg = _rgb[1];
  540. const float bb = _rgb[2];
  541. const float s0 = fstep(bb, gg);
  542. const float px = flerp(bb, gg, s0);
  543. const float py = flerp(gg, bb, s0);
  544. const float pz = flerp(-1.0f, 0.0f, s0);
  545. const float pw = flerp(2.0f/3.0f, -1.0f/3.0f, s0);
  546. const float s1 = fstep(px, rr);
  547. const float qx = flerp(px, rr, s1);
  548. const float qy = py;
  549. const float qz = flerp(pw, pz, s1);
  550. const float qw = flerp(rr, px, s1);
  551. const float dd = qx - fmin(qw, qy);
  552. const float ee = 1.0e-10f;
  553. _hsv[0] = fabsolute(qz + (qw - qy) / (6.0f * dd + ee) );
  554. _hsv[1] = dd / (qx + ee);
  555. _hsv[2] = qx;
  556. }
  557. inline void hsvToRgb(float _rgb[3], const float _hsv[3])
  558. {
  559. const float hh = _hsv[0];
  560. const float ss = _hsv[1];
  561. const float vv = _hsv[2];
  562. const float px = fabsolute(ffract(hh + 1.0f ) * 6.0f - 3.0f);
  563. const float py = fabsolute(ffract(hh + 2.0f/3.0f) * 6.0f - 3.0f);
  564. const float pz = fabsolute(ffract(hh + 1.0f/3.0f) * 6.0f - 3.0f);
  565. _rgb[0] = vv * flerp(1.0f, fsaturate(px - 1.0f), ss);
  566. _rgb[1] = vv * flerp(1.0f, fsaturate(py - 1.0f), ss);
  567. _rgb[2] = vv * flerp(1.0f, fsaturate(pz - 1.0f), ss);
  568. }
  569. } // namespace bx
  570. #endif // BX_FPU_MATH_H_HEADER_GUARD