OgreStructs.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. }
  407. // ISubMesh
  408. ISubMesh::ISubMesh() :
  409. index(0),
  410. materialIndex(-1),
  411. usesSharedVertexData(false),
  412. operationType(OT_POINT_LIST)
  413. {
  414. }
  415. // SubMesh
  416. SubMesh::SubMesh() :
  417. vertexData(0),
  418. indexData(new IndexData())
  419. {
  420. }
  421. SubMesh::~SubMesh()
  422. {
  423. Reset();
  424. }
  425. void SubMesh::Reset()
  426. {
  427. OGRE_SAFE_DELETE(vertexData)
  428. OGRE_SAFE_DELETE(indexData)
  429. }
  430. aiMesh *SubMesh::ConvertToAssimpMesh(Mesh *parent)
  431. {
  432. if (operationType != OT_TRIANGLE_LIST) {
  433. throw DeadlyImportError(Formatter::format() << "Only mesh operation type OT_TRIANGLE_LIST is supported. Found " << operationType);
  434. }
  435. aiMesh *dest = new aiMesh();
  436. dest->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  437. if (!name.empty())
  438. dest->mName = name;
  439. // Material index
  440. if (materialIndex != -1)
  441. dest->mMaterialIndex = materialIndex;
  442. // Pick source vertex data from shader geometry or from internal geometry.
  443. VertexData *src = (!usesSharedVertexData ? vertexData : parent->sharedVertexData);
  444. VertexElement *positionsElement = src->GetVertexElement(VertexElement::VES_POSITION);
  445. VertexElement *normalsElement = src->GetVertexElement(VertexElement::VES_NORMAL);
  446. VertexElement *uv1Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 0);
  447. VertexElement *uv2Element = src->GetVertexElement(VertexElement::VES_TEXTURE_COORDINATES, 1);
  448. // Sanity checks
  449. if (!positionsElement) {
  450. throw DeadlyImportError("Failed to import Ogre VertexElement::VES_POSITION. Mesh does not have vertex positions!");
  451. } else if (positionsElement->type != VertexElement::VET_FLOAT3) {
  452. throw DeadlyImportError("Ogre Mesh position vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  453. } else if (normalsElement && normalsElement->type != VertexElement::VET_FLOAT3) {
  454. throw DeadlyImportError("Ogre Mesh normal vertex element type != VertexElement::VET_FLOAT3. This is not supported.");
  455. }
  456. // Faces
  457. dest->mNumFaces = indexData->faceCount;
  458. dest->mFaces = new aiFace[dest->mNumFaces];
  459. // Assimp required unique vertices, we need to convert from Ogres shared indexing.
  460. size_t uniqueVertexCount = dest->mNumFaces * 3;
  461. dest->mNumVertices = uniqueVertexCount;
  462. dest->mVertices = new aiVector3D[dest->mNumVertices];
  463. // Source streams
  464. MemoryStream *positions = src->VertexBuffer(positionsElement->source);
  465. MemoryStream *normals = (normalsElement ? src->VertexBuffer(normalsElement->source) : 0);
  466. MemoryStream *uv1 = (uv1Element ? src->VertexBuffer(uv1Element->source) : 0);
  467. MemoryStream *uv2 = (uv2Element ? src->VertexBuffer(uv2Element->source) : 0);
  468. // Element size
  469. const size_t sizePosition = positionsElement->Size();
  470. const size_t sizeNormal = (normalsElement ? normalsElement->Size() : 0);
  471. const size_t sizeUv1 = (uv1Element ? uv1Element->Size() : 0);
  472. const size_t sizeUv2 = (uv2Element ? uv2Element->Size() : 0);
  473. // Vertex width
  474. const size_t vWidthPosition = src->VertexSize(positionsElement->source);
  475. const size_t vWidthNormal = (normalsElement ? src->VertexSize(normalsElement->source) : 0);
  476. const size_t vWidthUv1 = (uv1Element ? src->VertexSize(uv1Element->source) : 0);
  477. const size_t vWidthUv2 = (uv2Element ? src->VertexSize(uv2Element->source) : 0);
  478. bool boneAssignments = src->HasBoneAssignments();
  479. // Prepare normals
  480. if (normals)
  481. dest->mNormals = new aiVector3D[dest->mNumVertices];
  482. // Prepare UVs, ignoring incompatible UVs.
  483. if (uv1)
  484. {
  485. if (uv1Element->type == VertexElement::VET_FLOAT2 || uv1Element->type == VertexElement::VET_FLOAT3)
  486. {
  487. dest->mNumUVComponents[0] = uv1Element->ComponentCount();
  488. dest->mTextureCoords[0] = new aiVector3D[dest->mNumVertices];
  489. }
  490. else
  491. {
  492. DefaultLogger::get()->warn(Formatter::format() << "Ogre imported UV0 type " << uv1Element->TypeToString() << " is not compatible with Assimp. Ignoring UV.");
  493. uv1 = 0;
  494. }
  495. }
  496. if (uv2)
  497. {
  498. if (uv2Element->type == VertexElement::VET_FLOAT2 || uv2Element->type == VertexElement::VET_FLOAT3)
  499. {
  500. dest->mNumUVComponents[1] = uv2Element->ComponentCount();
  501. dest->mTextureCoords[1] = new aiVector3D[dest->mNumVertices];
  502. }
  503. else
  504. {
  505. DefaultLogger::get()->warn(Formatter::format() << "Ogre imported UV0 type " << uv2Element->TypeToString() << " is not compatible with Assimp. Ignoring UV.");
  506. uv2 = 0;
  507. }
  508. }
  509. aiVector3D *uv1Dest = (uv1 ? dest->mTextureCoords[0] : 0);
  510. aiVector3D *uv2Dest = (uv2 ? dest->mTextureCoords[1] : 0);
  511. MemoryStream *faces = indexData->buffer.get();
  512. for (size_t fi=0, isize=indexData->IndexSize(), fsize=indexData->FaceSize();
  513. fi<dest->mNumFaces; ++fi)
  514. {
  515. // Source Ogre face
  516. aiFace ogreFace;
  517. ogreFace.mNumIndices = 3;
  518. ogreFace.mIndices = new unsigned int[3];
  519. faces->Seek(fi * fsize, aiOrigin_SET);
  520. if (indexData->is32bit)
  521. {
  522. faces->Read(&ogreFace.mIndices[0], isize, 3);
  523. }
  524. else
  525. {
  526. uint16_t iout = 0;
  527. for (size_t ii=0; ii<3; ++ii)
  528. {
  529. faces->Read(&iout, isize, 1);
  530. ogreFace.mIndices[ii] = static_cast<unsigned int>(iout);
  531. }
  532. }
  533. // Destination Assimp face
  534. aiFace &face = dest->mFaces[fi];
  535. face.mNumIndices = 3;
  536. face.mIndices = new unsigned int[3];
  537. const size_t pos = fi * 3;
  538. for (size_t v=0; v<3; ++v)
  539. {
  540. const size_t newIndex = pos + v;
  541. // Write face index
  542. face.mIndices[v] = newIndex;
  543. // Ogres vertex index to ref into the source buffers.
  544. const size_t ogreVertexIndex = ogreFace.mIndices[v];
  545. src->AddVertexMapping(ogreVertexIndex, newIndex);
  546. // Position
  547. positions->Seek((vWidthPosition * ogreVertexIndex) + positionsElement->offset, aiOrigin_SET);
  548. positions->Read(&dest->mVertices[newIndex], sizePosition, 1);
  549. // Normal
  550. if (normals)
  551. {
  552. normals->Seek((vWidthNormal * ogreVertexIndex) + normalsElement->offset, aiOrigin_SET);
  553. normals->Read(&dest->mNormals[newIndex], sizeNormal, 1);
  554. }
  555. // UV0
  556. if (uv1 && uv1Dest)
  557. {
  558. uv1->Seek((vWidthUv1 * ogreVertexIndex) + uv1Element->offset, aiOrigin_SET);
  559. uv1->Read(&uv1Dest[newIndex], sizeUv1, 1);
  560. }
  561. // UV1
  562. if (uv2 && uv2Dest)
  563. {
  564. uv2->Seek((vWidthUv2 * ogreVertexIndex) + uv2Element->offset, aiOrigin_SET);
  565. uv2->Read(&uv2Dest[newIndex], sizeUv2, 1);
  566. }
  567. }
  568. }
  569. // Bones and bone weights
  570. if (parent->skeleton && boneAssignments)
  571. {
  572. AssimpVertexBoneWeightList weights = src->AssimpBoneWeights(dest->mNumVertices);
  573. std::set<uint16_t> referencedBones = src->ReferencedBonesByWeights();
  574. dest->mNumBones = referencedBones.size();
  575. dest->mBones = new aiBone*[dest->mNumBones];
  576. size_t assimpBoneIndex = 0;
  577. for(std::set<uint16_t>::const_iterator rbIter=referencedBones.begin(), rbEnd=referencedBones.end(); rbIter != rbEnd; ++rbIter, ++assimpBoneIndex)
  578. {
  579. Bone *bone = parent->skeleton->BoneById((*rbIter));
  580. dest->mBones[assimpBoneIndex] = bone->ConvertToAssimpBone(parent->skeleton, weights[bone->id]);
  581. }
  582. }
  583. return dest;
  584. }
  585. // MeshXml
  586. MeshXml::MeshXml() :
  587. sharedVertexData(0),
  588. skeleton(0)
  589. {
  590. }
  591. MeshXml::~MeshXml()
  592. {
  593. Reset();
  594. }
  595. void MeshXml::Reset()
  596. {
  597. OGRE_SAFE_DELETE(skeleton)
  598. OGRE_SAFE_DELETE(sharedVertexData)
  599. for(size_t i=0, len=subMeshes.size(); i<len; ++i) {
  600. OGRE_SAFE_DELETE(subMeshes[i])
  601. }
  602. subMeshes.clear();
  603. }
  604. size_t MeshXml::NumSubMeshes() const
  605. {
  606. return subMeshes.size();
  607. }
  608. SubMeshXml *MeshXml::GetSubMesh(uint16_t index) const
  609. {
  610. for(size_t i=0; i<subMeshes.size(); ++i)
  611. if (subMeshes[i]->index == index)
  612. return subMeshes[i];
  613. return 0;
  614. }
  615. void MeshXml::ConvertToAssimpScene(aiScene* dest)
  616. {
  617. // Setup
  618. dest->mNumMeshes = NumSubMeshes();
  619. dest->mMeshes = new aiMesh*[dest->mNumMeshes];
  620. // Create root node
  621. dest->mRootNode = new aiNode();
  622. dest->mRootNode->mNumMeshes = dest->mNumMeshes;
  623. dest->mRootNode->mMeshes = new unsigned int[dest->mRootNode->mNumMeshes];
  624. // Export meshes
  625. for(size_t i=0; i<dest->mNumMeshes; ++i)
  626. {
  627. dest->mMeshes[i] = subMeshes[i]->ConvertToAssimpMesh(this);
  628. dest->mRootNode->mMeshes[i] = i;
  629. }
  630. // Export skeleton
  631. if (skeleton)
  632. {
  633. // Bones
  634. if (!skeleton->bones.empty())
  635. {
  636. BoneList rootBones = skeleton->RootBones();
  637. dest->mRootNode->mNumChildren = rootBones.size();
  638. dest->mRootNode->mChildren = new aiNode*[dest->mRootNode->mNumChildren];
  639. for(size_t i=0, len=rootBones.size(); i<len; ++i)
  640. {
  641. dest->mRootNode->mChildren[i] = rootBones[i]->ConvertToAssimpNode(skeleton, dest->mRootNode);
  642. }
  643. }
  644. // Animations
  645. if (!skeleton->animations.empty())
  646. {
  647. dest->mNumAnimations = skeleton->animations.size();
  648. dest->mAnimations = new aiAnimation*[dest->mNumAnimations];
  649. for(size_t i=0, len=skeleton->animations.size(); i<len; ++i)
  650. {
  651. dest->mAnimations[i] = skeleton->animations[i]->ConvertToAssimpAnimation();
  652. }
  653. }
  654. }
  655. }
  656. // SubMeshXml
  657. SubMeshXml::SubMeshXml() :
  658. vertexData(0),
  659. indexData(new IndexDataXml())
  660. {
  661. }
  662. SubMeshXml::~SubMeshXml()
  663. {
  664. Reset();
  665. }
  666. void SubMeshXml::Reset()
  667. {
  668. OGRE_SAFE_DELETE(indexData)
  669. OGRE_SAFE_DELETE(vertexData)
  670. }
  671. aiMesh *SubMeshXml::ConvertToAssimpMesh(MeshXml *parent)
  672. {
  673. aiMesh *dest = new aiMesh();
  674. dest->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  675. if (!name.empty())
  676. dest->mName = name;
  677. // Material index
  678. if (materialIndex != -1)
  679. dest->mMaterialIndex = materialIndex;
  680. // Faces
  681. dest->mNumFaces = indexData->faceCount;
  682. dest->mFaces = new aiFace[dest->mNumFaces];
  683. // Assimp required unique vertices, we need to convert from Ogres shared indexing.
  684. size_t uniqueVertexCount = dest->mNumFaces * 3;
  685. dest->mNumVertices = uniqueVertexCount;
  686. dest->mVertices = new aiVector3D[dest->mNumVertices];
  687. VertexDataXml *src = (!usesSharedVertexData ? vertexData : parent->sharedVertexData);
  688. bool boneAssignments = src->HasBoneAssignments();
  689. bool normals = src->HasNormals();
  690. size_t uvs = src->NumUvs();
  691. // Prepare normals
  692. if (normals)
  693. dest->mNormals = new aiVector3D[dest->mNumVertices];
  694. // Prepare UVs
  695. for(size_t uvi=0; uvi<uvs; ++uvi)
  696. {
  697. dest->mNumUVComponents[uvi] = 2;
  698. dest->mTextureCoords[uvi] = new aiVector3D[dest->mNumVertices];
  699. }
  700. for (size_t fi=0; fi<dest->mNumFaces; ++fi)
  701. {
  702. // Source Ogre face
  703. aiFace &ogreFace = indexData->faces[fi];
  704. // Destination Assimp face
  705. aiFace &face = dest->mFaces[fi];
  706. face.mNumIndices = 3;
  707. face.mIndices = new unsigned int[3];
  708. const size_t pos = fi * 3;
  709. for (size_t v=0; v<3; ++v)
  710. {
  711. const size_t newIndex = pos + v;
  712. // Write face index
  713. face.mIndices[v] = newIndex;
  714. // Ogres vertex index to ref into the source buffers.
  715. const size_t ogreVertexIndex = ogreFace.mIndices[v];
  716. src->AddVertexMapping(ogreVertexIndex, newIndex);
  717. // Position
  718. dest->mVertices[newIndex] = src->positions[ogreVertexIndex];
  719. // Normal
  720. if (normals)
  721. dest->mNormals[newIndex] = src->normals[ogreVertexIndex];
  722. // UVs
  723. for(size_t uvi=0; uvi<uvs; ++uvi)
  724. {
  725. aiVector3D *uvDest = dest->mTextureCoords[uvi];
  726. std::vector<aiVector3D> &uvSrc = src->uvs[uvi];
  727. uvDest[newIndex] = uvSrc[ogreVertexIndex];
  728. }
  729. }
  730. }
  731. // Bones and bone weights
  732. if (parent->skeleton && boneAssignments)
  733. {
  734. AssimpVertexBoneWeightList weights = src->AssimpBoneWeights(dest->mNumVertices);
  735. std::set<uint16_t> referencedBones = src->ReferencedBonesByWeights();
  736. dest->mNumBones = referencedBones.size();
  737. dest->mBones = new aiBone*[dest->mNumBones];
  738. size_t assimpBoneIndex = 0;
  739. for(std::set<uint16_t>::const_iterator rbIter=referencedBones.begin(), rbEnd=referencedBones.end(); rbIter != rbEnd; ++rbIter, ++assimpBoneIndex)
  740. {
  741. Bone *bone = parent->skeleton->BoneById((*rbIter));
  742. dest->mBones[assimpBoneIndex] = bone->ConvertToAssimpBone(parent->skeleton, weights[bone->id]);
  743. }
  744. }
  745. return dest;
  746. }
  747. // Animation
  748. Animation::Animation(Skeleton *parent) :
  749. parentSkeleton(parent),
  750. parentMesh(0),
  751. length(0.0f),
  752. baseTime(-1.0f)
  753. {
  754. }
  755. Animation::Animation(Mesh *parent) :
  756. parentMesh(parent),
  757. parentSkeleton(0),
  758. length(0.0f),
  759. baseTime(-1.0f)
  760. {
  761. }
  762. VertexData *Animation::AssociatedVertexData(VertexAnimationTrack *track) const
  763. {
  764. if (!parentMesh)
  765. return 0;
  766. bool sharedGeom = (track->target == 0);
  767. if (sharedGeom)
  768. return parentMesh->sharedVertexData;
  769. else
  770. return parentMesh->GetSubMesh(track->target-1)->vertexData;
  771. }
  772. aiAnimation *Animation::ConvertToAssimpAnimation()
  773. {
  774. aiAnimation *anim = new aiAnimation();
  775. anim->mName = name;
  776. anim->mDuration = static_cast<double>(length);
  777. anim->mTicksPerSecond = 1.0;
  778. // Tracks
  779. if (!tracks.empty())
  780. {
  781. anim->mNumChannels = tracks.size();
  782. anim->mChannels = new aiNodeAnim*[anim->mNumChannels];
  783. for(size_t i=0, len=tracks.size(); i<len; ++i)
  784. {
  785. anim->mChannels[i] = tracks[i].ConvertToAssimpAnimationNode(parentSkeleton);
  786. }
  787. }
  788. return anim;
  789. }
  790. // Skeleton
  791. Skeleton::Skeleton()
  792. {
  793. }
  794. Skeleton::~Skeleton()
  795. {
  796. Reset();
  797. }
  798. void Skeleton::Reset()
  799. {
  800. for(size_t i=0, len=bones.size(); i<len; ++i) {
  801. OGRE_SAFE_DELETE(bones[i])
  802. }
  803. bones.clear();
  804. for(size_t i=0, len=animations.size(); i<len; ++i) {
  805. OGRE_SAFE_DELETE(animations[i])
  806. }
  807. animations.clear();
  808. }
  809. BoneList Skeleton::RootBones() const
  810. {
  811. BoneList rootBones;
  812. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  813. {
  814. if (!(*iter)->IsParented())
  815. rootBones.push_back((*iter));
  816. }
  817. return rootBones;
  818. }
  819. size_t Skeleton::NumRootBones() const
  820. {
  821. size_t num = 0;
  822. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  823. {
  824. if (!(*iter)->IsParented())
  825. num++;
  826. }
  827. return num;
  828. }
  829. Bone *Skeleton::BoneByName(const std::string &name) const
  830. {
  831. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  832. {
  833. if ((*iter)->name == name)
  834. return (*iter);
  835. }
  836. return 0;
  837. }
  838. Bone *Skeleton::BoneById(uint16_t id) const
  839. {
  840. for(BoneList::const_iterator iter = bones.begin(); iter != bones.end(); ++iter)
  841. {
  842. if ((*iter)->id == id)
  843. return (*iter);
  844. }
  845. return 0;
  846. }
  847. // Bone
  848. Bone::Bone() :
  849. id(0),
  850. parent(0),
  851. parentId(-1),
  852. rotationAngle(0.0f),
  853. scale(1.0f, 1.0f, 1.0f)
  854. {
  855. }
  856. bool Bone::IsParented() const
  857. {
  858. return (parentId != -1 && parent != 0);
  859. }
  860. uint16_t Bone::ParentId() const
  861. {
  862. return static_cast<uint16_t>(parentId);
  863. }
  864. void Bone::AddChild(Bone *bone)
  865. {
  866. if (!bone)
  867. return;
  868. if (bone->IsParented())
  869. throw DeadlyImportError("Attaching child Bone that is already parented: " + bone->name);
  870. bone->parent = this;
  871. bone->parentId = id;
  872. children.push_back(bone->id);
  873. }
  874. void Bone::CalculateWorldMatrixAndDefaultPose(Skeleton *skeleton)
  875. {
  876. aiMatrix4x4 t0, t1;
  877. aiMatrix4x4 transform = aiMatrix4x4::Rotation(-rotationAngle, rotation, t1) * aiMatrix4x4::Translation(-position, t0);
  878. if (!IsParented())
  879. worldMatrix = transform;
  880. else
  881. worldMatrix = transform * parent->worldMatrix;
  882. aiMatrix4x4 t2, t3; /// @todo t0 and t1 could probably be reused here?
  883. defaultPose = aiMatrix4x4::Translation(position, t2) * aiMatrix4x4::Rotation(rotationAngle, rotation, t3);
  884. // Recursively for all children now that the parent matrix has been calculated.
  885. for (size_t i=0, len=children.size(); i<len; ++i)
  886. {
  887. Bone *child = skeleton->BoneById(children[i]);
  888. if (!child) {
  889. throw DeadlyImportError(Formatter::format() << "CalculateWorldMatrixAndDefaultPose: Failed to find child bone " << children[i] << " for parent " << id << " " << name);
  890. }
  891. child->CalculateWorldMatrixAndDefaultPose(skeleton);
  892. }
  893. }
  894. aiNode *Bone::ConvertToAssimpNode(Skeleton *skeleton, aiNode *parentNode)
  895. {
  896. aiMatrix4x4 t0,t1;
  897. // Bone node
  898. aiNode* node = new aiNode(name);
  899. node->mParent = parentNode;
  900. node->mTransformation = defaultPose;
  901. // Children
  902. if (!children.empty())
  903. {
  904. node->mNumChildren = children.size();
  905. node->mChildren = new aiNode*[node->mNumChildren];
  906. for(size_t i=0, len=children.size(); i<len; ++i)
  907. {
  908. Bone *child = skeleton->BoneById(children[i]);
  909. if (!child) {
  910. throw DeadlyImportError(Formatter::format() << "ConvertToAssimpNode: Failed to find child bone " << children[i] << " for parent " << id << " " << name);
  911. }
  912. node->mChildren[i] = child->ConvertToAssimpNode(skeleton, node);
  913. }
  914. }
  915. return node;
  916. }
  917. aiBone *Bone::ConvertToAssimpBone(Skeleton *parent, const std::vector<aiVertexWeight> &boneWeights)
  918. {
  919. aiBone *bone = new aiBone();
  920. bone->mName = name;
  921. bone->mOffsetMatrix = worldMatrix;
  922. if (!boneWeights.empty())
  923. {
  924. bone->mNumWeights = boneWeights.size();
  925. bone->mWeights = new aiVertexWeight[boneWeights.size()];
  926. memcpy(bone->mWeights, &boneWeights[0], boneWeights.size() * sizeof(aiVertexWeight));
  927. }
  928. return bone;
  929. }
  930. // VertexAnimationTrack
  931. VertexAnimationTrack::VertexAnimationTrack() :
  932. target(0),
  933. type(VAT_NONE)
  934. {
  935. }
  936. aiNodeAnim *VertexAnimationTrack::ConvertToAssimpAnimationNode(Skeleton *skeleton)
  937. {
  938. if (boneName.empty() || type != VAT_TRANSFORM) {
  939. throw DeadlyImportError("VertexAnimationTrack::ConvertToAssimpAnimationNode: Cannot convert track that has no target bone name or is not type of VAT_TRANSFORM");
  940. }
  941. aiNodeAnim *nodeAnim = new aiNodeAnim();
  942. nodeAnim->mNodeName = boneName;
  943. Bone *bone = skeleton->BoneByName(boneName);
  944. if (!bone) {
  945. throw DeadlyImportError("VertexAnimationTrack::ConvertToAssimpAnimationNode: Failed to find bone " + boneName + " from parent Skeleton");
  946. }
  947. // Keyframes
  948. size_t numKeyframes = transformKeyFrames.size();
  949. nodeAnim->mPositionKeys = new aiVectorKey[numKeyframes];
  950. nodeAnim->mRotationKeys = new aiQuatKey[numKeyframes];
  951. nodeAnim->mScalingKeys = new aiVectorKey[numKeyframes];
  952. nodeAnim->mNumPositionKeys = numKeyframes;
  953. nodeAnim->mNumRotationKeys = numKeyframes;
  954. nodeAnim->mNumScalingKeys = numKeyframes;
  955. for(size_t kfi=0; kfi<numKeyframes; ++kfi)
  956. {
  957. const TransformKeyFrame &kfSource = transformKeyFrames[kfi];
  958. // Create a matrix to transform a vector from the bones
  959. // default pose to the bone bones in this animation key
  960. aiMatrix4x4 t0, t1;
  961. aiMatrix4x4 keyBonePose =
  962. aiMatrix4x4::Translation(kfSource.position, t0) *
  963. aiMatrix4x4(kfSource.rotation.GetMatrix()) *
  964. aiMatrix4x4::Scaling(kfSource.scale, t1);
  965. // Calculate the complete transformation from world space to bone space
  966. aiMatrix4x4 finalTransform = bone->defaultPose * keyBonePose;
  967. aiVector3D kfPos; aiQuaternion kfRot; aiVector3D kfScale;
  968. finalTransform.Decompose(kfScale, kfRot, kfPos);
  969. double t = static_cast<double>(kfSource.timePos);
  970. nodeAnim->mPositionKeys[kfi].mTime = t;
  971. nodeAnim->mRotationKeys[kfi].mTime = t;
  972. nodeAnim->mScalingKeys[kfi].mTime = t;
  973. nodeAnim->mPositionKeys[kfi].mValue = kfPos;
  974. nodeAnim->mRotationKeys[kfi].mValue = kfRot;
  975. nodeAnim->mScalingKeys[kfi].mValue = kfScale;
  976. }
  977. return nodeAnim;
  978. }
  979. } // Ogre
  980. } // Assimp
  981. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER