| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675 |
- /*
- * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
- * License: http://www.opensource.org/licenses/BSD-2-Clause
- */
- // FPU math lib
- #ifndef BX_FPU_MATH_H_HEADER_GUARD
- #define BX_FPU_MATH_H_HEADER_GUARD
- #include <math.h>
- #include <string.h>
- namespace bx
- {
- const float pi = 3.14159265358979323846f;
- const float piHalf = 1.57079632679489661923f;
- inline float toRad(float _deg)
- {
- return _deg * pi / 180.0f;;
- }
- inline float toDeg(float _rad)
- {
- return _rad * 180.0f / pi;
- }
- inline float fmin(float _a, float _b)
- {
- return _a < _b ? _a : _b;
- }
- inline float fmax(float _a, float _b)
- {
- return _a > _b ? _a : _b;
- }
- inline float fmin3(float _a, float _b, float _c)
- {
- return fmin(_a, fmin(_b, _c) );
- }
- inline float fmax3(float _a, float _b, float _c)
- {
- return fmax(_a, fmax(_b, _c) );
- }
- inline float fclamp(float _a, float _min, float _max)
- {
- return fmin(fmax(_a, _min), _max);
- }
- inline float fsaturate(float _a)
- {
- return fclamp(_a, 0.0f, 1.0f);
- }
- inline float flerp(float _a, float _b, float _t)
- {
- return _a + (_b - _a) * _t;
- }
- inline float fsign(float _a)
- {
- return _a < 0.0f ? -1.0f : 1.0f;
- }
- inline float fstep(float _edge, float _a)
- {
- return _a < _edge ? 0.0f : 1.0f;
- }
- inline float fpulse(float _a, float _start, float _end)
- {
- return fstep(_a, _start) - fstep(_a, _end);
- }
- inline float fabsolute(float _a)
- {
- return fabsf(_a);
- }
- inline float fsqrt(float _a)
- {
- return sqrtf(_a);
- }
- inline float ffract(float _a)
- {
- return _a - floorf(_a);
- }
- inline void vec3Move(float* __restrict _result, const float* __restrict _a)
- {
- _result[0] = _a[0];
- _result[1] = _a[1];
- _result[2] = _a[2];
- }
- inline void vec3Abs(float* __restrict _result, const float* __restrict _a)
- {
- _result[0] = fabsolute(_a[0]);
- _result[1] = fabsolute(_a[1]);
- _result[2] = fabsolute(_a[2]);
- }
- inline void vec3Neg(float* __restrict _result, const float* __restrict _a)
- {
- _result[0] = -_a[0];
- _result[1] = -_a[1];
- _result[2] = -_a[2];
- }
- inline void vec3Add(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
- {
- _result[0] = _a[0] + _b[0];
- _result[1] = _a[1] + _b[1];
- _result[2] = _a[2] + _b[2];
- }
- inline void vec3Sub(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
- {
- _result[0] = _a[0] - _b[0];
- _result[1] = _a[1] - _b[1];
- _result[2] = _a[2] - _b[2];
- }
- inline void vec3Mul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
- {
- _result[0] = _a[0] * _b[0];
- _result[1] = _a[1] * _b[1];
- _result[2] = _a[2] * _b[2];
- }
- inline void vec3Mul(float* __restrict _result, const float* __restrict _a, float _b)
- {
- _result[0] = _a[0] * _b;
- _result[1] = _a[1] * _b;
- _result[2] = _a[2] * _b;
- }
- inline float vec3Dot(const float* __restrict _a, const float* __restrict _b)
- {
- return _a[0]*_b[0] + _a[1]*_b[1] + _a[2]*_b[2];
- }
- inline void vec3Cross(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
- {
- _result[0] = _a[1]*_b[2] - _a[2]*_b[1];
- _result[1] = _a[2]*_b[0] - _a[0]*_b[2];
- _result[2] = _a[0]*_b[1] - _a[1]*_b[0];
- }
- inline float vec3Length(const float* _a)
- {
- return fsqrt(vec3Dot(_a, _a) );
- }
- inline float vec3Norm(float* __restrict _result, const float* __restrict _a)
- {
- const float len = vec3Length(_a);
- const float invLen = 1.0f/len;
- _result[0] = _a[0] * invLen;
- _result[1] = _a[1] * invLen;
- _result[2] = _a[2] * invLen;
- return len;
- }
- inline void mtxIdentity(float* _result)
- {
- memset(_result, 0, sizeof(float)*16);
- _result[0] = _result[5] = _result[10] = _result[15] = 1.0f;
- }
- inline void mtxTranslate(float* _result, float _tx, float _ty, float _tz)
- {
- mtxIdentity(_result);
- _result[12] = _tx;
- _result[13] = _ty;
- _result[14] = _tz;
- }
- inline void mtxScale(float* _result, float _sx, float _sy, float _sz)
- {
- memset(_result, 0, sizeof(float) * 16);
- _result[0] = _sx;
- _result[5] = _sy;
- _result[10] = _sz;
- _result[15] = 1.0f;
- }
- inline void mtxLookAt(float* __restrict _result, const float* __restrict _eye, const float* __restrict _at, const float* __restrict _up = NULL)
- {
- float tmp[4];
- vec3Sub(tmp, _at, _eye);
- float view[4];
- vec3Norm(view, tmp);
- float up[3] = { 0.0f, 1.0f, 0.0f };
- if (NULL != _up)
- {
- up[0] = _up[0];
- up[1] = _up[1];
- up[2] = _up[2];
- }
- vec3Cross(tmp, up, view);
- float right[4];
- vec3Norm(right, tmp);
- vec3Cross(up, view, right);
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = right[0];
- _result[ 1] = up[0];
- _result[ 2] = view[0];
- _result[ 4] = right[1];
- _result[ 5] = up[1];
- _result[ 6] = view[1];
- _result[ 8] = right[2];
- _result[ 9] = up[2];
- _result[10] = view[2];
- _result[12] = -vec3Dot(right, _eye);
- _result[13] = -vec3Dot(up, _eye);
- _result[14] = -vec3Dot(view, _eye);
- _result[15] = 1.0f;
- }
- inline void mtxProjXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, float _far, bool _oglNdc = false)
- {
- const float diff = _far-_near;
- const float aa = _oglNdc ? (_far+_near)/diff : _far/diff;
- const float bb = _oglNdc ? -(2.0f*_far*_near)/diff : -_near*aa;
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = _width;
- _result[ 5] = _height;
- _result[ 8] = _x;
- _result[ 9] = -_y;
- _result[10] = aa;
- _result[11] = 1.0f;
- _result[14] = bb;
- }
- inline void mtxProj(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc = false)
- {
- const float width = 2.0f / (_lt + _rt);
- const float height = 2.0f / (_ut + _dt);
- const float xx = (_lt - _rt) * width * 0.5f;
- const float yy = (_ut - _dt) * height * 0.5f;
- mtxProjXYWH(_result, xx, yy, width, height, _near, _far, _oglNdc);
- }
- inline void mtxProj(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc = false)
- {
- mtxProj(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _far, _oglNdc);
- }
- inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc = false)
- {
- const float height = 1.0f/tanf(toRad(_fovy)*0.5f);
- const float width = height * 1.0f/_aspect;
- mtxProjXYWH(_result, 0.0f, 0.0f, width, height, _near, _far, _oglNdc);
- }
- inline void mtxOrtho(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far)
- {
- const float aa = 2.0f/(_right - _left);
- const float bb = 2.0f/(_top - _bottom);
- const float cc = 1.0f/(_far - _near);
- const float dd = (_left + _right)/(_left - _right);
- const float ee = (_top + _bottom)/(_bottom - _top);
- const float ff = _near / (_near - _far);
- memset(_result, 0, sizeof(float)*16);
- _result[0] = aa;
- _result[5] = bb;
- _result[10] = cc;
- _result[12] = dd;
- _result[13] = ee;
- _result[14] = ff;
- _result[15] = 1.0f;
- }
- inline void mtxRotateX(float* _result, float _ax)
- {
- const float sx = sinf(_ax);
- const float cx = cosf(_ax);
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = 1.0f;
- _result[ 5] = cx;
- _result[ 6] = -sx;
- _result[ 9] = sx;
- _result[10] = cx;
- _result[15] = 1.0f;
- }
- inline void mtxRotateY(float* _result, float _ay)
- {
- const float sy = sinf(_ay);
- const float cy = cosf(_ay);
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = cy;
- _result[ 2] = sy;
- _result[ 5] = 1.0f;
- _result[ 8] = -sy;
- _result[10] = cy;
- _result[15] = 1.0f;
- }
- inline void mtxRotateZ(float* _result, float _az)
- {
- const float sz = sinf(_az);
- const float cz = cosf(_az);
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = cz;
- _result[ 1] = -sz;
- _result[ 4] = sz;
- _result[ 5] = cz;
- _result[10] = 1.0f;
- _result[15] = 1.0f;
- }
- inline void mtxRotateXY(float* _result, float _ax, float _ay)
- {
- const float sx = sinf(_ax);
- const float cx = cosf(_ax);
- const float sy = sinf(_ay);
- const float cy = cosf(_ay);
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = cy;
- _result[ 2] = sy;
- _result[ 4] = sx*sy;
- _result[ 5] = cx;
- _result[ 6] = -sx*cy;
- _result[ 8] = -cx*sy;
- _result[ 9] = sx;
- _result[10] = cx*cy;
- _result[15] = 1.0f;
- }
- inline void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az)
- {
- const float sx = sinf(_ax);
- const float cx = cosf(_ax);
- const float sy = sinf(_ay);
- const float cy = cosf(_ay);
- const float sz = sinf(_az);
- const float cz = cosf(_az);
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = cy*cz;
- _result[ 1] = -cy*sz;
- _result[ 2] = sy;
- _result[ 4] = cz*sx*sy + cx*sz;
- _result[ 5] = cx*cz - sx*sy*sz;
- _result[ 6] = -cy*sx;
- _result[ 8] = -cx*cz*sy + sx*sz;
- _result[ 9] = cz*sx + cx*sy*sz;
- _result[10] = cx*cy;
- _result[15] = 1.0f;
- }
- inline void mtxRotateZYX(float* _result, float _ax, float _ay, float _az)
- {
- const float sx = sinf(_ax);
- const float cx = cosf(_ax);
- const float sy = sinf(_ay);
- const float cy = cosf(_ay);
- const float sz = sinf(_az);
- const float cz = cosf(_az);
- memset(_result, 0, sizeof(float)*16);
- _result[ 0] = cy*cz;
- _result[ 1] = cz*sx*sy-cx*sz;
- _result[ 2] = cx*cz*sy+sx*sz;
- _result[ 4] = cy*sz;
- _result[ 5] = cx*cz + sx*sy*sz;
- _result[ 6] = -cz*sx + cx*sy*sz;
- _result[ 8] = -sy;
- _result[ 9] = cy*sx;
- _result[10] = cx*cy;
- _result[15] = 1.0f;
- };
- inline void mtxSRT(float* _result, float _sx, float _sy, float _sz, float _ax, float _ay, float _az, float _tx, float _ty, float _tz)
- {
- const float sx = sinf(_ax);
- const float cx = cosf(_ax);
- const float sy = sinf(_ay);
- const float cy = cosf(_ay);
- const float sz = sinf(_az);
- const float cz = cosf(_az);
- const float sxsz = sx*sz;
- const float cycz = cy*cz;
- _result[ 0] = _sx * (cycz - sxsz*sy);
- _result[ 1] = _sx * -cx*sz;
- _result[ 2] = _sx * (cz*sy + cy*sxsz);
- _result[ 3] = 0.0f;
- _result[ 4] = _sy * (cz*sx*sy + cy*sz);
- _result[ 5] = _sy * cx*cz;
- _result[ 6] = _sy * (sy*sz -cycz*sx);
- _result[ 7] = 0.0f;
- _result[ 8] = _sz * -cx*sy;
- _result[ 9] = _sz * sx;
- _result[10] = _sz * cx*cy;
- _result[11] = 0.0f;
- _result[12] = _tx;
- _result[13] = _ty;
- _result[14] = _tz;
- _result[15] = 1.0f;
- }
- inline void vec3MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
- {
- _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _mat[12];
- _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _mat[13];
- _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _mat[14];
- }
- inline void vec3MulMtxH(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
- {
- float xx = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _mat[12];
- float yy = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _mat[13];
- float zz = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _mat[14];
- float ww = _vec[0] * _mat[ 3] + _vec[1] * _mat[7] + _vec[2] * _mat[11] + _mat[15];
- float invW = fsign(ww)/ww;
- _result[0] = xx*invW;
- _result[1] = yy*invW;
- _result[2] = zz*invW;
- }
- inline void vec4MulMtx(float* __restrict _result, const float* __restrict _vec, const float* __restrict _mat)
- {
- _result[0] = _vec[0] * _mat[ 0] + _vec[1] * _mat[4] + _vec[2] * _mat[ 8] + _vec[3] * _mat[12];
- _result[1] = _vec[0] * _mat[ 1] + _vec[1] * _mat[5] + _vec[2] * _mat[ 9] + _vec[3] * _mat[13];
- _result[2] = _vec[0] * _mat[ 2] + _vec[1] * _mat[6] + _vec[2] * _mat[10] + _vec[3] * _mat[14];
- _result[3] = _vec[0] * _mat[ 3] + _vec[1] * _mat[7] + _vec[2] * _mat[11] + _vec[3] * _mat[15];
- }
- inline void mtxMul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
- {
- vec4MulMtx(&_result[ 0], &_a[ 0], _b);
- vec4MulMtx(&_result[ 4], &_a[ 4], _b);
- vec4MulMtx(&_result[ 8], &_a[ 8], _b);
- vec4MulMtx(&_result[12], &_a[12], _b);
- }
- inline void mtxTranspose(float* __restrict _result, const float* __restrict _a)
- {
- _result[ 0] = _a[ 0];
- _result[ 4] = _a[ 1];
- _result[ 8] = _a[ 2];
- _result[12] = _a[ 3];
- _result[ 1] = _a[ 4];
- _result[ 5] = _a[ 5];
- _result[ 9] = _a[ 6];
- _result[13] = _a[ 7];
- _result[ 2] = _a[ 8];
- _result[ 6] = _a[ 9];
- _result[10] = _a[10];
- _result[14] = _a[11];
- _result[ 3] = _a[12];
- _result[ 7] = _a[13];
- _result[11] = _a[14];
- _result[15] = _a[15];
- }
- inline void mtx3Inverse(float* __restrict _result, const float* __restrict _a)
- {
- float xx = _a[0];
- float xy = _a[1];
- float xz = _a[2];
- float yx = _a[3];
- float yy = _a[4];
- float yz = _a[5];
- float zx = _a[6];
- float zy = _a[7];
- float zz = _a[8];
- float det = 0.0f;
- det += xx * (yy*zz - yz*zy);
- det -= xy * (yx*zz - yz*zx);
- det += xz * (yx*zy - yy*zx);
- float invDet = 1.0f/det;
- _result[0] = +(yy*zz - yz*zy) * invDet;
- _result[1] = -(xy*zz - xz*zy) * invDet;
- _result[2] = +(xy*yz - xz*yy) * invDet;
- _result[3] = -(yx*zz - yz*zx) * invDet;
- _result[4] = +(xx*zz - xz*zx) * invDet;
- _result[5] = -(xx*yz - xz*yx) * invDet;
- _result[6] = +(yx*zy - yy*zx) * invDet;
- _result[7] = -(xx*zy - xy*zx) * invDet;
- _result[8] = +(xx*yy - xy*yx) * invDet;
- }
- inline void mtxInverse(float* __restrict _result, const float* __restrict _a)
- {
- float xx = _a[ 0];
- float xy = _a[ 1];
- float xz = _a[ 2];
- float xw = _a[ 3];
- float yx = _a[ 4];
- float yy = _a[ 5];
- float yz = _a[ 6];
- float yw = _a[ 7];
- float zx = _a[ 8];
- float zy = _a[ 9];
- float zz = _a[10];
- float zw = _a[11];
- float wx = _a[12];
- float wy = _a[13];
- float wz = _a[14];
- float ww = _a[15];
- float det = 0.0f;
- det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) );
- det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) );
- det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) );
- det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) );
- float invDet = 1.0f/det;
- _result[ 0] = +(yy*(zz*ww - wz*zw) - yz*(zy*ww - wy*zw) + yw*(zy*wz - wy*zz) ) * invDet;
- _result[ 1] = -(xy*(zz*ww - wz*zw) - xz*(zy*ww - wy*zw) + xw*(zy*wz - wy*zz) ) * invDet;
- _result[ 2] = +(xy*(yz*ww - wz*yw) - xz*(yy*ww - wy*yw) + xw*(yy*wz - wy*yz) ) * invDet;
- _result[ 3] = -(xy*(yz*zw - zz*yw) - xz*(yy*zw - zy*yw) + xw*(yy*zz - zy*yz) ) * invDet;
- _result[ 4] = -(yx*(zz*ww - wz*zw) - yz*(zx*ww - wx*zw) + yw*(zx*wz - wx*zz) ) * invDet;
- _result[ 5] = +(xx*(zz*ww - wz*zw) - xz*(zx*ww - wx*zw) + xw*(zx*wz - wx*zz) ) * invDet;
- _result[ 6] = -(xx*(yz*ww - wz*yw) - xz*(yx*ww - wx*yw) + xw*(yx*wz - wx*yz) ) * invDet;
- _result[ 7] = +(xx*(yz*zw - zz*yw) - xz*(yx*zw - zx*yw) + xw*(yx*zz - zx*yz) ) * invDet;
- _result[ 8] = +(yx*(zy*ww - wy*zw) - yy*(zx*ww - wx*zw) + yw*(zx*wy - wx*zy) ) * invDet;
- _result[ 9] = -(xx*(zy*ww - wy*zw) - xy*(zx*ww - wx*zw) + xw*(zx*wy - wx*zy) ) * invDet;
- _result[10] = +(xx*(yy*ww - wy*yw) - xy*(yx*ww - wx*yw) + xw*(yx*wy - wx*yy) ) * invDet;
- _result[11] = -(xx*(yy*zw - zy*yw) - xy*(yx*zw - zx*yw) + xw*(yx*zy - zx*yy) ) * invDet;
- _result[12] = -(yx*(zy*wz - wy*zz) - yy*(zx*wz - wx*zz) + yz*(zx*wy - wx*zy) ) * invDet;
- _result[13] = +(xx*(zy*wz - wy*zz) - xy*(zx*wz - wx*zz) + xz*(zx*wy - wx*zy) ) * invDet;
- _result[14] = -(xx*(yy*wz - wy*yz) - xy*(yx*wz - wx*yz) + xz*(yx*wy - wx*yy) ) * invDet;
- _result[15] = +(xx*(yy*zz - zy*yz) - xy*(yx*zz - zx*yz) + xz*(yx*zy - zx*yy) ) * invDet;
- }
- /// Convert LH to RH projection matrix and vice versa.
- inline void mtxProjFlipHandedness(float* __restrict _dst, const float* __restrict _src)
- {
- _dst[ 0] = -_src[ 0];
- _dst[ 1] = -_src[ 1];
- _dst[ 2] = -_src[ 2];
- _dst[ 3] = -_src[ 3];
- _dst[ 4] = _src[ 4];
- _dst[ 5] = _src[ 5];
- _dst[ 6] = _src[ 6];
- _dst[ 7] = _src[ 7];
- _dst[ 8] = -_src[ 8];
- _dst[ 9] = -_src[ 9];
- _dst[10] = -_src[10];
- _dst[11] = -_src[11];
- _dst[12] = _src[12];
- _dst[13] = _src[13];
- _dst[14] = _src[14];
- _dst[15] = _src[15];
- }
- /// Convert LH to RH view matrix and vice versa.
- inline void mtxViewFlipHandedness(float* __restrict _dst, const float* __restrict _src)
- {
- _dst[ 0] = -_src[ 0];
- _dst[ 1] = _src[ 1];
- _dst[ 2] = -_src[ 2];
- _dst[ 3] = _src[ 3];
- _dst[ 4] = -_src[ 4];
- _dst[ 5] = _src[ 5];
- _dst[ 6] = -_src[ 6];
- _dst[ 7] = _src[ 7];
- _dst[ 8] = -_src[ 8];
- _dst[ 9] = _src[ 9];
- _dst[10] = -_src[10];
- _dst[11] = _src[11];
- _dst[12] = -_src[12];
- _dst[13] = _src[13];
- _dst[14] = -_src[14];
- _dst[15] = _src[15];
- }
- inline void calcNormal(float _result[3], float _va[3], float _vb[3], float _vc[3])
- {
- float ba[3];
- vec3Sub(ba, _vb, _va);
- float ca[3];
- vec3Sub(ca, _vc, _va);
- float baxca[3];
- vec3Cross(baxca, ba, ca);
- vec3Norm(_result, baxca);
- }
- inline void calcPlane(float _result[4], float _va[3], float _vb[3], float _vc[3])
- {
- float normal[3];
- calcNormal(normal, _va, _vb, _vc);
- _result[0] = normal[0];
- _result[1] = normal[1];
- _result[2] = normal[2];
- _result[3] = -vec3Dot(normal, _va);
- }
- inline void rgbToHsv(float _hsv[3], const float _rgb[3])
- {
- const float rr = _rgb[0];
- const float gg = _rgb[1];
- const float bb = _rgb[2];
- const float s0 = fstep(bb, gg);
- const float px = flerp(bb, gg, s0);
- const float py = flerp(gg, bb, s0);
- const float pz = flerp(-1.0f, 0.0f, s0);
- const float pw = flerp(2.0f/3.0f, -1.0f/3.0f, s0);
- const float s1 = fstep(px, rr);
- const float qx = flerp(px, rr, s1);
- const float qy = py;
- const float qz = flerp(pw, pz, s1);
- const float qw = flerp(rr, px, s1);
- const float dd = qx - fmin(qw, qy);
- const float ee = 1.0e-10f;
- _hsv[0] = fabsolute(qz + (qw - qy) / (6.0f * dd + ee) );
- _hsv[1] = dd / (qx + ee);
- _hsv[2] = qx;
- }
- inline void hsvToRgb(float _rgb[3], const float _hsv[3])
- {
- const float hh = _hsv[0];
- const float ss = _hsv[1];
- const float vv = _hsv[2];
- const float px = fabsolute(ffract(hh + 1.0f ) * 6.0f - 3.0f);
- const float py = fabsolute(ffract(hh + 2.0f/3.0f) * 6.0f - 3.0f);
- const float pz = fabsolute(ffract(hh + 1.0f/3.0f) * 6.0f - 3.0f);
- _rgb[0] = vv * flerp(1.0f, fsaturate(px - 1.0f), ss);
- _rgb[1] = vv * flerp(1.0f, fsaturate(py - 1.0f), ss);
- _rgb[2] = vv * flerp(1.0f, fsaturate(pz - 1.0f), ss);
- }
- } // namespace bx
- #endif // BX_FPU_MATH_H_HEADER_GUARD
|