OgreStructs.cpp 29 KB

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