From c5d1a1a8e33f8698d0fd6d0675f7cc8f418702fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Sat, 16 Oct 2021 10:22:47 -0700 Subject: [PATCH] Moving bounds from bgfx to bx. --- examples/14-shadowvolumes/shadowvolumes.cpp | 14 +- examples/21-deferred/deferred.cpp | 6 +- examples/27-terrain/terrain.cpp | 7 +- examples/29-debugdraw/debugdraw.cpp | 144 +- examples/32-particles/particles.cpp | 2 +- examples/36-sky/sky.cpp | 1 - examples/38-bloom/bloom.cpp | 1 - examples/40-svt/svt.cpp | 1 - examples/41-tess/tess.cpp | 1 - examples/common/bgfx_utils.h | 14 +- examples/common/bounds.cpp | 2096 ------------------- examples/common/bounds.h | 456 ---- examples/common/bounds.inl | 198 -- examples/common/debugdraw/debugdraw.cpp | 34 +- examples/common/debugdraw/debugdraw.h | 18 +- examples/common/ps/particle_system.cpp | 10 +- examples/common/ps/particle_system.h | 5 +- tools/geometryc/geometryc.cpp | 16 +- tools/geometryv/geometryv.cpp | 42 +- 19 files changed, 165 insertions(+), 2901 deletions(-) delete mode 100644 examples/common/bounds.cpp delete mode 100644 examples/common/bounds.h delete mode 100644 examples/common/bounds.inl diff --git a/examples/14-shadowvolumes/shadowvolumes.cpp b/examples/14-shadowvolumes/shadowvolumes.cpp index 865b058dc..3a5b2f23e 100644 --- a/examples/14-shadowvolumes/shadowvolumes.cpp +++ b/examples/14-shadowvolumes/shadowvolumes.cpp @@ -919,9 +919,9 @@ struct Group uint8_t* m_vertices; uint32_t m_numIndices; uint16_t* m_indices; - Sphere m_sphere; - Aabb m_aabb; - Obb m_obb; + bx::Sphere m_sphere; + bx::Aabb m_aabb; + bx::Obb m_obb; PrimitiveArray m_prims; uint32_t m_numEdges; Edge* m_edges; @@ -990,9 +990,9 @@ struct Mesh group.m_ibh = bgfx::createIndexBuffer(mem); group.m_sphere = it->m_sphere; - group.m_aabb = it->m_aabb; - group.m_obb = it->m_obb; - group.m_prims = it->m_prims; + group.m_aabb = it->m_aabb; + group.m_obb = it->m_obb; + group.m_prims = it->m_prims; m_groups.push_back(group); } @@ -1686,7 +1686,7 @@ bool clipTest(const float* _planes, uint8_t _planeNum, const Mesh& _mesh, const { const Group& group = *it; - Sphere sphere = group.m_sphere; + bx::Sphere sphere = group.m_sphere; sphere.center.x = sphere.center.x * scale + _translate[0]; sphere.center.y = sphere.center.y * scale + _translate[1]; sphere.center.z = sphere.center.z * scale + _translate[2]; diff --git a/examples/21-deferred/deferred.cpp b/examples/21-deferred/deferred.cpp index 375edbab9..2b0b37b0c 100644 --- a/examples/21-deferred/deferred.cpp +++ b/examples/21-deferred/deferred.cpp @@ -3,11 +3,11 @@ * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ +#include #include "common.h" #include "bgfx_utils.h" #include "imgui/imgui.h" #include "camera.h" -#include "bounds.h" namespace { @@ -679,7 +679,7 @@ public: // Draw lights into light buffer. for (int32_t light = 0; light < m_numLights; ++light) { - Sphere lightPosRadius; + bx::Sphere lightPosRadius; float lightTime = time * m_lightAnimationSpeed * (bx::sin(light/float(m_numLights) * bx::kPiHalf ) * 0.5f + 0.5f); lightPosRadius.center.x = bx::sin( ( (lightTime + light*0.47f) + bx::kPiHalf*1.37f ) )*offset; @@ -687,7 +687,7 @@ public: lightPosRadius.center.z = bx::sin( ( (lightTime + light*0.37f) + bx::kPiHalf*1.57f ) )*2.0f; lightPosRadius.radius = 2.0f; - Aabb aabb; + bx::Aabb aabb; toAabb(aabb, lightPosRadius); const bx::Vec3 box[8] = diff --git a/examples/27-terrain/terrain.cpp b/examples/27-terrain/terrain.cpp index bcdaf16d7..819415488 100644 --- a/examples/27-terrain/terrain.cpp +++ b/examples/27-terrain/terrain.cpp @@ -3,14 +3,13 @@ * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ +#include +#include +#include #include "common.h" #include "bgfx_utils.h" #include "imgui/imgui.h" #include "camera.h" -#include "bounds.h" -#include -#include -#include namespace { diff --git a/examples/29-debugdraw/debugdraw.cpp b/examples/29-debugdraw/debugdraw.cpp index f7828b892..488b545d2 100644 --- a/examples/29-debugdraw/debugdraw.cpp +++ b/examples/29-debugdraw/debugdraw.cpp @@ -485,15 +485,15 @@ struct Shape }; Shape() : type(uint8_t(Type::Count) ) {} - Shape(const Aabb & _a) : type(uint8_t(Type::Aabb ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const Capsule & _a) : type(uint8_t(Type::Capsule ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const Cone & _a) : type(uint8_t(Type::Cone ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const Cylinder & _a) : type(uint8_t(Type::Cylinder) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const Disk & _a) : type(uint8_t(Type::Disk ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const Obb & _a) : type(uint8_t(Type::Obb ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const bx::Plane& _a) : type(uint8_t(Type::Plane ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const Sphere & _a) : type(uint8_t(Type::Sphere ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } - Shape(const Triangle & _a) : type(uint8_t(Type::Triangle) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Aabb & _a) : type(uint8_t(Type::Aabb ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Capsule & _a) : type(uint8_t(Type::Capsule ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Cone & _a) : type(uint8_t(Type::Cone ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Cylinder & _a) : type(uint8_t(Type::Cylinder) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Disk & _a) : type(uint8_t(Type::Disk ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Obb & _a) : type(uint8_t(Type::Obb ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Plane & _a) : type(uint8_t(Type::Plane ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Sphere & _a) : type(uint8_t(Type::Sphere ) ) { bx::memCopy(data, &_a, sizeof(_a) ); } + Shape(const bx::Triangle & _a) : type(uint8_t(Type::Triangle) ) { bx::memCopy(data, &_a, sizeof(_a) ); } uint8_t data[64]; uint8_t type; @@ -504,28 +504,28 @@ struct Shape { \ switch (_shapeB.type) \ { \ - case Shape::Type::Aabb: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Capsule: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Cone: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Cylinder: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Disk: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Obb: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Plane: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Sphere: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ - case Shape::Type::Triangle: return ::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Aabb: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Capsule: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Cone: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Cylinder: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Disk: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Obb: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Plane: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Sphere: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ + case Shape::Type::Triangle: return bx::overlap(_shapeA, *reinterpret_cast(_shapeB.data) ); \ } \ return false; \ } -OVERLAP(Aabb); -OVERLAP(Capsule); -OVERLAP(Cone); -OVERLAP(Cylinder); -OVERLAP(Disk); -OVERLAP(Obb); +OVERLAP(bx::Aabb); +OVERLAP(bx::Capsule); +OVERLAP(bx::Cone); +OVERLAP(bx::Cylinder); +OVERLAP(bx::Disk); +OVERLAP(bx::Obb); OVERLAP(bx::Plane); -OVERLAP(Sphere); -OVERLAP(Triangle); +OVERLAP(bx::Sphere); +OVERLAP(bx::Triangle); #undef OVERLAP @@ -535,14 +535,14 @@ void initA(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) { case Shape::Type::Aabb: { - Aabb aabb; + bx::Aabb aabb; toAabb(aabb, _pos, { 0.5f, 0.5f, 0.5f }); _outShape = Shape(aabb); } break; case Shape::Type::Capsule: - _outShape = Shape(Capsule + _outShape = Shape(bx::Capsule { { bx::add(_pos, {0.0f, -1.0f, 0.0f}) }, { bx::add(_pos, {0.0f, 1.0f, 0.0f}) }, @@ -551,7 +551,7 @@ void initA(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Cone: - _outShape = Shape(Cone + _outShape = Shape(bx::Cone { { bx::add(_pos, {0.0f, -1.0f, 0.0f}) }, { bx::add(_pos, {0.0f, 1.0f, 0.0f}) }, @@ -560,7 +560,7 @@ void initA(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Cylinder: - _outShape = Shape(Cylinder + _outShape = Shape(bx::Cylinder { { bx::add(_pos, {0.0f, -1.0f, 0.0f}) }, { bx::add(_pos, {0.0f, 1.0f, 0.0f}) }, @@ -569,7 +569,7 @@ void initA(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Disk: - _outShape = Shape(Disk + _outShape = Shape(bx::Disk { _pos, bx::normalize(bx::Vec3{0.0f, 1.0f, 1.0f}), @@ -579,7 +579,7 @@ void initA(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) case Shape::Type::Obb: { - Obb obb; + bx::Obb obb; bx::mtxSRT(obb.mtx , 0.25f , 1.0f @@ -596,7 +596,7 @@ void initA(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Sphere: - _outShape = Shape(Sphere{_pos, 0.5f}); + _outShape = Shape(bx::Sphere{_pos, 0.5f}); break; case Shape::Type::Plane: @@ -608,7 +608,7 @@ void initA(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Triangle: - _outShape = Shape(Triangle + _outShape = Shape(bx::Triangle { { bx::add(_pos, {-0.4f, 0.0f, -0.4f}) }, { bx::add(_pos, { 0.0f, -0.3f, 0.5f}) }, @@ -626,14 +626,14 @@ void initB(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) { case Shape::Type::Aabb: { - Aabb aabb; + bx::Aabb aabb; toAabb(aabb, _pos, { 0.5f, 0.5f, 0.5f }); _outShape = Shape(aabb); } break; case Shape::Type::Capsule: - _outShape = Shape(Capsule + _outShape = Shape(bx::Capsule { { bx::add(_pos, {0.0f, -1.0f, 0.1f}) }, { bx::add(_pos, {0.0f, 1.0f, 0.0f}) }, @@ -642,7 +642,7 @@ void initB(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Cone: - _outShape = Shape(Cone + _outShape = Shape(bx::Cone { { bx::add(_pos, {0.0f, -1.0f, 0.1f}) }, { bx::add(_pos, {0.0f, 1.0f, 0.0f}) }, @@ -651,7 +651,7 @@ void initB(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Cylinder: - _outShape = Shape(Cylinder + _outShape = Shape(bx::Cylinder { { bx::add(_pos, {0.0f, -1.0f, 0.1f}) }, { bx::add(_pos, {0.0f, 1.0f, 0.0f}) }, @@ -660,7 +660,7 @@ void initB(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Disk: - _outShape = Shape(Disk + _outShape = Shape(bx::Disk { _pos, bx::normalize(bx::Vec3{1.0f, 1.0f, 0.0f}), @@ -670,7 +670,7 @@ void initB(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) case Shape::Type::Obb: { - Obb obb; + bx::Obb obb; bx::mtxSRT(obb.mtx , 1.0f , 0.25f @@ -695,11 +695,11 @@ void initB(Shape& _outShape, Shape::Type::Enum _type, bx::Vec3 _pos) break; case Shape::Type::Sphere: - _outShape = Shape(Sphere{_pos, 0.5f}); + _outShape = Shape(bx::Sphere{_pos, 0.5f}); break; case Shape::Type::Triangle: - _outShape = Shape(Triangle + _outShape = Shape(bx::Triangle { { bx::add(_pos, {-0.4f, 0.0f, -0.4f}) }, { bx::add(_pos, {-0.5f, -0.3f, 0.0f}) }, @@ -715,15 +715,15 @@ int32_t overlap(const Shape& _shapeA, const Shape& _shapeB) { switch (_shapeA.type) { - case Shape::Type::Aabb: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Capsule: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Cone: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Cylinder: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Disk: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Obb: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Plane: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Sphere: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); - case Shape::Type::Triangle: return overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Aabb: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Capsule: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Cone: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Cylinder: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Disk: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Obb: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Plane: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Sphere: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); + case Shape::Type::Triangle: return ::overlap(*reinterpret_cast(_shapeA.data), _shapeB); } return 2; @@ -733,15 +733,15 @@ void draw(DebugDrawEncoder& _dde, const Shape& _shape, const bx::Vec3 _pos) { switch (_shape.type) { - case Shape::Type::Aabb: _dde.draw (*reinterpret_cast(_shape.data) ); break; - case Shape::Type::Capsule: _dde.draw (*reinterpret_cast(_shape.data) ); break; - case Shape::Type::Cone: _dde.draw (*reinterpret_cast(_shape.data) ); break; - case Shape::Type::Cylinder: _dde.draw (*reinterpret_cast(_shape.data) ); break; - case Shape::Type::Disk: _dde.draw (*reinterpret_cast(_shape.data) ); break; - case Shape::Type::Obb: _dde.draw (*reinterpret_cast(_shape.data) ); break; - case Shape::Type::Plane: { _dde.drawGrid( reinterpret_cast(_shape.data)->normal, _pos, 9, 0.3f); } break; - case Shape::Type::Sphere: _dde.draw (*reinterpret_cast(_shape.data) ); break; - case Shape::Type::Triangle: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Aabb: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Capsule: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Cone: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Cylinder: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Disk: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Obb: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Plane: { _dde.drawGrid( reinterpret_cast(_shape.data)->normal, _pos, 9, 0.3f); } break; + case Shape::Type::Sphere: _dde.draw (*reinterpret_cast(_shape.data) ); break; + case Shape::Type::Triangle: _dde.draw (*reinterpret_cast(_shape.data) ); break; } } @@ -835,10 +835,10 @@ public: } template - bool intersect(DebugDrawEncoder* _dde, const Ray& _ray, const Ty& _shape) + bool intersect(DebugDrawEncoder* _dde, const bx::Ray& _ray, const Ty& _shape) { - Hit hit; - if (::intersect(_ray, _shape, &hit) ) + bx::Hit hit; + if (bx::intersect(_ray, _shape, &hit) ) { _dde->push(); @@ -931,7 +931,7 @@ public: bx::mtxProj(proj, 45.0f, float(m_width)/float(m_height), 1.0f, 15.0f, bgfx::getCaps()->homogeneousDepth); bx::mtxMul(mtxVp, view, proj); - Ray ray = makeRay( + bx::Ray ray = bx::makeRay( (float(m_mouseState.m_mx)/float(m_width) * 2.0f - 1.0f) , -(float(m_mouseState.m_my)/float(m_height) * 2.0f - 1.0f) , mtxInvVp @@ -947,7 +947,7 @@ public: dde.drawAxis(0.0f, 0.0f, 0.0f); dde.push(); - Aabb aabb = + bx::Aabb aabb = { { 5.0f, 1.0f, 1.0f }, { 10.0f, 5.0f, 5.0f }, @@ -960,7 +960,7 @@ public: static float time = 0.0f; time += deltaTime*timeScale; - Obb obb; + bx::Obb obb; bx::mtxRotateX(obb.mtx, time); dde.setWireframe(true); dde.setColor(intersect(&dde, ray, obb) ? kSelected : 0xffffffff); @@ -969,7 +969,7 @@ public: bx::mtxSRT(obb.mtx, 1.0f, 1.0f, 1.0f, time*0.23f, time, 0.0f, 3.0f, 0.0f, 0.0f); dde.push(); - toAabb(aabb, obb); + bx::toAabb(aabb, obb); dde.setWireframe(true); dde.setColor(0xff0000ff); dde.draw(aabb); @@ -1014,7 +1014,7 @@ public: dde.drawFrustum(mtxVp); dde.push(); - Sphere sphere = { { 0.0f, 5.0f, 0.0f }, 1.0f }; + bx::Sphere sphere = { { 0.0f, 5.0f, 0.0f }, 1.0f }; dde.setColor(intersect(&dde, ray, sphere) ? kSelected : 0xfff0c0ff); dde.setWireframe(true); dde.setLod(3); @@ -1065,14 +1065,14 @@ public: dde.push(); dde.setSpin(time*0.3f); { - Cone cone = + bx::Cone cone = { { -11.0f, 4.0f, 0.0f }, { -13.0f, 6.0f, 1.0f }, 1.0f }; - Cylinder cylinder = + bx::Cylinder cylinder = { { -9.0f, 2.0f, -1.0f }, { -11.0f, 4.0f, 0.0f }, @@ -1093,7 +1093,7 @@ public: { dde.setLod(0); - Capsule capsule = + bx::Capsule capsule = { { 0.0f, 7.0f, 0.0f }, { -6.0f, 7.0f, 0.0f }, @@ -1113,7 +1113,7 @@ public: , -10.0f, 1.0f, 10.0f ); - Cylinder cylinder = + bx::Cylinder cylinder = { { -10.0f, 1.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, diff --git a/examples/32-particles/particles.cpp b/examples/32-particles/particles.cpp index 7a504eb69..3bd22e4ab 100644 --- a/examples/32-particles/particles.cpp +++ b/examples/32-particles/particles.cpp @@ -417,7 +417,7 @@ public: if (showBounds) { - Aabb aabb; + bx::Aabb aabb; psGetAabb(m_emitter[currentEmitter].m_handle, aabb); dde.push(); dde.setWireframe(true); diff --git a/examples/36-sky/sky.cpp b/examples/36-sky/sky.cpp index a4896d8f5..2975faf5f 100644 --- a/examples/36-sky/sky.cpp +++ b/examples/36-sky/sky.cpp @@ -52,7 +52,6 @@ #include "bgfx_utils.h" #include "imgui/imgui.h" #include "camera.h" -#include "bounds.h" #include diff --git a/examples/38-bloom/bloom.cpp b/examples/38-bloom/bloom.cpp index 36a0724d5..870925b8e 100644 --- a/examples/38-bloom/bloom.cpp +++ b/examples/38-bloom/bloom.cpp @@ -13,7 +13,6 @@ #include "bgfx_utils.h" #include "imgui/imgui.h" #include "camera.h" -#include "bounds.h" namespace { diff --git a/examples/40-svt/svt.cpp b/examples/40-svt/svt.cpp index 63be6ed74..3222d7bf5 100644 --- a/examples/40-svt/svt.cpp +++ b/examples/40-svt/svt.cpp @@ -17,7 +17,6 @@ #include "bgfx_utils.h" #include "imgui/imgui.h" #include "camera.h" -#include "bounds.h" #include "vt.h" namespace diff --git a/examples/41-tess/tess.cpp b/examples/41-tess/tess.cpp index ab764a5c0..fe8cb44a2 100644 --- a/examples/41-tess/tess.cpp +++ b/examples/41-tess/tess.cpp @@ -17,7 +17,6 @@ #include #include "bgfx_utils.h" -#include "bounds.h" #include "camera.h" #include "common.h" #include "imgui/imgui.h" diff --git a/examples/common/bgfx_utils.h b/examples/common/bgfx_utils.h index 37d11c411..daf8f1ed6 100644 --- a/examples/common/bgfx_utils.h +++ b/examples/common/bgfx_utils.h @@ -6,11 +6,11 @@ #ifndef BGFX_UTILS_H_HEADER_GUARD #define BGFX_UTILS_H_HEADER_GUARD +#include #include #include #include #include -#include "bounds.h" #include #include @@ -92,9 +92,9 @@ struct Primitive uint32_t m_startVertex; uint32_t m_numVertices; - Sphere m_sphere; - Aabb m_aabb; - Obb m_obb; + bx::Sphere m_sphere; + bx::Aabb m_aabb; + bx::Obb m_obb; }; typedef stl::vector PrimitiveArray; @@ -110,9 +110,9 @@ struct Group uint8_t* m_vertices; uint32_t m_numIndices; uint16_t* m_indices; - Sphere m_sphere; - Aabb m_aabb; - Obb m_obb; + bx::Sphere m_sphere; + bx::Aabb m_aabb; + bx::Obb m_obb; PrimitiveArray m_prims; }; typedef stl::vector GroupArray; diff --git a/examples/common/bounds.cpp b/examples/common/bounds.cpp deleted file mode 100644 index 010709c09..000000000 --- a/examples/common/bounds.cpp +++ /dev/null @@ -1,2096 +0,0 @@ -/* - * Copyright 2011-2021 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause - */ - -#include -#include -#include "bounds.h" - -using namespace bx; - -Vec3 getCenter(const Aabb& _aabb) -{ - return mul(add(_aabb.min, _aabb.max), 0.5f); -} - -Vec3 getExtents(const Aabb& _aabb) -{ - return mul(sub(_aabb.max, _aabb.min), 0.5f); -} - -Vec3 getCenter(const Triangle& _triangle) -{ - return mul(add(add(_triangle.v0, _triangle.v1), _triangle.v2), 1.0f/3.0f); -} - -void toAabb(Aabb& _outAabb, const Vec3& _extents) -{ - _outAabb.min = neg(_extents); - _outAabb.max = _extents; -} - -void toAabb(Aabb& _outAabb, const Vec3& _center, const Vec3& _extents) -{ - _outAabb.min = sub(_center, _extents); - _outAabb.max = add(_center, _extents); -} - -void toAabb(Aabb& _outAabb, const Cylinder& _cylinder) -{ - // Reference(s): - // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm - // - const Vec3 axis = sub(_cylinder.end, _cylinder.pos); - const Vec3 asq = mul(axis, axis); - const Vec3 nsq = mul(asq, 1.0f/dot(axis, axis) ); - const Vec3 tmp = sub(Vec3(1.0f), nsq); - - const float inv = 1.0f/(tmp.x*tmp.y*tmp.z); - - const Vec3 extent = - { - _cylinder.radius * tmp.x * bx::sqrt( (nsq.x + nsq.y * nsq.z) * inv), - _cylinder.radius * tmp.y * bx::sqrt( (nsq.y + nsq.z * nsq.x) * inv), - _cylinder.radius * tmp.z * bx::sqrt( (nsq.z + nsq.x * nsq.y) * inv), - }; - - const Vec3 minP = sub(_cylinder.pos, extent); - const Vec3 minE = sub(_cylinder.end, extent); - const Vec3 maxP = add(_cylinder.pos, extent); - const Vec3 maxE = add(_cylinder.end, extent); - - _outAabb.min = min(minP, minE); - _outAabb.max = max(maxP, maxE); -} - -void toAabb(Aabb& _outAabb, const Disk& _disk) -{ - // Reference(s): - // - https://web.archive.org/web/20181113055756/http://iquilezles.org/www/articles/diskbbox/diskbbox.htm - // - const Vec3 nsq = mul(_disk.normal, _disk.normal); - const Vec3 one = { 1.0f, 1.0f, 1.0f }; - const Vec3 tmp = sub(one, nsq); - const float inv = 1.0f / (tmp.x*tmp.y*tmp.z); - - const Vec3 extent = - { - _disk.radius * tmp.x * bx::sqrt( (nsq.x + nsq.y * nsq.z) * inv), - _disk.radius * tmp.y * bx::sqrt( (nsq.y + nsq.z * nsq.x) * inv), - _disk.radius * tmp.z * bx::sqrt( (nsq.z + nsq.x * nsq.y) * inv), - }; - - _outAabb.min = sub(_disk.center, extent); - _outAabb.max = add(_disk.center, extent); -} - -void toAabb(Aabb& _outAabb, const Obb& _obb) -{ - Vec3 xyz = { 1.0f, 1.0f, 1.0f }; - Vec3 tmp = mul(xyz, _obb.mtx); - - _outAabb.min = tmp; - _outAabb.max = tmp; - - for (uint32_t ii = 1; ii < 8; ++ii) - { - xyz.x = ii & 1 ? -1.0f : 1.0f; - xyz.y = ii & 2 ? -1.0f : 1.0f; - xyz.z = ii & 4 ? -1.0f : 1.0f; - tmp = mul(xyz, _obb.mtx); - - _outAabb.min = min(_outAabb.min, tmp); - _outAabb.max = max(_outAabb.max, tmp); - } -} - -void toAabb(Aabb& _outAabb, const Sphere& _sphere) -{ - const float radius = _sphere.radius; - _outAabb.min = sub(_sphere.center, radius); - _outAabb.max = add(_sphere.center, radius); -} - -void toAabb(Aabb& _outAabb, const Triangle& _triangle) -{ - _outAabb.min = min(_triangle.v0, _triangle.v1, _triangle.v2); - _outAabb.max = max(_triangle.v0, _triangle.v1, _triangle.v2); -} - -void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx) -{ - toObb(_obb, _aabb); - float result[16]; - mtxMul(result, _obb.mtx, _mtx); - memCopy(_obb.mtx, result, sizeof(result) ); -} - -void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride) -{ - Vec3 mn(init::None); - Vec3 mx(init::None); - uint8_t* vertex = (uint8_t*)_vertices; - - mn = mx = load(vertex); - vertex += _stride; - - for (uint32_t ii = 1; ii < _numVertices; ++ii) - { - const Vec3 pos = load(vertex); - vertex += _stride; - - mn = min(pos, mn); - mx = max(pos, mx); - } - - _outAabb.min = mn; - _outAabb.max = mx; -} - -void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride) -{ - Vec3 mn(init::None); - Vec3 mx(init::None); - uint8_t* vertex = (uint8_t*)_vertices; - mn = mx = mul(load(vertex), _mtx); - - vertex += _stride; - - for (uint32_t ii = 1; ii < _numVertices; ++ii) - { - Vec3 pos = mul(load(vertex), _mtx); - vertex += _stride; - - mn = min(pos, mn); - mx = max(pos, mx); - } - - _outAabb.min = mn; - _outAabb.max = mx; -} - -float calcAreaAabb(const Aabb& _aabb) -{ - const float ww = _aabb.max.x - _aabb.min.x; - const float hh = _aabb.max.y - _aabb.min.y; - const float dd = _aabb.max.z - _aabb.min.z; - return 2.0f * (ww*hh + ww*dd + hh*dd); -} - -void aabbExpand(Aabb& _outAabb, float _factor) -{ - _outAabb.min.x -= _factor; - _outAabb.min.y -= _factor; - _outAabb.min.z -= _factor; - _outAabb.max.x += _factor; - _outAabb.max.y += _factor; - _outAabb.max.z += _factor; -} - -void aabbExpand(Aabb& _outAabb, const Vec3& _pos) -{ - _outAabb.min = min(_outAabb.min, _pos); - _outAabb.max = max(_outAabb.max, _pos); -} - -void toObb(Obb& _outObb, const Aabb& _aabb) -{ - memSet(_outObb.mtx, 0, sizeof(_outObb.mtx) ); - _outObb.mtx[ 0] = (_aabb.max.x - _aabb.min.x) * 0.5f; - _outObb.mtx[ 5] = (_aabb.max.y - _aabb.min.y) * 0.5f; - _outObb.mtx[10] = (_aabb.max.z - _aabb.min.z) * 0.5f; - _outObb.mtx[12] = (_aabb.min.x + _aabb.max.x) * 0.5f; - _outObb.mtx[13] = (_aabb.min.y + _aabb.max.y) * 0.5f; - _outObb.mtx[14] = (_aabb.min.z + _aabb.max.z) * 0.5f; - _outObb.mtx[15] = 1.0f; -} - -void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps) -{ - Aabb aabb; - toAabb(aabb, _vertices, _numVertices, _stride); - float minArea = calcAreaAabb(aabb); - - Obb best; - toObb(best, aabb); - - float angleStep = float(kPiHalf/_steps); - float ax = 0.0f; - float mtx[16]; - - for (uint32_t ii = 0; ii < _steps; ++ii) - { - float ay = 0.0f; - - for (uint32_t jj = 0; jj < _steps; ++jj) - { - float az = 0.0f; - - for (uint32_t kk = 0; kk < _steps; ++kk) - { - mtxRotateXYZ(mtx, ax, ay, az); - - float mtxT[16]; - mtxTranspose(mtxT, mtx); - toAabb(aabb, mtxT, _vertices, _numVertices, _stride); - - float area = calcAreaAabb(aabb); - if (area < minArea) - { - minArea = area; - aabbTransformToObb(best, aabb, mtx); - } - - az += angleStep; - } - - ay += angleStep; - } - - ax += angleStep; - } - - memCopy(&_outObb, &best, sizeof(Obb) ); -} - -void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride) -{ - Aabb aabb; - toAabb(aabb, _vertices, _numVertices, _stride); - - Vec3 center = getCenter(aabb); - - float maxDistSq = 0.0f; - uint8_t* vertex = (uint8_t*)_vertices; - - for (uint32_t ii = 0; ii < _numVertices; ++ii) - { - const Vec3& pos = load(vertex); - vertex += _stride; - - const Vec3 tmp = sub(pos, center); - const float distSq = dot(tmp, tmp); - maxDistSq = max(distSq, maxDistSq); - } - - _sphere.center = center; - _sphere.radius = bx::sqrt(maxDistSq); -} - -void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step) -{ - RngMwc rng; - - uint8_t* vertex = (uint8_t*)_vertices; - - Vec3 center(init::None); - float* position = (float*)&vertex[0]; - center.x = position[0]; - center.y = position[1]; - center.z = position[2]; - - position = (float*)&vertex[1*_stride]; - center.x += position[0]; - center.y += position[1]; - center.z += position[2]; - - center.x *= 0.5f; - center.y *= 0.5f; - center.z *= 0.5f; - - float xx = position[0] - center.x; - float yy = position[1] - center.y; - float zz = position[2] - center.z; - float maxDistSq = xx*xx + yy*yy + zz*zz; - - float radiusStep = _step * 0.37f; - - bool done; - do - { - done = true; - for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices) - { - position = (float*)&vertex[index*_stride]; - - xx = position[0] - center.x; - yy = position[1] - center.y; - zz = position[2] - center.z; - float distSq = xx*xx + yy*yy + zz*zz; - - if (distSq > maxDistSq) - { - done = false; - - center.x += xx * radiusStep; - center.y += yy * radiusStep; - center.z += zz * radiusStep; - maxDistSq = lerp(maxDistSq, distSq, _step); - - break; - } - } - - } while (!done); - - _sphere.center = center; - _sphere.radius = bx::sqrt(maxDistSq); -} - -void buildFrustumPlanes(Plane* _result, const float* _viewProj) -{ - const float xw = _viewProj[ 3]; - const float yw = _viewProj[ 7]; - const float zw = _viewProj[11]; - const float ww = _viewProj[15]; - - const float xz = _viewProj[ 2]; - const float yz = _viewProj[ 6]; - const float zz = _viewProj[10]; - const float wz = _viewProj[14]; - - Plane& near = _result[0]; - Plane& far = _result[1]; - Plane& left = _result[2]; - Plane& right = _result[3]; - Plane& top = _result[4]; - Plane& bottom = _result[5]; - - near.normal.x = xw - xz; - near.normal.y = yw - yz; - near.normal.z = zw - zz; - near.dist = ww - wz; - - far.normal.x = xw + xz; - far.normal.y = yw + yz; - far.normal.z = zw + zz; - far.dist = ww + wz; - - const float xx = _viewProj[ 0]; - const float yx = _viewProj[ 4]; - const float zx = _viewProj[ 8]; - const float wx = _viewProj[12]; - - left.normal.x = xw - xx; - left.normal.y = yw - yx; - left.normal.z = zw - zx; - left.dist = ww - wx; - - right.normal.x = xw + xx; - right.normal.y = yw + yx; - right.normal.z = zw + zx; - right.dist = ww + wx; - - const float xy = _viewProj[ 1]; - const float yy = _viewProj[ 5]; - const float zy = _viewProj[ 9]; - const float wy = _viewProj[13]; - - top.normal.x = xw + xy; - top.normal.y = yw + yy; - top.normal.z = zw + zy; - top.dist = ww + wy; - - bottom.normal.x = xw - xy; - bottom.normal.y = yw - yy; - bottom.normal.z = zw - zy; - bottom.dist = ww - wy; - - Plane* plane = _result; - for (uint32_t ii = 0; ii < 6; ++ii) - { - const float invLen = 1.0f/length(plane->normal); - plane->normal = normalize(plane->normal); - plane->dist *= invLen; - ++plane; - } -} - -Ray makeRay(float _x, float _y, const float* _invVp) -{ - Ray ray; - - const Vec3 near = { _x, _y, 0.0f }; - ray.pos = mulH(near, _invVp); - - const Vec3 far = { _x, _y, 1.0f }; - Vec3 tmp = mulH(far, _invVp); - - const Vec3 dir = sub(tmp, ray.pos); - ray.dir = normalize(dir); - - return ray; -} - -bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit) -{ - const Vec3 invDir = rcp(_ray.dir); - const Vec3 tmp0 = sub(_aabb.min, _ray.pos); - const Vec3 t0 = mul(tmp0, invDir); - const Vec3 tmp1 = sub(_aabb.max, _ray.pos); - const Vec3 t1 = mul(tmp1, invDir); - - const Vec3 mn = min(t0, t1); - const Vec3 mx = max(t0, t1); - - const float tmin = max(mn.x, mn.y, mn.z); - const float tmax = min(mx.x, mx.y, mx.z); - - if (0.0f > tmax - || tmin > tmax) - { - return false; - } - - if (NULL != _hit) - { - _hit->plane.normal.x = float( (t1.x == tmin) - (t0.x == tmin) ); - _hit->plane.normal.y = float( (t1.y == tmin) - (t0.y == tmin) ); - _hit->plane.normal.z = float( (t1.z == tmin) - (t0.z == tmin) ); - - _hit->plane.dist = tmin; - _hit->pos = getPointAt(_ray, tmin); - } - - return true; -} - -static constexpr Aabb kUnitAabb = -{ - { -1.0f, -1.0f, -1.0f }, - { 1.0f, 1.0f, 1.0f }, -}; - -bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit) -{ - Aabb aabb; - toAabb(aabb, _obb); - - if (!intersect(_ray, aabb) ) - { - return false; - } - - float mtxInv[16]; - mtxInverse(mtxInv, _obb.mtx); - - Ray obbRay; - obbRay.pos = mul(_ray.pos, mtxInv); - obbRay.dir = mulXyz0(_ray.dir, mtxInv); - - if (intersect(obbRay, kUnitAabb, _hit) ) - { - if (NULL != _hit) - { - _hit->pos = mul(_hit->pos, _obb.mtx); - - const Vec3 tmp = mulXyz0(_hit->plane.normal, _obb.mtx); - _hit->plane.normal = normalize(tmp); - } - - return true; - } - - return false; -} - -bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit) -{ - Plane plane(_disk.normal, -dot(_disk.center, _disk.normal) ); - - Hit tmpHit; - _hit = NULL != _hit ? _hit : &tmpHit; - - if (intersect(_ray, plane, _hit) ) - { - const Vec3 tmp = sub(_disk.center, _hit->pos); - return dot(tmp, tmp) <= square(_disk.radius); - } - - return false; -} - -static bool intersect(const Ray& _ray, const Cylinder& _cylinder, bool _capsule, Hit* _hit) -{ - Vec3 axis = sub(_cylinder.end, _cylinder.pos); - const Vec3 rc = sub(_ray.pos, _cylinder.pos); - const Vec3 dxa = cross(_ray.dir, axis); - - const float len = length(dxa); - const Vec3 normal = normalize(dxa); - const float dist = bx::abs(dot(rc, normal) ); - - if (dist > _cylinder.radius) - { - return false; - } - - Vec3 vo = cross(rc, axis); - const float t0 = -dot(vo, normal) / len; - - vo = normalize(cross(normal, axis) ); - - const float rsq = square(_cylinder.radius); - const float ddoto = dot(_ray.dir, vo); - const float ss = t0 - bx::abs(bx::sqrt(rsq - square(dist) ) / ddoto); - - if (0.0f > ss) - { - return false; - } - - const Vec3 point = getPointAt(_ray, ss); - - const float axisLen = length(axis); - axis = normalize(axis); - const float pdota = dot(_cylinder.pos, axis); - const float height = dot(point, axis) - pdota; - - if (0.0f < height - && axisLen > height) - { - if (NULL != _hit) - { - const float t1 = height / axisLen; - const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1); - - _hit->pos = point; - - const Vec3 tmp = sub(point, pointOnAxis); - _hit->plane.normal = normalize(tmp); - - _hit->plane.dist = ss; - } - - return true; - } - - if (_capsule) - { - const float rdota = dot(_ray.pos, axis); - const float pp = rdota - pdota; - const float t1 = pp / axisLen; - - const Vec3 pointOnAxis = lerp(_cylinder.pos, _cylinder.end, t1); - const Vec3 axisToRay = sub(_ray.pos, pointOnAxis); - - if (_cylinder.radius < length(axisToRay) - && 0.0f > ss) - { - return false; - } - - Sphere sphere; - sphere.radius = _cylinder.radius; - - sphere.center = 0.0f >= height - ? _cylinder.pos - : _cylinder.end - ; - - return intersect(_ray, sphere, _hit); - } - - Plane plane(init::None); - Vec3 pos(init::None); - - if (0.0f >= height) - { - plane.normal = neg(axis); - pos = _cylinder.pos; - } - else - { - plane.normal = axis; - pos = _cylinder.end; - } - - plane.dist = -dot(pos, plane.normal); - - Hit tmpHit; - _hit = NULL != _hit ? _hit : &tmpHit; - - if (intersect(_ray, plane, _hit) ) - { - const Vec3 tmp = sub(pos, _hit->pos); - return dot(tmp, tmp) <= rsq; - } - - return false; -} - -bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit) -{ - return intersect(_ray, _cylinder, false, _hit); -} - -bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit) -{ - BX_STATIC_ASSERT(sizeof(Capsule) == sizeof(Cylinder) ); - return intersect(_ray, *( (const Cylinder*)&_capsule), true, _hit); -} - -bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit) -{ - const Vec3 axis = sub(_cone.pos, _cone.end); - - const float len = length(axis); - const Vec3 normal = normalize(axis); - - Disk disk; - disk.center = _cone.pos; - disk.normal = normal; - disk.radius = _cone.radius; - - Hit tmpInt; - Hit* out = NULL != _hit ? _hit : &tmpInt; - bool hit = intersect(_ray, disk, out); - - const Vec3 ro = sub(_ray.pos, _cone.end); - - const float hyp = bx::sqrt(square(_cone.radius) + square(len) ); - const float cosaSq = square(len/hyp); - const float ndoto = dot(normal, ro); - const float ndotd = dot(normal, _ray.dir); - - const float aa = square(ndotd) - cosaSq; - const float bb = 2.0f * (ndotd*ndoto - dot(_ray.dir, ro)*cosaSq); - const float cc = square(ndoto) - dot(ro, ro)*cosaSq; - - float det = bb*bb - 4.0f*aa*cc; - - if (0.0f > det) - { - return hit; - } - - det = bx::sqrt(det); - const float invA2 = 1.0f / (2.0f*aa); - const float t1 = (-bb - det) * invA2; - const float t2 = (-bb + det) * invA2; - - float tt = t1; - if (0.0f > t1 - || (0.0f < t2 && t2 < t1) ) - { - tt = t2; - } - - if (0.0f > tt) - { - return hit; - } - - const Vec3 hitPos = getPointAt(_ray, tt); - const Vec3 point = sub(hitPos, _cone.end); - - const float hh = dot(normal, point); - - if (0.0f > hh - || len < hh) - { - return hit; - } - - if (NULL != _hit) - { - if (!hit - || tt < _hit->plane.dist) - { - _hit->plane.dist = tt; - _hit->pos = hitPos; - - const float scale = hh / dot(point, point); - const Vec3 pointScaled = mul(point, scale); - - const Vec3 tmp = sub(pointScaled, normal); - _hit->plane.normal = normalize(tmp); - } - } - - return true; -} - -bool intersect(const Ray& _ray, const Plane& _plane, bool _doublesided, Hit* _hit) -{ - const float dist = distance(_plane, _ray.pos); - const float ndotd = dot(_ray.dir, _plane.normal); - - if (!_doublesided - && (0.0f > dist || 0.0f < ndotd) ) - { - return false; - } - - if (NULL != _hit) - { - _hit->plane.normal = _plane.normal; - - float tt = -dist/ndotd; - _hit->plane.dist = tt; - _hit->pos = getPointAt(_ray, tt); - } - - return true; -} - -bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit) -{ - const Vec3 rs = sub(_ray.pos, _sphere.center); - - const float bb = dot(rs, _ray.dir); - if (0.0f < bb) - { - return false; - } - - const float aa = dot(_ray.dir, _ray.dir); - const float cc = dot(rs, rs) - square(_sphere.radius); - - const float discriminant = bb*bb - aa*cc; - - if (0.0f >= discriminant) - { - return false; - } - - const float sqrtDiscriminant = bx::sqrt(discriminant); - const float invA = 1.0f / aa; - const float tt = -(bb + sqrtDiscriminant)*invA; - - if (0.0f >= tt) - { - return false; - } - - if (NULL != _hit) - { - _hit->plane.dist = tt; - - const Vec3 point = getPointAt(_ray, tt); - _hit->pos = point; - - const Vec3 tmp = sub(point, _sphere.center); - _hit->plane.normal = normalize(tmp); - } - - return true; -} - -bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit) -{ - const Vec3 edge10 = sub(_triangle.v1, _triangle.v0); - const Vec3 edge02 = sub(_triangle.v0, _triangle.v2); - const Vec3 normal = cross(edge02, edge10); - const Vec3 vo = sub(_triangle.v0, _ray.pos); - const Vec3 dxo = cross(_ray.dir, vo); - const float det = dot(normal, _ray.dir); - - if (0.0f < det) - { - return false; - } - - const float invDet = 1.0f/det; - const float bz = dot(dxo, edge02) * invDet; - const float by = dot(dxo, edge10) * invDet; - const float bx = 1.0f - by - bz; - - if (0.0f > bx - || 0.0f > by - || 0.0f > bz) - { - return false; - } - - if (NULL != _hit) - { - _hit->plane.normal = normalize(normal); - - const float tt = dot(normal, vo) * invDet; - _hit->plane.dist = tt; - _hit->pos = getPointAt(_ray, tt); - } - - return true; -} - -Vec3 barycentric(const Triangle& _triangle, const Vec3& _pos) -{ - const Vec3 v0 = sub(_triangle.v1, _triangle.v0); - const Vec3 v1 = sub(_triangle.v2, _triangle.v0); - const Vec3 v2 = sub(_pos, _triangle.v0); - - const float dot00 = dot(v0, v0); - const float dot01 = dot(v0, v1); - const float dot02 = dot(v0, v2); - const float dot11 = dot(v1, v1); - const float dot12 = dot(v1, v2); - - const float invDenom = 1.0f/(dot00*dot11 - square(dot01) ); - - const float vv = (dot11*dot02 - dot01*dot12)*invDenom; - const float ww = (dot00*dot12 - dot01*dot02)*invDenom; - const float uu = 1.0f - vv - ww; - - return { uu, vv, ww }; -} - -Vec3 cartesian(const Triangle& _triangle, const Vec3& _uvw) -{ - const Vec3 b0 = mul(_triangle.v0, _uvw.x); - const Vec3 b1 = mul(_triangle.v1, _uvw.y); - const Vec3 b2 = mul(_triangle.v2, _uvw.z); - - return add(add(b0, b1), b2); -} - -void calcPlane(Plane& _outPlane, const Disk& _disk) -{ - calcPlane(_outPlane, _disk.normal, _disk.center); -} - -void calcPlane(Plane& _outPlane, const Triangle& _triangle) -{ - calcPlane(_outPlane, _triangle.v0, _triangle.v1, _triangle.v2); -} - -struct Interval -{ - Interval(float _val) - : start(_val) - , end(_val) - { - } - - Interval(float _start, float _end) - : start(_start) - , end(_end) - { - } - - void set(float _val) - { - start = _val; - end = _val; - } - - void expand(float _val) - { - start = min(_val, start); - end = max(_val, end); - } - - float start; - float end; -}; - -bool overlap(const Interval& _a, const Interval& _b) -{ - return _a.end > _b.start - && _b.end > _a.start - ; -} - -float projectToAxis(const Vec3& _axis, const Vec3& _point) -{ - return dot(_axis, _point); -} - -Interval projectToAxis(const Vec3& _axis, const Vec3* _points, uint32_t _num) -{ - Interval interval(projectToAxis(_axis, _points[0]) ); - - for (uint32_t ii = 1; ii < _num; ++ii) - { - interval.expand(projectToAxis(_axis, _points[ii]) ); - } - - return interval; -} - -Interval projectToAxis(const Vec3& _axis, const Aabb& _aabb) -{ - const float extent = bx::abs(projectToAxis(abs(_axis), getExtents(_aabb) ) ); - const float center = projectToAxis( _axis , getCenter (_aabb) ); - return - { - center - extent, - center + extent, - }; -} - -Interval projectToAxis(const Vec3& _axis, const Triangle& _triangle) -{ - const float a0 = projectToAxis(_axis, _triangle.v0); - const float a1 = projectToAxis(_axis, _triangle.v1); - const float a2 = projectToAxis(_axis, _triangle.v2); - return - { - min(a0, a1, a2), - max(a0, a1, a2), - }; -} - -struct Srt -{ - Quaternion rotation = init::Identity; - Vec3 translation = init::Zero; - Vec3 scale = init::Zero; -}; - -Srt toSrt(const Aabb& _aabb) -{ - return { init::Identity, getCenter(_aabb), getExtents(_aabb) }; -} - -Srt toSrt(const void* _mtx) -{ - Srt result; - - const float* mtx = (const float*)_mtx; - - result.translation = { mtx[12], mtx[13], mtx[14] }; - - float xx = mtx[ 0]; - float xy = mtx[ 1]; - float xz = mtx[ 2]; - float yx = mtx[ 4]; - float yy = mtx[ 5]; - float yz = mtx[ 6]; - float zx = mtx[ 8]; - float zy = mtx[ 9]; - float zz = mtx[10]; - - result.scale = - { - bx::sqrt(xx*xx + xy*xy + xz*xz), - bx::sqrt(yx*yx + yy*yy + yz*yz), - bx::sqrt(zx*zx + zy*zy + zz*zz), - }; - - const Vec3 invScale = rcp(result.scale); - - xx *= invScale.x; - xy *= invScale.x; - xz *= invScale.x; - yx *= invScale.y; - yy *= invScale.y; - yz *= invScale.y; - zx *= invScale.z; - zy *= invScale.z; - zz *= invScale.z; - - const float trace = xx + yy + zz; - - if (0.0f < trace) - { - const float invS = 0.5f * rsqrt(trace + 1.0f); - result.rotation = - { - (yz - zy) * invS, - (zx - xz) * invS, - (xy - yx) * invS, - 0.25f / invS, - }; - } - else - { - if (xx > yy - && xx > zz) - { - const float invS = 0.5f * bx::sqrt(max(1.0f + xx - yy - zz, 1e-8f) ); - result.rotation = - { - 0.25f / invS, - (xy + yx) * invS, - (xz + zx) * invS, - (yz - zy) * invS, - }; - } - else if (yy > zz) - { - const float invS = 0.5f * bx::sqrt(max(1.0f + yy - xx - zz, 1e-8f) ); - result.rotation = - { - (xy + yx) * invS, - 0.25f / invS, - (yz + zy) * invS, - (zx - xz) * invS, - }; - } - else - { - const float invS = 0.5f * bx::sqrt(max(1.0f + zz - xx - yy, 1e-8f) ); - result.rotation = - { - (xz + zx) * invS, - (yz + zy) * invS, - 0.25f / invS, - (xy - yx) * invS, - }; - } - } - - return result; -} - -void mtxFromSrt(float* _outMtx, const Srt& _srt) -{ - mtxFromQuaternion(_outMtx, _srt.rotation); - - store(&_outMtx[0], mul(load(&_outMtx[0]), _srt.scale.x) ); - store(&_outMtx[4], mul(load(&_outMtx[4]), _srt.scale.y) ); - store(&_outMtx[8], mul(load(&_outMtx[8]), _srt.scale.z) ); - - store(&_outMtx[12], _srt.translation); -} - -bool isNearZero(float _v) -{ - return isEqual(_v, 0.0f, 0.00001f); -} - -bool isNearZero(const Vec3& _v) -{ - return isNearZero(dot(_v, _v) ); -} - -struct Line -{ - Vec3 pos = init::None; - Vec3 dir = init::None; -}; - -inline Vec3 getPointAt(const Line& _line, float _t) -{ - return mad(_line.dir, _t, _line.pos); -} - -bool intersect(Line& _outLine, const Plane& _planeA, const Plane& _planeB) -{ - const Vec3 axb = cross(_planeA.normal, _planeB.normal); - const float denom = dot(axb, axb); - - if (isNearZero(denom) ) - { - return false; - } - - const Vec3 bxaxb = cross(_planeB.normal, axb); - const Vec3 axbxa = cross(axb, _planeA.normal); - const Vec3 tmp0 = mul(bxaxb, _planeA.dist); - const Vec3 tmp1 = mul(axbxa, _planeB.dist); - const Vec3 tmp2 = add(tmp0, tmp1); - - _outLine.pos = mul(tmp2, -1.0f/denom); - _outLine.dir = normalize(axb); - - return true; -} - -Vec3 intersectPlanes(const Plane& _pa, const Plane& _pb, const Plane& _pc) -{ - const Vec3 axb = cross(_pa.normal, _pb.normal); - const Vec3 bxc = cross(_pb.normal, _pc.normal); - const Vec3 cxa = cross(_pc.normal, _pa.normal); - const Vec3 tmp0 = mul(bxc, _pa.dist); - const Vec3 tmp1 = mul(cxa, _pb.dist); - const Vec3 tmp2 = mul(axb, _pc.dist); - const Vec3 tmp3 = add(tmp0, tmp1); - const Vec3 tmp4 = add(tmp3, tmp2); - - const float denom = dot(_pa.normal, bxc); - const Vec3 result = mul(tmp4, -1.0f/denom); - - return result; -} - -struct LineSegment -{ - Vec3 pos; - Vec3 end; -}; - -inline Vec3 getPointAt(const LineSegment& _line, float _t) -{ - return lerp(_line.pos, _line.end, _t); -} - -bool intersect(float& _outTa, float& _outTb, const LineSegment& _a, const LineSegment& _b) -{ - // Reference(s): - // - // - The shortest line between two lines in 3D - // https://web.archive.org/web/20120309093234/http://paulbourke.net/geometry/lineline3d/ - - const Vec3 bd = sub(_b.end, _b.pos); - if (isNearZero(bd) ) - { - return false; - } - - const Vec3 ad = sub(_a.end, _a.pos); - if (isNearZero(ad) ) - { - return false; - } - - const Vec3 ab = sub(_a.pos, _b.pos); - - const float d0 = projectToAxis(ab, bd); - const float d1 = projectToAxis(ad, bd); - const float d2 = projectToAxis(ab, ad); - const float d3 = projectToAxis(bd, bd); - const float d4 = projectToAxis(ad, ad); - - const float denom = d4*d3 - square(d1); - - float ta = 0.0f; - - if (!isNearZero(denom) ) - { - ta = (d0*d1 - d2*d3)/denom; - } - - _outTa = ta; - _outTb = (d0+d1*ta)/d3; - - return true; -} - -bool intersect(const LineSegment& _a, const LineSegment& _b) -{ - float ta, tb; - if (!intersect(ta, tb, _a, _b) ) - { - return false; - } - - return 0.0f >= ta - && 1.0f <= ta - && 0.0f >= tb - && 1.0f <= tb - ; -} - -bool intersect(const LineSegment& _line, const Plane& _plane, Hit* _hit) -{ - const float dist = distance(_plane, _line.pos); - const float flip = sign(dist); - const Vec3 dir = normalize(sub(_line.end, _line.pos) ); - const float ndotd = dot(dir, _plane.normal); - const float tt = -dist/ndotd; - const float len = length(sub(_line.end, _line.pos) ); - - if (tt < 0.0f || tt > len) - { - return false; - } - - if (NULL != _hit) - { - _hit->pos = mad(dir, tt, _line.pos); - - _hit->plane.normal = mul(_plane.normal, flip); - _hit->plane.dist = -dot(_hit->plane.normal, _hit->pos); - } - - return true; -} - -float distance(const Plane& _plane, const LineSegment& _line) -{ - const float pd = distance(_plane, _line.pos); - const float ed = distance(_plane, _line.end); - return min(max(pd*ed, 0.0f), bx::abs(pd), bx::abs(ed) ); -} - -Vec3 closestPoint(const Line& _line, const Vec3& _point) -{ - const float tt = projectToAxis(_line.dir, sub(_point, _line.pos) ); - return getPointAt(_line, tt); -} - -Vec3 closestPoint(const LineSegment& _line, const Vec3& _point, float& _outT) -{ - const Vec3 axis = sub(_line.end, _line.pos); - const float lengthSq = dot(axis, axis); - const float tt = clamp(projectToAxis(axis, sub(_point, _line.pos) ) / lengthSq, 0.0f, 1.0f); - _outT = tt; - return mad(axis, tt, _line.pos); -} - -Vec3 closestPoint(const LineSegment& _line, const Vec3& _point) -{ - float ignored; - return closestPoint(_line, _point, ignored); -} - -Vec3 closestPoint(const Plane& _plane, const Vec3& _point) -{ - const float dist = distance(_plane, _point); - return sub(_point, mul(_plane.normal, dist) ); -} - -Vec3 closestPoint(const Aabb& _aabb, const Vec3& _point) -{ - return clamp(_point, _aabb.min, _aabb.max); -} - -Vec3 closestPoint(const Obb& _obb, const Vec3& _point) -{ - const Srt srt = toSrt(_obb.mtx); - - Aabb aabb; - toAabb(aabb, srt.scale); - - const Quaternion invRotation = invert(srt.rotation); - const Vec3 obbSpacePos = mul(sub(_point, srt.translation), srt.rotation); - const Vec3 pos = closestPoint(aabb, obbSpacePos); - - return add(mul(pos, invRotation), srt.translation); -} - -Vec3 closestPoint(const Triangle& _triangle, const Vec3& _point) -{ - Plane plane(init::None); - calcPlane(plane, _triangle); - - const Vec3 pos = closestPoint(plane, _point); - const Vec3 uvw = barycentric(_triangle, pos); - - return cartesian(_triangle, clamp(uvw, Vec3(0.0f), Vec3(1.0f) ) ); -} - -bool overlap(const Aabb& _aabb, const Vec3& _pos) -{ - const Vec3 ac = getCenter(_aabb); - const Vec3 ae = getExtents(_aabb); - const Vec3 abc = bx::abs(sub(ac, _pos) ); - - return abc.x <= ae.x - && abc.y <= ae.y - && abc.z <= ae.z - ; -} - -bool overlap(const Aabb& _aabbA, const Aabb& _aabbB) -{ - return true - && overlap(Interval{_aabbA.min.x, _aabbA.max.x}, Interval{_aabbB.min.x, _aabbB.max.x}) - && overlap(Interval{_aabbA.min.y, _aabbA.max.y}, Interval{_aabbB.min.y, _aabbB.max.y}) - && overlap(Interval{_aabbA.min.z, _aabbA.max.z}, Interval{_aabbB.min.z, _aabbB.max.z}) - ; -} - -bool overlap(const Aabb& _aabb, const Plane& _plane) -{ - const Vec3 center = getCenter(_aabb); - const float dist = distance(_plane, center); - - const Vec3 extents = getExtents(_aabb); - const Vec3 normal = bx::abs(_plane.normal); - const float radius = dot(extents, normal); - - return bx::abs(dist) <= radius; -} - -static constexpr Vec3 kAxis[] = -{ - { 1.0f, 0.0f, 0.0f }, - { 0.0f, 1.0f, 0.0f }, - { 0.0f, 0.0f, 1.0f }, -}; - -bool overlap(const Aabb& _aabb, const Triangle& _triangle) -{ - Aabb triAabb; - toAabb(triAabb, _triangle); - - if (!overlap(_aabb, triAabb) ) - { - return false; - } - - Plane plane(init::None); - calcPlane(plane, _triangle); - - if (!overlap(_aabb, plane) ) - { - return false; - } - - const Vec3 center = getCenter(_aabb); - const Vec3 v0 = sub(_triangle.v0, center); - const Vec3 v1 = sub(_triangle.v1, center); - const Vec3 v2 = sub(_triangle.v2, center); - - const Vec3 edge[] = - { - sub(v1, v0), - sub(v2, v1), - sub(v0, v2), - }; - - for (uint32_t ii = 0; ii < 3; ++ii) - { - for (uint32_t jj = 0; jj < 3; ++jj) - { - const Vec3 axis = cross(kAxis[ii], edge[jj]); - - const Interval aabbR = projectToAxis(axis, _aabb); - const Interval triR = projectToAxis(axis, _triangle); - - if (!overlap(aabbR, triR) ) - { - return false; - } - } - } - - return true; -} - -bool overlap(const Aabb& _aabb, const Capsule& _capsule) -{ - const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, getCenter(_aabb) ); - return overlap(_aabb, Sphere{pos, _capsule.radius}); -} - -bool overlap(const Aabb& _aabb, const Cone& _cone) -{ - float tt; - const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, getCenter(_aabb), tt); - return overlap(_aabb, Sphere{pos, lerp(_cone.radius, 0.0f, tt)}); -} - -bool overlap(const Aabb& _aabb, const Disk& _disk) -{ - if (!overlap(_aabb, Sphere{_disk.center, _disk.radius}) ) - { - return false; - } - - Plane plane(init::None); - calcPlane(plane, _disk.normal, _disk.center); - - return overlap(_aabb, plane); -} - -static void calcObbVertices( - bx::Vec3* _outVertices - , const bx::Vec3& _axisX - , const bx::Vec3& _axisY - , const bx::Vec3& _axisZ - , const bx::Vec3& _pos - , const bx::Vec3& _scale - ) -{ - const Vec3 ax = mul(_axisX, _scale.x); - const Vec3 ay = mul(_axisY, _scale.y); - const Vec3 az = mul(_axisZ, _scale.z); - - const Vec3 ppx = add(_pos, ax); - const Vec3 pmx = sub(_pos, ax); - const Vec3 ypz = add(ay, az); - const Vec3 ymz = sub(ay, az); - - _outVertices[0] = sub(pmx, ymz); - _outVertices[1] = sub(ppx, ymz); - _outVertices[2] = add(ppx, ymz); - _outVertices[3] = add(pmx, ymz); - _outVertices[4] = sub(pmx, ypz); - _outVertices[5] = sub(ppx, ypz); - _outVertices[6] = add(ppx, ypz); - _outVertices[7] = add(pmx, ypz); -} - -static bool overlaps(const Vec3& _axis, const Vec3* _vertsA, const Vec3* _vertsB) -{ - Interval ia = projectToAxis(_axis, _vertsA, 8); - Interval ib = projectToAxis(_axis, _vertsB, 8); - - return overlap(ia, ib); -} - -static bool overlap(const Srt& _srtA, const Srt& _srtB) -{ - const Vec3 ax = toXAxis(_srtA.rotation); - const Vec3 ay = toYAxis(_srtA.rotation); - const Vec3 az = toZAxis(_srtA.rotation); - - const Vec3 bx = toXAxis(_srtB.rotation); - const Vec3 by = toYAxis(_srtB.rotation); - const Vec3 bz = toZAxis(_srtB.rotation); - - Vec3 vertsA[8] = { init::None, init::None, init::None, init::None, init::None, init::None, init::None, init::None }; - calcObbVertices(vertsA, ax, ay, az, init::Zero, _srtA.scale); - - Vec3 vertsB[8] = { init::None, init::None, init::None, init::None, init::None, init::None, init::None, init::None }; - calcObbVertices(vertsB, bx, by, bz, sub(_srtB.translation, _srtA.translation), _srtB.scale); - - return overlaps(ax, vertsA, vertsB) - && overlaps(ay, vertsA, vertsB) - && overlaps(az, vertsA, vertsB) - && overlaps(bx, vertsA, vertsB) - && overlaps(by, vertsA, vertsB) - && overlaps(bz, vertsA, vertsB) - && overlaps(cross(ax, bx), vertsA, vertsB) - && overlaps(cross(ax, by), vertsA, vertsB) - && overlaps(cross(ax, bz), vertsA, vertsB) - && overlaps(cross(ay, bx), vertsA, vertsB) - && overlaps(cross(ay, by), vertsA, vertsB) - && overlaps(cross(ay, bz), vertsA, vertsB) - && overlaps(cross(az, bx), vertsA, vertsB) - && overlaps(cross(az, by), vertsA, vertsB) - && overlaps(cross(az, bz), vertsA, vertsB) - ; -} - -bool overlap(const Aabb& _aabb, const Obb& _obb) -{ - const Srt srtA = toSrt(_aabb); - const Srt srtB = toSrt(_obb.mtx); - - return overlap(srtA, srtB); -} - -bool overlap(const Capsule& _capsule, const Vec3& _pos) -{ - const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _pos); - return overlap(Sphere{pos, _capsule.radius}, _pos); -} - -bool overlap(const Capsule& _capsule, const Plane& _plane) -{ - return distance(_plane, LineSegment{_capsule.pos, _capsule.end}) <= _capsule.radius; -} - -bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB) -{ - float ta, tb; - if (!intersect(ta, tb, {_capsuleA.pos, _capsuleA.end}, {_capsuleB.pos, _capsuleB.end}) ) - { - return false; - } - - if (0.0f <= ta - && 1.0f >= ta - && 0.0f <= tb - && 1.0f >= tb) - { - const Vec3 ad = sub(_capsuleA.end, _capsuleA.pos); - const Vec3 bd = sub(_capsuleB.end, _capsuleB.pos); - - return overlap( - Sphere{mad(ad, ta, _capsuleA.pos), _capsuleA.radius} - , Sphere{mad(bd, tb, _capsuleB.pos), _capsuleB.radius} - ); - } - - if (0.0f <= ta - && 1.0f >= ta) - { - return overlap(_capsuleA, Sphere{0.0f >= tb ? _capsuleB.pos : _capsuleB.end, _capsuleB.radius}); - } - - if (0.0f <= tb - && 1.0f >= tb) - { - return overlap(_capsuleB, Sphere{0.0f >= ta ? _capsuleA.pos : _capsuleA.end, _capsuleA.radius}); - } - - const Vec3 pa = 0.0f > ta ? _capsuleA.pos : _capsuleA.end; - const Vec3 pb = 0.0f > tb ? _capsuleB.pos : _capsuleB.end; - const Vec3 closestA = closestPoint(LineSegment{_capsuleA.pos, _capsuleA.end}, pb); - const Vec3 closestB = closestPoint(LineSegment{_capsuleB.pos, _capsuleB.end}, pa); - - if (dot(closestA, pb) <= dot(closestB, pa) ) - { - return overlap(_capsuleA, Sphere{closestB, _capsuleB.radius}); - } - - return overlap(_capsuleB, Sphere{closestA, _capsuleA.radius}); -} - -bool overlap(const Cone& _cone, const Vec3& _pos) -{ - float tt; - const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _pos, tt); - return overlap(Disk{pos, normalize(sub(_cone.end, _cone.pos) ), lerp(_cone.radius, 0.0f, tt)}, _pos); -} - -bool overlap(const Cone& _cone, const Cylinder& _cylinder) -{ - BX_UNUSED(_cone, _cylinder); - return false; -} - -bool overlap(const Cone& _cone, const Capsule& _capsule) -{ - BX_UNUSED(_cone, _capsule); - return false; -} - -bool overlap(const Cone& _coneA, const Cone& _coneB) -{ - BX_UNUSED(_coneA, _coneB); - return false; -} - -bool overlap(const Cone& _cone, const Disk& _disk) -{ - BX_UNUSED(_cone, _disk); - return false; -} - -bool overlap(const Cone& _cone, const Obb& _obb) -{ - BX_UNUSED(_cone, _obb); - return false; -} - -bool overlap(const Cylinder& _cylinder, const Vec3& _pos) -{ - const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, _pos); - return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _pos); -} - -bool overlap(const Cylinder& _cylinder, const Sphere& _sphere) -{ - const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, _sphere.center); - return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _sphere); -} - -bool overlap(const Cylinder& _cylinder, const Aabb& _aabb) -{ - const Vec3 pos = closestPoint(LineSegment{_cylinder.pos, _cylinder.end}, getCenter(_aabb) ); - return overlap(Disk{pos, normalize(sub(_cylinder.end, _cylinder.pos) ), _cylinder.radius}, _aabb); -} - -bool overlap(const Cylinder& _cylinder, const Plane& _plane) -{ - BX_UNUSED(_cylinder, _plane); - return false; -} - -bool overlap(const Cylinder& _cylinderA, const Cylinder& _cylinderB) -{ - BX_UNUSED(_cylinderA, _cylinderB); - return false; -} - -bool overlap(const Cylinder& _cylinder, const Capsule& _capsule) -{ - BX_UNUSED(_cylinder, _capsule); - return false; -} - -bool overlap(const Cylinder& _cylinder, const Disk& _disk) -{ - BX_UNUSED(_cylinder, _disk); - return false; -} - -bool overlap(const Cylinder& _cylinder, const Obb& _obb) -{ - BX_UNUSED(_cylinder, _obb); - return false; -} - -bool overlap(const Disk& _disk, const Vec3& _pos) -{ - Plane plane(init::None); - calcPlane(plane, _disk.normal, _disk.center); - - if (!isNearZero(distance(plane, _pos) ) ) - { - return false; - } - - return distanceSq(_disk.center, _pos) <= square(_disk.radius); -} - -bool overlap(const Disk& _disk, const Plane& _plane) -{ - Plane plane(init::None); - calcPlane(plane, _disk.normal, _disk.center); - - if (!overlap(plane, _plane) ) - { - return false; - } - - return overlap(_plane, Sphere{_disk.center, _disk.radius}); -} - -bool overlap(const Disk& _disk, const Capsule& _capsule) -{ - if (!overlap(_capsule, Sphere{_disk.center, _disk.radius}) ) - { - return false; - } - - Plane plane(init::None); - calcPlane(plane, _disk.normal, _disk.center); - - return overlap(_capsule, plane); -} - -bool overlap(const Disk& _diskA, const Disk& _diskB) -{ - Plane planeA(init::None); - calcPlane(planeA, _diskA.normal, _diskA.center); - - Plane planeB(init::None); - calcPlane(planeB, _diskB); - - Line line; - - if (!intersect(line, planeA, planeB) ) - { - return false; - } - - const Vec3 pa = closestPoint(line, _diskA.center); - const Vec3 pb = closestPoint(line, _diskB.center); - - const float lenA = distance(pa, _diskA.center); - const float lenB = distance(pb, _diskB.center); - - return bx::sqrt(square(_diskA.radius) - square(lenA) ) - + bx::sqrt(square(_diskB.radius) - square(lenB) ) - >= distance(pa, pb) - ; -} - -bool overlap(const Disk& _disk, const Obb& _obb) -{ - if (!overlap(_obb, Sphere{_disk.center, _disk.radius}) ) - { - return false; - } - - Plane plane(init::None); - calcPlane(plane, _disk.normal, _disk.center); - - return overlap(_obb, plane); -} - -bool overlap(const Obb& _obb, const Vec3& _pos) -{ - const Srt srt = toSrt(_obb.mtx); - - Aabb aabb; - toAabb(aabb, srt.scale); - - const Quaternion invRotation = invert(srt.rotation); - const Vec3 pos = mul(sub(_pos, srt.translation), invRotation); - - return overlap(aabb, pos); -} - -bool overlap(const Obb& _obb, const Plane& _plane) -{ - const Srt srt = toSrt(_obb.mtx); - - const Quaternion invRotation = invert(srt.rotation); - const Vec3 axis = - { - projectToAxis(_plane.normal, mul(Vec3{1.0f, 0.0f, 0.0f}, invRotation) ), - projectToAxis(_plane.normal, mul(Vec3{0.0f, 1.0f, 0.0f}, invRotation) ), - projectToAxis(_plane.normal, mul(Vec3{0.0f, 0.0f, 1.0f}, invRotation) ), - }; - - const float dist = bx::abs(distance(_plane, srt.translation) ); - const float radius = dot(srt.scale, bx::abs(axis) ); - - return dist <= radius; -} - -bool overlap(const Obb& _obb, const Capsule& _capsule) -{ - const Srt srt = toSrt(_obb.mtx); - - Aabb aabb; - toAabb(aabb, srt.scale); - - const Quaternion invRotation = invert(srt.rotation); - - const Capsule capsule = - { - mul(sub(_capsule.pos, srt.translation), invRotation), - mul(sub(_capsule.end, srt.translation), invRotation), - _capsule.radius, - }; - - return overlap(aabb, capsule); -} - -bool overlap(const Obb& _obbA, const Obb& _obbB) -{ - const Srt srtA = toSrt(_obbA.mtx); - const Srt srtB = toSrt(_obbB.mtx); - - return overlap(srtA, srtB); -} - -bool overlap(const Plane& _plane, const LineSegment& _line) -{ - return isNearZero(distance(_plane, _line) ); -} - -bool overlap(const Plane& _plane, const Vec3& _pos) -{ - return isNearZero(distance(_plane, _pos) ); -} - -bool overlap(const Plane& _planeA, const Plane& _planeB) -{ - const Vec3 dir = cross(_planeA.normal, _planeB.normal); - const float len = length(dir); - - return !isNearZero(len); -} - -bool overlap(const Plane& _plane, const Cone& _cone) -{ - const Vec3 axis = sub(_cone.pos, _cone.end); - const float len = length(axis); - const Vec3 dir = normalize(axis); - - const Vec3 v1 = cross(_plane.normal, dir); - const Vec3 v2 = cross(v1, dir); - - const float bb = len; - const float aa = _cone.radius; - const float cc = bx::sqrt(square(aa) + square(bb) ); - - const Vec3 pos = add(add(_cone.end - , mul(dir, len * bb/cc) ) - , mul(v2, len * aa/cc) - ); - - return overlap(_plane, LineSegment{pos, _cone.end}); -} - -bool overlap(const Sphere& _sphere, const Vec3& _pos) -{ - const float distSq = distanceSq(_sphere.center, _pos); - const float radiusSq = square(_sphere.radius); - return distSq <= radiusSq; -} - -bool overlap(const Sphere& _sphereA, const Sphere& _sphereB) -{ - const float distSq = distanceSq(_sphereA.center, _sphereB.center); - const float radiusSq = square(_sphereA.radius + _sphereB.radius); - return distSq <= radiusSq; -} - -bool overlap(const Sphere& _sphere, const Aabb& _aabb) -{ - const Vec3 pos = closestPoint(_aabb, _sphere.center); - return overlap(_sphere, pos); -} - -bool overlap(const Sphere& _sphere, const Plane& _plane) -{ - return bx::abs(distance(_plane, _sphere.center) ) <= _sphere.radius; -} - -bool overlap(const Sphere& _sphere, const Triangle& _triangle) -{ - Plane plane(init::None); - calcPlane(plane, _triangle); - - if (!overlap(_sphere, plane) ) - { - return false; - } - - const Vec3 pos = closestPoint(plane, _sphere.center); - const Vec3 uvw = barycentric(_triangle, pos); - const float nr = -_sphere.radius; - - return uvw.x >= nr - && uvw.y >= nr - && uvw.z >= nr - ; -} - -bool overlap(const Sphere& _sphere, const Capsule& _capsule) -{ - const Vec3 pos = closestPoint(LineSegment{_capsule.pos, _capsule.end}, _sphere.center); - return overlap(_sphere, Sphere{pos, _capsule.radius}); -} - -bool overlap(const Sphere& _sphere, const Cone& _cone) -{ - float tt; - const Vec3 pos = closestPoint(LineSegment{_cone.pos, _cone.end}, _sphere.center, tt); - return overlap(_sphere, Sphere{pos, lerp(_cone.radius, 0.0f, tt)}); -} - -bool overlap(const Sphere& _sphere, const Disk& _disk) -{ - if (!overlap(_sphere, Sphere{_disk.center, _disk.radius}) ) - { - return false; - } - - Plane plane(init::None); - calcPlane(plane, _disk.normal, _disk.center); - - return overlap(_sphere, plane); -} - -bool overlap(const Sphere& _sphere, const Obb& _obb) -{ - const Vec3 pos = closestPoint(_obb, _sphere.center); - return overlap(_sphere, pos); -} - -bool overlap(const Triangle& _triangle, const Vec3& _pos) -{ - const Vec3 uvw = barycentric(_triangle, _pos); - - return uvw.x >= 0.0f - && uvw.y >= 0.0f - && uvw.z >= 0.0f - ; -} - -bool overlap(const Triangle& _triangle, const Plane& _plane) -{ - const float dist0 = distance(_plane, _triangle.v0); - const float dist1 = distance(_plane, _triangle.v1); - const float dist2 = distance(_plane, _triangle.v2); - - const float minDist = min(dist0, dist1, dist2); - const float maxDist = max(dist0, dist1, dist2); - - return 0.0f > minDist - && 0.0f < maxDist - ; -} - -inline bool overlap(const Triangle& _triangleA, const Triangle& _triangleB, const Vec3& _axis) -{ - const Interval ia = projectToAxis(_axis, _triangleA); - const Interval ib = projectToAxis(_axis, _triangleB); - return overlap(ia, ib); -} - -bool overlap(const Triangle& _triangleA, const Triangle& _triangleB) -{ - const Vec3 baA = sub(_triangleA.v1, _triangleA.v0); - const Vec3 cbA = sub(_triangleA.v2, _triangleA.v1); - const Vec3 acA = sub(_triangleA.v0, _triangleA.v2); - - const Vec3 baB = sub(_triangleB.v1, _triangleB.v0); - const Vec3 cbB = sub(_triangleB.v2, _triangleB.v1); - const Vec3 acB = sub(_triangleB.v0, _triangleB.v2); - - return overlap(_triangleA, _triangleB, cross(baA, cbA) ) - && overlap(_triangleA, _triangleB, cross(baB, cbB) ) - && overlap(_triangleA, _triangleB, cross(baB, baA) ) - && overlap(_triangleA, _triangleB, cross(baB, cbA) ) - && overlap(_triangleA, _triangleB, cross(baB, acA) ) - && overlap(_triangleA, _triangleB, cross(cbB, baA) ) - && overlap(_triangleA, _triangleB, cross(cbB, cbA) ) - && overlap(_triangleA, _triangleB, cross(cbB, acA) ) - && overlap(_triangleA, _triangleB, cross(acB, baA) ) - && overlap(_triangleA, _triangleB, cross(acB, cbA) ) - && overlap(_triangleA, _triangleB, cross(acB, acA) ) - ; -} - -template -bool overlap(const Triangle& _triangle, const Ty& _ty) -{ - Plane plane(init::None); - calcPlane(plane, _triangle); - - plane.normal = neg(plane.normal); - plane.dist = -plane.dist; - - const LineSegment line = - { - _ty.pos, - _ty.end, - }; - - Hit hit; - if (intersect(line, plane, &hit) ) - { - return true; - } - - const Vec3 pos = closestPoint(plane, hit.pos); - const Vec3 uvw = barycentric(_triangle, pos); - - const float nr = -_ty.radius; - - if (uvw.x >= nr - && uvw.y >= nr - && uvw.z >= nr) - { - return true; - } - - const LineSegment ab = LineSegment{_triangle.v0, _triangle.v1}; - const LineSegment bc = LineSegment{_triangle.v1, _triangle.v2}; - const LineSegment ca = LineSegment{_triangle.v2, _triangle.v0}; - - float ta0 = 0.0f, tb0 = 0.0f; - const bool i0 = intersect(ta0, tb0, ab, line); - - float ta1, tb1; - const bool i1 = intersect(ta1, tb1, bc, line); - - float ta2, tb2; - const bool i2 = intersect(ta2, tb2, ca, line); - - if (!i0 - || !i1 - || !i2) - { - return false; - } - - ta0 = clamp(ta0, 0.0f, 1.0f); - ta1 = clamp(ta1, 0.0f, 1.0f); - ta2 = clamp(ta2, 0.0f, 1.0f); - tb0 = clamp(tb0, 0.0f, 1.0f); - tb1 = clamp(tb1, 0.0f, 1.0f); - tb2 = clamp(tb2, 0.0f, 1.0f); - - const Vec3 pa0 = getPointAt(ab, ta0); - const Vec3 pa1 = getPointAt(bc, ta1); - const Vec3 pa2 = getPointAt(ca, ta2); - - const Vec3 pb0 = getPointAt(line, tb0); - const Vec3 pb1 = getPointAt(line, tb1); - const Vec3 pb2 = getPointAt(line, tb2); - - const float d0 = distanceSq(pa0, pb0); - const float d1 = distanceSq(pa1, pb1); - const float d2 = distanceSq(pa2, pb2); - - if (d0 <= d1 - && d0 <= d2) - { - return overlap(_ty, pa0); - } - else if (d1 <= d2) - { - return overlap(_ty, pa1); - } - - return overlap(_ty, pa2); -} - -bool overlap(const Triangle& _triangle, const Cylinder& _cylinder) -{ - return overlap(_triangle, _cylinder); -} - -bool overlap(const Triangle& _triangle, const Capsule& _capsule) -{ - return overlap(_triangle, _capsule); -} - -bool overlap(const Triangle& _triangle, const Cone& _cone) -{ - const LineSegment ab = LineSegment{_triangle.v0, _triangle.v1}; - const LineSegment bc = LineSegment{_triangle.v1, _triangle.v2}; - const LineSegment ca = LineSegment{_triangle.v2, _triangle.v0}; - - const LineSegment line = - { - _cone.pos, - _cone.end, - }; - - float ta0 = 0.0f, tb0 = 0.0f; - const bool i0 = intersect(ta0, tb0, ab, line); - - float ta1, tb1; - const bool i1 = intersect(ta1, tb1, bc, line); - - float ta2, tb2; - const bool i2 = intersect(ta2, tb2, ca, line); - - if (!i0 - || !i1 - || !i2) - { - return false; - } - - ta0 = clamp(ta0, 0.0f, 1.0f); - ta1 = clamp(ta1, 0.0f, 1.0f); - ta2 = clamp(ta2, 0.0f, 1.0f); - tb0 = clamp(tb0, 0.0f, 1.0f); - tb1 = clamp(tb1, 0.0f, 1.0f); - tb2 = clamp(tb2, 0.0f, 1.0f); - - const Vec3 pa0 = getPointAt(ab, ta0); - const Vec3 pa1 = getPointAt(bc, ta1); - const Vec3 pa2 = getPointAt(ca, ta2); - - const Vec3 pb0 = getPointAt(line, tb0); - const Vec3 pb1 = getPointAt(line, tb1); - const Vec3 pb2 = getPointAt(line, tb2); - - const float d0 = distanceSq(pa0, pb0); - const float d1 = distanceSq(pa1, pb1); - const float d2 = distanceSq(pa2, pb2); - - if (d0 <= d1 - && d0 <= d2) - { - return overlap(_cone, pa0); - } - else if (d1 <= d2) - { - return overlap(_cone, pa1); - } - - return overlap(_cone, pa2); -} - -bool overlap(const Triangle& _triangle, const Disk& _disk) -{ - if (!overlap(_triangle, Sphere{_disk.center, _disk.radius}) ) - { - return false; - } - - Plane plane(init::None); - calcPlane(plane, _disk.normal, _disk.center); - - return overlap(_triangle, plane); -} - -bool overlap(const Triangle& _triangle, const Obb& _obb) -{ - const Srt srt = toSrt(_obb.mtx); - - Aabb aabb; - toAabb(aabb, srt.scale); - - const Quaternion invRotation = invert(srt.rotation); - - const Triangle triangle = - { - mul(sub(_triangle.v0, srt.translation), invRotation), - mul(sub(_triangle.v1, srt.translation), invRotation), - mul(sub(_triangle.v2, srt.translation), invRotation), - }; - - return overlap(triangle, aabb); -} diff --git a/examples/common/bounds.h b/examples/common/bounds.h deleted file mode 100644 index bf810adbd..000000000 --- a/examples/common/bounds.h +++ /dev/null @@ -1,456 +0,0 @@ -/* - * Copyright 2011-2021 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause - */ - -#ifndef BOUNDS_H_HEADER_GUARD -#define BOUNDS_H_HEADER_GUARD - -#include - -/// -struct Aabb -{ - bx::Vec3 min = bx::init::None; - bx::Vec3 max = bx::init::None; -}; - -/// -struct Capsule -{ - bx::Vec3 pos = bx::init::None; - bx::Vec3 end = bx::init::None; - float radius; -}; - -/// -struct Cone -{ - bx::Vec3 pos = bx::init::None; - bx::Vec3 end = bx::init::None; - float radius; -}; - -/// -struct Cylinder -{ - bx::Vec3 pos = bx::init::None; - bx::Vec3 end = bx::init::None; - float radius; -}; - -/// -struct Disk -{ - bx::Vec3 center = bx::init::None; - bx::Vec3 normal = bx::init::None; - float radius; -}; - -/// -struct Obb -{ - float mtx[16]; -}; - -/// -struct Sphere -{ - bx::Vec3 center = bx::init::None; - float radius; -}; - -/// -struct Triangle -{ - bx::Vec3 v0 = bx::init::None; - bx::Vec3 v1 = bx::init::None; - bx::Vec3 v2 = bx::init::None; -}; - -/// -struct Ray -{ - bx::Vec3 pos = bx::init::None; - bx::Vec3 dir = bx::init::None; -}; - -/// -struct Hit -{ - bx::Vec3 pos = bx::init::None; - bx::Plane plane = bx::init::None; -}; - -/// -bx::Vec3 getPointAt(const Ray& _ray, float _t); - -/// -bx::Vec3 getCenter(const Aabb& _aabb); - -/// -bx::Vec3 getExtents(const Aabb& _aabb); - -/// -bx::Vec3 getCenter(const Triangle& _triangle); - -/// -void toAabb(Aabb& _outAabb, const bx::Vec3& _extents); - -/// -void toAabb(Aabb& _outAabb, const bx::Vec3& _center, const bx::Vec3& _extents); - -/// Convert cylinder to axis aligned bounding box. -void toAabb(Aabb& _outAabb, const Cylinder& _cylinder); - -/// Convert disk to axis aligned bounding box. -void toAabb(Aabb& _outAabb, const Disk& _disk); - -/// Convert oriented bounding box to axis aligned bounding box. -void toAabb(Aabb& _outAabb, const Obb& _obb); - -/// Convert sphere to axis aligned bounding box. -void toAabb(Aabb& _outAabb, const Sphere& _sphere); - -/// Convert triangle to axis aligned bounding box. -void toAabb(Aabb& _outAabb, const Triangle& _triangle); - -/// Calculate axis aligned bounding box. -void toAabb(Aabb& _outAabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride); - -/// Transform vertices and calculate axis aligned bounding box. -void toAabb(Aabb& _outAabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride); - -/// Expand AABB. -void aabbExpand(Aabb& _outAabb, float _factor); - -/// Expand AABB with xyz. -void aabbExpand(Aabb& _outAabb, const bx::Vec3& _pos); - -/// Calculate surface area of axis aligned bounding box. -float calcAreaAabb(const Aabb& _aabb); - -/// Convert axis aligned bounding box to oriented bounding box. -void toObb(Obb& _outObb, const Aabb& _aabb); - -/// Calculate oriented bounding box. -void calcObb(Obb& _outObb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps = 17); - -/// Calculate maximum bounding sphere. -void calcMaxBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride); - -/// Calculate minimum bounding sphere. -void calcMinBoundingSphere(Sphere& _outSphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step = 0.01f); - -/// Returns 6 (near, far, left, right, top, bottom) planes representing frustum planes. -void buildFrustumPlanes(bx::Plane* _outPlanes, const float* _viewProj); - -/// Returns point from 3 intersecting planes. -bx::Vec3 intersectPlanes(const bx::Plane& _pa, const bx::Plane& _pb, const bx::Plane& _pc); - -/// Make screen space ray from x, y coordinate and inverse view-projection matrix. -Ray makeRay(float _x, float _y, const float* _invVp); - -/// Intersect ray / AABB. -bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit = NULL); - -/// Intersect ray / OBB. -bool intersect(const Ray& _ray, const Obb& _obb, Hit* _hit = NULL); - -/// Intersect ray / cylinder. -bool intersect(const Ray& _ray, const Cylinder& _cylinder, Hit* _hit = NULL); - -/// Intersect ray / capsule. -bool intersect(const Ray& _ray, const Capsule& _capsule, Hit* _hit = NULL); - -/// Intersect ray / cone. -bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit = NULL); - -/// Intersect ray / disk. -bool intersect(const Ray& _ray, const Disk& _disk, Hit* _hit = NULL); - -/// Intersect ray / plane. -bool intersect(const Ray& _ray, const bx::Plane& _plane, Hit* _hit = NULL); - -/// Intersect ray / plane. -bool intersect(const Ray& _ray, const bx::Plane& _plane, bool _doublesided, Hit* _hit = NULL); - -/// Intersect ray / sphere. -bool intersect(const Ray& _ray, const Sphere& _sphere, Hit* _hit = NULL); - -/// Intersect ray / triangle. -bool intersect(const Ray& _ray, const Triangle& _triangle, Hit* _hit = NULL); - -/// -bool overlap(const Aabb& _aabb, const bx::Vec3& _pos); - -/// -bool overlap(const Aabb& _aabb, const Sphere& _sphere); - -/// -bool overlap(const Aabb& _aabbA, const Aabb& _aabbB); - -/// -bool overlap(const Aabb& _aabb, const bx::Plane& _plane); - -/// -bool overlap(const Aabb& _aabb, const Triangle& _triangle); - -/// -bool overlap(const Aabb& _aabb, const Cylinder& _cylinder); - -/// -bool overlap(const Aabb& _aabb, const Capsule& _capsule); - -/// -bool overlap(const Aabb& _aabb, const Cone& _cone); - -/// -bool overlap(const Aabb& _aabb, const Disk& _disk); - -/// -bool overlap(const Aabb& _aabb, const Obb& _obb); - -/// -bool overlap(const Capsule& _capsule, const bx::Vec3& _pos); - -/// -bool overlap(const Capsule& _capsule, const Sphere& _sphere); - -/// -bool overlap(const Capsule& _capsule, const Aabb& _aabb); - -/// -bool overlap(const Capsule& _capsule, const bx::Plane& _plane); - -/// -bool overlap(const Capsule& _capsule, const Triangle& _triangle); - -/// -bool overlap(const Capsule& _capsule, const Cylinder& _cylinder); - -/// -bool overlap(const Capsule& _capsuleA, const Capsule& _capsuleB); - -/// -bool overlap(const Capsule& _capsule, const Cone& _cone); - -/// -bool overlap(const Capsule& _capsule, const Disk& _disk); - -/// -bool overlap(const Capsule& _capsule, const Obb& _obb); - -/// -bool overlap(const Cone& _cone, const bx::Vec3& _pos); - -/// -bool overlap(const Cone& _cone, const Sphere& _sphere); - -/// -bool overlap(const Cone& _cone, const Aabb& _aabb); - -/// -bool overlap(const Cone& _cone, const bx::Plane& _plane); - -/// -bool overlap(const Cone& _cone, const Triangle& _triangle); - -/// -bool overlap(const Cone& _cone, const Cylinder& _cylinder); - -/// -bool overlap(const Cone& _cone, const Capsule& _capsule); - -/// -bool overlap(const Cone& _coneA, const Cone& _coneB); - -/// -bool overlap(const Cone& _cone, const Disk& _disk); - -/// -bool overlap(const Cone& _cone, const Obb& _obb); - -/// -bool overlap(const Cylinder& _cylinder, const bx::Vec3& _pos); - -/// -bool overlap(const Cylinder& _cylinder, const Sphere& _sphere); - -/// -bool overlap(const Cylinder& _cylinder, const Aabb& _aabb); - -/// -bool overlap(const Cylinder& _cylinder, const bx::Plane& _plane); - -/// -bool overlap(const Cylinder& _cylinder, const Triangle& _triangle); - -/// -bool overlap(const Cylinder& _cylinderA, const Cylinder& _cylinderB); - -/// -bool overlap(const Cylinder& _cylinder, const Capsule& _capsule); - -/// -bool overlap(const Cylinder& _cylinder, const Cone& _cone); - -/// -bool overlap(const Cylinder& _cylinder, const Disk& _disk); - -/// -bool overlap(const Cylinder& _cylinder, const Obb& _obb); - -/// -bool overlap(const Disk& _disk, const bx::Vec3& _pos); - -/// -bool overlap(const Disk& _disk, const Sphere& _sphere); - -/// -bool overlap(const Disk& _disk, const Aabb& _aabb); - -/// -bool overlap(const Disk& _disk, const bx::Plane& _plane); - -/// -bool overlap(const Disk& _disk, const Triangle& _triangle); - -/// -bool overlap(const Disk& _disk, const Cylinder& _cylinder); - -/// -bool overlap(const Disk& _disk, const Capsule& _capsule); - -/// -bool overlap(const Disk& _disk, const Cone& _cone); - -/// -bool overlap(const Disk& _diskA, const Disk& _diskB); - -/// -bool overlap(const Disk& _disk, const Obb& _obb); - -/// -bool overlap(const Obb& _obb, const bx::Vec3& _pos); - -/// -bool overlap(const Obb& _obb, const Sphere& _sphere); - -/// -bool overlap(const Obb& _obb, const Aabb& _aabb); - -/// -bool overlap(const Obb& _obb, const bx::Plane& _plane); - -/// -bool overlap(const Obb& _obb, const Triangle& _triangle); - -/// -bool overlap(const Obb& _obb, const Cylinder& _cylinder); - -/// -bool overlap(const Obb& _obb, const Capsule& _capsule); - -/// -bool overlap(const Obb& _obb, const Cone& _cone); - -/// -bool overlap(const Obb& _obb, const Disk& _disk); - -/// -bool overlap(const Obb& _obbA, const Obb& _obbB); - -/// -bool overlap(const bx::Plane& _plane, const bx::Vec3& _pos); - -/// -bool overlap(const bx::Plane& _plane, const Sphere& _sphere); - -/// -bool overlap(const bx::Plane& _plane, const Aabb& _aabb); - -/// -bool overlap(const bx::Plane& _planeA, const bx::Plane& _planeB); - -/// -bool overlap(const bx::Plane& _plane, const Triangle& _triangle); - -/// -bool overlap(const bx::Plane& _plane, const Cylinder& _cylinder); - -/// -bool overlap(const bx::Plane& _plane, const Capsule& _capsule); - -/// -bool overlap(const bx::Plane& _plane, const Cone& _cone); - -/// -bool overlap(const bx::Plane& _plane, const Disk& _disk); - -/// -bool overlap(const bx::Plane& _plane, const Obb& _obb); - -/// -bool overlap(const Sphere& _sphere, const bx::Vec3& _pos); - -/// -bool overlap(const Sphere& _sphereA, const Sphere& _sphereB); - -/// -bool overlap(const Sphere& _sphere, const Aabb& _aabb); - -/// -bool overlap(const Sphere& _sphere, const bx::Plane& _plane); - -/// -bool overlap(const Sphere& _sphere, const Triangle& _triangle); - -/// -bool overlap(const Sphere& _sphere, const Cylinder& _cylinder); - -/// -bool overlap(const Sphere& _sphere, const Capsule& _capsule); - -/// -bool overlap(const Sphere& _sphere, const Cone& _cone); - -/// -bool overlap(const Sphere& _sphere, const Disk& _disk); - -/// -bool overlap(const Sphere& _sphere, const Obb& _obb); - -/// -bool overlap(const Triangle& _triangle, const bx::Vec3& _pos); - -/// -bool overlap(const Triangle& _triangle, const Sphere& _sphere); - -/// -bool overlap(const Triangle& _triangle, const Aabb& _aabb); - -/// -bool overlap(const Triangle& _triangle, const bx::Plane& _plane); - -/// -bool overlap(const Triangle& _triangleA, const Triangle& _triangleB); - -/// -bool overlap(const Triangle& _triangle, const Cylinder& _cylinder); - -/// -bool overlap(const Triangle& _triangle, const Capsule& _capsule); - -/// -bool overlap(const Triangle& _triangle, const Cone& _cone); - -/// -bool overlap(const Triangle& _triangle, const Disk& _disk); - -/// -bool overlap(const Triangle& _triangle, const Obb& _obb); - -#include "bounds.inl" - -#endif // BOUNDS_H_HEADER_GUARD diff --git a/examples/common/bounds.inl b/examples/common/bounds.inl deleted file mode 100644 index 00753f537..000000000 --- a/examples/common/bounds.inl +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright 2011-2021 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause - */ - -#ifndef BOUNDS_H_HEADER_GUARD -# error "Must be included from bounds.h!" -#endif // BOUNDS_H_HEADER_GUARD - -inline bx::Vec3 getPointAt(const Ray& _ray, float _t) -{ - return bx::mad(_ray.dir, _t, _ray.pos); -} - -inline bool intersect(const Ray& _ray, const bx::Plane& _plane, Hit* _hit) -{ - return intersect(_ray, _plane, false, _hit); -} - -inline bool overlap(const Aabb& _aabb, const Sphere& _sphere) -{ - return overlap(_sphere, _aabb); -} - -inline bool overlap(const Aabb& _aabb, const Cylinder& _cylinder) -{ - return overlap(_cylinder, _aabb); -} - -inline bool overlap(const Capsule& _capsule, const Sphere& _sphere) -{ - return overlap(_sphere, _capsule); -} - -inline bool overlap(const Capsule& _capsule, const Aabb& _aabb) -{ - return overlap(_aabb, _capsule); -} - -inline bool overlap(const Capsule& _capsule, const Triangle& _triangle) -{ - return overlap(_triangle, _capsule); -} - -inline bool overlap(const Capsule& _capsule, const Cone& _cone) -{ - return overlap(_cone, _capsule); -} - -inline bool overlap(const Capsule& _capsule, const Cylinder& _cylinder) -{ - return overlap(_cylinder, _capsule); -} - -inline bool overlap(const Capsule& _capsule, const Disk& _disk) -{ - return overlap(_disk, _capsule); -} - -inline bool overlap(const Capsule& _capsule, const Obb& _obb) -{ - return overlap(_obb, _capsule); -} - -inline bool overlap(const Cone& _cone, const Sphere& _sphere) -{ - return overlap(_sphere, _cone); -} - -inline bool overlap(const Cone& _cone, const Aabb& _aabb) -{ - return overlap(_aabb, _cone); -} - -inline bool overlap(const Cone& _cone, const bx::Plane& _plane) -{ - return overlap(_plane, _cone); -} - -inline bool overlap(const Cone& _cone, const Triangle& _triangle) -{ - return overlap(_triangle, _cone); -} - -inline bool overlap(const Cylinder& _cylinder, const Triangle& _triangle) -{ - return overlap(_triangle, _cylinder); -} - -inline bool overlap(const Cylinder& _cylinder, const Cone& _cone) -{ - return overlap(_cone, _cylinder); -} - -inline bool overlap(const Disk& _disk, const Sphere& _sphere) -{ - return overlap(_sphere, _disk); -} - -inline bool overlap(const Disk& _disk, const Aabb& _aabb) -{ - return overlap(_aabb, _disk); -} - -inline bool overlap(const Disk& _disk, const Triangle& _triangle) -{ - return overlap(_triangle, _disk); -} - -inline bool overlap(const Disk& _disk, const Cylinder& _cylinder) -{ - return overlap(_cylinder, _disk); -} - -inline bool overlap(const Disk& _disk, const Cone& _cone) -{ - return overlap(_cone, _disk); -} - -inline bool overlap(const Obb& _obb, const Sphere& _sphere) -{ - return overlap(_sphere, _obb); -} - -inline bool overlap(const Obb& _obb, const Aabb& _aabb) -{ - return overlap(_aabb, _obb); -} - -inline bool overlap(const Obb& _obb, const Triangle& _triangle) -{ - return overlap(_triangle, _obb); -} - -inline bool overlap(const Obb& _obb, const Cylinder& _cylinder) -{ - return overlap(_cylinder, _obb); -} - -inline bool overlap(const Obb& _obb, const Cone& _cone) -{ - return overlap(_cone, _obb); -} - -inline bool overlap(const Obb& _obb, const Disk& _disk) -{ - return overlap(_disk, _obb); -} - -inline bool overlap(const bx::Plane& _plane, const Sphere& _sphere) -{ - return overlap(_sphere, _plane); -} - -inline bool overlap(const bx::Plane& _plane, const Aabb& _aabb) -{ - return overlap(_aabb, _plane); -} - -inline bool overlap(const bx::Plane& _plane, const Triangle& _triangle) -{ - return overlap(_triangle, _plane); -} - -inline bool overlap(const bx::Plane& _plane, const Cylinder& _cylinder) -{ - return overlap(_cylinder, _plane); -} - -inline bool overlap(const bx::Plane& _plane, const Capsule& _capsule) -{ - return overlap(_capsule, _plane); -} - -inline bool overlap(const bx::Plane& _plane, const Disk& _disk) -{ - return overlap(_disk, _plane); -} - -inline bool overlap(const bx::Plane& _plane, const Obb& _obb) -{ - return overlap(_obb, _plane); -} - -inline bool overlap(const Sphere& _sphere, const Cylinder& _cylinder) -{ - return overlap(_cylinder, _sphere); -} - -inline bool overlap(const Triangle& _triangle, const Sphere& _sphere) -{ - return overlap(_sphere, _triangle); -} - -inline bool overlap(const Triangle& _triangle, const Aabb& _aabb) -{ - return overlap(_aabb, _triangle); -} diff --git a/examples/common/debugdraw/debugdraw.cpp b/examples/common/debugdraw/debugdraw.cpp index 782e3ddeb..d2c806712 100644 --- a/examples/common/debugdraw/debugdraw.cpp +++ b/examples/common/debugdraw/debugdraw.cpp @@ -1383,7 +1383,7 @@ struct DebugDrawEncoderImpl m_state = State::None; } - void draw(const Aabb& _aabb) + void draw(const bx::Aabb& _aabb) { const Attrib& attrib = m_attrib[m_stack]; if (attrib.m_wireframe) @@ -1414,23 +1414,23 @@ struct DebugDrawEncoderImpl } else { - Obb obb; + bx::Obb obb; toObb(obb, _aabb); draw(DebugMesh::Cube, obb.mtx, 1, false); } } - void draw(const Cylinder& _cylinder, bool _capsule) + void draw(const bx::Cylinder& _cylinder, bool _capsule) { drawCylinder(_cylinder.pos, _cylinder.end, _cylinder.radius, _capsule); } - void draw(const Disk& _disk) + void draw(const bx::Disk& _disk) { drawCircle(_disk.normal, _disk.center, _disk.radius, 0.0f); } - void draw(const Obb& _obb) + void draw(const bx::Obb& _obb) { const Attrib& attrib = m_attrib[m_stack]; if (attrib.m_wireframe) @@ -1469,7 +1469,7 @@ struct DebugDrawEncoderImpl } } - void draw(const Sphere& _sphere) + void draw(const bx::Sphere& _sphere) { const Attrib& attrib = m_attrib[m_stack]; float mtx[16]; @@ -1491,7 +1491,7 @@ struct DebugDrawEncoderImpl draw(DebugMesh::Enum(DebugMesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe); } - void draw(const Triangle& _triangle) + void draw(const bx::Triangle& _triangle) { Attrib& attrib = m_attrib[m_stack]; if (attrib.m_wireframe) @@ -1955,7 +1955,7 @@ struct DebugDrawEncoderImpl ; draw(DebugMesh::Enum(DebugMesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe); - Sphere sphere; + bx::Sphere sphere; sphere.center = _from; sphere.radius = _radius; draw(sphere); @@ -2425,42 +2425,42 @@ void DebugDrawEncoder::close() DEBUG_DRAW_ENCODER(close() ); } -void DebugDrawEncoder::draw(const Aabb& _aabb) +void DebugDrawEncoder::draw(const bx::Aabb& _aabb) { DEBUG_DRAW_ENCODER(draw(_aabb) ); } -void DebugDrawEncoder::draw(const Cylinder& _cylinder) +void DebugDrawEncoder::draw(const bx::Cylinder& _cylinder) { DEBUG_DRAW_ENCODER(draw(_cylinder, false) ); } -void DebugDrawEncoder::draw(const Capsule& _capsule) +void DebugDrawEncoder::draw(const bx::Capsule& _capsule) { - DEBUG_DRAW_ENCODER(draw(*( (const Cylinder*)&_capsule), true) ); + DEBUG_DRAW_ENCODER(draw(*( (const bx::Cylinder*)&_capsule), true) ); } -void DebugDrawEncoder::draw(const Disk& _disk) +void DebugDrawEncoder::draw(const bx::Disk& _disk) { DEBUG_DRAW_ENCODER(draw(_disk) ); } -void DebugDrawEncoder::draw(const Obb& _obb) +void DebugDrawEncoder::draw(const bx::Obb& _obb) { DEBUG_DRAW_ENCODER(draw(_obb) ); } -void DebugDrawEncoder::draw(const Sphere& _sphere) +void DebugDrawEncoder::draw(const bx::Sphere& _sphere) { DEBUG_DRAW_ENCODER(draw(_sphere) ); } -void DebugDrawEncoder::draw(const Triangle& _triangle) +void DebugDrawEncoder::draw(const bx::Triangle& _triangle) { DEBUG_DRAW_ENCODER(draw(_triangle) ); } -void DebugDrawEncoder::draw(const Cone& _cone) +void DebugDrawEncoder::draw(const bx::Cone& _cone) { DEBUG_DRAW_ENCODER(drawCone(_cone.pos, _cone.end, _cone.radius) ); } diff --git a/examples/common/debugdraw/debugdraw.h b/examples/common/debugdraw/debugdraw.h index d6af87080..238ca7032 100644 --- a/examples/common/debugdraw/debugdraw.h +++ b/examples/common/debugdraw/debugdraw.h @@ -7,8 +7,8 @@ #define DEBUGDRAW_H_HEADER_GUARD #include +#include #include -#include "../bounds.h" struct Axis { @@ -121,28 +121,28 @@ struct DebugDrawEncoder void close(); /// - void draw(const Aabb& _aabb); + void draw(const bx::Aabb& _aabb); /// - void draw(const Cylinder& _cylinder); + void draw(const bx::Cylinder& _cylinder); /// - void draw(const Capsule& _capsule); + void draw(const bx::Capsule& _capsule); /// - void draw(const Disk& _disk); + void draw(const bx::Disk& _disk); /// - void draw(const Obb& _obb); + void draw(const bx::Obb& _obb); /// - void draw(const Sphere& _sphere); + void draw(const bx::Sphere& _sphere); /// - void draw(const Triangle& _triangle); + void draw(const bx::Triangle& _triangle); /// - void draw(const Cone& _cone); + void draw(const bx::Cone& _cone); /// void draw(GeometryHandle _handle); diff --git a/examples/common/ps/particle_system.cpp b/examples/common/ps/particle_system.cpp index cbcf2b874..53146597a 100644 --- a/examples/common/ps/particle_system.cpp +++ b/examples/common/ps/particle_system.cpp @@ -190,7 +190,7 @@ namespace ps m_dt = 0.0f; m_uniforms.reset(); m_num = 0; - bx::memSet(&m_aabb, 0, sizeof(Aabb) ); + bx::memSet(&m_aabb, 0, sizeof(bx::Aabb) ); m_rng.reset(); } @@ -329,7 +329,7 @@ namespace ps bx::EaseFn easeBlend = bx::getEaseFunc(m_uniforms.m_easeBlend); bx::EaseFn easeScale = bx::getEaseFunc(m_uniforms.m_easeScale); - Aabb aabb = + bx::Aabb aabb = { { bx::kInfinity, bx::kInfinity, bx::kInfinity }, { -bx::kInfinity, -bx::kInfinity, -bx::kInfinity }, @@ -425,7 +425,7 @@ namespace ps bx::RngMwc m_rng; EmitterUniforms m_uniforms; - Aabb m_aabb; + bx::Aabb m_aabb; Particle* m_particles; uint32_t m_num; @@ -643,7 +643,7 @@ namespace ps } } - void getAabb(EmitterHandle _handle, Aabb& _outAabb) + void getAabb(EmitterHandle _handle, bx::Aabb& _outAabb) { BX_ASSERT(m_emitterAlloc.isValid(_handle.idx) , "getAabb handle %d is not valid." @@ -730,7 +730,7 @@ void psUpdateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms) s_ctx.updateEmitter(_handle, _uniforms); } -void psGetAabb(EmitterHandle _handle, Aabb& _outAabb) +void psGetAabb(EmitterHandle _handle, bx::Aabb& _outAabb) { s_ctx.getAabb(_handle, _outAabb); } diff --git a/examples/common/ps/particle_system.h b/examples/common/ps/particle_system.h index 1dc76cd59..11fa4c876 100644 --- a/examples/common/ps/particle_system.h +++ b/examples/common/ps/particle_system.h @@ -7,11 +7,10 @@ #define PARTICLE_SYSTEM_H_HEADER_GUARD #include +#include #include #include -#include "../bounds.h" - struct EmitterHandle { uint16_t idx; }; struct EmitterSpriteHandle { uint16_t idx; }; @@ -92,7 +91,7 @@ EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum void psUpdateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms = NULL); /// -void psGetAabb(EmitterHandle _handle, Aabb& _outAabb); +void psGetAabb(EmitterHandle _handle, bx::Aabb& _outAabb); /// void psDestroyEmitter(EmitterHandle _handle); diff --git a/tools/geometryc/geometryc.cpp b/tools/geometryc/geometryc.cpp index d604fd382..75e653139 100644 --- a/tools/geometryc/geometryc.cpp +++ b/tools/geometryc/geometryc.cpp @@ -307,11 +307,11 @@ void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexLayout _la void write(bx::WriterI* _writer, const void* _vertices, uint32_t _numVertices, uint32_t _stride) { - Sphere maxSphere; - calcMaxBoundingSphere(maxSphere, _vertices, _numVertices, _stride); + bx::Sphere maxSphere; + bx::calcMaxBoundingSphere(maxSphere, _vertices, _numVertices, _stride); - Sphere minSphere; - calcMinBoundingSphere(minSphere, _vertices, _numVertices, _stride); + bx::Sphere minSphere; + bx::calcMinBoundingSphere(minSphere, _vertices, _numVertices, _stride); if (minSphere.radius > maxSphere.radius) { @@ -322,12 +322,12 @@ void write(bx::WriterI* _writer, const void* _vertices, uint32_t _numVertices, u bx::write(_writer, minSphere); } - Aabb aabb; - toAabb(aabb, _vertices, _numVertices, _stride); + bx::Aabb aabb; + bx::toAabb(aabb, _vertices, _numVertices, _stride); bx::write(_writer, aabb); - Obb obb; - calcObb(obb, _vertices, _numVertices, _stride, s_obbSteps); + bx::Obb obb; + bx::calcObb(obb, _vertices, _numVertices, _stride, s_obbSteps); bx::write(_writer, obb); } diff --git a/tools/geometryv/geometryv.cpp b/tools/geometryv/geometryv.cpp index adf15b2f3..25d044244 100644 --- a/tools/geometryv/geometryv.cpp +++ b/tools/geometryv/geometryv.cpp @@ -1002,20 +1002,40 @@ int _main_(int _argc, char** _argv) ImGui::Text("Name: %s %s", view.m_fileList[view.m_fileIndex].c_str(), layout); ImGui::Indent(); - for (GroupArray::const_iterator itGroup = mesh->m_groups.begin(), itGroupEnd = mesh->m_groups.end(); itGroup != itGroupEnd; ++itGroup) + for (GroupArray::const_iterator itGroup = mesh->m_groups.begin(), itGroupEnd = mesh->m_groups.end() + ; itGroup != itGroupEnd + ; ++itGroup + ) { - ImGui::Text("Group v %d i %d c %.2f %.2f %.2f r %.2f", itGroup->m_numVertices, itGroup->m_numIndices, - itGroup->m_sphere.center.x, itGroup->m_sphere.center.y, itGroup->m_sphere.center.z, - itGroup->m_sphere.radius); + ImGui::Text("Group v %d i %d c %.2f %.2f %.2f r %.2f" + , itGroup->m_numVertices + , itGroup->m_numIndices + , itGroup->m_sphere.center.x + , itGroup->m_sphere.center.y + , itGroup->m_sphere.center.z + , itGroup->m_sphere.radius + ); + ImGui::Indent(); - for (PrimitiveArray::const_iterator itPrim = itGroup->m_prims.begin(), itPrimEnd = itGroup->m_prims.end(); itPrim != itPrimEnd; ++itPrim) + + for (PrimitiveArray::const_iterator itPrim = itGroup->m_prims.begin(), itPrimEnd = itGroup->m_prims.end() + ; itPrim != itPrimEnd + ; ++itPrim + ) { - ImGui::Text("Primitive v %d i %d c %.2f %.2f %.2f r %.2f", itPrim->m_numVertices, itPrim->m_numIndices, - itPrim->m_sphere.center.x, itPrim->m_sphere.center.y, itPrim->m_sphere.center.z, - itPrim->m_sphere.radius); + ImGui::Text("Primitive v %d i %d c %.2f %.2f %.2f r %.2f" + , itPrim->m_numVertices + , itPrim->m_numIndices + , itPrim->m_sphere.center.x + , itPrim->m_sphere.center.y + , itPrim->m_sphere.center.z + , itPrim->m_sphere.radius + ); } + ImGui::Unindent(); } + ImGui::Unindent(); ImGui::Separator(); @@ -1189,7 +1209,7 @@ int _main_(int _argc, char** _argv) uint32_t numPrimitives = 0; uint32_t numVertices = 0; uint32_t numIndices = 0; - Aabb boundingBox = {}; + bx::Aabb boundingBox = {}; for (GroupArray::const_iterator it = mesh->m_groups.begin(), itEnd = mesh->m_groups.end(); it != itEnd; ++it) { @@ -1218,8 +1238,8 @@ int _main_(int _argc, char** _argv) , numIndices ); - view.m_meshCenter = getCenter(boundingBox); - view.m_meshRadius = bx::length(getExtents(boundingBox)); + view.m_meshCenter = bx::getCenter(boundingBox); + view.m_meshRadius = bx::length(bx::getExtents(boundingBox) ); view.m_camera.init( view.m_meshCenter, view.m_meshRadius * 2.0f, 0.01f, view.m_meshRadius * 10.0f); }