CCMeshVertexIndexData.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include <list>
  22. #include <fstream>
  23. #include <iostream>
  24. #include <sstream>
  25. #include "3d/CCMeshVertexIndexData.h"
  26. #include "3d/CCObjLoader.h"
  27. #include "3d/CCSprite3DMaterial.h"
  28. #include "3d/CCMesh.h"
  29. #include "3d/CCBundle3D.h"
  30. #include "base/ccMacros.h"
  31. #include "base/CCEventCustom.h"
  32. #include "base/CCEventListenerCustom.h"
  33. #include "base/CCEventDispatcher.h"
  34. #include "base/CCEventType.h"
  35. #include "base/CCDirector.h"
  36. #include "renderer/ccGLStateCache.h"
  37. using namespace std;
  38. NS_CC_BEGIN
  39. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. MeshIndexData* MeshIndexData::create(const std::string& id, MeshVertexData* vertexData, IndexBuffer* indexbuffer, const AABB& aabb)
  41. {
  42. auto meshindex = new (std::nothrow) MeshIndexData();
  43. meshindex->_id = id;
  44. meshindex->_indexBuffer = indexbuffer;
  45. meshindex->_vertexData = vertexData;
  46. indexbuffer->retain();
  47. meshindex->_aabb = aabb;
  48. meshindex->autorelease();
  49. return meshindex;
  50. }
  51. const VertexBuffer* MeshIndexData::getVertexBuffer() const
  52. {
  53. return _vertexData->getVertexBuffer();
  54. }
  55. MeshIndexData::MeshIndexData()
  56. : _indexBuffer(nullptr)
  57. , _vertexData(nullptr)
  58. , _primitiveType(GL_TRIANGLES)
  59. {
  60. }
  61. MeshIndexData::~MeshIndexData()
  62. {
  63. CC_SAFE_RELEASE(_indexBuffer);
  64. }
  65. MeshVertexData* MeshVertexData::create(const MeshData& meshdata)
  66. {
  67. auto vertexdata = new (std::nothrow) MeshVertexData();
  68. int pervertexsize = meshdata.getPerVertexSize();
  69. vertexdata->_vertexBuffer = VertexBuffer::create(pervertexsize, (int)(meshdata.vertex.size() / (pervertexsize / 4)));
  70. vertexdata->_vertexData = VertexData::create();
  71. CC_SAFE_RETAIN(vertexdata->_vertexData);
  72. CC_SAFE_RETAIN(vertexdata->_vertexBuffer);
  73. int offset = 0;
  74. for (const auto& it : meshdata.attribs) {
  75. vertexdata->_vertexData->setStream(vertexdata->_vertexBuffer, VertexStreamAttribute(offset, it.vertexAttrib, it.type, it.size));
  76. offset += it.attribSizeBytes;
  77. }
  78. vertexdata->_attribs = meshdata.attribs;
  79. if(vertexdata->_vertexBuffer)
  80. {
  81. vertexdata->_vertexBuffer->updateVertices((void*)&meshdata.vertex[0], (int)meshdata.vertex.size() * 4 / vertexdata->_vertexBuffer->getSizePerVertex(), 0);
  82. }
  83. bool needCalcAABB = (meshdata.subMeshAABB.size() != meshdata.subMeshIndices.size());
  84. for (size_t i = 0, size = meshdata.subMeshIndices.size(); i < size; ++i) {
  85. auto& index = meshdata.subMeshIndices[i];
  86. auto indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, (int)(index.size()));
  87. indexBuffer->updateIndices(&index[0], (int)index.size(), 0);
  88. std::string id = (i < meshdata.subMeshIds.size() ? meshdata.subMeshIds[i] : "");
  89. MeshIndexData* indexdata = nullptr;
  90. if (needCalcAABB)
  91. {
  92. auto aabb = Bundle3D::calculateAABB(meshdata.vertex, meshdata.getPerVertexSize(), index);
  93. indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, aabb);
  94. }
  95. else
  96. indexdata = MeshIndexData::create(id, vertexdata, indexBuffer, meshdata.subMeshAABB[i]);
  97. vertexdata->_indexs.pushBack(indexdata);
  98. }
  99. vertexdata->autorelease();
  100. return vertexdata;
  101. }
  102. MeshIndexData* MeshVertexData::getMeshIndexDataById(const std::string& id) const
  103. {
  104. for (auto it : _indexs) {
  105. if (it->getId() == id)
  106. return it;
  107. }
  108. return nullptr;
  109. }
  110. bool MeshVertexData::hasVertexAttrib(int attrib) const
  111. {
  112. for (const auto& it : _attribs) {
  113. if (it.vertexAttrib == attrib)
  114. return true;
  115. }
  116. return false;
  117. }
  118. MeshVertexData::MeshVertexData()
  119. : _vertexData(nullptr)
  120. , _vertexBuffer(nullptr)
  121. , _vertexCount(0)
  122. {
  123. }
  124. MeshVertexData::~MeshVertexData()
  125. {
  126. CC_SAFE_RELEASE(_vertexData);
  127. CC_SAFE_RELEASE(_vertexBuffer);
  128. _indexs.clear();
  129. }
  130. NS_CC_END