diff options
author | 2017-01-02 14:05:50 +0100 | |
---|---|---|
committer | 2017-01-02 14:05:50 +0100 | |
commit | fadb547f630d377b1ff69a7ab09f852855930c56 (patch) | |
tree | a77a08bbed897113aec7a95657fc5e0d902fc0b6 /3rdparty/bgfx/examples/common/bounds.cpp | |
parent | 04faaaf7ffb422f390262e119fe50db21683a831 (diff) |
Updated BGFX (nw)
Diffstat (limited to '3rdparty/bgfx/examples/common/bounds.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/common/bounds.cpp | 90 |
1 files changed, 75 insertions, 15 deletions
diff --git a/3rdparty/bgfx/examples/common/bounds.cpp b/3rdparty/bgfx/examples/common/bounds.cpp index ff34238c3f3..96a775024d2 100644 --- a/3rdparty/bgfx/examples/common/bounds.cpp +++ b/3rdparty/bgfx/examples/common/bounds.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2011-2016 Branimir Karadzic. All rights reserved. + * Copyright 2011-2017 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ @@ -19,13 +19,73 @@ void aabbToObb(Obb& _obb, const Aabb& _aabb) _obb.m_mtx[15] = 1.0f; } -void sphereToAabb(Aabb& _aabb, const Sphere& _sphere) +void toAabb(Aabb& _aabb, const Sphere& _sphere) { float radius = _sphere.m_radius; bx::vec3Sub(_aabb.m_min, _sphere.m_center, radius); bx::vec3Add(_aabb.m_max, _sphere.m_center, radius); } +void toAabb(Aabb& _aabb, const Disk& _disk) +{ + // Reference: http://iquilezles.org/www/articles/diskbbox/diskbbox.htm + float nsq[3]; + bx::vec3Mul(nsq, _disk.m_normal, _disk.m_normal); + + float one[3] = { 1.0f, 1.0f, 1.0f }; + float tmp[3]; + bx::vec3Sub(tmp, one, nsq); + + const float inv = 1.0f / (tmp[0]*tmp[1]*tmp[2]); + + float extent[3]; + extent[0] = _disk.m_radius * tmp[0] * bx::fsqrt( (nsq[0] + nsq[1] * nsq[2]) * inv); + extent[1] = _disk.m_radius * tmp[1] * bx::fsqrt( (nsq[1] + nsq[2] * nsq[0]) * inv); + extent[2] = _disk.m_radius * tmp[2] * bx::fsqrt( (nsq[2] + nsq[0] * nsq[1]) * inv); + + bx::vec3Sub(_aabb.m_min, _disk.m_center, extent); + bx::vec3Add(_aabb.m_max, _disk.m_center, extent); +} + +void toAabb(Aabb& _aabb, const Cylinder& _cylinder) +{ + // Reference: http://iquilezles.org/www/articles/diskbbox/diskbbox.htm + float axis[3]; + bx::vec3Sub(axis, _cylinder.m_end, _cylinder.m_pos); + + float asq[3]; + bx::vec3Mul(asq, axis, axis); + + float nsq[3]; + bx::vec3Mul(nsq, asq, 1.0f/bx::vec3Dot(axis, axis) ); + + float one[3] = { 1.0f, 1.0f, 1.0f }; + float tmp[3]; + bx::vec3Sub(tmp, one, nsq); + + const float inv = 1.0f / (tmp[0]*tmp[1]*tmp[2]); + + float extent[3]; + extent[0] = _cylinder.m_radius * tmp[0] * bx::fsqrt( (nsq[0] + nsq[1] * nsq[2]) * inv); + extent[1] = _cylinder.m_radius * tmp[1] * bx::fsqrt( (nsq[1] + nsq[2] * nsq[0]) * inv); + extent[2] = _cylinder.m_radius * tmp[2] * bx::fsqrt( (nsq[2] + nsq[0] * nsq[1]) * inv); + + float minP[3]; + bx::vec3Sub(minP, _cylinder.m_pos, extent); + + float minE[3]; + bx::vec3Sub(minE, _cylinder.m_end, extent); + + float maxP[3]; + bx::vec3Add(maxP, _cylinder.m_pos, extent); + + float maxE[3]; + bx::vec3Add(maxE, _cylinder.m_end, extent); + + bx::vec3Min(_aabb.m_min, minP, minE); + bx::vec3Max(_aabb.m_max, maxP, maxE); +} + void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx) { aabbToObb(_obb, _aabb); @@ -34,15 +94,7 @@ void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx) memcpy(_obb.m_mtx, result, sizeof(result) ); } -float calcAreaAabb(Aabb& _aabb) -{ - float ww = _aabb.m_max[0] - _aabb.m_min[0]; - float hh = _aabb.m_max[1] - _aabb.m_min[1]; - float dd = _aabb.m_max[2] - _aabb.m_min[2]; - return 2.0f * (ww*hh + ww*dd + hh*dd); -} - -void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride) +void toAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride) { float min[3], max[3]; uint8_t* vertex = (uint8_t*)_vertices; @@ -76,7 +128,7 @@ void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_ _aabb.m_max[2] = max[2]; } -void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride) +void toAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride) { float min[3], max[3]; uint8_t* vertex = (uint8_t*)_vertices; @@ -112,6 +164,14 @@ void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _n _aabb.m_max[2] = max[2]; } +float calcAreaAabb(const Aabb& _aabb) +{ + float ww = _aabb.m_max[0] - _aabb.m_min[0]; + float hh = _aabb.m_max[1] - _aabb.m_min[1]; + float dd = _aabb.m_max[2] - _aabb.m_min[2]; + return 2.0f * (ww*hh + ww*dd + hh*dd); +} + void aabbExpand(Aabb& _aabb, float _factor) { _aabb.m_min[0] -= _factor; @@ -144,7 +204,7 @@ uint32_t aabbOverlapTest(const Aabb& _aabb0, const Aabb& _aabb1) void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps) { Aabb aabb; - calcAabb(aabb, _vertices, _numVertices, _stride); + toAabb(aabb, _vertices, _numVertices, _stride); float minArea = calcAreaAabb(aabb); Obb best; @@ -168,7 +228,7 @@ void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _ float mtxT[16]; bx::mtxTranspose(mtxT, mtx); - calcAabb(aabb, mtxT, _vertices, _numVertices, _stride); + toAabb(aabb, mtxT, _vertices, _numVertices, _stride); float area = calcAreaAabb(aabb); if (area < minArea) @@ -192,7 +252,7 @@ void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _ void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride) { Aabb aabb; - calcAabb(aabb, _vertices, _numVertices, _stride); + toAabb(aabb, _vertices, _numVertices, _stride); float center[3]; center[0] = (aabb.m_min[0] + aabb.m_max[0]) * 0.5f; |