calculate aabb if not exist

This commit is contained in:
yangxiao 2014-12-08 10:30:06 +08:00
parent a76e2e430d
commit a1ec27d0b1
2 changed files with 14 additions and 4 deletions

View File

@ -80,6 +80,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<float>& vertex, int stride, const std::vector<unsigned short>& index);
protected:
bool loadJson(const std::string& path);
@ -154,8 +157,6 @@ CC_CONSTRUCTOR_ACCESS:
Bundle3D();
virtual ~Bundle3D();
static AABB calculateAABB(const std::vector<float>& vertex, int stride, const std::vector<unsigned short>& index);
protected:
static Bundle3D* _instance;
std::string _modelPath;

View File

@ -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);
}