OgreMesh.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 "OgreImporter.h"
  36. #include "TinyFormatter.h"
  37. using namespace std;
  38. namespace Assimp
  39. {
  40. namespace Ogre
  41. {
  42. void OgreImporter::ReadSubMesh(const unsigned int submeshIndex, SubMesh &submesh, XmlReader *reader)
  43. {
  44. if (reader->getAttributeValue("material"))
  45. submesh.MaterialName = GetAttribute<string>(reader, "material");
  46. if (reader->getAttributeValue("use32bitindexes"))
  47. submesh.Use32bitIndexes = GetAttribute<bool>(reader, "use32bitindexes");
  48. if (reader->getAttributeValue("usesharedvertices"))
  49. submesh.UseSharedGeometry = GetAttribute<bool>(reader, "usesharedvertices");
  50. DefaultLogger::get()->debug(Formatter::format() << "Reading submesh " << submeshIndex);
  51. DefaultLogger::get()->debug(Formatter::format() << " - Material '" << submesh.MaterialName << "'");
  52. DefaultLogger::get()->debug(Formatter::format() << " - Shader geometry = " << (submesh.UseSharedGeometry ? "true" : "false") <<
  53. ", 32bit indexes = " << (submesh.Use32bitIndexes ? "true" : "false"));
  54. //TODO: maybe we have alsways just 1 faces and 1 geometry and always in this order. this loop will only work correct, when the order
  55. //of faces and geometry changed, and not if we have more than one of one
  56. /// @todo Fix above comment with better read logic below
  57. NextNode(reader);
  58. string currentNodeName = reader->getNodeName();
  59. const string nnFaces = "faces";
  60. const string nnFace = "face";
  61. const string nnGeometry = "geometry";
  62. const string nnBoneAssignments = "boneassignments";
  63. const string nnVertexBuffer = "vertexbuffer";
  64. bool quadWarned = false;
  65. while(currentNodeName == nnFaces ||
  66. currentNodeName == nnGeometry ||
  67. currentNodeName == nnBoneAssignments)
  68. {
  69. if (currentNodeName == nnFaces)
  70. {
  71. unsigned int numFaces = GetAttribute<unsigned int>(reader, "count");
  72. NextNode(reader);
  73. currentNodeName = reader->getNodeName();
  74. while(currentNodeName == nnFace)
  75. {
  76. Face NewFace;
  77. NewFace.VertexIndices[0] = GetAttribute<int>(reader, "v1");
  78. NewFace.VertexIndices[1] = GetAttribute<int>(reader, "v2");
  79. NewFace.VertexIndices[2] = GetAttribute<int>(reader, "v3");
  80. /// @todo Support quads
  81. if (!quadWarned && reader->getAttributeValue("v4"))
  82. DefaultLogger::get()->warn("Submesh has quads, only triangles are supported at the moment!");
  83. submesh.Faces.push_back(NewFace);
  84. // Advance
  85. NextNode(reader);
  86. currentNodeName = reader->getNodeName();
  87. }
  88. if (submesh.Faces.size() == numFaces)
  89. DefaultLogger::get()->debug(Formatter::format() << " - Faces " << numFaces);
  90. else
  91. throw DeadlyImportError(Formatter::format() << "Read only " << submesh.Faces.size() << " faces when should have read " << numFaces);
  92. }
  93. else if (currentNodeName == nnGeometry)
  94. {
  95. unsigned int numVertices = GetAttribute<int>(reader, "vertexcount");
  96. NextNode(reader);
  97. while(string(reader->getNodeName()) == nnVertexBuffer)
  98. ReadVertexBuffer(submesh, reader, numVertices);
  99. }
  100. else if (reader->getNodeName() == nnBoneAssignments)
  101. ReadBoneWeights(submesh, reader);
  102. currentNodeName = reader->getNodeName();
  103. }
  104. }
  105. void OgreImporter::ReadVertexBuffer(SubMesh &submesh, XmlReader *reader, const unsigned int numVertices)
  106. {
  107. DefaultLogger::get()->debug(Formatter::format() << "Reading vertex buffer with " << numVertices << " vertices");
  108. submesh.HasGeometry = true;
  109. if (reader->getAttributeValue("positions") && GetAttribute<bool>(reader, "positions"))
  110. {
  111. submesh.HasPositions = true;
  112. submesh.Positions.reserve(numVertices);
  113. DefaultLogger::get()->debug(" - Has positions");
  114. }
  115. if (reader->getAttributeValue("normals") && GetAttribute<bool>(reader, "normals"))
  116. {
  117. submesh.HasNormals = true;
  118. submesh.Normals.reserve(numVertices);
  119. DefaultLogger::get()->debug(" - Has normals");
  120. }
  121. if (reader->getAttributeValue("tangents") && GetAttribute<bool>(reader, "tangents"))
  122. {
  123. submesh.HasTangents = true;
  124. submesh.Tangents.reserve(numVertices);
  125. DefaultLogger::get()->debug(" - Has tangents");
  126. }
  127. if (reader->getAttributeValue("texture_coords"))
  128. {
  129. submesh.Uvs.resize(GetAttribute<unsigned int>(reader, "texture_coords"));
  130. for(size_t i=0, len=submesh.Uvs.size(); i<len; ++i)
  131. submesh.Uvs[i].reserve(numVertices);
  132. DefaultLogger::get()->debug(Formatter::format() << " - Has " << submesh.Uvs.size() << " texture coords");
  133. }
  134. if (!submesh.HasPositions)
  135. throw DeadlyImportError("Vertex buffer does not contain positions!");
  136. const string nnVertex = "vertex";
  137. const string nnPosition = "position";
  138. const string nnNormal = "normal";
  139. const string nnTangent = "tangent";
  140. const string nnBinormal = "binormal";
  141. const string nnTexCoord = "texcoord";
  142. const string nnColorDiffuse = "colour_diffuse";
  143. const string nnColorSpecular = "colour_specular";
  144. bool warnBinormal = true;
  145. bool warnColorDiffuse = true;
  146. bool warnColorSpecular = true;
  147. NextNode(reader);
  148. string currentNodeName = reader->getNodeName();
  149. /// @todo Make this loop nicer.
  150. while(currentNodeName == nnVertex ||
  151. currentNodeName == nnPosition ||
  152. currentNodeName == nnNormal ||
  153. currentNodeName == nnTangent ||
  154. currentNodeName == nnBinormal ||
  155. currentNodeName == nnTexCoord ||
  156. currentNodeName == nnColorDiffuse ||
  157. currentNodeName == nnColorSpecular)
  158. {
  159. if (currentNodeName == nnVertex)
  160. {
  161. NextNode(reader);
  162. currentNodeName = reader->getNodeName();
  163. }
  164. /// @todo Implement nnBinormal, nnColorDiffuse and nnColorSpecular
  165. if (submesh.HasPositions && currentNodeName == nnPosition)
  166. {
  167. aiVector3D NewPos;
  168. NewPos.x = GetAttribute<float>(reader, "x");
  169. NewPos.y = GetAttribute<float>(reader, "y");
  170. NewPos.z = GetAttribute<float>(reader, "z");
  171. submesh.Positions.push_back(NewPos);
  172. }
  173. else if (submesh.HasNormals && currentNodeName == nnNormal)
  174. {
  175. aiVector3D NewNormal;
  176. NewNormal.x = GetAttribute<float>(reader, "x");
  177. NewNormal.y = GetAttribute<float>(reader, "y");
  178. NewNormal.z = GetAttribute<float>(reader, "z");
  179. submesh.Normals.push_back(NewNormal);
  180. }
  181. else if (submesh.HasTangents && currentNodeName == nnTangent)
  182. {
  183. aiVector3D NewTangent;
  184. NewTangent.x = GetAttribute<float>(reader, "x");
  185. NewTangent.y = GetAttribute<float>(reader, "y");
  186. NewTangent.z = GetAttribute<float>(reader, "z");
  187. submesh.Tangents.push_back(NewTangent);
  188. }
  189. else if (submesh.Uvs.size() > 0 && currentNodeName == nnTexCoord)
  190. {
  191. for(size_t i=0, len=submesh.Uvs.size(); i<len; ++i)
  192. {
  193. if (currentNodeName != nnTexCoord)
  194. throw DeadlyImportError("Vertex buffer declared more UVs than can be found in a vertex");
  195. aiVector3D NewUv;
  196. NewUv.x = GetAttribute<float>(reader, "u");
  197. NewUv.y = GetAttribute<float>(reader, "v") * (-1)+1; //flip the uv vertikal, blender exports them so! (ahem... @todo ????)
  198. submesh.Uvs[i].push_back(NewUv);
  199. NextNode(reader);
  200. currentNodeName = reader->getNodeName();
  201. }
  202. // Continue main loop as above already read next node
  203. continue;
  204. }
  205. else
  206. {
  207. /// @todo Remove this stuff once implemented. We only want to log warnings once per element.
  208. bool warn = true;
  209. if (currentNodeName == nnBinormal)
  210. {
  211. if (warnBinormal)
  212. warnBinormal = false;
  213. else
  214. warn = false;
  215. }
  216. else if (currentNodeName == nnColorDiffuse)
  217. {
  218. if (warnColorDiffuse)
  219. warnColorDiffuse = false;
  220. else
  221. warn = false;
  222. }
  223. else if (currentNodeName == nnColorSpecular)
  224. {
  225. if (warnColorSpecular)
  226. warnColorSpecular = false;
  227. else
  228. warn = false;
  229. }
  230. if (warn)
  231. DefaultLogger::get()->warn(string("Vertex buffer attribute read not implemented for element: ") + currentNodeName);
  232. }
  233. // Advance
  234. NextNode(reader);
  235. currentNodeName = reader->getNodeName();
  236. }
  237. DefaultLogger::get()->debug(Formatter::format() <<
  238. " - Positions " << submesh.Positions.size() <<
  239. " Normals " << submesh.Normals.size() <<
  240. " TexCoords " << submesh.Uvs.size() <<
  241. " Tangents " << submesh.Tangents.size());
  242. // Sanity checks
  243. if (submesh.HasNormals && submesh.Normals.size() != numVertices)
  244. throw DeadlyImportError(Formatter::format() << "Read only " << submesh.Normals.size() << " normals when should have read " << numVertices);
  245. if (submesh.HasTangents && submesh.Tangents.size() != numVertices)
  246. throw DeadlyImportError(Formatter::format() << "Read only " << submesh.Tangents.size() << " tangents when should have read " << numVertices);
  247. for(unsigned int i=0; i<submesh.Uvs.size(); ++i)
  248. {
  249. if (submesh.Uvs[i].size() != numVertices)
  250. throw DeadlyImportError(Formatter::format() << "Read only " << submesh.Uvs[i].size()
  251. << " uvs for uv index " << i << " when should have read " << numVertices);
  252. }
  253. }
  254. void OgreImporter::ReadBoneWeights(SubMesh &submesh, XmlReader *reader)
  255. {
  256. submesh.Weights.resize(submesh.Positions.size());
  257. unsigned int numRead = 0;
  258. const string nnVertexBoneAssignment = "vertexboneassignment";
  259. NextNode(reader);
  260. while(CurrentNodeNameEquals(reader, nnVertexBoneAssignment))
  261. {
  262. numRead++;
  263. BoneWeight weight;
  264. weight.Id = GetAttribute<int>(reader, "boneindex");
  265. weight.Value = GetAttribute<float>(reader, "weight");
  266. //calculate the number of bones used (this is the highest id +1 becuase bone ids start at 0)
  267. /// @todo This can probably be refactored to something else.
  268. submesh.BonesUsed = max(submesh.BonesUsed, weight.Id+1);
  269. const unsigned int vertexId = GetAttribute<int>(reader, "vertexindex");
  270. submesh.Weights[vertexId].push_back(weight);
  271. NextNode(reader);
  272. }
  273. DefaultLogger::get()->debug(Formatter::format() << " - Bone weights " << numRead);
  274. }
  275. void OgreImporter::ProcessSubMesh(SubMesh &submesh, SubMesh &sharedGeometry)
  276. {
  277. //---------------Make all Vertexes unique: (this is required by assimp)-----------------------
  278. vector<Face> UniqueFaceList(submesh.Faces.size());
  279. unsigned int UniqueVertexCount=submesh.Faces.size()*3;//*3 because each face consists of 3 vertexes, because we only support triangles^^
  280. vector<aiVector3D> UniquePositions(UniqueVertexCount);
  281. vector<aiVector3D> UniqueNormals(UniqueVertexCount);
  282. vector<aiVector3D> UniqueTangents(UniqueVertexCount);
  283. vector< vector<BoneWeight> > UniqueWeights(UniqueVertexCount);
  284. vector< vector<aiVector3D> > UniqueUvs(submesh.Uvs.size());
  285. for(unsigned int i=0; i<UniqueUvs.size(); ++i) UniqueUvs[i].resize(UniqueVertexCount);
  286. //Support for shared data:
  287. /*We can use this loop to copy vertex informations from the shared data pool. In order to do so
  288. we just use a reference to a submodel instead of our submodel itself*/
  289. SubMesh& VertexSource= submesh.UseSharedGeometry ? sharedGeometry : submesh;
  290. if(submesh.UseSharedGeometry)//copy vertexinformations to our mesh:
  291. {
  292. submesh.HasPositions=sharedGeometry.HasPositions;
  293. submesh.HasNormals=sharedGeometry.HasNormals;
  294. submesh.HasTangents=sharedGeometry.HasTangents;
  295. submesh.BonesUsed=sharedGeometry.BonesUsed;
  296. UniqueUvs.resize(sharedGeometry.Uvs.size());
  297. for(unsigned int i=0; i<UniqueUvs.size(); ++i) UniqueUvs[i].resize(UniqueVertexCount);
  298. }
  299. for(unsigned int i=0; i<submesh.Faces.size(); ++i)
  300. {
  301. //We precalculate the index vlaues her, because we need them in all vertex attributes
  302. unsigned int Vertex1=submesh.Faces[i].VertexIndices[0];
  303. unsigned int Vertex2=submesh.Faces[i].VertexIndices[1];
  304. unsigned int Vertex3=submesh.Faces[i].VertexIndices[2];
  305. UniquePositions[3*i+0]=VertexSource.Positions[Vertex1];
  306. UniquePositions[3*i+1]=VertexSource.Positions[Vertex2];
  307. UniquePositions[3*i+2]=VertexSource.Positions[Vertex3];
  308. if(VertexSource.HasNormals)
  309. {
  310. UniqueNormals[3*i+0]=VertexSource.Normals[Vertex1];
  311. UniqueNormals[3*i+1]=VertexSource.Normals[Vertex2];
  312. UniqueNormals[3*i+2]=VertexSource.Normals[Vertex3];
  313. }
  314. if(VertexSource.HasTangents)
  315. {
  316. UniqueTangents[3*i+0]=VertexSource.Tangents[Vertex1];
  317. UniqueTangents[3*i+1]=VertexSource.Tangents[Vertex2];
  318. UniqueTangents[3*i+2]=VertexSource.Tangents[Vertex3];
  319. }
  320. if(UniqueUvs.size()>0)
  321. {
  322. for(unsigned int j=0; j<UniqueUvs.size(); ++j)
  323. {
  324. UniqueUvs[j][3*i+0]=VertexSource.Uvs[j][Vertex1];
  325. UniqueUvs[j][3*i+1]=VertexSource.Uvs[j][Vertex2];
  326. UniqueUvs[j][3*i+2]=VertexSource.Uvs[j][Vertex3];
  327. }
  328. }
  329. if(VertexSource.Weights.size() > 0)
  330. {
  331. UniqueWeights[3*i+0]=VertexSource.Weights[Vertex1];
  332. UniqueWeights[3*i+1]=VertexSource.Weights[Vertex2];
  333. UniqueWeights[3*i+2]=VertexSource.Weights[Vertex3];
  334. }
  335. //The indexvalues a just continuous numbers (0, 1, 2, 3, 4, 5, 6...)
  336. UniqueFaceList[i].VertexIndices[0]=3*i+0;
  337. UniqueFaceList[i].VertexIndices[1]=3*i+1;
  338. UniqueFaceList[i].VertexIndices[2]=3*i+2;
  339. }
  340. //_________________________________________________________________________________________
  341. //now we have the unique datas, but want them in the SubMesh, so we swap all the containers:
  342. //if we don't have one of them, we just swap empty containers, so everything is ok
  343. submesh.Faces.swap(UniqueFaceList);
  344. submesh.Positions.swap(UniquePositions);
  345. submesh.Normals.swap(UniqueNormals);
  346. submesh.Tangents.swap(UniqueTangents);
  347. submesh.Uvs.swap(UniqueUvs);
  348. submesh.Weights.swap(UniqueWeights);
  349. //------------- normalize weights -----------------------------
  350. //The Blender exporter doesn't care about whether the sum of all boneweights for a single vertex equals 1 or not,
  351. //so we have to make this sure:
  352. for(unsigned int VertexId=0; VertexId<submesh.Weights.size(); ++VertexId)//iterate over all vertices
  353. {
  354. float WeightSum=0.0f;
  355. for(unsigned int BoneId=0; BoneId<submesh.Weights[VertexId].size(); ++BoneId)//iterate over all bones
  356. {
  357. WeightSum+=submesh.Weights[VertexId][BoneId].Value;
  358. }
  359. //check if the sum is too far away from 1
  360. if(WeightSum<1.0f-0.05f || WeightSum>1.0f+0.05f)
  361. {
  362. //normalize all weights:
  363. for(unsigned int BoneId=0; BoneId<submesh.Weights[VertexId].size(); ++BoneId)//iterate over all bones
  364. {
  365. submesh.Weights[VertexId][BoneId].Value/=WeightSum;
  366. }
  367. }
  368. }
  369. //_________________________________________________________
  370. }
  371. aiMesh* OgreImporter::CreateAssimpSubMesh(aiScene *pScene, const SubMesh& submesh, const vector<Bone>& bones) const
  372. {
  373. aiMesh* NewAiMesh = new aiMesh();
  374. //Positions
  375. NewAiMesh->mVertices=new aiVector3D[submesh.Positions.size()];
  376. memcpy(NewAiMesh->mVertices, &submesh.Positions[0], submesh.Positions.size()*sizeof(aiVector3D));
  377. NewAiMesh->mNumVertices=submesh.Positions.size();
  378. //Normals
  379. if(submesh.HasNormals)
  380. {
  381. NewAiMesh->mNormals=new aiVector3D[submesh.Normals.size()];
  382. memcpy(NewAiMesh->mNormals, &submesh.Normals[0], submesh.Normals.size()*sizeof(aiVector3D));
  383. }
  384. //until we have support for bitangents, no tangents will be written
  385. /*
  386. //Tangents
  387. if(submesh.HasTangents)
  388. {
  389. NewAiMesh->mTangents=new aiVector3D[submesh.Tangents.size()];
  390. memcpy(NewAiMesh->mTangents, &submesh.Tangents[0], submesh.Tangents.size()*sizeof(aiVector3D));
  391. }
  392. */
  393. //Uvs
  394. if(submesh.Uvs.size()>0)
  395. {
  396. for(unsigned int i=0; i<submesh.Uvs.size(); ++i)
  397. {
  398. NewAiMesh->mNumUVComponents[i]=2;
  399. NewAiMesh->mTextureCoords[i]=new aiVector3D[submesh.Uvs[i].size()];
  400. memcpy(NewAiMesh->mTextureCoords[i], &(submesh.Uvs[i][0]), submesh.Uvs[i].size()*sizeof(aiVector3D));
  401. }
  402. }
  403. //---------------------------------------- bones --------------------------------------------
  404. //Copy the weights in in Bone-Vertices Struktur
  405. //(we have them in a Vertex-bones Structur, this is much easier for making them unique, which is required by assimp
  406. vector< vector<aiVertexWeight> > aiWeights(submesh.BonesUsed);//now the outer list are the bones, and the inner vector the vertices
  407. for(unsigned int VertexId=0; VertexId<submesh.Weights.size(); ++VertexId)//iterate over all vertices
  408. {
  409. for(unsigned int BoneId=0; BoneId<submesh.Weights[VertexId].size(); ++BoneId)//iterate over all bones
  410. {
  411. aiVertexWeight NewWeight;
  412. NewWeight.mVertexId=VertexId;//the current Vertex, we can't use the Id form the submehs weights, because they are bone id's
  413. NewWeight.mWeight=submesh.Weights[VertexId][BoneId].Value;
  414. aiWeights[submesh.Weights[VertexId][BoneId].Id].push_back(NewWeight);
  415. }
  416. }
  417. vector<aiBone*> aiBones;
  418. aiBones.reserve(submesh.BonesUsed);//the vector might be smaller, because there might be empty bones (bones that are not attached to any vertex)
  419. //create all the bones and fill them with informations
  420. for(unsigned int i=0; i<submesh.BonesUsed; ++i)
  421. {
  422. if(aiWeights[i].size()>0)
  423. {
  424. aiBone* NewBone=new aiBone();
  425. NewBone->mNumWeights=aiWeights[i].size();
  426. NewBone->mWeights=new aiVertexWeight[aiWeights[i].size()];
  427. memcpy(NewBone->mWeights, &(aiWeights[i][0]), sizeof(aiVertexWeight)*aiWeights[i].size());
  428. NewBone->mName=bones[i].Name;//The bone list should be sorted after its id's, this was done in LoadSkeleton
  429. NewBone->mOffsetMatrix=bones[i].BoneToWorldSpace;
  430. aiBones.push_back(NewBone);
  431. }
  432. }
  433. NewAiMesh->mNumBones=aiBones.size();
  434. // mBones must be NULL if mNumBones is non 0 or the validation fails.
  435. if (aiBones.size()) {
  436. NewAiMesh->mBones=new aiBone* [aiBones.size()];
  437. memcpy(NewAiMesh->mBones, &(aiBones[0]), aiBones.size()*sizeof(aiBone*));
  438. }
  439. //______________________________________________________________________________________________________
  440. //Faces
  441. NewAiMesh->mFaces=new aiFace[submesh.Faces.size()];
  442. for(unsigned int i=0; i<submesh.Faces.size(); ++i)
  443. {
  444. NewAiMesh->mFaces[i].mNumIndices=3;
  445. NewAiMesh->mFaces[i].mIndices=new unsigned int[3];
  446. NewAiMesh->mFaces[i].mIndices[0]=submesh.Faces[i].VertexIndices[0];
  447. NewAiMesh->mFaces[i].mIndices[1]=submesh.Faces[i].VertexIndices[1];
  448. NewAiMesh->mFaces[i].mIndices[2]=submesh.Faces[i].VertexIndices[2];
  449. }
  450. NewAiMesh->mNumFaces=submesh.Faces.size();
  451. //Link the material:
  452. NewAiMesh->mMaterialIndex=submesh.MaterialIndex;//the index is set by the function who called ReadSubMesh
  453. return NewAiMesh;
  454. }
  455. } // Ogre
  456. } // Assimp
  457. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER