OgreStructs.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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 "../include/assimp/scene.h"
  37. #include "../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 (VertexBoneAssignmentList::const_iterator iter=boneAssignments.begin(), end=boneAssignments.end();
  236. iter!=end; ++iter)
  237. {
  238. if (iter->vertexIndex == currentIndex)
  239. {
  240. VertexBoneAssignment a = (*iter);
  241. a.vertexIndex = newIndex;
  242. dest.push_back(a);
  243. }
  244. }
  245. }
  246. AssimpVertexBoneWeightList IVertexData::AssimpBoneWeights(size_t vertices)
  247. {
  248. AssimpVertexBoneWeightList weights;
  249. for(size_t vi=0; vi<vertices; ++vi)
  250. {
  251. VertexBoneAssignmentList &vertexWeights = boneAssignmentsMap[vi];
  252. for (VertexBoneAssignmentList::const_iterator iter=vertexWeights.begin(), end=vertexWeights.end();
  253. iter!=end; ++iter)
  254. {
  255. std::vector<aiVertexWeight> &boneWeights = weights[iter->boneIndex];
  256. boneWeights.push_back(aiVertexWeight(vi, iter->weight));
  257. }
  258. }
  259. return weights;
  260. }
  261. std::set<uint16_t> IVertexData::ReferencedBonesByWeights() const
  262. {
  263. std::set<uint16_t> referenced;
  264. for (VertexBoneAssignmentList::const_iterator iter=boneAssignments.begin(), end=boneAssignments.end();
  265. iter!=end; ++iter)
  266. {
  267. referenced.insert(iter->boneIndex);
  268. }
  269. return referenced;
  270. }
  271. // VertexData
  272. VertexData::VertexData()
  273. {
  274. }
  275. VertexData::~VertexData()
  276. {
  277. Reset();
  278. }
  279. void VertexData::Reset()
  280. {
  281. // Releases shared ptr memory streams.
  282. vertexBindings.clear();
  283. vertexElements.clear();
  284. }
  285. uint32_t VertexData::VertexSize(uint16_t source) const
  286. {
  287. uint32_t size = 0;
  288. for(VertexElementList::const_iterator iter=vertexElements.begin(), end=vertexElements.end(); iter != end; ++iter)
  289. {
  290. if (iter->source == source)
  291. size += iter->Size();
  292. }
  293. return size;
  294. }
  295. MemoryStream *VertexData::VertexBuffer(uint16_t source)
  296. {
  297. if (vertexBindings.find(source) != vertexBindings.end())
  298. return vertexBindings[source].get();
  299. return 0;
  300. }
  301. VertexElement *VertexData::GetVertexElement(VertexElement::Semantic semantic, uint16_t index)
  302. {
  303. for(VertexElementList::iterator iter=vertexElements.begin(), end=vertexElements.end(); iter != end; ++iter)
  304. {
  305. VertexElement &element = (*iter);
  306. if (element.semantic == semantic && element.index == index)
  307. return &element;
  308. }
  309. return 0;
  310. }
  311. // VertexDataXml
  312. VertexDataXml::VertexDataXml()
  313. {
  314. }
  315. bool VertexDataXml::HasPositions() const
  316. {
  317. return !positions.empty();
  318. }
  319. bool VertexDataXml::HasNormals() const
  320. {
  321. return !normals.empty();
  322. }
  323. bool VertexDataXml::HasTangents() const
  324. {
  325. return !tangents.empty();
  326. }
  327. bool VertexDataXml::HasUvs() const
  328. {
  329. return !uvs.empty();
  330. }
  331. size_t VertexDataXml::NumUvs() const
  332. {
  333. return uvs.size();
  334. }
  335. // IndexData
  336. IndexData::IndexData() :
  337. count(0),
  338. faceCount(0),
  339. is32bit(false)
  340. {
  341. }
  342. IndexData::~IndexData()
  343. {
  344. Reset();
  345. }
  346. void IndexData::Reset()
  347. {
  348. // Release shared ptr memory stream.
  349. buffer.reset();
  350. }
  351. size_t IndexData::IndexSize() const
  352. {
  353. return (is32bit ? sizeof(uint32_t) : sizeof(uint16_t));
  354. }
  355. size_t IndexData::FaceSize() const
  356. {
  357. return IndexSize() * 3;
  358. }
  359. // Mesh
  360. Mesh::Mesh()
  361. : hasSkeletalAnimations(false)
  362. , skeleton(NULL)
  363. , sharedVertexData(NULL)
  364. , subMeshes()
  365. , animations()
  366. , poses()
  367. {
  368. }
  369. Mesh::~Mesh()
  370. {
  371. Reset();
  372. }
  373. void Mesh::Reset()
  374. {
  375. OGRE_SAFE_DELETE(skeleton)
  376. OGRE_SAFE_DELETE(sharedVertexData)
  377. for(size_t i=0, len=subMeshes.size(); i<len; ++i) {
  378. OGRE_SAFE_DELETE(subMeshes[i])
  379. }
  380. subMeshes.clear();
  381. for(size_t i=0, len=animations.size(); i<len; ++i) {
  382. OGRE_SAFE_DELETE(animations[i])
  383. }
  384. animations.clear();
  385. for(size_t i=0, len=poses.size(); i<len; ++i) {
  386. OGRE_SAFE_DELETE(poses[i])
  387. }
  388. poses.clear();
  389. }
  390. size_t Mesh::NumSubMeshes() const
  391. {
  392. return subMeshes.size();
  393. }
  394. SubMesh *Mesh::GetSubMesh( size_t index ) const
  395. {
  396. for ( size_t i = 0; i < subMeshes.size(); ++i ) {
  397. if ( subMeshes[ i ]->index == index ) {
  398. return subMeshes[ i ];
  399. }
  400. }
  401. return 0;
  402. }
  403. void Mesh::ConvertToAssimpScene(aiScene* dest)
  404. {
  405. if ( nullptr == dest ) {
  406. return;
  407. }
  408. // Setup
  409. dest->mNumMeshes = NumSubMeshes();
  410. dest->mMeshes = new aiMesh*[dest->mNumMeshes];
  411. // Create root node
  412. dest->mRootNode = new aiNode();
  413. dest->mRootNode->mNumMeshes = dest->mNumMeshes;
  414. dest->mRootNode->mMeshes = new unsigned int[dest->mRootNode->mNumMeshes];
  415. // Export meshes
  416. for(size_t i=0; i<dest->mNumMeshes; ++i) {
  417. dest->mMeshes[i] = subMeshes[i]->ConvertToAssimpMesh(this);
  418. dest->mRootNode->mMeshes[i] = i;
  419. }
  420. // Export skeleton
  421. if (skeleton)
  422. {
  423. // Bones
  424. if (!skeleton->bones.empty())
  425. {
  426. BoneList rootBones = skeleton->RootBones();
  427. dest->mRootNode->mNumChildren = rootBones.size();
  428. dest->mRootNode->mChildren = new aiNode*[dest->mRootNode->mNumChildren];
  429. for(size_t i=0, len=rootBones.size(); i<len; ++i)
  430. {
  431. dest->mRootNode->mChildren[i] = rootBones[i]->ConvertToAssimpNode(skeleton, dest->mRootNode);
  432. }
  433. }
  434. // Animations
  435. if (!skeleton->animations.empty())
  436. {
  437. dest->mNumAnimations = skeleton->animations.size();
  438. dest->mAnimations = new aiAnimation*[dest->mNumAnimations];
  439. for(size_t i=0, len=skeleton->animations.size(); i<len; ++i)
  440. {
  441. dest->mAnimations[i] = skeleton->animations[i]->ConvertToAssimpAnimation();
  442. }
  443. }
  444. }
  445. }
  446. // ISubMesh
  447. ISubMesh::ISubMesh() :
  448. index(0),
  449. materialIndex(-1),
  450. usesSharedVertexData(false),
  451. operationType(OT_POINT_LIST)
  452. {
  453. }
  454. // SubMesh
  455. SubMesh::SubMesh() :
  456. vertexData(0),
  457. indexData(new IndexData())
  458. {
  459. }
  460. SubMesh::~SubMesh()
  461. {
  462. Reset();
  463. }
  464. void SubMesh::Reset()
  465. {
  466. OGRE_SAFE_DELETE(vertexData)
  467. OGRE_SAFE_DELETE(indexData)
  468. }
  469. aiMesh *SubMesh::ConvertToAssimpMesh(Mesh *parent)
  470. {
  471. if (operationType != OT_TRIANGLE_LIST) {
  472. throw DeadlyImportError(Formatter::format() << "Only mesh operation type OT_TRIANGLE_LIST is supported. Found " << operationType);
  473. }
  474. aiMesh *dest = new aiMesh();
  475. dest->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  476. if (!name.empty())
  477. dest->mName = name;
  478. // Material index
  479. if (materialIndex != -1)
  480. dest->mMaterialIndex = materialIndex;
  481. // Pick source vertex data from shader geometry or from internal geometry.
  482. VertexData *src = (!usesSharedVertexData ? vertexData : parent->sharedVertexData);
  483. VertexElement *positionsElement = src->GetVertexElement(VertexElement::VES_POSITION);
  484. VertexElement *normalsElement = src->GetVertexElement(VertexElement::VES_NORMAL);
  485. VertexElement *uv1Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 0);
  486. VertexElement *uv2Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 1);
  487. // Sanity checks
  488. if (!positionsElement) {
  489. throw DeadlyImportError("Failed to import Ogre VertexElement::VES_POSITION. Mesh does not have vertex positions!");
  490. } else if (positionsElement->type != VertexElement::VET_FLOAT3) {
  491. throw DeadlyImportError("Ogre Mesh position vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  492. } else if (normalsElement && normalsElement->type != VertexElement::VET_FLOAT3) {
  493. throw DeadlyImportError("Ogre Mesh normal vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  494. }
  495. // Faces
  496. dest->mNumFaces = indexData->faceCount;
  497. dest->mFaces = new aiFace[dest->mNumFaces];
  498. // Assimp required unique vertices, we need to convert from Ogres shared indexing.
  499. size_t uniqueVertexCount = dest->mNumFaces * 3;
  500. dest->mNumVertices = uniqueVertexCount;
  501. dest->mVertices = new aiVector3D[dest->mNumVertices];
  502. // Source streams
  503. MemoryStream *positions = src->VertexBuffer(positionsElement->source);
  504. MemoryStream *normals = (normalsElement ? src->VertexBuffer(normalsElement->source) : 0);
  505. MemoryStream *uv1 = (uv1Element ? src->VertexBuffer(uv1Element->source) : 0);
  506. MemoryStream *uv2 = (uv2Element ? src->VertexBuffer(uv2Element->source) : 0);
  507. // Element size
  508. const size_t sizePosition = positionsElement->Size();
  509. const size_t sizeNormal = (normalsElement ? normalsElement->Size() : 0);
  510. const size_t sizeUv1 = (uv1Element ? uv1Element->Size() : 0);
  511. const size_t sizeUv2 = (uv2Element ? uv2Element->Size() : 0);
  512. // Vertex width
  513. const size_t vWidthPosition = src->VertexSize(positionsElement->source);
  514. const size_t vWidthNormal = (normalsElement ? src->VertexSize(normalsElement->source) : 0);
  515. const size_t vWidthUv1 = (uv1Element ? src->VertexSize(uv1Element->source) : 0);
  516. const size_t vWidthUv2 = (uv2Element ? src->VertexSize(uv2Element->source) : 0);
  517. bool boneAssignments = src->HasBoneAssignments();
  518. // Prepare normals
  519. if (normals)
  520. dest->mNormals = new aiVector3D[dest->mNumVertices];
  521. // Prepare UVs, ignoring incompatible UVs.
  522. if (uv1)
  523. {
  524. if (uv1Element->type == VertexElement::VET_FLOAT2 || uv1Element->type == VertexElement::VET_FLOAT3)
  525. {
  526. dest->mNumUVComponents[0] = uv1Element->ComponentCount();
  527. dest->mTextureCoords[0] = new aiVector3D[dest->mNumVertices];
  528. }
  529. else
  530. {
  531. DefaultLogger::get()->warn(Formatter::format() << "Ogre imported UV0 type " << uv1Element->TypeToString() << " is not compatible with Assimp. Ignoring UV.");
  532. uv1 = 0;
  533. }
  534. }
  535. if (uv2)
  536. {
  537. if (uv2Element->type == VertexElement::VET_FLOAT2 || uv2Element->type == VertexElement::VET_FLOAT3)
  538. {
  539. dest->mNumUVComponents[1] = uv2Element->ComponentCount();
  540. dest->mTextureCoords[1] = new aiVector3D[dest->mNumVertices];
  541. }
  542. else
  543. {
  544. DefaultLogger::get()->warn(Formatter::format() << "Ogre imported UV0 type " << uv2Element->TypeToString() << " is not compatible with Assimp. Ignoring UV.");
  545. uv2 = 0;
  546. }
  547. }
  548. aiVector3D *uv1Dest = (uv1 ? dest->mTextureCoords[0] : 0);
  549. aiVector3D *uv2Dest = (uv2 ? dest->mTextureCoords[1] : 0);
  550. MemoryStream *faces = indexData->buffer.get();
  551. for (size_t fi=0, isize=indexData->IndexSize(), fsize=indexData->FaceSize();
  552. fi<dest->mNumFaces; ++fi)
  553. {
  554. // Source Ogre face
  555. aiFace ogreFace;
  556. ogreFace.mNumIndices = 3;
  557. ogreFace.mIndices = new unsigned int[3];
  558. faces->Seek(fi * fsize, aiOrigin_SET);
  559. if (indexData->is32bit)
  560. {
  561. faces->Read(&ogreFace.mIndices[0], isize, 3);
  562. }
  563. else
  564. {
  565. uint16_t iout = 0;
  566. for (size_t ii=0; ii<3; ++ii)
  567. {
  568. faces->Read(&iout, isize, 1);
  569. ogreFace.mIndices[ii] = static_cast<unsigned int>(iout);
  570. }
  571. }
  572. // Destination Assimp face
  573. aiFace &face = dest->mFaces[fi];
  574. face.mNumIndices = 3;
  575. face.mIndices = new unsigned int[3];
  576. const size_t pos = fi * 3;
  577. for (size_t v=0; v<3; ++v)
  578. {
  579. const size_t newIndex = pos + v;
  580. // Write face index
  581. face.mIndices[v] = newIndex;
  582. // Ogres vertex index to ref into the source buffers.
  583. const size_t ogreVertexIndex = ogreFace.mIndices[v];
  584. src->AddVertexMapping(ogreVertexIndex, newIndex);
  585. // Position
  586. positions->Seek((vWidthPosition * ogreVertexIndex) + positionsElement->offset, aiOrigin_SET);
  587. positions->Read(&dest->mVertices[newIndex], sizePosition, 1);
  588. // Normal
  589. if (normals)
  590. {
  591. normals->Seek((vWidthNormal * ogreVertexIndex) + normalsElement->offset, aiOrigin_SET);
  592. normals->Read(&dest->mNormals[newIndex], sizeNormal, 1);
  593. }
  594. // UV0
  595. if (uv1 && uv1Dest)
  596. {
  597. uv1->Seek((vWidthUv1 * ogreVertexIndex) + uv1Element->offset, aiOrigin_SET);
  598. uv1->Read(&uv1Dest[newIndex], sizeUv1, 1);
  599. uv1Dest[newIndex].y = (uv1Dest[newIndex].y * -1) + 1; // Flip UV from Ogre to Assimp form
  600. }
  601. // UV1
  602. if (uv2 && uv2Dest)
  603. {
  604. uv2->Seek((vWidthUv2 * ogreVertexIndex) + uv2Element->offset, aiOrigin_SET);
  605. uv2->Read(&uv2Dest[newIndex], sizeUv2, 1);
  606. uv2Dest[newIndex].y = (uv2Dest[newIndex].y * -1) + 1; // Flip UV from Ogre to Assimp form
  607. }
  608. }
  609. }
  610. // Bones and bone weights
  611. if (parent->skeleton && boneAssignments)
  612. {
  613. AssimpVertexBoneWeightList weights = src->AssimpBoneWeights(dest->mNumVertices);
  614. std::set<uint16_t> referencedBones = src->ReferencedBonesByWeights();
  615. dest->mNumBones = referencedBones.size();
  616. dest->mBones = new aiBone*[dest->mNumBones];
  617. size_t assimpBoneIndex = 0;
  618. for(std::set<uint16_t>::const_iterator rbIter=referencedBones.begin(), rbEnd=referencedBones.end(); rbIter != rbEnd; ++rbIter, ++assimpBoneIndex)
  619. {
  620. Bone *bone = parent->skeleton->BoneById((*rbIter));
  621. dest->mBones[assimpBoneIndex] = bone->ConvertToAssimpBone(parent->skeleton, weights[bone->id]);
  622. }
  623. }
  624. return dest;
  625. }
  626. // MeshXml
  627. MeshXml::MeshXml() :
  628. skeleton(0),
  629. sharedVertexData(0)
  630. {
  631. }
  632. MeshXml::~MeshXml()
  633. {
  634. Reset();
  635. }
  636. void MeshXml::Reset()
  637. {
  638. OGRE_SAFE_DELETE(skeleton)
  639. OGRE_SAFE_DELETE(sharedVertexData)
  640. for(size_t i=0, len=subMeshes.size(); i<len; ++i) {
  641. OGRE_SAFE_DELETE(subMeshes[i])
  642. }
  643. subMeshes.clear();
  644. }
  645. size_t MeshXml::NumSubMeshes() const
  646. {
  647. return subMeshes.size();
  648. }
  649. SubMeshXml *MeshXml::GetSubMesh(uint16_t index) const
  650. {
  651. for(size_t i=0; i<subMeshes.size(); ++i)
  652. if (subMeshes[i]->index == index)
  653. return subMeshes[i];
  654. return 0;
  655. }
  656. void MeshXml::ConvertToAssimpScene(aiScene* dest)
  657. {
  658. // Setup
  659. dest->mNumMeshes = NumSubMeshes();
  660. dest->mMeshes = new aiMesh*[dest->mNumMeshes];
  661. // Create root node
  662. dest->mRootNode = new aiNode();
  663. dest->mRootNode->mNumMeshes = dest->mNumMeshes;
  664. dest->mRootNode->mMeshes = new unsigned int[dest->mRootNode->mNumMeshes];
  665. // Export meshes
  666. for(size_t i=0; i<dest->mNumMeshes; ++i)
  667. {
  668. dest->mMeshes[i] = subMeshes[i]->ConvertToAssimpMesh(this);
  669. dest->mRootNode->mMeshes[i] = i;
  670. }
  671. // Export skeleton
  672. if (skeleton)
  673. {
  674. // Bones
  675. if (!skeleton->bones.empty())
  676. {
  677. BoneList rootBones = skeleton->RootBones();
  678. dest->mRootNode->mNumChildren = rootBones.size();
  679. dest->mRootNode->mChildren = new aiNode*[dest->mRootNode->mNumChildren];
  680. for(size_t i=0, len=rootBones.size(); i<len; ++i)
  681. {
  682. dest->mRootNode->mChildren[i] = rootBones[i]->ConvertToAssimpNode(skeleton, dest->mRootNode);
  683. }
  684. }
  685. // Animations
  686. if (!skeleton->animations.empty())
  687. {
  688. dest->mNumAnimations = skeleton->animations.size();
  689. dest->mAnimations = new aiAnimation*[dest->mNumAnimations];
  690. for(size_t i=0, len=skeleton->animations.size(); i<len; ++i)
  691. {
  692. dest->mAnimations[i] = skeleton->animations[i]->ConvertToAssimpAnimation();
  693. }
  694. }
  695. }
  696. }
  697. // SubMeshXml
  698. SubMeshXml::SubMeshXml() :
  699. indexData(new IndexDataXml()),
  700. vertexData(0)
  701. {
  702. }
  703. SubMeshXml::~SubMeshXml()
  704. {
  705. Reset();
  706. }
  707. void SubMeshXml::Reset()
  708. {
  709. OGRE_SAFE_DELETE(indexData)
  710. OGRE_SAFE_DELETE(vertexData)
  711. }
  712. aiMesh *SubMeshXml::ConvertToAssimpMesh(MeshXml *parent)
  713. {
  714. aiMesh *dest = new aiMesh();
  715. dest->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  716. if (!name.empty())
  717. dest->mName = name;
  718. // Material index
  719. if (materialIndex != -1)
  720. dest->mMaterialIndex = materialIndex;
  721. // Faces
  722. dest->mNumFaces = indexData->faceCount;
  723. dest->mFaces = new aiFace[dest->mNumFaces];
  724. // Assimp required unique vertices, we need to convert from Ogres shared indexing.
  725. size_t uniqueVertexCount = dest->mNumFaces * 3;
  726. dest->mNumVertices = uniqueVertexCount;
  727. dest->mVertices = new aiVector3D[dest->mNumVertices];
  728. VertexDataXml *src = (!usesSharedVertexData ? vertexData : parent->sharedVertexData);
  729. bool boneAssignments = src->HasBoneAssignments();
  730. bool normals = src->HasNormals();
  731. size_t uvs = src->NumUvs();
  732. // Prepare normals
  733. if (normals)
  734. dest->mNormals = new aiVector3D[dest->mNumVertices];
  735. // Prepare UVs
  736. for(size_t uvi=0; uvi<uvs; ++uvi)
  737. {
  738. dest->mNumUVComponents[uvi] = 2;
  739. dest->mTextureCoords[uvi] = new aiVector3D[dest->mNumVertices];
  740. }
  741. for (size_t fi=0; fi<dest->mNumFaces; ++fi)
  742. {
  743. // Source Ogre face
  744. aiFace &ogreFace = indexData->faces[fi];
  745. // Destination Assimp face
  746. aiFace &face = dest->mFaces[fi];
  747. face.mNumIndices = 3;
  748. face.mIndices = new unsigned int[3];
  749. const size_t pos = fi * 3;
  750. for (size_t v=0; v<3; ++v)
  751. {
  752. const size_t newIndex = pos + v;
  753. // Write face index
  754. face.mIndices[v] = newIndex;
  755. // Ogres vertex index to ref into the source buffers.
  756. const size_t ogreVertexIndex = ogreFace.mIndices[v];
  757. src->AddVertexMapping(ogreVertexIndex, newIndex);
  758. // Position
  759. dest->mVertices[newIndex] = src->positions[ogreVertexIndex];
  760. // Normal
  761. if (normals)
  762. dest->mNormals[newIndex] = src->normals[ogreVertexIndex];
  763. // UVs
  764. for(size_t uvi=0; uvi<uvs; ++uvi)
  765. {
  766. aiVector3D *uvDest = dest->mTextureCoords[uvi];
  767. std::vector<aiVector3D> &uvSrc = src->uvs[uvi];
  768. uvDest[newIndex] = uvSrc[ogreVertexIndex];
  769. }
  770. }
  771. }
  772. // Bones and bone weights
  773. if (parent->skeleton && boneAssignments)
  774. {
  775. AssimpVertexBoneWeightList weights = src->AssimpBoneWeights(dest->mNumVertices);
  776. std::set<uint16_t> referencedBones = src->ReferencedBonesByWeights();
  777. dest->mNumBones = referencedBones.size();
  778. dest->mBones = new aiBone*[dest->mNumBones];
  779. size_t assimpBoneIndex = 0;
  780. for(std::set<uint16_t>::const_iterator rbIter=referencedBones.begin(), rbEnd=referencedBones.end(); rbIter != rbEnd; ++rbIter, ++assimpBoneIndex)
  781. {
  782. Bone *bone = parent->skeleton->BoneById((*rbIter));
  783. dest->mBones[assimpBoneIndex] = bone->ConvertToAssimpBone(parent->skeleton, weights[bone->id]);
  784. }
  785. }
  786. return dest;
  787. }
  788. // Animation
  789. Animation::Animation(Skeleton *parent) :
  790. parentMesh(NULL),
  791. parentSkeleton(parent),
  792. length(0.0f),
  793. baseTime(-1.0f)
  794. {
  795. }
  796. Animation::Animation(Mesh *parent) :
  797. parentMesh(parent),
  798. parentSkeleton(0),
  799. length(0.0f),
  800. baseTime(-1.0f)
  801. {
  802. }
  803. VertexData *Animation::AssociatedVertexData(VertexAnimationTrack *track) const
  804. {
  805. if (!parentMesh)
  806. return 0;
  807. bool sharedGeom = (track->target == 0);
  808. if (sharedGeom)
  809. return parentMesh->sharedVertexData;
  810. else
  811. return parentMesh->GetSubMesh(track->target-1)->vertexData;
  812. }
  813. aiAnimation *Animation::ConvertToAssimpAnimation()
  814. {
  815. aiAnimation *anim = new aiAnimation();
  816. anim->mName = name;
  817. anim->mDuration = static_cast<double>(length);
  818. anim->mTicksPerSecond = 1.0;
  819. // Tracks
  820. if (!tracks.empty())
  821. {
  822. anim->mNumChannels = tracks.size();
  823. anim->mChannels = new aiNodeAnim*[anim->mNumChannels];
  824. for(size_t i=0, len=tracks.size(); i<len; ++i)
  825. {
  826. anim->mChannels[i] = tracks[i].ConvertToAssimpAnimationNode(parentSkeleton);
  827. }
  828. }
  829. return anim;
  830. }
  831. // Skeleton
  832. Skeleton::Skeleton() :
  833. bones(),
  834. animations(),
  835. blendMode(ANIMBLEND_AVERAGE)
  836. {
  837. }
  838. Skeleton::~Skeleton()
  839. {
  840. Reset();
  841. }
  842. void Skeleton::Reset()
  843. {
  844. for(size_t i=0, len=bones.size(); i<len; ++i) {
  845. OGRE_SAFE_DELETE(bones[i])
  846. }
  847. bones.clear();
  848. for(size_t i=0, len=animations.size(); i<len; ++i) {
  849. OGRE_SAFE_DELETE(animations[i])
  850. }
  851. animations.clear();
  852. }
  853. BoneList Skeleton::RootBones() const
  854. {
  855. BoneList rootBones;
  856. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  857. {
  858. if (!(*iter)->IsParented())
  859. rootBones.push_back((*iter));
  860. }
  861. return rootBones;
  862. }
  863. size_t Skeleton::NumRootBones() const
  864. {
  865. size_t num = 0;
  866. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  867. {
  868. if (!(*iter)->IsParented())
  869. num++;
  870. }
  871. return num;
  872. }
  873. Bone *Skeleton::BoneByName(const std::string &name) const
  874. {
  875. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  876. {
  877. if ((*iter)->name == name)
  878. return (*iter);
  879. }
  880. return 0;
  881. }
  882. Bone *Skeleton::BoneById(uint16_t id) const
  883. {
  884. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  885. {
  886. if ((*iter)->id == id)
  887. return (*iter);
  888. }
  889. return 0;
  890. }
  891. // Bone
  892. Bone::Bone() :
  893. id(0),
  894. parent(0),
  895. parentId(-1),
  896. scale(1.0f, 1.0f, 1.0f)
  897. {
  898. }
  899. bool Bone::IsParented() const
  900. {
  901. return (parentId != -1 && parent != 0);
  902. }
  903. uint16_t Bone::ParentId() const
  904. {
  905. return static_cast<uint16_t>(parentId);
  906. }
  907. void Bone::AddChild(Bone *bone)
  908. {
  909. if (!bone)
  910. return;
  911. if (bone->IsParented())
  912. throw DeadlyImportError("Attaching child Bone that is already parented: " + bone->name);
  913. bone->parent = this;
  914. bone->parentId = id;
  915. children.push_back(bone->id);
  916. }
  917. void Bone::CalculateWorldMatrixAndDefaultPose(Skeleton *skeleton)
  918. {
  919. if (!IsParented())
  920. worldMatrix = aiMatrix4x4(scale, rotation, position).Inverse();
  921. else
  922. worldMatrix = aiMatrix4x4(scale, rotation, position).Inverse() * parent->worldMatrix;
  923. defaultPose = aiMatrix4x4(scale, rotation, position);
  924. // Recursively for all children now that the parent matrix has been calculated.
  925. for (size_t i=0, len=children.size(); i<len; ++i)
  926. {
  927. Bone *child = skeleton->BoneById(children[i]);
  928. if (!child) {
  929. throw DeadlyImportError(Formatter::format() << "CalculateWorldMatrixAndDefaultPose: Failed to find child bone " << children[i] << " for parent " << id << " " << name);
  930. }
  931. child->CalculateWorldMatrixAndDefaultPose(skeleton);
  932. }
  933. }
  934. aiNode *Bone::ConvertToAssimpNode(Skeleton *skeleton, aiNode *parentNode)
  935. {
  936. // Bone node
  937. aiNode* node = new aiNode(name);
  938. node->mParent = parentNode;
  939. node->mTransformation = defaultPose;
  940. // Children
  941. if (!children.empty())
  942. {
  943. node->mNumChildren = children.size();
  944. node->mChildren = new aiNode*[node->mNumChildren];
  945. for(size_t i=0, len=children.size(); i<len; ++i)
  946. {
  947. Bone *child = skeleton->BoneById(children[i]);
  948. if (!child) {
  949. throw DeadlyImportError(Formatter::format() << "ConvertToAssimpNode: Failed to find child bone " << children[i] << " for parent " << id << " " << name);
  950. }
  951. node->mChildren[i] = child->ConvertToAssimpNode(skeleton, node);
  952. }
  953. }
  954. return node;
  955. }
  956. aiBone *Bone::ConvertToAssimpBone(Skeleton * /*parent*/, const std::vector<aiVertexWeight> &boneWeights)
  957. {
  958. aiBone *bone = new aiBone();
  959. bone->mName = name;
  960. bone->mOffsetMatrix = worldMatrix;
  961. if (!boneWeights.empty())
  962. {
  963. bone->mNumWeights = boneWeights.size();
  964. bone->mWeights = new aiVertexWeight[boneWeights.size()];
  965. memcpy(bone->mWeights, &boneWeights[0], boneWeights.size() * sizeof(aiVertexWeight));
  966. }
  967. return bone;
  968. }
  969. // VertexAnimationTrack
  970. VertexAnimationTrack::VertexAnimationTrack() :
  971. type(VAT_NONE),
  972. target(0)
  973. {
  974. }
  975. aiNodeAnim *VertexAnimationTrack::ConvertToAssimpAnimationNode(Skeleton *skeleton)
  976. {
  977. if (boneName.empty() || type != VAT_TRANSFORM) {
  978. throw DeadlyImportError("VertexAnimationTrack::ConvertToAssimpAnimationNode: Cannot convert track that has no target bone name or is not type of VAT_TRANSFORM");
  979. }
  980. aiNodeAnim *nodeAnim = new aiNodeAnim();
  981. nodeAnim->mNodeName = boneName;
  982. Bone *bone = skeleton->BoneByName(boneName);
  983. if (!bone) {
  984. throw DeadlyImportError("VertexAnimationTrack::ConvertToAssimpAnimationNode: Failed to find bone " + boneName + " from parent Skeleton");
  985. }
  986. // Keyframes
  987. size_t numKeyframes = transformKeyFrames.size();
  988. nodeAnim->mPositionKeys = new aiVectorKey[numKeyframes];
  989. nodeAnim->mRotationKeys = new aiQuatKey[numKeyframes];
  990. nodeAnim->mScalingKeys = new aiVectorKey[numKeyframes];
  991. nodeAnim->mNumPositionKeys = numKeyframes;
  992. nodeAnim->mNumRotationKeys = numKeyframes;
  993. nodeAnim->mNumScalingKeys = numKeyframes;
  994. for(size_t kfi=0; kfi<numKeyframes; ++kfi)
  995. {
  996. TransformKeyFrame &kfSource = transformKeyFrames[kfi];
  997. // Calculate the complete transformation from world space to bone space
  998. aiVector3D pos; aiQuaternion rot; aiVector3D scale;
  999. aiMatrix4x4 finalTransform = bone->defaultPose * kfSource.Transform();
  1000. finalTransform.Decompose(scale, rot, pos);
  1001. double t = static_cast<double>(kfSource.timePos);
  1002. nodeAnim->mPositionKeys[kfi].mTime = t;
  1003. nodeAnim->mRotationKeys[kfi].mTime = t;
  1004. nodeAnim->mScalingKeys[kfi].mTime = t;
  1005. nodeAnim->mPositionKeys[kfi].mValue = pos;
  1006. nodeAnim->mRotationKeys[kfi].mValue = rot;
  1007. nodeAnim->mScalingKeys[kfi].mValue = scale;
  1008. }
  1009. return nodeAnim;
  1010. }
  1011. // TransformKeyFrame
  1012. TransformKeyFrame::TransformKeyFrame() :
  1013. timePos(0.0f),
  1014. scale(1.0f, 1.0f, 1.0f)
  1015. {
  1016. }
  1017. aiMatrix4x4 TransformKeyFrame::Transform()
  1018. {
  1019. return aiMatrix4x4(scale, rotation, position);
  1020. }
  1021. } // Ogre
  1022. } // Assimp
  1023. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER