2014-07-29 10:49:06 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-07-29 10:49:06 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2014-07-29 10:49:06 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-08-22 13:52:03 +08:00
|
|
|
#include "3d/CCMeshVertexIndexData.h"
|
2014-07-29 10:49:06 +08:00
|
|
|
#include "3d/CCObjLoader.h"
|
2022-07-05 14:48:46 +08:00
|
|
|
#include "3d/CCMeshMaterial.h"
|
2014-08-15 14:58:30 +08:00
|
|
|
#include "3d/CCMesh.h"
|
2014-12-08 10:30:06 +08:00
|
|
|
#include "3d/CCBundle3D.h"
|
2014-07-29 10:49:06 +08:00
|
|
|
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCEventCustom.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventType.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
#include "renderer/backend/Buffer.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
2014-07-29 10:49:06 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2021-12-26 23:26:34 +08:00
|
|
|
MeshIndexData* MeshIndexData::create(std::string_view id,
|
2021-12-25 10:04:45 +08:00
|
|
|
MeshVertexData* vertexData,
|
|
|
|
backend::Buffer* indexbuffer,
|
|
|
|
const AABB& aabb)
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto meshindex = new MeshIndexData();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
meshindex->_id = id;
|
2014-08-22 13:25:26 +08:00
|
|
|
meshindex->_indexBuffer = indexbuffer;
|
2021-12-25 10:04:45 +08:00
|
|
|
meshindex->_vertexData = vertexData;
|
2014-08-22 13:25:26 +08:00
|
|
|
indexbuffer->retain();
|
|
|
|
meshindex->_aabb = aabb;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
meshindex->autorelease();
|
|
|
|
return meshindex;
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
backend::Buffer* MeshIndexData::getVertexBuffer() const
|
2014-07-29 10:49:06 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
return _vertexData->getVertexBuffer();
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 10:31:22 +08:00
|
|
|
MeshIndexData::MeshIndexData()
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) {
|
2022-07-04 13:22:06 +08:00
|
|
|
_indexBuffer->updateData((void*)_indexData.data(), _indexData.bsize());
|
2019-04-30 10:31:22 +08:00
|
|
|
});
|
2019-04-30 15:19:53 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
|
2019-04-30 10:31:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MeshIndexData::setIndexData(const cocos2d::MeshData::IndexArray& indexdata)
|
2019-04-30 10:31:22 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_indexData.empty())
|
2019-04-30 11:11:03 +08:00
|
|
|
return;
|
2019-04-30 10:31:22 +08:00
|
|
|
_indexData = indexdata;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
MeshIndexData::~MeshIndexData()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_indexBuffer);
|
2019-04-30 11:11:03 +08:00
|
|
|
_indexData.clear();
|
2019-04-30 10:31:22 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MeshVertexData::setVertexData(const std::vector<float>& vertexData)
|
2019-04-30 10:31:22 +08:00
|
|
|
{
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_vertexData.empty())
|
2019-04-30 11:11:03 +08:00
|
|
|
return;
|
|
|
|
_vertexData = vertexData;
|
2019-04-30 10:31:22 +08:00
|
|
|
#endif
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2022-07-01 19:40:09 +08:00
|
|
|
MeshVertexData* MeshVertexData::create(const MeshData& meshdata, CustomCommand::IndexFormat format)
|
2014-08-15 14:58:30 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto vertexdata = new MeshVertexData();
|
|
|
|
vertexdata->_vertexBuffer = backend::Device::getInstance()->newBuffer(
|
|
|
|
meshdata.vertex.size() * sizeof(meshdata.vertex[0]), backend::BufferType::VERTEX, backend::BufferUsage::STATIC);
|
|
|
|
// CC_SAFE_RETAIN(vertexdata->_vertexBuffer);
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
vertexdata->_sizePerVertex = meshdata.getPerVertexSize();
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
vertexdata->_attribs = meshdata.attribs;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (vertexdata->_vertexBuffer)
|
2014-08-22 13:25:26 +08:00
|
|
|
{
|
2019-04-30 10:31:22 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2019-04-30 13:39:38 +08:00
|
|
|
vertexdata->setVertexData(meshdata.vertex);
|
2019-04-30 15:19:53 +08:00
|
|
|
vertexdata->_vertexBuffer->usingDefaultStoredData(false);
|
2019-04-30 10:31:22 +08:00
|
|
|
#endif
|
2021-12-25 10:04:45 +08:00
|
|
|
vertexdata->_vertexBuffer->updateData((void*)&meshdata.vertex[0],
|
|
|
|
meshdata.vertex.size() * sizeof(meshdata.vertex[0]));
|
2014-08-22 13:25:26 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-12-08 10:30:06 +08:00
|
|
|
bool needCalcAABB = (meshdata.subMeshAABB.size() != meshdata.subMeshIndices.size());
|
2019-04-30 10:31:22 +08:00
|
|
|
for (size_t i = 0, size = meshdata.subMeshIndices.size(); i < size; ++i)
|
|
|
|
{
|
2022-07-03 21:17:49 +08:00
|
|
|
auto& indices = meshdata.subMeshIndices[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
auto indexBuffer = backend::Device::getInstance()->newBuffer(
|
2022-07-05 02:15:11 +08:00
|
|
|
indices.bsize(), backend::BufferType::INDEX, backend::BufferUsage::STATIC);
|
2020-01-06 09:34:53 +08:00
|
|
|
indexBuffer->autorelease();
|
2019-04-30 10:31:22 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2019-04-30 15:19:53 +08:00
|
|
|
indexBuffer->usingDefaultStoredData(false);
|
2019-04-30 10:31:22 +08:00
|
|
|
#endif
|
2022-07-05 02:14:33 +08:00
|
|
|
indexBuffer->updateData((void*)indices.data(), indices.bsize());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
std::string id = (i < meshdata.subMeshIds.size() ? meshdata.subMeshIds[i] : "");
|
2014-12-08 10:30:06 +08:00
|
|
|
MeshIndexData* indexdata = nullptr;
|
|
|
|
if (needCalcAABB)
|
|
|
|
{
|
2022-07-03 21:17:49 +08:00
|
|
|
auto aabb = Bundle3D::calculateAABB(meshdata.vertex, meshdata.getPerVertexSize(), indices);
|
2014-12-08 10:30:06 +08:00
|
|
|
indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, aabb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, meshdata.subMeshAABB[i]);
|
2019-04-30 10:31:22 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2022-07-04 03:50:19 +08:00
|
|
|
indexdata->setIndexData(indices);
|
2019-04-30 10:31:22 +08:00
|
|
|
#endif
|
2022-07-01 04:21:21 +08:00
|
|
|
vertexdata->_indices.pushBack(indexdata);
|
2014-08-22 13:25:26 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
vertexdata->autorelease();
|
|
|
|
return vertexdata;
|
2014-08-15 14:58:30 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
MeshIndexData* MeshVertexData::getMeshIndexDataById(std::string_view id) const
|
2014-08-22 13:25:26 +08:00
|
|
|
{
|
2022-07-01 04:21:21 +08:00
|
|
|
for (auto it : _indices)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
if (it->getId() == id)
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-30 09:35:17 +08:00
|
|
|
bool MeshVertexData::hasVertexAttrib(shaderinfos::VertexKey attrib) const
|
2014-08-22 13:25:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& it : _attribs)
|
|
|
|
{
|
2014-08-22 13:25:26 +08:00
|
|
|
if (it.vertexAttrib == attrib)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-30 10:31:22 +08:00
|
|
|
MeshVertexData::MeshVertexData()
|
|
|
|
{
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) {
|
2019-04-30 15:19:53 +08:00
|
|
|
_vertexBuffer->updateData((void*)_vertexData.data(), _vertexData.size() * sizeof(_vertexData[0]));
|
2019-04-30 10:31:22 +08:00
|
|
|
});
|
2019-04-30 15:19:53 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, 1);
|
2019-04-30 10:31:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:25:26 +08:00
|
|
|
MeshVertexData::~MeshVertexData()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_vertexBuffer);
|
2022-07-01 04:21:21 +08:00
|
|
|
_indices.clear();
|
2019-04-30 11:11:03 +08:00
|
|
|
_vertexData.clear();
|
2019-04-30 10:31:22 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2014-07-29 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|