OgreStructs.cpp 33 KB

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