OgreStructs.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  34. #include "OgreStructs.h"
  35. #include <assimp/Exceptional.h>
  36. #include <assimp/TinyFormatter.h>
  37. #include <assimp/scene.h>
  38. #include <assimp/DefaultLogger.hpp>
  39. namespace Assimp {
  40. namespace Ogre {
  41. // VertexElement
  42. VertexElement::VertexElement() :
  43. index(0),
  44. source(0),
  45. offset(0),
  46. type(VET_FLOAT1),
  47. semantic(VES_POSITION) {
  48. }
  49. size_t VertexElement::Size() const {
  50. return TypeSize(type);
  51. }
  52. size_t VertexElement::ComponentCount() const {
  53. return ComponentCount(type);
  54. }
  55. size_t VertexElement::ComponentCount(Type type) {
  56. switch (type) {
  57. case VET_COLOUR:
  58. case VET_COLOUR_ABGR:
  59. case VET_COLOUR_ARGB:
  60. case VET_FLOAT1:
  61. case VET_DOUBLE1:
  62. case VET_SHORT1:
  63. case VET_USHORT1:
  64. case VET_INT1:
  65. case VET_UINT1:
  66. return 1;
  67. case VET_FLOAT2:
  68. case VET_DOUBLE2:
  69. case VET_SHORT2:
  70. case VET_USHORT2:
  71. case VET_INT2:
  72. case VET_UINT2:
  73. return 2;
  74. case VET_FLOAT3:
  75. case VET_DOUBLE3:
  76. case VET_SHORT3:
  77. case VET_USHORT3:
  78. case VET_INT3:
  79. case VET_UINT3:
  80. return 3;
  81. case VET_FLOAT4:
  82. case VET_DOUBLE4:
  83. case VET_SHORT4:
  84. case VET_USHORT4:
  85. case VET_INT4:
  86. case VET_UINT4:
  87. case VET_UBYTE4:
  88. return 4;
  89. }
  90. return 0;
  91. }
  92. size_t VertexElement::TypeSize(Type type) {
  93. switch (type) {
  94. case VET_COLOUR:
  95. case VET_COLOUR_ABGR:
  96. case VET_COLOUR_ARGB:
  97. return sizeof(unsigned int);
  98. case VET_FLOAT1:
  99. return sizeof(float);
  100. case VET_FLOAT2:
  101. return sizeof(float) * 2;
  102. case VET_FLOAT3:
  103. return sizeof(float) * 3;
  104. case VET_FLOAT4:
  105. return sizeof(float) * 4;
  106. case VET_DOUBLE1:
  107. return sizeof(double);
  108. case VET_DOUBLE2:
  109. return sizeof(double) * 2;
  110. case VET_DOUBLE3:
  111. return sizeof(double) * 3;
  112. case VET_DOUBLE4:
  113. return sizeof(double) * 4;
  114. case VET_SHORT1:
  115. return sizeof(short);
  116. case VET_SHORT2:
  117. return sizeof(short) * 2;
  118. case VET_SHORT3:
  119. return sizeof(short) * 3;
  120. case VET_SHORT4:
  121. return sizeof(short) * 4;
  122. case VET_USHORT1:
  123. return sizeof(unsigned short);
  124. case VET_USHORT2:
  125. return sizeof(unsigned short) * 2;
  126. case VET_USHORT3:
  127. return sizeof(unsigned short) * 3;
  128. case VET_USHORT4:
  129. return sizeof(unsigned short) * 4;
  130. case VET_INT1:
  131. return sizeof(int);
  132. case VET_INT2:
  133. return sizeof(int) * 2;
  134. case VET_INT3:
  135. return sizeof(int) * 3;
  136. case VET_INT4:
  137. return sizeof(int) * 4;
  138. case VET_UINT1:
  139. return sizeof(unsigned int);
  140. case VET_UINT2:
  141. return sizeof(unsigned int) * 2;
  142. case VET_UINT3:
  143. return sizeof(unsigned int) * 3;
  144. case VET_UINT4:
  145. return sizeof(unsigned int) * 4;
  146. case VET_UBYTE4:
  147. return sizeof(unsigned char) * 4;
  148. }
  149. return 0;
  150. }
  151. std::string VertexElement::TypeToString() {
  152. return TypeToString(type);
  153. }
  154. std::string VertexElement::TypeToString(Type type) {
  155. switch (type) {
  156. case VET_COLOUR: return "COLOUR";
  157. case VET_COLOUR_ABGR: return "COLOUR_ABGR";
  158. case VET_COLOUR_ARGB: return "COLOUR_ARGB";
  159. case VET_FLOAT1: return "FLOAT1";
  160. case VET_FLOAT2: return "FLOAT2";
  161. case VET_FLOAT3: return "FLOAT3";
  162. case VET_FLOAT4: return "FLOAT4";
  163. case VET_DOUBLE1: return "DOUBLE1";
  164. case VET_DOUBLE2: return "DOUBLE2";
  165. case VET_DOUBLE3: return "DOUBLE3";
  166. case VET_DOUBLE4: return "DOUBLE4";
  167. case VET_SHORT1: return "SHORT1";
  168. case VET_SHORT2: return "SHORT2";
  169. case VET_SHORT3: return "SHORT3";
  170. case VET_SHORT4: return "SHORT4";
  171. case VET_USHORT1: return "USHORT1";
  172. case VET_USHORT2: return "USHORT2";
  173. case VET_USHORT3: return "USHORT3";
  174. case VET_USHORT4: return "USHORT4";
  175. case VET_INT1: return "INT1";
  176. case VET_INT2: return "INT2";
  177. case VET_INT3: return "INT3";
  178. case VET_INT4: return "INT4";
  179. case VET_UINT1: return "UINT1";
  180. case VET_UINT2: return "UINT2";
  181. case VET_UINT3: return "UINT3";
  182. case VET_UINT4: return "UINT4";
  183. case VET_UBYTE4: return "UBYTE4";
  184. }
  185. return "Uknown_VertexElement::Type";
  186. }
  187. std::string VertexElement::SemanticToString() {
  188. return SemanticToString(semantic);
  189. }
  190. std::string VertexElement::SemanticToString(Semantic semantic) {
  191. switch (semantic) {
  192. case VES_POSITION: return "POSITION";
  193. case VES_BLEND_WEIGHTS: return "BLEND_WEIGHTS";
  194. case VES_BLEND_INDICES: return "BLEND_INDICES";
  195. case VES_NORMAL: return "NORMAL";
  196. case VES_DIFFUSE: return "DIFFUSE";
  197. case VES_SPECULAR: return "SPECULAR";
  198. case VES_TEXTURE_COORDINATES: return "TEXTURE_COORDINATES";
  199. case VES_BINORMAL: return "BINORMAL";
  200. case VES_TANGENT: return "TANGENT";
  201. }
  202. return "Uknown_VertexElement::Semantic";
  203. }
  204. // IVertexData
  205. IVertexData::IVertexData() :
  206. count(0) {
  207. }
  208. bool IVertexData::HasBoneAssignments() const {
  209. return !boneAssignments.empty();
  210. }
  211. void IVertexData::AddVertexMapping(uint32_t oldIndex, uint32_t newIndex) {
  212. BoneAssignmentsForVertex(oldIndex, newIndex, boneAssignmentsMap[newIndex]);
  213. vertexIndexMapping[oldIndex].push_back(newIndex);
  214. }
  215. void IVertexData::BoneAssignmentsForVertex(uint32_t currentIndex, uint32_t newIndex, VertexBoneAssignmentList &dest) const {
  216. for (const auto &boneAssign : boneAssignments) {
  217. if (boneAssign.vertexIndex == currentIndex) {
  218. VertexBoneAssignment a = boneAssign;
  219. a.vertexIndex = newIndex;
  220. dest.push_back(a);
  221. }
  222. }
  223. }
  224. AssimpVertexBoneWeightList IVertexData::AssimpBoneWeights(size_t vertices) {
  225. AssimpVertexBoneWeightList weights;
  226. for (size_t vi = 0; vi < vertices; ++vi) {
  227. VertexBoneAssignmentList &vertexWeights = boneAssignmentsMap[static_cast<unsigned int>(vi)];
  228. for (VertexBoneAssignmentList::const_iterator iter = vertexWeights.begin(), end = vertexWeights.end();
  229. iter != end; ++iter) {
  230. std::vector<aiVertexWeight> &boneWeights = weights[iter->boneIndex];
  231. boneWeights.emplace_back(static_cast<unsigned int>(vi), iter->weight);
  232. }
  233. }
  234. return weights;
  235. }
  236. std::set<uint16_t> IVertexData::ReferencedBonesByWeights() const {
  237. std::set<uint16_t> referenced;
  238. for (const auto &boneAssign : boneAssignments) {
  239. referenced.insert(boneAssign.boneIndex);
  240. }
  241. return referenced;
  242. }
  243. // VertexData
  244. VertexData::VertexData() = default;
  245. VertexData::~VertexData() {
  246. Reset();
  247. }
  248. void VertexData::Reset() {
  249. // Releases shared ptr memory streams.
  250. vertexBindings.clear();
  251. vertexElements.clear();
  252. }
  253. uint32_t VertexData::VertexSize(uint16_t source) const {
  254. uint32_t size = 0;
  255. for (const auto &element : vertexElements) {
  256. if (element.source == source)
  257. size += static_cast<uint32_t>(element.Size());
  258. }
  259. return size;
  260. }
  261. MemoryStream *VertexData::VertexBuffer(uint16_t source) {
  262. if (vertexBindings.find(source) != vertexBindings.end())
  263. return vertexBindings[source].get();
  264. return nullptr;
  265. }
  266. VertexElement *VertexData::GetVertexElement(VertexElement::Semantic semantic, uint16_t index) {
  267. for (auto &element : vertexElements) {
  268. if (element.semantic == semantic && element.index == index)
  269. return &element;
  270. }
  271. return nullptr;
  272. }
  273. // VertexDataXml
  274. VertexDataXml::VertexDataXml() = default;
  275. bool VertexDataXml::HasPositions() const {
  276. return !positions.empty();
  277. }
  278. bool VertexDataXml::HasNormals() const {
  279. return !normals.empty();
  280. }
  281. bool VertexDataXml::HasTangents() const {
  282. return !tangents.empty();
  283. }
  284. bool VertexDataXml::HasUvs() const {
  285. return !uvs.empty();
  286. }
  287. size_t VertexDataXml::NumUvs() const {
  288. return uvs.size();
  289. }
  290. // IndexData
  291. IndexData::IndexData() :
  292. count(0),
  293. faceCount(0),
  294. is32bit(false) {
  295. }
  296. IndexData::~IndexData() {
  297. Reset();
  298. }
  299. void IndexData::Reset() {
  300. // Release shared ptr memory stream.
  301. buffer.reset();
  302. }
  303. size_t IndexData::IndexSize() const {
  304. return (is32bit ? sizeof(uint32_t) : sizeof(uint16_t));
  305. }
  306. size_t IndexData::FaceSize() const {
  307. return IndexSize() * 3;
  308. }
  309. // Mesh
  310. Mesh::Mesh() :
  311. hasSkeletalAnimations(false),
  312. skeleton(nullptr),
  313. sharedVertexData(nullptr),
  314. subMeshes(),
  315. animations(),
  316. poses() {
  317. }
  318. Mesh::~Mesh() {
  319. Reset();
  320. }
  321. void Mesh::Reset() {
  322. OGRE_SAFE_DELETE(skeleton)
  323. OGRE_SAFE_DELETE(sharedVertexData)
  324. for (auto &mesh : subMeshes) {
  325. OGRE_SAFE_DELETE(mesh)
  326. }
  327. subMeshes.clear();
  328. for (auto &anim : animations) {
  329. OGRE_SAFE_DELETE(anim)
  330. }
  331. animations.clear();
  332. for (auto &pose : poses) {
  333. OGRE_SAFE_DELETE(pose)
  334. }
  335. poses.clear();
  336. }
  337. size_t Mesh::NumSubMeshes() const {
  338. return subMeshes.size();
  339. }
  340. SubMesh *Mesh::GetSubMesh(size_t index) const {
  341. for (size_t i = 0; i < subMeshes.size(); ++i) {
  342. if (subMeshes[i]->index == index) {
  343. return subMeshes[i];
  344. }
  345. }
  346. return nullptr;
  347. }
  348. void Mesh::ConvertToAssimpScene(aiScene *dest) {
  349. if (nullptr == dest) {
  350. return;
  351. }
  352. // Setup
  353. dest->mNumMeshes = static_cast<unsigned int>(NumSubMeshes());
  354. dest->mMeshes = new aiMesh *[dest->mNumMeshes];
  355. // Create root node
  356. dest->mRootNode = new aiNode();
  357. dest->mRootNode->mNumMeshes = dest->mNumMeshes;
  358. dest->mRootNode->mMeshes = new unsigned int[dest->mRootNode->mNumMeshes];
  359. // Export meshes
  360. for (size_t i = 0; i < dest->mNumMeshes; ++i) {
  361. dest->mMeshes[i] = subMeshes[i]->ConvertToAssimpMesh(this);
  362. dest->mRootNode->mMeshes[i] = static_cast<unsigned int>(i);
  363. }
  364. // Export skeleton
  365. if (skeleton) {
  366. // Bones
  367. if (!skeleton->bones.empty()) {
  368. BoneList rootBones = skeleton->RootBones();
  369. dest->mRootNode->mNumChildren = static_cast<unsigned int>(rootBones.size());
  370. dest->mRootNode->mChildren = new aiNode *[dest->mRootNode->mNumChildren];
  371. for (size_t i = 0, len = rootBones.size(); i < len; ++i) {
  372. dest->mRootNode->mChildren[i] = rootBones[i]->ConvertToAssimpNode(skeleton, dest->mRootNode);
  373. }
  374. }
  375. // Animations
  376. if (!skeleton->animations.empty()) {
  377. dest->mNumAnimations = static_cast<unsigned int>(skeleton->animations.size());
  378. dest->mAnimations = new aiAnimation *[dest->mNumAnimations];
  379. for (size_t i = 0, len = skeleton->animations.size(); i < len; ++i) {
  380. dest->mAnimations[i] = skeleton->animations[i]->ConvertToAssimpAnimation();
  381. }
  382. }
  383. }
  384. }
  385. // ISubMesh
  386. ISubMesh::ISubMesh() :
  387. index(0),
  388. materialIndex(-1),
  389. usesSharedVertexData(false),
  390. operationType(OT_POINT_LIST) {
  391. }
  392. // SubMesh
  393. SubMesh::SubMesh() :
  394. vertexData(nullptr),
  395. indexData(new IndexData()) {
  396. }
  397. SubMesh::~SubMesh() {
  398. Reset();
  399. }
  400. void SubMesh::Reset(){
  401. OGRE_SAFE_DELETE(vertexData)
  402. OGRE_SAFE_DELETE(indexData)
  403. }
  404. aiMesh *SubMesh::ConvertToAssimpMesh(Mesh *parent) {
  405. if (operationType != OT_TRIANGLE_LIST) {
  406. throw DeadlyImportError("Only mesh operation type OT_TRIANGLE_LIST is supported. Found ", operationType);
  407. }
  408. aiMesh *dest = new aiMesh();
  409. dest->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  410. if (!name.empty())
  411. dest->mName = name;
  412. // Material index
  413. if (materialIndex != -1)
  414. dest->mMaterialIndex = materialIndex;
  415. // Pick source vertex data from shader geometry or from internal geometry.
  416. VertexData *src = (!usesSharedVertexData ? vertexData : parent->sharedVertexData);
  417. VertexElement *positionsElement = src->GetVertexElement(VertexElement::VES_POSITION);
  418. VertexElement *normalsElement = src->GetVertexElement(VertexElement::VES_NORMAL);
  419. VertexElement *uv1Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 0);
  420. VertexElement *uv2Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 1);
  421. // Sanity checks
  422. if (!positionsElement) {
  423. throw DeadlyImportError("Failed to import Ogre VertexElement::VES_POSITION. Mesh does not have vertex positions!");
  424. } else if (positionsElement->type != VertexElement::VET_FLOAT3) {
  425. throw DeadlyImportError("Ogre Mesh position vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  426. } else if (normalsElement && normalsElement->type != VertexElement::VET_FLOAT3) {
  427. throw DeadlyImportError("Ogre Mesh normal vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  428. }
  429. // Faces
  430. dest->mNumFaces = indexData->faceCount;
  431. dest->mFaces = new aiFace[dest->mNumFaces];
  432. // Assimp required unique vertices, we need to convert from Ogres shared indexing.
  433. size_t uniqueVertexCount = dest->mNumFaces * 3;
  434. dest->mNumVertices = static_cast<unsigned int>(uniqueVertexCount);
  435. dest->mVertices = new aiVector3D[dest->mNumVertices];
  436. // Source streams
  437. MemoryStream *positions = src->VertexBuffer(positionsElement->source);
  438. MemoryStream *normals = (normalsElement ? src->VertexBuffer(normalsElement->source) : nullptr);
  439. MemoryStream *uv1 = (uv1Element ? src->VertexBuffer(uv1Element->source) : nullptr);
  440. MemoryStream *uv2 = (uv2Element ? src->VertexBuffer(uv2Element->source) : nullptr);
  441. // Element size
  442. const size_t sizePosition = positionsElement->Size();
  443. const size_t sizeNormal = (normalsElement ? normalsElement->Size() : 0);
  444. const size_t sizeUv1 = (uv1Element ? uv1Element->Size() : 0);
  445. const size_t sizeUv2 = (uv2Element ? uv2Element->Size() : 0);
  446. // Vertex width
  447. const size_t vWidthPosition = src->VertexSize(positionsElement->source);
  448. const size_t vWidthNormal = (normalsElement ? src->VertexSize(normalsElement->source) : 0);
  449. const size_t vWidthUv1 = (uv1Element ? src->VertexSize(uv1Element->source) : 0);
  450. const size_t vWidthUv2 = (uv2Element ? src->VertexSize(uv2Element->source) : 0);
  451. bool boneAssignments = src->HasBoneAssignments();
  452. // Prepare normals
  453. if (normals)
  454. dest->mNormals = new aiVector3D[dest->mNumVertices];
  455. // Prepare UVs, ignoring incompatible UVs.
  456. if (uv1) {
  457. if (uv1Element->type == VertexElement::VET_FLOAT2 || uv1Element->type == VertexElement::VET_FLOAT3) {
  458. dest->mNumUVComponents[0] = static_cast<unsigned int>(uv1Element->ComponentCount());
  459. dest->mTextureCoords[0] = new aiVector3D[dest->mNumVertices];
  460. } else {
  461. ASSIMP_LOG_WARN("Ogre imported UV0 type ", uv1Element->TypeToString(), " is not compatible with Assimp. Ignoring UV.");
  462. uv1 = nullptr;
  463. }
  464. }
  465. if (uv2) {
  466. if (uv2Element->type == VertexElement::VET_FLOAT2 || uv2Element->type == VertexElement::VET_FLOAT3) {
  467. dest->mNumUVComponents[1] = static_cast<unsigned int>(uv2Element->ComponentCount());
  468. dest->mTextureCoords[1] = new aiVector3D[dest->mNumVertices];
  469. } else {
  470. ASSIMP_LOG_WARN("Ogre imported UV0 type ", uv2Element->TypeToString(), " is not compatible with Assimp. Ignoring UV.");
  471. uv2 = nullptr;
  472. }
  473. }
  474. aiVector3D *uv1Dest = (uv1 ? dest->mTextureCoords[0] : nullptr);
  475. aiVector3D *uv2Dest = (uv2 ? dest->mTextureCoords[1] : nullptr);
  476. MemoryStream *faces = indexData->buffer.get();
  477. for (size_t fi = 0, isize = indexData->IndexSize(), fsize = indexData->FaceSize();
  478. fi < dest->mNumFaces; ++fi) {
  479. // Source Ogre face
  480. aiFace ogreFace;
  481. ogreFace.mNumIndices = 3;
  482. ogreFace.mIndices = new unsigned int[3];
  483. faces->Seek(fi * fsize, aiOrigin_SET);
  484. if (indexData->is32bit) {
  485. faces->Read(&ogreFace.mIndices[0], isize, 3);
  486. } else {
  487. uint16_t iout = 0;
  488. for (size_t ii = 0; ii < 3; ++ii) {
  489. faces->Read(&iout, isize, 1);
  490. ogreFace.mIndices[ii] = static_cast<unsigned int>(iout);
  491. }
  492. }
  493. // Destination Assimp face
  494. aiFace &face = dest->mFaces[fi];
  495. face.mNumIndices = 3;
  496. face.mIndices = new unsigned int[3];
  497. const size_t pos = fi * 3;
  498. for (size_t v = 0; v < 3; ++v) {
  499. const size_t newIndex = pos + v;
  500. // Write face index
  501. face.mIndices[v] = static_cast<unsigned int>(newIndex);
  502. // Ogres vertex index to ref into the source buffers.
  503. const size_t ogreVertexIndex = ogreFace.mIndices[v];
  504. src->AddVertexMapping(static_cast<uint32_t>(ogreVertexIndex), static_cast<uint32_t>(newIndex));
  505. // Position
  506. positions->Seek((vWidthPosition * ogreVertexIndex) + positionsElement->offset, aiOrigin_SET);
  507. positions->Read(&dest->mVertices[newIndex], sizePosition, 1);
  508. // Normal
  509. if (normals) {
  510. normals->Seek((vWidthNormal * ogreVertexIndex) + normalsElement->offset, aiOrigin_SET);
  511. normals->Read(&dest->mNormals[newIndex], sizeNormal, 1);
  512. }
  513. // UV0
  514. if (uv1 && uv1Dest) {
  515. uv1->Seek((vWidthUv1 * ogreVertexIndex) + uv1Element->offset, aiOrigin_SET);
  516. uv1->Read(&uv1Dest[newIndex], sizeUv1, 1);
  517. uv1Dest[newIndex].y = (uv1Dest[newIndex].y * -1) + 1; // Flip UV from Ogre to Assimp form
  518. }
  519. // UV1
  520. if (uv2 && uv2Dest) {
  521. uv2->Seek((vWidthUv2 * ogreVertexIndex) + uv2Element->offset, aiOrigin_SET);
  522. uv2->Read(&uv2Dest[newIndex], sizeUv2, 1);
  523. uv2Dest[newIndex].y = (uv2Dest[newIndex].y * -1) + 1; // Flip UV from Ogre to Assimp form
  524. }
  525. }
  526. }
  527. // Bones and bone weights
  528. if (parent->skeleton && boneAssignments) {
  529. AssimpVertexBoneWeightList weights = src->AssimpBoneWeights(dest->mNumVertices);
  530. std::set<uint16_t> referencedBones = src->ReferencedBonesByWeights();
  531. dest->mNumBones = static_cast<unsigned int>(referencedBones.size());
  532. dest->mBones = new aiBone *[dest->mNumBones];
  533. size_t assimpBoneIndex = 0;
  534. for (std::set<uint16_t>::const_iterator rbIter = referencedBones.begin(), rbEnd = referencedBones.end(); rbIter != rbEnd; ++rbIter, ++assimpBoneIndex) {
  535. Bone *bone = parent->skeleton->BoneById((*rbIter));
  536. dest->mBones[assimpBoneIndex] = bone->ConvertToAssimpBone(parent->skeleton, weights[bone->id]);
  537. }
  538. }
  539. return dest;
  540. }
  541. // MeshXml
  542. MeshXml::MeshXml() :
  543. skeleton(nullptr),
  544. sharedVertexData(nullptr) {
  545. }
  546. MeshXml::~MeshXml() {
  547. Reset();
  548. }
  549. void MeshXml::Reset() {
  550. OGRE_SAFE_DELETE(skeleton)
  551. OGRE_SAFE_DELETE(sharedVertexData)
  552. for (auto &mesh : subMeshes) {
  553. OGRE_SAFE_DELETE(mesh)
  554. }
  555. subMeshes.clear();
  556. }
  557. size_t MeshXml::NumSubMeshes() const {
  558. return subMeshes.size();
  559. }
  560. SubMeshXml *MeshXml::GetSubMesh(uint16_t index) const {
  561. for (size_t i = 0; i < subMeshes.size(); ++i)
  562. if (subMeshes[i]->index == index)
  563. return subMeshes[i];
  564. return nullptr;
  565. }
  566. void MeshXml::ConvertToAssimpScene(aiScene *dest) {
  567. // Setup
  568. dest->mNumMeshes = static_cast<unsigned int>(NumSubMeshes());
  569. dest->mMeshes = new aiMesh *[dest->mNumMeshes];
  570. // Create root node
  571. dest->mRootNode = new aiNode();
  572. dest->mRootNode->mNumMeshes = dest->mNumMeshes;
  573. dest->mRootNode->mMeshes = new unsigned int[dest->mRootNode->mNumMeshes];
  574. // Export meshes
  575. for (size_t i = 0; i < dest->mNumMeshes; ++i) {
  576. dest->mMeshes[i] = subMeshes[i]->ConvertToAssimpMesh(this);
  577. dest->mRootNode->mMeshes[i] = static_cast<unsigned int>(i);
  578. }
  579. // Export skeleton
  580. if (skeleton) {
  581. // Bones
  582. if (!skeleton->bones.empty()) {
  583. BoneList rootBones = skeleton->RootBones();
  584. dest->mRootNode->mNumChildren = static_cast<unsigned int>(rootBones.size());
  585. dest->mRootNode->mChildren = new aiNode *[dest->mRootNode->mNumChildren];
  586. for (size_t i = 0, len = rootBones.size(); i < len; ++i) {
  587. dest->mRootNode->mChildren[i] = rootBones[i]->ConvertToAssimpNode(skeleton, dest->mRootNode);
  588. }
  589. }
  590. // Animations
  591. if (!skeleton->animations.empty()) {
  592. dest->mNumAnimations = static_cast<unsigned int>(skeleton->animations.size());
  593. dest->mAnimations = new aiAnimation *[dest->mNumAnimations];
  594. for (size_t i = 0, len = skeleton->animations.size(); i < len; ++i) {
  595. dest->mAnimations[i] = skeleton->animations[i]->ConvertToAssimpAnimation();
  596. }
  597. }
  598. }
  599. }
  600. // SubMeshXml
  601. SubMeshXml::SubMeshXml() :
  602. indexData(new IndexDataXml()),
  603. vertexData(nullptr) {
  604. }
  605. SubMeshXml::~SubMeshXml() {
  606. Reset();
  607. }
  608. void SubMeshXml::Reset(){
  609. OGRE_SAFE_DELETE(indexData)
  610. OGRE_SAFE_DELETE(vertexData)
  611. }
  612. aiMesh *SubMeshXml::ConvertToAssimpMesh(MeshXml *parent) {
  613. aiMesh *dest = new aiMesh();
  614. dest->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  615. if (!name.empty())
  616. dest->mName = name;
  617. // Material index
  618. if (materialIndex != -1)
  619. dest->mMaterialIndex = materialIndex;
  620. // Faces
  621. dest->mNumFaces = indexData->faceCount;
  622. dest->mFaces = new aiFace[dest->mNumFaces];
  623. // Assimp required unique vertices, we need to convert from Ogres shared indexing.
  624. size_t uniqueVertexCount = dest->mNumFaces * 3;
  625. dest->mNumVertices = static_cast<unsigned int>(uniqueVertexCount);
  626. dest->mVertices = new aiVector3D[dest->mNumVertices];
  627. VertexDataXml *src = (!usesSharedVertexData ? vertexData : parent->sharedVertexData);
  628. bool boneAssignments = src->HasBoneAssignments();
  629. bool normals = src->HasNormals();
  630. size_t uvs = src->NumUvs();
  631. // Prepare normals
  632. if (normals)
  633. dest->mNormals = new aiVector3D[dest->mNumVertices];
  634. // Prepare UVs
  635. for (size_t uvi = 0; uvi < uvs; ++uvi) {
  636. dest->mNumUVComponents[uvi] = 2;
  637. dest->mTextureCoords[uvi] = new aiVector3D[dest->mNumVertices];
  638. }
  639. for (size_t fi = 0; fi < dest->mNumFaces; ++fi) {
  640. // Source Ogre face
  641. aiFace &ogreFace = indexData->faces[fi];
  642. // Destination Assimp face
  643. aiFace &face = dest->mFaces[fi];
  644. face.mNumIndices = 3;
  645. face.mIndices = new unsigned int[3];
  646. const size_t pos = fi * 3;
  647. for (size_t v = 0; v < 3; ++v) {
  648. const size_t newIndex = pos + v;
  649. // Write face index
  650. face.mIndices[v] = static_cast<unsigned int>(newIndex);
  651. // Ogres vertex index to ref into the source buffers.
  652. const size_t ogreVertexIndex = ogreFace.mIndices[v];
  653. src->AddVertexMapping(static_cast<uint32_t>(ogreVertexIndex), static_cast<uint32_t>(newIndex));
  654. // Position
  655. dest->mVertices[newIndex] = src->positions[ogreVertexIndex];
  656. // Normal
  657. if (normals)
  658. dest->mNormals[newIndex] = src->normals[ogreVertexIndex];
  659. // UVs
  660. for (size_t uvi = 0; uvi < uvs; ++uvi) {
  661. aiVector3D *uvDest = dest->mTextureCoords[uvi];
  662. std::vector<aiVector3D> &uvSrc = src->uvs[uvi];
  663. uvDest[newIndex] = uvSrc[ogreVertexIndex];
  664. }
  665. }
  666. }
  667. // Bones and bone weights
  668. if (parent->skeleton && boneAssignments) {
  669. AssimpVertexBoneWeightList weights = src->AssimpBoneWeights(dest->mNumVertices);
  670. std::set<uint16_t> referencedBones = src->ReferencedBonesByWeights();
  671. dest->mNumBones = static_cast<unsigned int>(referencedBones.size());
  672. dest->mBones = new aiBone *[dest->mNumBones];
  673. size_t assimpBoneIndex = 0;
  674. for (std::set<uint16_t>::const_iterator rbIter = referencedBones.begin(), rbEnd = referencedBones.end(); rbIter != rbEnd; ++rbIter, ++assimpBoneIndex) {
  675. Bone *bone = parent->skeleton->BoneById((*rbIter));
  676. dest->mBones[assimpBoneIndex] = bone->ConvertToAssimpBone(parent->skeleton, weights[bone->id]);
  677. }
  678. }
  679. return dest;
  680. }
  681. // Animation
  682. Animation::Animation(Skeleton *parent) :
  683. parentMesh(nullptr),
  684. parentSkeleton(parent),
  685. length(0.0f),
  686. baseTime(-1.0f) {
  687. // empty
  688. }
  689. Animation::Animation(Mesh *parent) :
  690. parentMesh(parent),
  691. parentSkeleton(nullptr),
  692. length(0.0f),
  693. baseTime(-1.0f) {
  694. // empty
  695. }
  696. VertexData *Animation::AssociatedVertexData(VertexAnimationTrack *track) const {
  697. if (nullptr == parentMesh) {
  698. return nullptr;
  699. }
  700. bool sharedGeom = (track->target == 0);
  701. if (sharedGeom) {
  702. return parentMesh->sharedVertexData;
  703. }
  704. return parentMesh->GetSubMesh(track->target - 1)->vertexData;
  705. }
  706. aiAnimation *Animation::ConvertToAssimpAnimation() {
  707. aiAnimation *anim = new aiAnimation();
  708. anim->mName = name;
  709. anim->mDuration = static_cast<double>(length);
  710. anim->mTicksPerSecond = 1.0;
  711. // Tracks
  712. if (!tracks.empty()) {
  713. anim->mNumChannels = static_cast<unsigned int>(tracks.size());
  714. anim->mChannels = new aiNodeAnim *[anim->mNumChannels];
  715. for (size_t i = 0, len = tracks.size(); i < len; ++i) {
  716. anim->mChannels[i] = tracks[i].ConvertToAssimpAnimationNode(parentSkeleton);
  717. }
  718. }
  719. return anim;
  720. }
  721. // Skeleton
  722. Skeleton::Skeleton() :
  723. bones(),
  724. animations(),
  725. blendMode(ANIMBLEND_AVERAGE) {
  726. }
  727. Skeleton::~Skeleton() {
  728. Reset();
  729. }
  730. void Skeleton::Reset() {
  731. for (auto &bone : bones) {
  732. OGRE_SAFE_DELETE(bone)
  733. }
  734. bones.clear();
  735. for (auto &anim : animations) {
  736. OGRE_SAFE_DELETE(anim)
  737. }
  738. animations.clear();
  739. }
  740. BoneList Skeleton::RootBones() const {
  741. BoneList rootBones;
  742. for (BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter) {
  743. if (!(*iter)->IsParented())
  744. rootBones.push_back((*iter));
  745. }
  746. return rootBones;
  747. }
  748. size_t Skeleton::NumRootBones() const {
  749. size_t num = 0;
  750. for (BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter) {
  751. if (!(*iter)->IsParented())
  752. num++;
  753. }
  754. return num;
  755. }
  756. Bone *Skeleton::BoneByName(const std::string &name) const {
  757. for (BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter) {
  758. if ((*iter)->name == name)
  759. return (*iter);
  760. }
  761. return nullptr;
  762. }
  763. Bone *Skeleton::BoneById(uint16_t id) const {
  764. for (BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter) {
  765. if ((*iter)->id == id)
  766. return (*iter);
  767. }
  768. return nullptr;
  769. }
  770. // Bone
  771. Bone::Bone() :
  772. id(0),
  773. parent(nullptr),
  774. parentId(-1),
  775. scale(1.0f, 1.0f, 1.0f) {
  776. }
  777. bool Bone::IsParented() const {
  778. return (parentId != -1 && parent != nullptr);
  779. }
  780. uint16_t Bone::ParentId() const {
  781. return static_cast<uint16_t>(parentId);
  782. }
  783. void Bone::AddChild(Bone *bone) {
  784. if (!bone)
  785. return;
  786. if (bone->IsParented())
  787. throw DeadlyImportError("Attaching child Bone that is already parented: ", bone->name);
  788. bone->parent = this;
  789. bone->parentId = id;
  790. children.push_back(bone->id);
  791. }
  792. void Bone::CalculateWorldMatrixAndDefaultPose(Skeleton *skeleton) {
  793. if (!IsParented())
  794. worldMatrix = aiMatrix4x4(scale, rotation, position).Inverse();
  795. else
  796. worldMatrix = aiMatrix4x4(scale, rotation, position).Inverse() * parent->worldMatrix;
  797. defaultPose = aiMatrix4x4(scale, rotation, position);
  798. // Recursively for all children now that the parent matrix has been calculated.
  799. for (auto boneId : children) {
  800. Bone *child = skeleton->BoneById(boneId);
  801. if (!child) {
  802. throw DeadlyImportError("CalculateWorldMatrixAndDefaultPose: Failed to find child bone ", boneId, " for parent ", id, " ", name);
  803. }
  804. child->CalculateWorldMatrixAndDefaultPose(skeleton);
  805. }
  806. }
  807. aiNode *Bone::ConvertToAssimpNode(Skeleton *skeleton, aiNode *parentNode) {
  808. // Bone node
  809. aiNode *node = new aiNode(name);
  810. node->mParent = parentNode;
  811. node->mTransformation = defaultPose;
  812. // Children
  813. if (!children.empty()) {
  814. node->mNumChildren = static_cast<unsigned int>(children.size());
  815. node->mChildren = new aiNode *[node->mNumChildren];
  816. for (size_t i = 0, len = children.size(); i < len; ++i) {
  817. Bone *child = skeleton->BoneById(children[i]);
  818. if (!child) {
  819. throw DeadlyImportError("ConvertToAssimpNode: Failed to find child bone ", children[i], " for parent ", id, " ", name);
  820. }
  821. node->mChildren[i] = child->ConvertToAssimpNode(skeleton, node);
  822. }
  823. }
  824. return node;
  825. }
  826. aiBone *Bone::ConvertToAssimpBone(Skeleton * /*parent*/, const std::vector<aiVertexWeight> &boneWeights) {
  827. aiBone *bone = new aiBone();
  828. bone->mName = name;
  829. bone->mOffsetMatrix = worldMatrix;
  830. if (!boneWeights.empty()) {
  831. bone->mNumWeights = static_cast<unsigned int>(boneWeights.size());
  832. bone->mWeights = new aiVertexWeight[boneWeights.size()];
  833. memcpy(bone->mWeights, &boneWeights[0], boneWeights.size() * sizeof(aiVertexWeight));
  834. }
  835. return bone;
  836. }
  837. // VertexAnimationTrack
  838. VertexAnimationTrack::VertexAnimationTrack() :
  839. type(VAT_NONE),
  840. target(0) {
  841. }
  842. aiNodeAnim *VertexAnimationTrack::ConvertToAssimpAnimationNode(Skeleton *skeleton) {
  843. if (boneName.empty() || type != VAT_TRANSFORM) {
  844. throw DeadlyImportError("VertexAnimationTrack::ConvertToAssimpAnimationNode: Cannot convert track that has no target bone name or is not type of VAT_TRANSFORM");
  845. }
  846. aiNodeAnim *nodeAnim = new aiNodeAnim();
  847. nodeAnim->mNodeName = boneName;
  848. Bone *bone = skeleton->BoneByName(boneName);
  849. if (!bone) {
  850. throw DeadlyImportError("VertexAnimationTrack::ConvertToAssimpAnimationNode: Failed to find bone ", boneName, " from parent Skeleton");
  851. }
  852. // Keyframes
  853. size_t numKeyframes = transformKeyFrames.size();
  854. nodeAnim->mPositionKeys = new aiVectorKey[numKeyframes];
  855. nodeAnim->mRotationKeys = new aiQuatKey[numKeyframes];
  856. nodeAnim->mScalingKeys = new aiVectorKey[numKeyframes];
  857. nodeAnim->mNumPositionKeys = static_cast<unsigned int>(numKeyframes);
  858. nodeAnim->mNumRotationKeys = static_cast<unsigned int>(numKeyframes);
  859. nodeAnim->mNumScalingKeys = static_cast<unsigned int>(numKeyframes);
  860. for (size_t kfi = 0; kfi < numKeyframes; ++kfi) {
  861. TransformKeyFrame &kfSource = transformKeyFrames[kfi];
  862. // Calculate the complete transformation from world space to bone space
  863. aiVector3D pos;
  864. aiQuaternion rot;
  865. aiVector3D scale;
  866. aiMatrix4x4 finalTransform = bone->defaultPose * kfSource.Transform();
  867. finalTransform.Decompose(scale, rot, pos);
  868. double t = static_cast<double>(kfSource.timePos);
  869. nodeAnim->mPositionKeys[kfi].mTime = t;
  870. nodeAnim->mRotationKeys[kfi].mTime = t;
  871. nodeAnim->mScalingKeys[kfi].mTime = t;
  872. nodeAnim->mPositionKeys[kfi].mValue = pos;
  873. nodeAnim->mRotationKeys[kfi].mValue = rot;
  874. nodeAnim->mScalingKeys[kfi].mValue = scale;
  875. }
  876. return nodeAnim;
  877. }
  878. // TransformKeyFrame
  879. TransformKeyFrame::TransformKeyFrame() :
  880. timePos(0.0f),
  881. scale(1.0f, 1.0f, 1.0f) {
  882. }
  883. aiMatrix4x4 TransformKeyFrame::Transform() {
  884. return aiMatrix4x4(scale, rotation, position);
  885. }
  886. } // namespace Ogre
  887. } // namespace Assimp
  888. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER