OgreStructs.cpp 33 KB

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