OgreMesh.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.hpp"
  36. #include "TinyFormatter.h"
  37. using namespace std;
  38. namespace Assimp
  39. {
  40. namespace Ogre
  41. {
  42. void OgreImporter::ReadSubMesh(SubMesh &theSubMesh, XmlReader *Reader)
  43. {
  44. if(Reader->getAttributeValue("usesharedvertices"))
  45. theSubMesh.SharedData=GetAttribute<bool>(Reader, "usesharedvertices");
  46. XmlRead(Reader);
  47. //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
  48. //of faces and geometry changed, and not if we have more than one of one
  49. while( Reader->getNodeName()==string("faces")
  50. || Reader->getNodeName()==string("geometry")
  51. || Reader->getNodeName()==string("boneassignments"))
  52. {
  53. if(string(Reader->getNodeName())=="faces")//Read the face list
  54. {
  55. //some info logging:
  56. unsigned int NumFaces=GetAttribute<int>(Reader, "count");
  57. ostringstream ss; ss <<"Submesh has " << NumFaces << " Faces.";
  58. DefaultLogger::get()->debug(ss.str());
  59. while(XmlRead(Reader) && Reader->getNodeName()==string("face"))
  60. {
  61. Face NewFace;
  62. NewFace.VertexIndices[0]=GetAttribute<int>(Reader, "v1");
  63. NewFace.VertexIndices[1]=GetAttribute<int>(Reader, "v2");
  64. NewFace.VertexIndices[2]=GetAttribute<int>(Reader, "v3");
  65. if(Reader->getAttributeValue("v4"))//this should be supported in the future
  66. {
  67. DefaultLogger::get()->warn("Submesh has quads, only traingles are supported!");
  68. //throw DeadlyImportError("Submesh has quads, only traingles are supported!");
  69. }
  70. theSubMesh.FaceList.push_back(NewFace);
  71. }
  72. }//end of faces
  73. else if(string(Reader->getNodeName())=="geometry")//Read the vertexdata
  74. {
  75. //some info logging:
  76. unsigned int NumVertices=GetAttribute<int>(Reader, "vertexcount");
  77. ostringstream ss; ss<<"VertexCount: " << NumVertices;
  78. DefaultLogger::get()->debug(ss.str());
  79. //General Informations about vertices
  80. XmlRead(Reader);
  81. while(Reader->getNodeName()==string("vertexbuffer"))
  82. {
  83. ReadVertexBuffer(theSubMesh, Reader, NumVertices);
  84. }
  85. //some error checking on the loaded data
  86. if(!theSubMesh.HasPositions)
  87. throw DeadlyImportError("No positions could be loaded!");
  88. if(theSubMesh.HasNormals && theSubMesh.Normals.size() != NumVertices)
  89. throw DeadlyImportError("Wrong Number of Normals loaded!");
  90. if(theSubMesh.HasTangents && theSubMesh.Tangents.size() != NumVertices)
  91. throw DeadlyImportError("Wrong Number of Tangents loaded!");
  92. if(theSubMesh.NumUvs==1 && theSubMesh.Uvs.size() != NumVertices)
  93. throw DeadlyImportError("Wrong Number of Uvs loaded!");
  94. }//end of "geometry
  95. else if(Reader->getNodeName()==string("boneassignments"))
  96. {
  97. ReadBoneWeights(theSubMesh, Reader);
  98. }
  99. }
  100. DefaultLogger::get()->debug((Formatter::format(),
  101. "Positionen: ",theSubMesh.Positions.size(),
  102. " Normale: ",theSubMesh.Normals.size(),
  103. " TexCoords: ",theSubMesh.Uvs.size(),
  104. " Tantents: ",theSubMesh.Tangents.size()
  105. ));
  106. DefaultLogger::get()->warn(Reader->getNodeName());
  107. }
  108. void OgreImporter::ReadVertexBuffer(SubMesh &theSubMesh, XmlReader *Reader, unsigned int NumVertices)
  109. {
  110. DefaultLogger::get()->debug("new Vertex Buffer");
  111. bool ReadPositions=false;
  112. bool ReadNormals=false;
  113. bool ReadTangents=false;
  114. bool ReadUvs=false;
  115. //-------------------- check, what we need to read: --------------------------------
  116. if(Reader->getAttributeValue("positions") && GetAttribute<bool>(Reader, "positions"))
  117. {
  118. ReadPositions=theSubMesh.HasPositions=true;
  119. theSubMesh.Positions.reserve(NumVertices);
  120. DefaultLogger::get()->debug("reading positions");
  121. }
  122. if(Reader->getAttributeValue("normals") && GetAttribute<bool>(Reader, "normals"))
  123. {
  124. ReadNormals=theSubMesh.HasNormals=true;
  125. theSubMesh.Normals.reserve(NumVertices);
  126. DefaultLogger::get()->debug("reading normals");
  127. }
  128. if(Reader->getAttributeValue("tangents") && GetAttribute<bool>(Reader, "tangents"))
  129. {
  130. ReadTangents=theSubMesh.HasTangents=true;
  131. theSubMesh.Tangents.reserve(NumVertices);
  132. DefaultLogger::get()->debug("reading tangents");
  133. }
  134. //we can have 1 or 0 uv channels, and if the mesh has no uvs, it also doesn't have the attribute
  135. if(!Reader->getAttributeValue("texture_coords"))
  136. theSubMesh.NumUvs=0;
  137. else
  138. {
  139. ReadUvs=!!(theSubMesh.NumUvs=GetAttribute<int>(Reader, "texture_coords"));
  140. theSubMesh.Uvs.reserve(NumVertices);
  141. DefaultLogger::get()->debug("reading texture coords");
  142. }
  143. if(theSubMesh.NumUvs>1)
  144. {
  145. DefaultLogger::get()->warn("too many texcoords (just 1 supported!), just the first texcoords will be loaded!");
  146. theSubMesh.NumUvs=1;
  147. }
  148. //___________________________________________________________________
  149. //check if we will load anything
  150. if(!(ReadPositions || ReadNormals || ReadTangents || ReadUvs))
  151. DefaultLogger::get()->warn("vertexbuffer seams to be empty!");
  152. //read all the vertices:
  153. XmlRead(Reader);
  154. /*it might happen, that we have more than one attribute per vertex (they are not splitted to different buffers)
  155. so the break condition is a bit tricky (well, IrrXML just sucks :( )*/
  156. while(Reader->getNodeName()==string("vertex")
  157. ||Reader->getNodeName()==string("position")
  158. ||Reader->getNodeName()==string("normal")
  159. ||Reader->getNodeName()==string("tangent")
  160. ||Reader->getNodeName()==string("texcoord"))
  161. {
  162. if(Reader->getNodeName()==string("vertex"))
  163. XmlRead(Reader);//Read an attribute tag
  164. //Position
  165. if(ReadPositions && Reader->getNodeName()==string("position"))
  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. theSubMesh.Positions.push_back(NewPos);
  172. }
  173. //Normal
  174. else if(ReadNormals && Reader->getNodeName()==string("normal"))
  175. {
  176. aiVector3D NewNormal;
  177. NewNormal.x=GetAttribute<float>(Reader, "x");
  178. NewNormal.y=GetAttribute<float>(Reader, "y");
  179. NewNormal.z=GetAttribute<float>(Reader, "z");
  180. theSubMesh.Normals.push_back(NewNormal);
  181. }
  182. //Tangent
  183. else if(ReadTangents && Reader->getNodeName()==string("tangent"))
  184. {
  185. aiVector3D NewTangent;
  186. NewTangent.x=GetAttribute<float>(Reader, "x");
  187. NewTangent.y=GetAttribute<float>(Reader, "y");
  188. NewTangent.z=GetAttribute<float>(Reader, "z");
  189. theSubMesh.Tangents.push_back(NewTangent);
  190. }
  191. //Uv:
  192. else if(ReadUvs && Reader->getNodeName()==string("texcoord"))
  193. {
  194. aiVector3D NewUv;
  195. NewUv.x=GetAttribute<float>(Reader, "u");
  196. NewUv.y=GetAttribute<float>(Reader, "v")*(-1)+1;//flip the uv vertikal, blender exports them so!
  197. theSubMesh.Uvs.push_back(NewUv);
  198. //skip all the following texcoords:
  199. while(Reader->getNodeName()==string("texcoord"))
  200. XmlRead(Reader);
  201. continue;//don't read another line at the end of the loop
  202. }
  203. //Attribute could not be read
  204. else
  205. {
  206. DefaultLogger::get()->warn(string("Attribute was not read: ")+Reader->getNodeName());
  207. }
  208. XmlRead(Reader);//Read the Vertex tag
  209. }
  210. }
  211. void OgreImporter::ReadBoneWeights(SubMesh &theSubMesh, XmlReader *Reader)
  212. {
  213. theSubMesh.Weights.resize(theSubMesh.Positions.size());
  214. while(XmlRead(Reader) && Reader->getNodeName()==string("vertexboneassignment"))
  215. {
  216. Weight NewWeight;
  217. unsigned int VertexId=GetAttribute<int>(Reader, "vertexindex");
  218. NewWeight.BoneId=GetAttribute<int>(Reader, "boneindex");
  219. NewWeight.Value=GetAttribute<float>(Reader, "weight");
  220. //calculate the number of bones used (this is the highest id +1 becuase bone ids start at 0)
  221. theSubMesh.BonesUsed=max(theSubMesh.BonesUsed, NewWeight.BoneId+1);
  222. theSubMesh.Weights[VertexId].push_back(NewWeight);
  223. }
  224. }
  225. void OgreImporter::ProcessSubMesh(SubMesh &theSubMesh, SubMesh &theSharedGeometry)
  226. {
  227. //---------------Make all Vertexes unique: (this is required by assimp)-----------------------
  228. vector<Face> UniqueFaceList(theSubMesh.FaceList.size());
  229. unsigned int UniqueVertexCount=theSubMesh.FaceList.size()*3;//*3 because each face consists of 3 vertexes, because we only support triangles^^
  230. vector<aiVector3D> UniquePositions(UniqueVertexCount);
  231. vector<aiVector3D> UniqueNormals(UniqueVertexCount);
  232. vector<aiVector3D> UniqueTangents(UniqueVertexCount);
  233. vector<aiVector3D> UniqueUvs(UniqueVertexCount);
  234. vector< vector<Weight> > UniqueWeights(UniqueVertexCount);
  235. //Support for shared data:
  236. /*We can use this loop to copy vertex informations from the shared data pool. In order to do so
  237. we just use a reference to a submodel instead of our submodel itself*/
  238. SubMesh& VertexSource= theSubMesh.SharedData ? theSharedGeometry : theSubMesh;
  239. if(theSubMesh.SharedData)//copy vertexinformations to our mesh:
  240. {
  241. theSubMesh.HasPositions=theSharedGeometry.HasPositions;
  242. theSubMesh.HasNormals=theSharedGeometry.HasNormals;
  243. theSubMesh.HasTangents=theSharedGeometry.HasTangents;
  244. theSubMesh.NumUvs=theSharedGeometry.NumUvs;
  245. theSubMesh.BonesUsed=theSharedGeometry.BonesUsed;
  246. }
  247. if(VertexSource.NumUvs > 0)
  248. {
  249. DefaultLogger::get()->error("Not all Uvs will be made unique!");
  250. }
  251. for(unsigned int i=0; i<theSubMesh.FaceList.size(); ++i)
  252. {
  253. //We precalculate the index vlaues her, because we need them in all vertex attributes
  254. unsigned int Vertex1=theSubMesh.FaceList[i].VertexIndices[0];
  255. unsigned int Vertex2=theSubMesh.FaceList[i].VertexIndices[1];
  256. unsigned int Vertex3=theSubMesh.FaceList[i].VertexIndices[2];
  257. UniquePositions[3*i+0]=VertexSource.Positions[Vertex1];
  258. UniquePositions[3*i+1]=VertexSource.Positions[Vertex2];
  259. UniquePositions[3*i+2]=VertexSource.Positions[Vertex3];
  260. if(VertexSource.HasNormals)
  261. {
  262. UniqueNormals[3*i+0]=VertexSource.Normals[Vertex1];
  263. UniqueNormals[3*i+1]=VertexSource.Normals[Vertex2];
  264. UniqueNormals[3*i+2]=VertexSource.Normals[Vertex3];
  265. }
  266. if(VertexSource.HasTangents)
  267. {
  268. UniqueTangents[3*i+0]=VertexSource.Tangents[Vertex1];
  269. UniqueTangents[3*i+1]=VertexSource.Tangents[Vertex2];
  270. UniqueTangents[3*i+2]=VertexSource.Tangents[Vertex3];
  271. }
  272. if(VertexSource.NumUvs > 0)
  273. {
  274. UniqueUvs[3*i+0]=VertexSource.Uvs[Vertex1];
  275. UniqueUvs[3*i+1]=VertexSource.Uvs[Vertex2];
  276. UniqueUvs[3*i+2]=VertexSource.Uvs[Vertex3];
  277. }
  278. if(VertexSource.Weights.size() > 0)
  279. {
  280. UniqueWeights[3*i+0]=VertexSource.Weights[Vertex1];
  281. UniqueWeights[3*i+1]=VertexSource.Weights[Vertex2];
  282. UniqueWeights[3*i+2]=VertexSource.Weights[Vertex3];
  283. }
  284. //The indexvalues a just continuous numbers (0, 1, 2, 3, 4, 5, 6...)
  285. UniqueFaceList[i].VertexIndices[0]=3*i+0;
  286. UniqueFaceList[i].VertexIndices[1]=3*i+1;
  287. UniqueFaceList[i].VertexIndices[2]=3*i+2;
  288. }
  289. //_________________________________________________________________________________________
  290. //now we have the unique datas, but want them in the SubMesh, so we swap all the containers:
  291. //if we don't have one of them, we just swap empty containers, so everything is ok
  292. theSubMesh.FaceList.swap(UniqueFaceList);
  293. theSubMesh.Positions.swap(UniquePositions);
  294. theSubMesh.Normals.swap(UniqueNormals);
  295. theSubMesh.Tangents.swap(UniqueTangents);
  296. theSubMesh.Uvs.swap(UniqueUvs);
  297. theSubMesh.Weights.swap(UniqueWeights);
  298. //------------- normalize weights -----------------------------
  299. //The Blender exporter doesn't care about whether the sum of all boneweights for a single vertex equals 1 or not,
  300. //so we have to make this sure:
  301. for(unsigned int VertexId=0; VertexId<theSubMesh.Weights.size(); ++VertexId)//iterate over all vertices
  302. {
  303. float WeightSum=0.0f;
  304. for(unsigned int BoneId=0; BoneId<theSubMesh.Weights[VertexId].size(); ++BoneId)//iterate over all bones
  305. {
  306. WeightSum+=theSubMesh.Weights[VertexId][BoneId].Value;
  307. }
  308. //check if the sum is too far away from 1
  309. if(WeightSum<1.0f-0.05f || WeightSum>1.0f+0.05f)
  310. {
  311. //normalize all weights:
  312. for(unsigned int BoneId=0; BoneId<theSubMesh.Weights[VertexId].size(); ++BoneId)//iterate over all bones
  313. {
  314. theSubMesh.Weights[VertexId][BoneId].Value/=WeightSum;
  315. }
  316. }
  317. }
  318. //_________________________________________________________
  319. }
  320. aiMesh* OgreImporter::CreateAssimpSubMesh(const SubMesh& theSubMesh, const vector<Bone>& Bones) const
  321. {
  322. const aiScene* const m_CurrentScene=this->m_CurrentScene;//make sure, that we can access but not change the scene
  323. (void)m_CurrentScene;
  324. aiMesh* NewAiMesh=new aiMesh();
  325. //Positions
  326. NewAiMesh->mVertices=new aiVector3D[theSubMesh.Positions.size()];
  327. memcpy(NewAiMesh->mVertices, &theSubMesh.Positions[0], theSubMesh.Positions.size()*sizeof(aiVector3D));
  328. NewAiMesh->mNumVertices=theSubMesh.Positions.size();
  329. //Normals
  330. if(theSubMesh.HasNormals)
  331. {
  332. NewAiMesh->mNormals=new aiVector3D[theSubMesh.Normals.size()];
  333. memcpy(NewAiMesh->mNormals, &theSubMesh.Normals[0], theSubMesh.Normals.size()*sizeof(aiVector3D));
  334. }
  335. //until we have support for bitangents, no tangents will be written
  336. /*
  337. //Tangents
  338. if(theSubMesh.HasTangents)
  339. {
  340. NewAiMesh->mTangents=new aiVector3D[theSubMesh.Tangents.size()];
  341. memcpy(NewAiMesh->mTangents, &theSubMesh.Tangents[0], theSubMesh.Tangents.size()*sizeof(aiVector3D));
  342. }
  343. */
  344. //Uvs
  345. if(0!=theSubMesh.NumUvs)
  346. {
  347. NewAiMesh->mNumUVComponents[0]=2;
  348. NewAiMesh->mTextureCoords[0]= new aiVector3D[theSubMesh.Uvs.size()];
  349. memcpy(NewAiMesh->mTextureCoords[0], &theSubMesh.Uvs[0], theSubMesh.Uvs.size()*sizeof(aiVector3D));
  350. }
  351. //---------------------------------------- Bones --------------------------------------------
  352. //Copy the weights in in Bone-Vertices Struktur
  353. //(we have them in a Vertex-Bones Structur, this is much easier for making them unique, which is required by assimp
  354. vector< vector<aiVertexWeight> > aiWeights(theSubMesh.BonesUsed);//now the outer list are the bones, and the inner vector the vertices
  355. for(unsigned int VertexId=0; VertexId<theSubMesh.Weights.size(); ++VertexId)//iterate over all vertices
  356. {
  357. for(unsigned int BoneId=0; BoneId<theSubMesh.Weights[VertexId].size(); ++BoneId)//iterate over all bones
  358. {
  359. aiVertexWeight NewWeight;
  360. NewWeight.mVertexId=VertexId;//the current Vertex, we can't use the Id form the submehs weights, because they are bone id's
  361. NewWeight.mWeight=theSubMesh.Weights[VertexId][BoneId].Value;
  362. aiWeights[theSubMesh.Weights[VertexId][BoneId].BoneId].push_back(NewWeight);
  363. }
  364. }
  365. vector<aiBone*> aiBones;
  366. aiBones.reserve(theSubMesh.BonesUsed);//the vector might be smaller, because there might be empty bones (bones that are not attached to any vertex)
  367. //create all the bones and fill them with informations
  368. for(unsigned int i=0; i<theSubMesh.BonesUsed; ++i)
  369. {
  370. if(aiWeights[i].size()>0)
  371. {
  372. aiBone* NewBone=new aiBone();
  373. NewBone->mNumWeights=aiWeights[i].size();
  374. NewBone->mWeights=new aiVertexWeight[aiWeights[i].size()];
  375. memcpy(NewBone->mWeights, &(aiWeights[i][0]), sizeof(aiVertexWeight)*aiWeights[i].size());
  376. NewBone->mName=Bones[i].Name;//The bone list should be sorted after its id's, this was done in LoadSkeleton
  377. NewBone->mOffsetMatrix=Bones[i].BoneToWorldSpace;
  378. aiBones.push_back(NewBone);
  379. }
  380. }
  381. NewAiMesh->mNumBones=aiBones.size();
  382. // mBones must be NULL if mNumBones is non 0 or the validation fails.
  383. if (aiBones.size()) {
  384. NewAiMesh->mBones=new aiBone* [aiBones.size()];
  385. memcpy(NewAiMesh->mBones, &(aiBones[0]), aiBones.size()*sizeof(aiBone*));
  386. }
  387. //______________________________________________________________________________________________________
  388. //Faces
  389. NewAiMesh->mFaces=new aiFace[theSubMesh.FaceList.size()];
  390. for(unsigned int i=0; i<theSubMesh.FaceList.size(); ++i)
  391. {
  392. NewAiMesh->mFaces[i].mNumIndices=3;
  393. NewAiMesh->mFaces[i].mIndices=new unsigned int[3];
  394. NewAiMesh->mFaces[i].mIndices[0]=theSubMesh.FaceList[i].VertexIndices[0];
  395. NewAiMesh->mFaces[i].mIndices[1]=theSubMesh.FaceList[i].VertexIndices[1];
  396. NewAiMesh->mFaces[i].mIndices[2]=theSubMesh.FaceList[i].VertexIndices[2];
  397. }
  398. NewAiMesh->mNumFaces=theSubMesh.FaceList.size();
  399. //Link the material:
  400. NewAiMesh->mMaterialIndex=theSubMesh.MaterialIndex;//the index is set by the function who called ReadSubMesh
  401. return NewAiMesh;
  402. }
  403. }//namespace Ogre
  404. }//namespace Assimp
  405. #endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER