OgreMesh.cpp 17 KB

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