fpumath.h 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-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 "bx.h"
  9. #include <math.h>
  10. #include <string.h>
  11. namespace bx
  12. {
  13. static const float pi = 3.14159265358979323846f;
  14. static const float invPi = 1.0f/3.14159265358979323846f;
  15. static const float piHalf = 1.57079632679489661923f;
  16. static const float sqrt2 = 1.41421356237309504880f;
  17. struct Handness
  18. {
  19. enum Enum
  20. {
  21. Left,
  22. Right,
  23. };
  24. };
  25. struct NearFar
  26. {
  27. enum Enum
  28. {
  29. Default,
  30. Reverse,
  31. };
  32. };
  33. inline float toRad(float _deg)
  34. {
  35. return _deg * pi / 180.0f;
  36. }
  37. inline float toDeg(float _rad)
  38. {
  39. return _rad * 180.0f / pi;
  40. }
  41. inline float ffloor(float _f)
  42. {
  43. return floorf(_f);
  44. }
  45. inline float fceil(float _f)
  46. {
  47. return ceilf(_f);
  48. }
  49. inline float fround(float _f)
  50. {
  51. return ffloor(_f + 0.5f);
  52. }
  53. inline float fmin(float _a, float _b)
  54. {
  55. return _a < _b ? _a : _b;
  56. }
  57. inline float fmax(float _a, float _b)
  58. {
  59. return _a > _b ? _a : _b;
  60. }
  61. inline float fmin3(float _a, float _b, float _c)
  62. {
  63. return fmin(_a, fmin(_b, _c) );
  64. }
  65. inline float fmax3(float _a, float _b, float _c)
  66. {
  67. return fmax(_a, fmax(_b, _c) );
  68. }
  69. inline float fclamp(float _a, float _min, float _max)
  70. {
  71. return fmin(fmax(_a, _min), _max);
  72. }
  73. inline float fsaturate(float _a)
  74. {
  75. return fclamp(_a, 0.0f, 1.0f);
  76. }
  77. inline float flerp(float _a, float _b, float _t)
  78. {
  79. return _a + (_b - _a) * _t;
  80. }
  81. inline float fsign(float _a)
  82. {
  83. return _a < 0.0f ? -1.0f : 1.0f;
  84. }
  85. inline float fstep(float _edge, float _a)
  86. {
  87. return _a < _edge ? 0.0f : 1.0f;
  88. }
  89. inline float fpulse(float _a, float _start, float _end)
  90. {
  91. return fstep(_a, _start) - fstep(_a, _end);
  92. }
  93. inline float fabsolute(float _a)
  94. {
  95. return fabsf(_a);
  96. }
  97. inline float fsq(float _a)
  98. {
  99. return _a * _a;
  100. }
  101. inline float fsin(float _a)
  102. {
  103. return sinf(_a);
  104. }
  105. inline float fcos(float _a)
  106. {
  107. return cosf(_a);
  108. }
  109. inline float fpow(float _a, float _b)
  110. {
  111. return powf(_a, _b);
  112. }
  113. inline float fexp2(float _a)
  114. {
  115. return fpow(2.0f, _a);
  116. }
  117. inline float flog(float _a)
  118. {
  119. return logf(_a);
  120. }
  121. inline float flog2(float _a)
  122. {
  123. return flog(_a) * 1.442695041f;
  124. }
  125. inline float fsqrt(float _a)
  126. {
  127. return sqrtf(_a);
  128. }
  129. inline float frsqrt(float _a)
  130. {
  131. return 1.0f/fsqrt(_a);
  132. }
  133. inline float ffract(float _a)
  134. {
  135. return _a - floorf(_a);
  136. }
  137. inline float fmod(float _a, float _b)
  138. {
  139. return fmodf(_a, _b);
  140. }
  141. inline bool fequal(float _a, float _b, float _epsilon)
  142. {
  143. // http://realtimecollisiondetection.net/blog/?p=89
  144. const float lhs = fabsolute(_a - _b);
  145. const float rhs = _epsilon * fmax3(1.0f, fabsolute(_a), fabsolute(_b) );
  146. return lhs <= rhs;
  147. }
  148. inline bool fequal(const float* __restrict _a, const float* __restrict _b, uint32_t _num, float _epsilon)
  149. {
  150. bool equal = fequal(_a[0], _b[0], _epsilon);
  151. for (uint32_t ii = 1; equal && ii < _num; ++ii)
  152. {
  153. equal = fequal(_a[ii], _b[ii], _epsilon);
  154. }
  155. return equal;
  156. }
  157. inline float fwrap(float _a, float _wrap)
  158. {
  159. const float mod = fmod(_a, _wrap);
  160. const float result = mod < 0.0f ? _wrap + mod : mod;
  161. return result;
  162. }
  163. // References:
  164. // - Bias And Gain Are Your Friend
  165. // http://blog.demofox.org/2012/09/24/bias-and-gain-are-your-friend/
  166. // - http://demofox.org/biasgain.html
  167. inline float fbias(float _time, float _bias)
  168. {
  169. return _time / ( ( (1.0f/_bias - 2.0f)*(1.0f - _time) ) + 1.0f);
  170. }
  171. inline float fgain(float _time, float _gain)
  172. {
  173. if (_time < 0.5f)
  174. {
  175. return fbias(_time * 2.0f, _gain) * 0.5f;
  176. }
  177. return fbias(_time * 2.0f - 1.0f, 1.0f - _gain) * 0.5f + 0.5f;
  178. }
  179. inline void vec3Move(float* __restrict _result, const float* __restrict _a)
  180. {
  181. _result[0] = _a[0];
  182. _result[1] = _a[1];
  183. _result[2] = _a[2];
  184. }
  185. inline void vec3Abs(float* __restrict _result, const float* __restrict _a)
  186. {
  187. _result[0] = fabsolute(_a[0]);
  188. _result[1] = fabsolute(_a[1]);
  189. _result[2] = fabsolute(_a[2]);
  190. }
  191. inline void vec3Neg(float* __restrict _result, const float* __restrict _a)
  192. {
  193. _result[0] = -_a[0];
  194. _result[1] = -_a[1];
  195. _result[2] = -_a[2];
  196. }
  197. inline void vec3Add(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  198. {
  199. _result[0] = _a[0] + _b[0];
  200. _result[1] = _a[1] + _b[1];
  201. _result[2] = _a[2] + _b[2];
  202. }
  203. inline void vec3Add(float* __restrict _result, const float* __restrict _a, float _b)
  204. {
  205. _result[0] = _a[0] + _b;
  206. _result[1] = _a[1] + _b;
  207. _result[2] = _a[2] + _b;
  208. }
  209. inline void vec3Sub(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  210. {
  211. _result[0] = _a[0] - _b[0];
  212. _result[1] = _a[1] - _b[1];
  213. _result[2] = _a[2] - _b[2];
  214. }
  215. inline void vec3Sub(float* __restrict _result, const float* __restrict _a, float _b)
  216. {
  217. _result[0] = _a[0] - _b;
  218. _result[1] = _a[1] - _b;
  219. _result[2] = _a[2] - _b;
  220. }
  221. inline void vec3Mul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  222. {
  223. _result[0] = _a[0] * _b[0];
  224. _result[1] = _a[1] * _b[1];
  225. _result[2] = _a[2] * _b[2];
  226. }
  227. inline void vec3Mul(float* __restrict _result, const float* __restrict _a, float _b)
  228. {
  229. _result[0] = _a[0] * _b;
  230. _result[1] = _a[1] * _b;
  231. _result[2] = _a[2] * _b;
  232. }
  233. inline float vec3Dot(const float* __restrict _a, const float* __restrict _b)
  234. {
  235. return _a[0]*_b[0] + _a[1]*_b[1] + _a[2]*_b[2];
  236. }
  237. inline void vec3Cross(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  238. {
  239. _result[0] = _a[1]*_b[2] - _a[2]*_b[1];
  240. _result[1] = _a[2]*_b[0] - _a[0]*_b[2];
  241. _result[2] = _a[0]*_b[1] - _a[1]*_b[0];
  242. }
  243. inline float vec3Length(const float* _a)
  244. {
  245. return fsqrt(vec3Dot(_a, _a) );
  246. }
  247. inline void vec3Lerp(float* __restrict _result, const float* __restrict _a, const float* __restrict _b, float _t)
  248. {
  249. _result[0] = flerp(_a[0], _b[0], _t);
  250. _result[1] = flerp(_a[1], _b[1], _t);
  251. _result[2] = flerp(_a[2], _b[2], _t);
  252. }
  253. inline void vec3Lerp(float* __restrict _result, const float* __restrict _a, const float* __restrict _b, const float* __restrict _c)
  254. {
  255. _result[0] = flerp(_a[0], _b[0], _c[0]);
  256. _result[1] = flerp(_a[1], _b[1], _c[1]);
  257. _result[2] = flerp(_a[2], _b[2], _c[2]);
  258. }
  259. inline float vec3Norm(float* __restrict _result, const float* __restrict _a)
  260. {
  261. const float len = vec3Length(_a);
  262. const float invLen = 1.0f/len;
  263. _result[0] = _a[0] * invLen;
  264. _result[1] = _a[1] * invLen;
  265. _result[2] = _a[2] * invLen;
  266. return len;
  267. }
  268. inline void vec3Min(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  269. {
  270. _result[0] = fmin(_a[0], _b[0]);
  271. _result[1] = fmin(_a[1], _b[1]);
  272. _result[2] = fmin(_a[2], _b[2]);
  273. }
  274. inline void vec3Max(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  275. {
  276. _result[0] = fmax(_a[0], _b[0]);
  277. _result[1] = fmax(_a[1], _b[1]);
  278. _result[2] = fmax(_a[2], _b[2]);
  279. }
  280. inline void vec3Rcp(float* __restrict _result, const float* __restrict _a)
  281. {
  282. _result[0] = 1.0f / _a[0];
  283. _result[1] = 1.0f / _a[1];
  284. _result[2] = 1.0f / _a[2];
  285. }
  286. inline void vec3TangentFrame(const float* __restrict _n, float* __restrict _t, float* __restrict _b)
  287. {
  288. const float nx = _n[0];
  289. const float ny = _n[1];
  290. const float nz = _n[2];
  291. if (bx::fabsolute(nx) > bx::fabsolute(nz) )
  292. {
  293. float invLen = 1.0f / bx::fsqrt(nx*nx + nz*nz);
  294. _t[0] = -nz * invLen;
  295. _t[1] = 0.0f;
  296. _t[2] = nx * invLen;
  297. }
  298. else
  299. {
  300. float invLen = 1.0f / bx::fsqrt(ny*ny + nz*nz);
  301. _t[0] = 0.0f;
  302. _t[1] = nz * invLen;
  303. _t[2] = -ny * invLen;
  304. }
  305. bx::vec3Cross(_b, _n, _t);
  306. }
  307. inline void quatIdentity(float* _result)
  308. {
  309. _result[0] = 0.0f;
  310. _result[1] = 0.0f;
  311. _result[2] = 0.0f;
  312. _result[3] = 1.0f;
  313. }
  314. inline void quatMove(float* __restrict _result, const float* __restrict _a)
  315. {
  316. _result[0] = _a[0];
  317. _result[1] = _a[1];
  318. _result[2] = _a[2];
  319. _result[3] = _a[3];
  320. }
  321. inline void quatMulXYZ(float* __restrict _result, const float* __restrict _qa, const float* __restrict _qb)
  322. {
  323. const float ax = _qa[0];
  324. const float ay = _qa[1];
  325. const float az = _qa[2];
  326. const float aw = _qa[3];
  327. const float bx = _qb[0];
  328. const float by = _qb[1];
  329. const float bz = _qb[2];
  330. const float bw = _qb[3];
  331. _result[0] = aw * bx + ax * bw + ay * bz - az * by;
  332. _result[1] = aw * by - ax * bz + ay * bw + az * bx;
  333. _result[2] = aw * bz + ax * by - ay * bx + az * bw;
  334. }
  335. inline void quatMul(float* __restrict _result, const float* __restrict _qa, const float* __restrict _qb)
  336. {
  337. const float ax = _qa[0];
  338. const float ay = _qa[1];
  339. const float az = _qa[2];
  340. const float aw = _qa[3];
  341. const float bx = _qb[0];
  342. const float by = _qb[1];
  343. const float bz = _qb[2];
  344. const float bw = _qb[3];
  345. _result[0] = aw * bx + ax * bw + ay * bz - az * by;
  346. _result[1] = aw * by - ax * bz + ay * bw + az * bx;
  347. _result[2] = aw * bz + ax * by - ay * bx + az * bw;
  348. _result[3] = aw * bw - ax * bx - ay * by - az * bz;
  349. }
  350. inline void quatInvert(float* __restrict _result, const float* __restrict _quat)
  351. {
  352. _result[0] = -_quat[0];
  353. _result[1] = -_quat[1];
  354. _result[2] = -_quat[2];
  355. _result[3] = _quat[3];
  356. }
  357. inline float quatDot(const float* __restrict _a, const float* __restrict _b)
  358. {
  359. return _a[0]*_b[0]
  360. + _a[1]*_b[1]
  361. + _a[2]*_b[2]
  362. + _a[3]*_b[3]
  363. ;
  364. }
  365. inline void quatNorm(float* __restrict _result, const float* __restrict _quat)
  366. {
  367. const float norm = quatDot(_quat, _quat);
  368. if (0.0f < norm)
  369. {
  370. const float invNorm = 1.0f / fsqrt(norm);
  371. _result[0] = _quat[0] * invNorm;
  372. _result[1] = _quat[1] * invNorm;
  373. _result[2] = _quat[2] * invNorm;
  374. _result[3] = _quat[3] * invNorm;
  375. }
  376. else
  377. {
  378. quatIdentity(_result);
  379. }
  380. }
  381. inline void quatToEuler(float* __restrict _result, const float* __restrict _quat)
  382. {
  383. const float x = _quat[0];
  384. const float y = _quat[1];
  385. const float z = _quat[2];
  386. const float w = _quat[3];
  387. const float yy = y * y;
  388. const float zz = z * z;
  389. const float xx = x * x;
  390. _result[0] = atan2f(2.0f * (x * w - y * z), 1.0f - 2.0f * (xx + zz) );
  391. _result[1] = atan2f(2.0f * (y * w + x * z), 1.0f - 2.0f * (yy + zz) );
  392. _result[2] = asinf (2.0f * (x * y + z * w) );
  393. }
  394. inline void quatRotateAxis(float* __restrict _result, const float* _axis, float _angle)
  395. {
  396. const float ha = _angle * 0.5f;
  397. const float ca = fcos(ha);
  398. const float sa = fsin(ha);
  399. _result[0] = _axis[0] * sa;
  400. _result[1] = _axis[1] * sa;
  401. _result[2] = _axis[2] * sa;
  402. _result[3] = ca;
  403. }
  404. inline void quatRotateX(float* _result, float _ax)
  405. {
  406. const float hx = _ax * 0.5f;
  407. const float cx = fcos(hx);
  408. const float sx = fsin(hx);
  409. _result[0] = sx;
  410. _result[1] = 0.0f;
  411. _result[2] = 0.0f;
  412. _result[3] = cx;
  413. }
  414. inline void quatRotateY(float* _result, float _ay)
  415. {
  416. const float hy = _ay * 0.5f;
  417. const float cy = fcos(hy);
  418. const float sy = fsin(hy);
  419. _result[0] = 0.0f;
  420. _result[1] = sy;
  421. _result[2] = 0.0f;
  422. _result[3] = cy;
  423. }
  424. inline void quatRotateZ(float* _result, float _az)
  425. {
  426. const float hz = _az * 0.5f;
  427. const float cz = fcos(hz);
  428. const float sz = fsin(hz);
  429. _result[0] = 0.0f;
  430. _result[1] = 0.0f;
  431. _result[2] = sz;
  432. _result[3] = cz;
  433. }
  434. inline void vec3MulQuat(float* __restrict _result, const float* __restrict _vec, const float* __restrict _quat)
  435. {
  436. float tmp0[4];
  437. quatInvert(tmp0, _quat);
  438. float qv[4];
  439. qv[0] = _vec[0];
  440. qv[1] = _vec[1];
  441. qv[2] = _vec[2];
  442. qv[3] = 0.0f;
  443. float tmp1[4];
  444. quatMul(tmp1, tmp0, qv);
  445. quatMulXYZ(_result, tmp1, _quat);
  446. }
  447. inline void mtxIdentity(float* _result)
  448. {
  449. memset(_result, 0, sizeof(float)*16);
  450. _result[0] = _result[5] = _result[10] = _result[15] = 1.0f;
  451. }
  452. inline void mtxTranslate(float* _result, float _tx, float _ty, float _tz)
  453. {
  454. mtxIdentity(_result);
  455. _result[12] = _tx;
  456. _result[13] = _ty;
  457. _result[14] = _tz;
  458. }
  459. inline void mtxScale(float* _result, float _sx, float _sy, float _sz)
  460. {
  461. memset(_result, 0, sizeof(float) * 16);
  462. _result[0] = _sx;
  463. _result[5] = _sy;
  464. _result[10] = _sz;
  465. _result[15] = 1.0f;
  466. }
  467. inline void mtxScale(float* _result, float _scale)
  468. {
  469. mtxScale(_result, _scale, _scale, _scale);
  470. }
  471. inline void mtxFromNormal(float* __restrict _result, const float* __restrict _normal, float _scale, const float* __restrict _pos)
  472. {
  473. float tangent[3];
  474. float bitangent[3];
  475. vec3TangentFrame(_normal, tangent, bitangent);
  476. vec3Mul(&_result[ 0], bitangent, _scale);
  477. vec3Mul(&_result[ 4], _normal, _scale);
  478. vec3Mul(&_result[ 8], tangent, _scale);
  479. _result[ 3] = 0.0f;
  480. _result[ 7] = 0.0f;
  481. _result[11] = 0.0f;
  482. _result[12] = _pos[0];
  483. _result[13] = _pos[1];
  484. _result[14] = _pos[2];
  485. _result[15] = 1.0f;
  486. }
  487. inline void mtxQuat(float* __restrict _result, const float* __restrict _quat)
  488. {
  489. const float x = _quat[0];
  490. const float y = _quat[1];
  491. const float z = _quat[2];
  492. const float w = _quat[3];
  493. const float x2 = x + x;
  494. const float y2 = y + y;
  495. const float z2 = z + z;
  496. const float x2x = x2 * x;
  497. const float x2y = x2 * y;
  498. const float x2z = x2 * z;
  499. const float x2w = x2 * w;
  500. const float y2y = y2 * y;
  501. const float y2z = y2 * z;
  502. const float y2w = y2 * w;
  503. const float z2z = z2 * z;
  504. const float z2w = z2 * w;
  505. _result[ 0] = 1.0f - (y2y + z2z);
  506. _result[ 1] = x2y - z2w;
  507. _result[ 2] = x2z + y2w;
  508. _result[ 3] = 0.0f;
  509. _result[ 4] = x2y + z2w;
  510. _result[ 5] = 1.0f - (x2x + z2z);
  511. _result[ 6] = y2z - x2w;
  512. _result[ 7] = 0.0f;
  513. _result[ 8] = x2z - y2w;
  514. _result[ 9] = y2z + x2w;
  515. _result[10] = 1.0f - (x2x + y2y);
  516. _result[11] = 0.0f;
  517. _result[12] = 0.0f;
  518. _result[13] = 0.0f;
  519. _result[14] = 0.0f;
  520. _result[15] = 1.0f;
  521. }
  522. inline void mtxQuatTranslation(float* __restrict _result, const float* __restrict _quat, const float* __restrict _translation)
  523. {
  524. mtxQuat(_result, _quat);
  525. _result[12] = -(_result[0]*_translation[0] + _result[4]*_translation[1] + _result[ 8]*_translation[2]);
  526. _result[13] = -(_result[1]*_translation[0] + _result[5]*_translation[1] + _result[ 9]*_translation[2]);
  527. _result[14] = -(_result[2]*_translation[0] + _result[6]*_translation[1] + _result[10]*_translation[2]);
  528. }
  529. inline void mtxQuatTranslationHMD(float* __restrict _result, const float* __restrict _quat, const float* __restrict _translation)
  530. {
  531. float quat[4];
  532. quat[0] = -_quat[0];
  533. quat[1] = -_quat[1];
  534. quat[2] = _quat[2];
  535. quat[3] = _quat[3];
  536. mtxQuatTranslation(_result, quat, _translation);
  537. }
  538. inline void mtxLookAt_Impl(float* __restrict _result, const float* __restrict _eye, const float* __restrict _view, const float* __restrict _up = NULL)
  539. {
  540. float up[3] = { 0.0f, 1.0f, 0.0f };
  541. if (NULL != _up)
  542. {
  543. up[0] = _up[0];
  544. up[1] = _up[1];
  545. up[2] = _up[2];
  546. }
  547. float tmp[4];
  548. vec3Cross(tmp, up, _view);
  549. float right[4];
  550. vec3Norm(right, tmp);
  551. vec3Cross(up, _view, right);
  552. memset(_result, 0, sizeof(float)*16);
  553. _result[ 0] = right[0];
  554. _result[ 1] = up[0];
  555. _result[ 2] = _view[0];
  556. _result[ 4] = right[1];
  557. _result[ 5] = up[1];
  558. _result[ 6] = _view[1];
  559. _result[ 8] = right[2];
  560. _result[ 9] = up[2];
  561. _result[10] = _view[2];
  562. _result[12] = -vec3Dot(right, _eye);
  563. _result[13] = -vec3Dot(up, _eye);
  564. _result[14] = -vec3Dot(_view, _eye);
  565. _result[15] = 1.0f;
  566. }
  567. inline void mtxLookAtLh(float* __restrict _result, const float* __restrict _eye, const float* __restrict _at, const float* __restrict _up = NULL)
  568. {
  569. float tmp[4];
  570. vec3Sub(tmp, _at, _eye);
  571. float view[4];
  572. vec3Norm(view, tmp);
  573. mtxLookAt_Impl(_result, _eye, view, _up);
  574. }
  575. inline void mtxLookAtRh(float* __restrict _result, const float* __restrict _eye, const float* __restrict _at, const float* __restrict _up = NULL)
  576. {
  577. float tmp[4];
  578. vec3Sub(tmp, _eye, _at);
  579. float view[4];
  580. vec3Norm(view, tmp);
  581. mtxLookAt_Impl(_result, _eye, view, _up);
  582. }
  583. inline void mtxLookAt(float* __restrict _result, const float* __restrict _eye, const float* __restrict _at, const float* __restrict _up = NULL)
  584. {
  585. mtxLookAtLh(_result, _eye, _at, _up);
  586. }
  587. template <Handness::Enum HandnessT>
  588. inline void mtxProjXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, float _far, bool _oglNdc = false)
  589. {
  590. const float diff = _far-_near;
  591. const float aa = _oglNdc ? (_far+_near)/diff : _far/diff;
  592. const float bb = _oglNdc ? (2.0f*_far*_near)/diff : _near*aa;
  593. memset(_result, 0, sizeof(float)*16);
  594. _result[ 0] = _width;
  595. _result[ 5] = _height;
  596. _result[ 8] = (Handness::Right == HandnessT) ? _x : -_x;
  597. _result[ 9] = (Handness::Right == HandnessT) ? _y : -_y;
  598. _result[10] = (Handness::Right == HandnessT) ? -aa : aa;
  599. _result[11] = (Handness::Right == HandnessT) ? -1.0f : 1.0f;
  600. _result[14] = -bb;
  601. }
  602. template <Handness::Enum HandnessT>
  603. inline void mtxProj_impl(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc = false)
  604. {
  605. const float invDiffRl = 1.0f/(_rt - _lt);
  606. const float invDiffUd = 1.0f/(_ut - _dt);
  607. const float width = 2.0f*_near * invDiffRl;
  608. const float height = 2.0f*_near * invDiffUd;
  609. const float xx = (_rt + _lt) * invDiffRl;
  610. const float yy = (_ut + _dt) * invDiffUd;
  611. mtxProjXYWH<HandnessT>(_result, xx, yy, width, height, _near, _far, _oglNdc);
  612. }
  613. template <Handness::Enum HandnessT>
  614. inline void mtxProj_impl(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc = false)
  615. {
  616. mtxProj_impl<HandnessT>(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _far, _oglNdc);
  617. }
  618. template <Handness::Enum HandnessT>
  619. inline void mtxProj_impl(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc = false)
  620. {
  621. const float height = 1.0f/tanf(toRad(_fovy)*0.5f);
  622. const float width = height * 1.0f/_aspect;
  623. mtxProjXYWH<HandnessT>(_result, 0.0f, 0.0f, width, height, _near, _far, _oglNdc);
  624. }
  625. inline void mtxProj(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc = false)
  626. {
  627. mtxProj_impl<Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
  628. }
  629. inline void mtxProj(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc = false)
  630. {
  631. mtxProj_impl<Handness::Left>(_result, _fov, _near, _far, _oglNdc);
  632. }
  633. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc = false)
  634. {
  635. mtxProj_impl<Handness::Left>(_result, _fovy, _aspect, _near, _far, _oglNdc);
  636. }
  637. inline void mtxProjLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc = false)
  638. {
  639. mtxProj_impl<Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
  640. }
  641. inline void mtxProjLh(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc = false)
  642. {
  643. mtxProj_impl<Handness::Left>(_result, _fov, _near, _far, _oglNdc);
  644. }
  645. inline void mtxProjLh(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc = false)
  646. {
  647. mtxProj_impl<Handness::Left>(_result, _fovy, _aspect, _near, _far, _oglNdc);
  648. }
  649. inline void mtxProjRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc = false)
  650. {
  651. mtxProj_impl<Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
  652. }
  653. inline void mtxProjRh(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc = false)
  654. {
  655. mtxProj_impl<Handness::Right>(_result, _fov, _near, _far, _oglNdc);
  656. }
  657. inline void mtxProjRh(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc = false)
  658. {
  659. mtxProj_impl<Handness::Right>(_result, _fovy, _aspect, _near, _far, _oglNdc);
  660. }
  661. template <NearFar::Enum NearFarT, Handness::Enum HandnessT>
  662. inline void mtxProjInfXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, bool _oglNdc = false)
  663. {
  664. float aa;
  665. float bb;
  666. if (BX_ENABLED(NearFar::Reverse == NearFarT) )
  667. {
  668. aa = _oglNdc ? -1.0f : 0.0f;
  669. bb = _oglNdc ? -2.0f*_near : -_near;
  670. }
  671. else
  672. {
  673. aa = 1.0f;
  674. bb = _oglNdc ? 2.0f*_near : _near;
  675. }
  676. memset(_result, 0, sizeof(float)*16);
  677. _result[ 0] = _width;
  678. _result[ 5] = _height;
  679. _result[ 8] = (Handness::Right == HandnessT) ? _x : -_x;
  680. _result[ 9] = (Handness::Right == HandnessT) ? _y : -_y;
  681. _result[10] = (Handness::Right == HandnessT) ? -aa : aa;
  682. _result[11] = (Handness::Right == HandnessT) ? -1.0f : 1.0f;
  683. _result[14] = -bb;
  684. }
  685. template <NearFar::Enum NearFarT, Handness::Enum HandnessT>
  686. inline void mtxProjInf_impl(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc = false)
  687. {
  688. const float invDiffRl = 1.0f/(_rt - _lt);
  689. const float invDiffUd = 1.0f/(_ut - _dt);
  690. const float width = 2.0f*_near * invDiffRl;
  691. const float height = 2.0f*_near * invDiffUd;
  692. const float xx = (_rt + _lt) * invDiffRl;
  693. const float yy = (_ut + _dt) * invDiffUd;
  694. mtxProjInfXYWH<NearFarT,HandnessT>(_result, xx, yy, width, height, _near, _oglNdc);
  695. }
  696. template <NearFar::Enum NearFarT, Handness::Enum HandnessT>
  697. inline void mtxProjInf_impl(float* _result, const float _fov[4], float _near, bool _oglNdc = false)
  698. {
  699. mtxProjInf_impl<NearFarT,HandnessT>(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _oglNdc);
  700. }
  701. template <NearFar::Enum NearFarT, Handness::Enum HandnessT>
  702. inline void mtxProjInf_impl(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc = false)
  703. {
  704. const float height = 1.0f/tanf(toRad(_fovy)*0.5f);
  705. const float width = height * 1.0f/_aspect;
  706. mtxProjInfXYWH<NearFarT,HandnessT>(_result, 0.0f, 0.0f, width, height, _near, _oglNdc);
  707. }
  708. inline void mtxProjInf(float* _result, const float _fov[4], float _near, bool _oglNdc = false)
  709. {
  710. mtxProjInf_impl<NearFar::Default,Handness::Left>(_result, _fov, _near, _oglNdc);
  711. }
  712. inline void mtxProjInf(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc = false)
  713. {
  714. mtxProjInf_impl<NearFar::Default,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  715. }
  716. inline void mtxProjInf(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc = false)
  717. {
  718. mtxProjInf_impl<NearFar::Default,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
  719. }
  720. inline void mtxProjInfLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc = false)
  721. {
  722. mtxProjInf_impl<NearFar::Default,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  723. }
  724. inline void mtxProjInfLh(float* _result, const float _fov[4], float _near, bool _oglNdc = false)
  725. {
  726. mtxProjInf_impl<NearFar::Default,Handness::Left>(_result, _fov, _near, _oglNdc);
  727. }
  728. inline void mtxProjInfLh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc = false)
  729. {
  730. mtxProjInf_impl<NearFar::Default,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
  731. }
  732. inline void mtxProjInfRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc = false)
  733. {
  734. mtxProjInf_impl<NearFar::Default,Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  735. }
  736. inline void mtxProjInfRh(float* _result, const float _fov[4], float _near, bool _oglNdc = false)
  737. {
  738. mtxProjInf_impl<NearFar::Default,Handness::Right>(_result, _fov, _near, _oglNdc);
  739. }
  740. inline void mtxProjInfRh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc = false)
  741. {
  742. mtxProjInf_impl<NearFar::Default,Handness::Right>(_result, _fovy, _aspect, _near, _oglNdc);
  743. }
  744. inline void mtxProjRevInfLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc = false)
  745. {
  746. mtxProjInf_impl<NearFar::Reverse,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  747. }
  748. inline void mtxProjRevInfLh(float* _result, const float _fov[4], float _near, bool _oglNdc = false)
  749. {
  750. mtxProjInf_impl<NearFar::Reverse,Handness::Left>(_result, _fov, _near, _oglNdc);
  751. }
  752. inline void mtxProjRevInfLh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc = false)
  753. {
  754. mtxProjInf_impl<NearFar::Reverse,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
  755. }
  756. inline void mtxProjRevInfRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc = false)
  757. {
  758. mtxProjInf_impl<NearFar::Reverse,Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  759. }
  760. inline void mtxProjRevInfRh(float* _result, const float _fov[4], float _near, bool _oglNdc = false)
  761. {
  762. mtxProjInf_impl<NearFar::Reverse,Handness::Right>(_result, _fov, _near, _oglNdc);
  763. }
  764. inline void mtxProjRevInfRh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc = false)
  765. {
  766. mtxProjInf_impl<NearFar::Reverse,Handness::Right>(_result, _fovy, _aspect, _near, _oglNdc);
  767. }
  768. template <Handness::Enum HandnessT>
  769. inline void mtxOrtho_impl(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset = 0.0f, bool _oglNdc = false)
  770. {
  771. const float aa = 2.0f/(_right - _left);
  772. const float bb = 2.0f/(_top - _bottom);
  773. const float cc = (_oglNdc ? 2.0f : 1.0f) / (_far - _near);
  774. const float dd = (_left + _right)/(_left - _right);
  775. const float ee = (_top + _bottom)/(_bottom - _top);
  776. const float ff = _oglNdc ? (_near + _far)/(_near - _far) : _near/(_near - _far);
  777. memset(_result, 0, sizeof(float)*16);
  778. _result[ 0] = aa;
  779. _result[ 5] = bb;
  780. _result[10] = (Handness::Right == HandnessT) ? -cc : cc;
  781. _result[12] = dd + _offset;
  782. _result[13] = ee;
  783. _result[14] = ff;
  784. _result[15] = 1.0f;
  785. }
  786. inline void mtxOrtho(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset = 0.0f, bool _oglNdc = false)
  787. {
  788. mtxOrtho_impl<Handness::Left>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
  789. }
  790. inline void mtxOrthoLh(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset = 0.0f, bool _oglNdc = false)
  791. {
  792. mtxOrtho_impl<Handness::Left>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
  793. }
  794. inline void mtxOrthoRh(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset = 0.0f, bool _oglNdc = false)
  795. {
  796. mtxOrtho_impl<Handness::Right>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
  797. }
  798. inline void mtxRotateX(float* _result, float _ax)
  799. {
  800. const float sx = fsin(_ax);
  801. const float cx = fcos(_ax);
  802. memset(_result, 0, sizeof(float)*16);
  803. _result[ 0] = 1.0f;
  804. _result[ 5] = cx;
  805. _result[ 6] = -sx;
  806. _result[ 9] = sx;
  807. _result[10] = cx;
  808. _result[15] = 1.0f;
  809. }
  810. inline void mtxRotateY(float* _result, float _ay)
  811. {
  812. const float sy = fsin(_ay);
  813. const float cy = fcos(_ay);
  814. memset(_result, 0, sizeof(float)*16);
  815. _result[ 0] = cy;
  816. _result[ 2] = sy;
  817. _result[ 5] = 1.0f;
  818. _result[ 8] = -sy;
  819. _result[10] = cy;
  820. _result[15] = 1.0f;
  821. }
  822. inline void mtxRotateZ(float* _result, float _az)
  823. {
  824. const float sz = fsin(_az);
  825. const float cz = fcos(_az);
  826. memset(_result, 0, sizeof(float)*16);
  827. _result[ 0] = cz;
  828. _result[ 1] = -sz;
  829. _result[ 4] = sz;
  830. _result[ 5] = cz;
  831. _result[10] = 1.0f;
  832. _result[15] = 1.0f;
  833. }
  834. inline void mtxRotateXY(float* _result, float _ax, float _ay)
  835. {
  836. const float sx = fsin(_ax);
  837. const float cx = fcos(_ax);
  838. const float sy = fsin(_ay);
  839. const float cy = fcos(_ay);
  840. memset(_result, 0, sizeof(float)*16);
  841. _result[ 0] = cy;
  842. _result[ 2] = sy;
  843. _result[ 4] = sx*sy;
  844. _result[ 5] = cx;
  845. _result[ 6] = -sx*cy;
  846. _result[ 8] = -cx*sy;
  847. _result[ 9] = sx;
  848. _result[10] = cx*cy;
  849. _result[15] = 1.0f;
  850. }
  851. inline void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az)
  852. {
  853. const float sx = fsin(_ax);
  854. const float cx = fcos(_ax);
  855. const float sy = fsin(_ay);
  856. const float cy = fcos(_ay);
  857. const float sz = fsin(_az);
  858. const float cz = fcos(_az);
  859. memset(_result, 0, sizeof(float)*16);
  860. _result[ 0] = cy*cz;
  861. _result[ 1] = -cy*sz;
  862. _result[ 2] = sy;
  863. _result[ 4] = cz*sx*sy + cx*sz;
  864. _result[ 5] = cx*cz - sx*sy*sz;
  865. _result[ 6] = -cy*sx;
  866. _result[ 8] = -cx*cz*sy + sx*sz;
  867. _result[ 9] = cz*sx + cx*sy*sz;
  868. _result[10] = cx*cy;
  869. _result[15] = 1.0f;
  870. }
  871. inline void mtxRotateZYX(float* _result, float _ax, float _ay, float _az)
  872. {
  873. const float sx = fsin(_ax);
  874. const float cx = fcos(_ax);
  875. const float sy = fsin(_ay);
  876. const float cy = fcos(_ay);
  877. const float sz = fsin(_az);
  878. const float cz = fcos(_az);
  879. memset(_result, 0, sizeof(float)*16);
  880. _result[ 0] = cy*cz;
  881. _result[ 1] = cz*sx*sy-cx*sz;
  882. _result[ 2] = cx*cz*sy+sx*sz;
  883. _result[ 4] = cy*sz;
  884. _result[ 5] = cx*cz + sx*sy*sz;
  885. _result[ 6] = -cz*sx + cx*sy*sz;
  886. _result[ 8] = -sy;
  887. _result[ 9] = cy*sx;
  888. _result[10] = cx*cy;
  889. _result[15] = 1.0f;
  890. };
  891. inline void mtxSRT(float* _result, float _sx, float _sy, float _sz, float _ax, float _ay, float _az, float _tx, float _ty, float _tz)
  892. {
  893. const float sx = fsin(_ax);
  894. const float cx = fcos(_ax);
  895. const float sy = fsin(_ay);
  896. const float cy = fcos(_ay);
  897. const float sz = fsin(_az);
  898. const float cz = fcos(_az);
  899. const float sxsz = sx*sz;
  900. const float cycz = cy*cz;
  901. _result[ 0] = _sx * (cycz - sxsz*sy);
  902. _result[ 1] = _sx * -cx*sz;
  903. _result[ 2] = _sx * (cz*sy + cy*sxsz);
  904. _result[ 3] = 0.0f;
  905. _result[ 4] = _sy * (cz*sx*sy + cy*sz);
  906. _result[ 5] = _sy * cx*cz;
  907. _result[ 6] = _sy * (sy*sz -cycz*sx);
  908. _result[ 7] = 0.0f;
  909. _result[ 8] = _sz * -cx*sy;
  910. _result[ 9] = _sz * sx;
  911. _result[10] = _sz * cx*cy;
  912. _result[11] = 0.0f;
  913. _result[12] = _tx;
  914. _result[13] = _ty;
  915. _result[14] = _tz;
  916. _result[15] = 1.0f;
  917. }
  918. inline void vec3MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
  919. {
  920. _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _mat[12];
  921. _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _mat[13];
  922. _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _mat[14];
  923. }
  924. inline void vec3MulMtxH(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
  925. {
  926. float xx = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _mat[12];
  927. float yy = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _mat[13];
  928. float zz = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _mat[14];
  929. float ww = _vec[0] * _mat[ 3] + _vec[1] * _mat[7] + _vec[2] * _mat[11] + _mat[15];
  930. float invW = fsign(ww)/ww;
  931. _result[0] = xx*invW;
  932. _result[1] = yy*invW;
  933. _result[2] = zz*invW;
  934. }
  935. inline void vec4MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
  936. {
  937. _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _vec[3] * _mat[12];
  938. _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _vec[3] * _mat[13];
  939. _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _vec[3] * _mat[14];
  940. _result[3] = _vec[0] * _mat[ 3] + _vec[1] * _mat[7] + _vec[2] * _mat[11] + _vec[3] * _mat[15];
  941. }
  942. inline void mtxMul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  943. {
  944. vec4MulMtx(&_result[ 0], &_a[ 0], _b);
  945. vec4MulMtx(&_result[ 4], &_a[ 4], _b);
  946. vec4MulMtx(&_result[ 8], &_a[ 8], _b);
  947. vec4MulMtx(&_result[12], &_a[12], _b);
  948. }
  949. inline void mtxTranspose(float* __restrict _result, const float* __restrict _a)
  950. {
  951. _result[ 0] = _a[ 0];
  952. _result[ 4] = _a[ 1];
  953. _result[ 8] = _a[ 2];
  954. _result[12] = _a[ 3];
  955. _result[ 1] = _a[ 4];
  956. _result[ 5] = _a[ 5];
  957. _result[ 9] = _a[ 6];
  958. _result[13] = _a[ 7];
  959. _result[ 2] = _a[ 8];
  960. _result[ 6] = _a[ 9];
  961. _result[10] = _a[10];
  962. _result[14] = _a[11];
  963. _result[ 3] = _a[12];
  964. _result[ 7] = _a[13];
  965. _result[11] = _a[14];
  966. _result[15] = _a[15];
  967. }
  968. inline void mtx3Inverse(float* __restrict _result, const float* __restrict _a)
  969. {
  970. float xx = _a[0];
  971. float xy = _a[1];
  972. float xz = _a[2];
  973. float yx = _a[3];
  974. float yy = _a[4];
  975. float yz = _a[5];
  976. float zx = _a[6];
  977. float zy = _a[7];
  978. float zz = _a[8];
  979. float det = 0.0f;
  980. det += xx * (yy*zz - yz*zy);
  981. det -= xy * (yx*zz - yz*zx);
  982. det += xz * (yx*zy - yy*zx);
  983. float invDet = 1.0f/det;
  984. _result[0] = +(yy*zz - yz*zy) * invDet;
  985. _result[1] = -(xy*zz - xz*zy) * invDet;
  986. _result[2] = +(xy*yz - xz*yy) * invDet;
  987. _result[3] = -(yx*zz - yz*zx) * invDet;
  988. _result[4] = +(xx*zz - xz*zx) * invDet;
  989. _result[5] = -(xx*yz - xz*yx) * invDet;
  990. _result[6] = +(yx*zy - yy*zx) * invDet;
  991. _result[7] = -(xx*zy - xy*zx) * invDet;
  992. _result[8] = +(xx*yy - xy*yx) * invDet;
  993. }
  994. inline void mtxInverse(float* __restrict _result, const float* __restrict _a)
  995. {
  996. float xx = _a[ 0];
  997. float xy = _a[ 1];
  998. float xz = _a[ 2];
  999. float xw = _a[ 3];
  1000. float yx = _a[ 4];
  1001. float yy = _a[ 5];
  1002. float yz = _a[ 6];
  1003. float yw = _a[ 7];
  1004. float zx = _a[ 8];
  1005. float zy = _a[ 9];
  1006. float zz = _a[10];
  1007. float zw = _a[11];
  1008. float wx = _a[12];
  1009. float wy = _a[13];
  1010. float wz = _a[14];
  1011. float ww = _a[15];
  1012. float det = 0.0f;
  1013. det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) );
  1014. det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) );
  1015. det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) );
  1016. det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) );
  1017. float invDet = 1.0f/det;
  1018. _result[ 0] = +(yy*(zz*ww - wz*zw) - yz*(zy*ww - wy*zw) + yw*(zy*wz - wy*zz) ) * invDet;
  1019. _result[ 1] = -(xy*(zz*ww - wz*zw) - xz*(zy*ww - wy*zw) + xw*(zy*wz - wy*zz) ) * invDet;
  1020. _result[ 2] = +(xy*(yz*ww - wz*yw) - xz*(yy*ww - wy*yw) + xw*(yy*wz - wy*yz) ) * invDet;
  1021. _result[ 3] = -(xy*(yz*zw - zz*yw) - xz*(yy*zw - zy*yw) + xw*(yy*zz - zy*yz) ) * invDet;
  1022. _result[ 4] = -(yx*(zz*ww - wz*zw) - yz*(zx*ww - wx*zw) + yw*(zx*wz - wx*zz) ) * invDet;
  1023. _result[ 5] = +(xx*(zz*ww - wz*zw) - xz*(zx*ww - wx*zw) + xw*(zx*wz - wx*zz) ) * invDet;
  1024. _result[ 6] = -(xx*(yz*ww - wz*yw) - xz*(yx*ww - wx*yw) + xw*(yx*wz - wx*yz) ) * invDet;
  1025. _result[ 7] = +(xx*(yz*zw - zz*yw) - xz*(yx*zw - zx*yw) + xw*(yx*zz - zx*yz) ) * invDet;
  1026. _result[ 8] = +(yx*(zy*ww - wy*zw) - yy*(zx*ww - wx*zw) + yw*(zx*wy - wx*zy) ) * invDet;
  1027. _result[ 9] = -(xx*(zy*ww - wy*zw) - xy*(zx*ww - wx*zw) + xw*(zx*wy - wx*zy) ) * invDet;
  1028. _result[10] = +(xx*(yy*ww - wy*yw) - xy*(yx*ww - wx*yw) + xw*(yx*wy - wx*yy) ) * invDet;
  1029. _result[11] = -(xx*(yy*zw - zy*yw) - xy*(yx*zw - zx*yw) + xw*(yx*zy - zx*yy) ) * invDet;
  1030. _result[12] = -(yx*(zy*wz - wy*zz) - yy*(zx*wz - wx*zz) + yz*(zx*wy - wx*zy) ) * invDet;
  1031. _result[13] = +(xx*(zy*wz - wy*zz) - xy*(zx*wz - wx*zz) + xz*(zx*wy - wx*zy) ) * invDet;
  1032. _result[14] = -(xx*(yy*wz - wy*yz) - xy*(yx*wz - wx*yz) + xz*(yx*wy - wx*yy) ) * invDet;
  1033. _result[15] = +(xx*(yy*zz - zy*yz) - xy*(yx*zz - zx*yz) + xz*(yx*zy - zx*yy) ) * invDet;
  1034. }
  1035. /// Convert LH to RH projection matrix and vice versa.
  1036. inline void mtxProjFlipHandedness(float* __restrict _dst, const float* __restrict _src)
  1037. {
  1038. _dst[ 0] = -_src[ 0];
  1039. _dst[ 1] = -_src[ 1];
  1040. _dst[ 2] = -_src[ 2];
  1041. _dst[ 3] = -_src[ 3];
  1042. _dst[ 4] = _src[ 4];
  1043. _dst[ 5] = _src[ 5];
  1044. _dst[ 6] = _src[ 6];
  1045. _dst[ 7] = _src[ 7];
  1046. _dst[ 8] = -_src[ 8];
  1047. _dst[ 9] = -_src[ 9];
  1048. _dst[10] = -_src[10];
  1049. _dst[11] = -_src[11];
  1050. _dst[12] = _src[12];
  1051. _dst[13] = _src[13];
  1052. _dst[14] = _src[14];
  1053. _dst[15] = _src[15];
  1054. }
  1055. /// Convert LH to RH view matrix and vice versa.
  1056. inline void mtxViewFlipHandedness(float* __restrict _dst, const float* __restrict _src)
  1057. {
  1058. _dst[ 0] = -_src[ 0];
  1059. _dst[ 1] = _src[ 1];
  1060. _dst[ 2] = -_src[ 2];
  1061. _dst[ 3] = _src[ 3];
  1062. _dst[ 4] = -_src[ 4];
  1063. _dst[ 5] = _src[ 5];
  1064. _dst[ 6] = -_src[ 6];
  1065. _dst[ 7] = _src[ 7];
  1066. _dst[ 8] = -_src[ 8];
  1067. _dst[ 9] = _src[ 9];
  1068. _dst[10] = -_src[10];
  1069. _dst[11] = _src[11];
  1070. _dst[12] = -_src[12];
  1071. _dst[13] = _src[13];
  1072. _dst[14] = -_src[14];
  1073. _dst[15] = _src[15];
  1074. }
  1075. inline void calcNormal(float _result[3], float _va[3], float _vb[3], float _vc[3])
  1076. {
  1077. float ba[3];
  1078. vec3Sub(ba, _vb, _va);
  1079. float ca[3];
  1080. vec3Sub(ca, _vc, _va);
  1081. float baxca[3];
  1082. vec3Cross(baxca, ba, ca);
  1083. vec3Norm(_result, baxca);
  1084. }
  1085. inline void calcPlane(float _result[4], float _va[3], float _vb[3], float _vc[3])
  1086. {
  1087. float normal[3];
  1088. calcNormal(normal, _va, _vb, _vc);
  1089. _result[0] = normal[0];
  1090. _result[1] = normal[1];
  1091. _result[2] = normal[2];
  1092. _result[3] = -vec3Dot(normal, _va);
  1093. }
  1094. inline void calcLinearFit2D(float _result[2], const void* _points, uint32_t _stride, uint32_t _numPoints)
  1095. {
  1096. float sumX = 0.0f;
  1097. float sumY = 0.0f;
  1098. float sumXX = 0.0f;
  1099. float sumXY = 0.0f;
  1100. const uint8_t* ptr = (const uint8_t*)_points;
  1101. for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
  1102. {
  1103. const float* point = (const float*)ptr;
  1104. float xx = point[0];
  1105. float yy = point[1];
  1106. sumX += xx;
  1107. sumY += yy;
  1108. sumXX += xx*xx;
  1109. sumXY += xx*yy;
  1110. }
  1111. // [ sum(x^2) sum(x) ] [ A ] = [ sum(x*y) ]
  1112. // [ sum(x) numPoints ] [ B ] [ sum(y) ]
  1113. float det = (sumXX*_numPoints - sumX*sumX);
  1114. float invDet = 1.0f/det;
  1115. _result[0] = (-sumX * sumY + _numPoints * sumXY) * invDet;
  1116. _result[1] = (sumXX * sumY - sumX * sumXY) * invDet;
  1117. }
  1118. inline void calcLinearFit3D(float _result[3], const void* _points, uint32_t _stride, uint32_t _numPoints)
  1119. {
  1120. float sumX = 0.0f;
  1121. float sumY = 0.0f;
  1122. float sumZ = 0.0f;
  1123. float sumXX = 0.0f;
  1124. float sumXY = 0.0f;
  1125. float sumXZ = 0.0f;
  1126. float sumYY = 0.0f;
  1127. float sumYZ = 0.0f;
  1128. const uint8_t* ptr = (const uint8_t*)_points;
  1129. for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
  1130. {
  1131. const float* point = (const float*)ptr;
  1132. float xx = point[0];
  1133. float yy = point[1];
  1134. float zz = point[2];
  1135. sumX += xx;
  1136. sumY += yy;
  1137. sumZ += zz;
  1138. sumXX += xx*xx;
  1139. sumXY += xx*yy;
  1140. sumXZ += xx*zz;
  1141. sumYY += yy*yy;
  1142. sumYZ += yy*zz;
  1143. }
  1144. // [ sum(x^2) sum(x*y) sum(x) ] [ A ] [ sum(x*z) ]
  1145. // [ sum(x*y) sum(y^2) sum(y) ] [ B ] = [ sum(y*z) ]
  1146. // [ sum(x) sum(y) numPoints ] [ C ] [ sum(z) ]
  1147. float mtx[9] =
  1148. {
  1149. sumXX, sumXY, sumX,
  1150. sumXY, sumYY, sumY,
  1151. sumX, sumY, float(_numPoints),
  1152. };
  1153. float invMtx[9];
  1154. mtx3Inverse(invMtx, mtx);
  1155. _result[0] = invMtx[0]*sumXZ + invMtx[1]*sumYZ + invMtx[2]*sumZ;
  1156. _result[1] = invMtx[3]*sumXZ + invMtx[4]*sumYZ + invMtx[5]*sumZ;
  1157. _result[2] = invMtx[6]*sumXZ + invMtx[7]*sumYZ + invMtx[8]*sumZ;
  1158. }
  1159. inline void rgbToHsv(float _hsv[3], const float _rgb[3])
  1160. {
  1161. const float rr = _rgb[0];
  1162. const float gg = _rgb[1];
  1163. const float bb = _rgb[2];
  1164. const float s0 = fstep(bb, gg);
  1165. const float px = flerp(bb, gg, s0);
  1166. const float py = flerp(gg, bb, s0);
  1167. const float pz = flerp(-1.0f, 0.0f, s0);
  1168. const float pw = flerp(2.0f/3.0f, -1.0f/3.0f, s0);
  1169. const float s1 = fstep(px, rr);
  1170. const float qx = flerp(px, rr, s1);
  1171. const float qy = py;
  1172. const float qz = flerp(pw, pz, s1);
  1173. const float qw = flerp(rr, px, s1);
  1174. const float dd = qx - fmin(qw, qy);
  1175. const float ee = 1.0e-10f;
  1176. _hsv[0] = fabsolute(qz + (qw - qy) / (6.0f * dd + ee) );
  1177. _hsv[1] = dd / (qx + ee);
  1178. _hsv[2] = qx;
  1179. }
  1180. inline void hsvToRgb(float _rgb[3], const float _hsv[3])
  1181. {
  1182. const float hh = _hsv[0];
  1183. const float ss = _hsv[1];
  1184. const float vv = _hsv[2];
  1185. const float px = fabsolute(ffract(hh + 1.0f ) * 6.0f - 3.0f);
  1186. const float py = fabsolute(ffract(hh + 2.0f/3.0f) * 6.0f - 3.0f);
  1187. const float pz = fabsolute(ffract(hh + 1.0f/3.0f) * 6.0f - 3.0f);
  1188. _rgb[0] = vv * flerp(1.0f, fsaturate(px - 1.0f), ss);
  1189. _rgb[1] = vv * flerp(1.0f, fsaturate(py - 1.0f), ss);
  1190. _rgb[2] = vv * flerp(1.0f, fsaturate(pz - 1.0f), ss);
  1191. }
  1192. } // namespace bx
  1193. #endif // BX_FPU_MATH_H_HEADER_GUARD