From a1ec27d0b1d4f48dcf7749225cbedb2bf63b6c70 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 8 Dec 2014 10:30:06 +0800 Subject: [PATCH] calculate aabb if not exist --- cocos/3d/CCBundle3D.h | 5 +++-- cocos/3d/CCMeshVertexIndexData.cpp | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cocos/3d/CCBundle3D.h b/cocos/3d/CCBundle3D.h index 55d67940b8..8b0af28cd5 100644 --- a/cocos/3d/CCBundle3D.h +++ b/cocos/3d/CCBundle3D.h @@ -79,6 +79,9 @@ public: //load .obj file static bool loadObj(MeshDatas& meshdatas, MaterialDatas& materialdatas, NodeDatas& nodedatas, const std::string& fullPath, const char* mtl_basepath = nullptr); + + //calculate aabb + static AABB calculateAABB(const std::vector& vertex, int stride, const std::vector& index); protected: @@ -154,8 +157,6 @@ CC_CONSTRUCTOR_ACCESS: Bundle3D(); virtual ~Bundle3D(); - static AABB calculateAABB(const std::vector& vertex, int stride, const std::vector& index); - protected: static Bundle3D* _instance; std::string _modelPath; diff --git a/cocos/3d/CCMeshVertexIndexData.cpp b/cocos/3d/CCMeshVertexIndexData.cpp index c30032867c..6cd420b8d5 100644 --- a/cocos/3d/CCMeshVertexIndexData.cpp +++ b/cocos/3d/CCMeshVertexIndexData.cpp @@ -31,6 +31,7 @@ #include "3d/CCObjLoader.h" #include "3d/CCSprite3DMaterial.h" #include "3d/CCMesh.h" +#include "3d/CCBundle3D.h" #include "base/ccMacros.h" #include "base/CCEventCustom.h" @@ -102,14 +103,22 @@ MeshVertexData* MeshVertexData::create(const MeshData& meshdata) vertexdata->_vertexBuffer->updateVertices((void*)&meshdata.vertex[0], (int)meshdata.vertex.size() * 4 / vertexdata->_vertexBuffer->getSizePerVertex(), 0); } - + bool needCalcAABB = (meshdata.subMeshAABB.size() != meshdata.subMeshIndices.size()); for (size_t i = 0; i < meshdata.subMeshIndices.size(); i++) { auto& index = meshdata.subMeshIndices[i]; auto indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, (int)(index.size())); indexBuffer->updateIndices(&index[0], (int)index.size(), 0); std::string id = (i < meshdata.subMeshIds.size() ? meshdata.subMeshIds[i] : ""); - MeshIndexData* indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, meshdata.subMeshAABB[i]); + MeshIndexData* indexdata = nullptr; + if (needCalcAABB) + { + auto aabb = Bundle3D::calculateAABB(meshdata.vertex, meshdata.getPerVertexSize(), index); + indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, aabb); + } + else + indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, meshdata.subMeshAABB[i]); + vertexdata->_indexs.pushBack(indexdata); }