From af99999f0a428ca8f3f144e662c1b23fd03b9ceb Mon Sep 17 00:00:00 2001 From: Pham Hong Duc Date: Sat, 9 Oct 2021 23:10:20 +0700 Subject: [PATCH] #80 Update bbox for collision node --- .../Source/Collision/CBBCollisionManager.cpp | 3 ++- .../Source/Collision/CBBoxPatchBuilder.cpp | 16 ++++++++++++++++ .../Source/Collision/CCollisionNode.cpp | 15 ++++++++++++++- .../Collision/Source/Collision/CCollisionNode.h | 7 +++---- .../Source/Collision/COctreeBuilder.cpp | 5 ++--- .../Collision/Source/Collision/COctreeBuilder.h | 1 - .../Collision/Source/Collision/COctreeNode.h | 2 +- .../Collision/Source/Collision/CTriangleBB.cpp | 9 +++++++-- .../Collision/Source/Collision/CTriangleBB.h | 5 ++--- .../Source/Collision/CTriangleSelector.cpp | 15 +++++++++++++++ .../Source/Collision/CTriangleSelector.h | 3 +++ 11 files changed, 65 insertions(+), 16 deletions(-) diff --git a/Projects/Skylicht/Collision/Source/Collision/CBBCollisionManager.cpp b/Projects/Skylicht/Collision/Source/Collision/CBBCollisionManager.cpp index e64a02adf..795886d16 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CBBCollisionManager.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CBBCollisionManager.cpp @@ -41,9 +41,10 @@ namespace Skylicht CCollisionNode* CBBCollisionManager::addBBCollision(CGameObject* object, const core::aabbox3df& bbox) { CEntity* entity = object->getEntity(); - CCollisionNode* node = new CCollisionNode(object, entity, new CTriangleBB(entity, bbox)); + CCollisionNode* node = new CCollisionNode(object, entity, new CTriangleBB(entity, bbox)); m_nodes.push_back(node); + build(); return node; diff --git a/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp b/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp index 93a45bbaf..32510a97a 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp @@ -44,6 +44,22 @@ namespace Skylicht void CBBoxPatchBuilder::build() { + const u32 start = os::Timer::getRealTime(); + u32 numPoly = 0; + core::aabbox3df size; + + // step 1: caculator the box size + for (u32 i = 0, n = m_nodes.size(); i < n; i++) + { + if (i == 0) + size = m_nodes[i]->getTransformBBox(); + else + size.addInternalBox(m_nodes[i]->getTransformBBox()); + } + + c8 tmp[256]; + sprintf(tmp, "Needed %ums to CBBoxPatchBuilder::build", os::Timer::getRealTime() - start); + os::Printer::log(tmp, ELL_INFORMATION); } } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp index 8754a0368..da11c041d 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp @@ -46,11 +46,24 @@ namespace Skylicht if (Selector != NULL && Entity != NULL) { CWorldTransformData* world = Entity->getData(); - // CCollisionData* data = Entity->getData(); Triangles.set_used(Selector->getTriangleCount()); Selector->getTriangles(Triangles.pointer(), &world->World); } } + + core::aabbox3df CCollisionNode::getTransformBBox() + { + core::aabbox3df ret; + + if (Selector != NULL && Entity != NULL) + { + CWorldTransformData* world = Entity->getData(); + ret = Selector->getBBox(); + world->World.transformBoxEx(ret); + } + + return ret; + } } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h index 9aea3e995..6cd94f490 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h +++ b/Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h @@ -39,18 +39,17 @@ namespace Skylicht CTriangleSelector* Selector; - //! Transform BBox - core::aabbox3df BBox; - //! Triangles that calculated World Transform core::array Triangles; public: - CCollisionNode(CGameObject *gameObject, CEntity *entity, CTriangleSelector *selector); + CCollisionNode(CGameObject* gameObject, CEntity* entity, CTriangleSelector* selector); virtual ~CCollisionNode(); void updateTransform(); + + core::aabbox3df getTransformBBox(); }; } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.cpp b/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.cpp index 5c27d4add..095c361d5 100644 --- a/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.cpp @@ -29,8 +29,7 @@ namespace Skylicht { COctreeBuilder::COctreeBuilder() : m_root(NULL), - m_minimalPolysPerNode(128), - m_nodeCount(0) + m_minimalPolysPerNode(128) { } @@ -102,7 +101,7 @@ namespace Skylicht constructOctree(m_root); c8 tmp[256]; - sprintf(tmp, "Needed %ums to COctreeBuilder::build (%d nodes, %u polys)", os::Timer::getRealTime() - start, m_nodeCount, numPoly); + sprintf(tmp, "Needed %ums to COctreeBuilder::build (%u polys)", os::Timer::getRealTime() - start, numPoly); os::Printer::log(tmp, ELL_INFORMATION); } diff --git a/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.h b/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.h index 7b81c7403..b0432174e 100644 --- a/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.h +++ b/Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.h @@ -34,7 +34,6 @@ namespace Skylicht COctreeNode* m_root; u32 m_minimalPolysPerNode; - u32 m_nodeCount; public: COctreeBuilder(); diff --git a/Projects/Skylicht/Collision/Source/Collision/COctreeNode.h b/Projects/Skylicht/Collision/Source/Collision/COctreeNode.h index 9cce47476..215811642 100644 --- a/Projects/Skylicht/Collision/Source/Collision/COctreeNode.h +++ b/Projects/Skylicht/Collision/Source/Collision/COctreeNode.h @@ -50,4 +50,4 @@ namespace Skylicht virtual ~COctreeNode(); }; -} +} \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.cpp b/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.cpp index 0443f7de3..0c98e49c2 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.cpp @@ -28,10 +28,10 @@ This file is part of the "Skylicht Engine". namespace Skylicht { CTriangleBB::CTriangleBB(CEntity* entity, const core::aabbox3df& bbox) : - CTriangleSelector(entity), - m_bbox(bbox) + CTriangleSelector(entity) { m_triangles.set_used(12); + m_bbox = bbox; } CTriangleBB::~CTriangleBB() @@ -64,4 +64,9 @@ namespace Skylicht CTriangleSelector::getTriangles(triangles, transform); } + + const core::aabbox3df& CTriangleBB::getBBox() + { + return m_bbox; + } } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.h b/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.h index c0c94004f..2686e7fae 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.h +++ b/Projects/Skylicht/Collision/Source/Collision/CTriangleBB.h @@ -30,14 +30,13 @@ namespace Skylicht { class CTriangleBB : public CTriangleSelector { - protected: - core::aabbox3df m_bbox; - public: CTriangleBB(CEntity* entity, const core::aabbox3df& bbox); virtual ~CTriangleBB(); virtual void getTriangles(core::triangle3df* triangles, const core::matrix4* transform); + + virtual const core::aabbox3df& getBBox(); }; } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp index 627e83451..acbc0404b 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp +++ b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp @@ -53,4 +53,19 @@ namespace Skylicht mat.transformVect(triangles[i].pointC, m_triangles[i].pointC); } } + + const core::aabbox3df& CTriangleSelector::getBBox() + { + m_bbox.reset(m_triangles[0].pointA); + + u32 cnt = m_triangles.size(); + for (u32 i = 0; i < cnt; ++i) + { + m_bbox.addInternalPoint(m_triangles[i].pointA); + m_bbox.addInternalPoint(m_triangles[i].pointB); + m_bbox.addInternalPoint(m_triangles[i].pointC); + } + + return m_bbox; + } } \ No newline at end of file diff --git a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h index c07acb86c..119c68560 100644 --- a/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h +++ b/Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.h @@ -33,6 +33,7 @@ namespace Skylicht protected: CEntity* m_entity; + core::aabbox3df m_bbox; core::array m_triangles; public: @@ -42,6 +43,8 @@ namespace Skylicht virtual void getTriangles(core::triangle3df* triangles, const core::matrix4* transform); + virtual const core::aabbox3df& getBBox(); + inline u32 getTriangleCount() { return m_triangles.size();