OgreStructs.cpp 29 KB

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