IRRMeshLoader.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the IrrMesh importer class */
  35. #ifndef ASSIMP_BUILD_NO_IRRMESH_IMPORTER
  36. #include "IRRMeshLoader.h"
  37. #include <assimp/ParsingUtils.h>
  38. #include <assimp/fast_atof.h>
  39. #include <assimp/importerdesc.h>
  40. #include <assimp/material.h>
  41. #include <assimp/mesh.h>
  42. #include <assimp/scene.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. #include <assimp/IOSystem.hpp>
  45. #include <memory>
  46. using namespace Assimp;
  47. static const aiImporterDesc desc = {
  48. "Irrlicht Mesh Reader",
  49. "",
  50. "",
  51. "http://irrlicht.sourceforge.net/",
  52. aiImporterFlags_SupportTextFlavour,
  53. 0,
  54. 0,
  55. 0,
  56. 0,
  57. "xml irrmesh"
  58. };
  59. // ------------------------------------------------------------------------------------------------
  60. // Returns whether the class can handle the format of the given file.
  61. bool IRRMeshImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
  62. /* NOTE: A simple check for the file extension is not enough
  63. * here. Irrmesh and irr are easy, but xml is too generic
  64. * and could be collada, too. So we need to open the file and
  65. * search for typical tokens.
  66. */
  67. static const char *tokens[] = { "irrmesh" };
  68. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Get a list of all file extensions which are handled by this class
  72. const aiImporterDesc *IRRMeshImporter::GetInfo() const {
  73. return &desc;
  74. }
  75. static void releaseMaterial(aiMaterial **mat) {
  76. if (*mat != nullptr) {
  77. delete *mat;
  78. *mat = nullptr;
  79. }
  80. }
  81. static void releaseMesh(aiMesh **mesh) {
  82. if (*mesh != nullptr) {
  83. delete *mesh;
  84. *mesh = nullptr;
  85. }
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // Imports the given file into the given scene structure.
  89. void IRRMeshImporter::InternReadFile(const std::string &pFile,
  90. aiScene *pScene, IOSystem *pIOHandler) {
  91. std::unique_ptr<IOStream> file(pIOHandler->Open(pFile));
  92. // Check whether we can read from the file
  93. if (file == nullptr)
  94. throw DeadlyImportError("Failed to open IRRMESH file ", pFile);
  95. // Construct the irrXML parser
  96. XmlParser parser;
  97. if (!parser.parse( file.get() )) {
  98. throw DeadlyImportError("XML parse error while loading IRRMESH file ", pFile);
  99. }
  100. XmlNode root = parser.getRootNode();
  101. // final data
  102. std::vector<aiMaterial *> materials;
  103. std::vector<aiMesh *> meshes;
  104. materials.reserve(5);
  105. meshes.reserve(5);
  106. // temporary data - current mesh buffer
  107. aiMaterial *curMat = nullptr;
  108. aiMesh *curMesh = nullptr;
  109. unsigned int curMatFlags = 0;
  110. std::vector<aiVector3D> curVertices, curNormals, curTangents, curBitangents;
  111. std::vector<aiColor4D> curColors;
  112. std::vector<aiVector3D> curUVs, curUV2s;
  113. // some temporary variables
  114. int textMeaning = 0;
  115. int vertexFormat = 0; // 0 = normal; 1 = 2 tcoords, 2 = tangents
  116. bool useColors = false;
  117. // Parse the XML file
  118. for (pugi::xml_node child : root.children()) {
  119. if (child.type() == pugi::node_element) {
  120. if (!ASSIMP_stricmp(child.name(), "buffer") && (curMat || curMesh)) {
  121. // end of previous buffer. A material and a mesh should be there
  122. if (!curMat || !curMesh) {
  123. ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material");
  124. releaseMaterial(&curMat);
  125. releaseMesh(&curMesh);
  126. } else {
  127. materials.push_back(curMat);
  128. meshes.push_back(curMesh);
  129. }
  130. curMat = nullptr;
  131. curMesh = nullptr;
  132. curVertices.clear();
  133. curColors.clear();
  134. curNormals.clear();
  135. curUV2s.clear();
  136. curUVs.clear();
  137. curTangents.clear();
  138. curBitangents.clear();
  139. }
  140. if (!ASSIMP_stricmp(child.name(), "material")) {
  141. if (curMat) {
  142. ASSIMP_LOG_WARN("IRRMESH: Only one material description per buffer, please");
  143. releaseMaterial(&curMat);
  144. }
  145. curMat = ParseMaterial(curMatFlags);
  146. }
  147. /* no else here! */ if (!ASSIMP_stricmp(child.name(), "vertices")) {
  148. pugi::xml_attribute attr = child.attribute("vertexCount");
  149. int num = attr.as_int();
  150. //int num = reader->getAttributeValueAsInt("vertexCount");
  151. if (!num) {
  152. // This is possible ... remove the mesh from the list and skip further reading
  153. ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero vertices");
  154. releaseMaterial(&curMat);
  155. releaseMesh(&curMesh);
  156. textMeaning = 0;
  157. continue;
  158. }
  159. curVertices.reserve(num);
  160. curNormals.reserve(num);
  161. curColors.reserve(num);
  162. curUVs.reserve(num);
  163. // Determine the file format
  164. //const char *t = reader->getAttributeValueSafe("type");
  165. pugi::xml_attribute t = child.attribute("type");
  166. if (!ASSIMP_stricmp("2tcoords", t.name())) {
  167. curUV2s.reserve(num);
  168. vertexFormat = 1;
  169. if (curMatFlags & AI_IRRMESH_EXTRA_2ND_TEXTURE) {
  170. // *********************************************************
  171. // We have a second texture! So use this UV channel
  172. // for it. The 2nd texture can be either a normal
  173. // texture (solid_2layer or lightmap_xxx) or a normal
  174. // map (normal_..., parallax_...)
  175. // *********************************************************
  176. int idx = 1;
  177. aiMaterial *mat = (aiMaterial *)curMat;
  178. if (curMatFlags & AI_IRRMESH_MAT_lightmap) {
  179. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_LIGHTMAP(0));
  180. } else if (curMatFlags & AI_IRRMESH_MAT_normalmap_solid) {
  181. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_NORMALS(0));
  182. } else if (curMatFlags & AI_IRRMESH_MAT_solid_2layer) {
  183. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_DIFFUSE(1));
  184. }
  185. }
  186. } else if (!ASSIMP_stricmp("tangents", t.name())) {
  187. curTangents.reserve(num);
  188. curBitangents.reserve(num);
  189. vertexFormat = 2;
  190. } else if (ASSIMP_stricmp("standard", t.name())) {
  191. releaseMaterial(&curMat);
  192. ASSIMP_LOG_WARN("IRRMESH: Unknown vertex format");
  193. } else
  194. vertexFormat = 0;
  195. textMeaning = 1;
  196. } else if (!ASSIMP_stricmp(child.name(), "indices")) {
  197. if (curVertices.empty() && curMat) {
  198. releaseMaterial(&curMat);
  199. throw DeadlyImportError("IRRMESH: indices must come after vertices");
  200. }
  201. textMeaning = 2;
  202. // start a new mesh
  203. curMesh = new aiMesh();
  204. // allocate storage for all faces
  205. pugi::xml_attribute attr = child.attribute("indexCount");
  206. curMesh->mNumVertices = attr.as_int();
  207. if (!curMesh->mNumVertices) {
  208. // This is possible ... remove the mesh from the list and skip further reading
  209. ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero indices");
  210. // mesh - away
  211. releaseMesh(&curMesh);
  212. // material - away
  213. releaseMaterial(&curMat);
  214. textMeaning = 0;
  215. continue;
  216. }
  217. if (curMesh->mNumVertices % 3) {
  218. ASSIMP_LOG_WARN("IRRMESH: Number if indices isn't divisible by 3");
  219. }
  220. curMesh->mNumFaces = curMesh->mNumVertices / 3;
  221. curMesh->mFaces = new aiFace[curMesh->mNumFaces];
  222. // setup some members
  223. curMesh->mMaterialIndex = (unsigned int)materials.size();
  224. curMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  225. // allocate storage for all vertices
  226. curMesh->mVertices = new aiVector3D[curMesh->mNumVertices];
  227. if (curNormals.size() == curVertices.size()) {
  228. curMesh->mNormals = new aiVector3D[curMesh->mNumVertices];
  229. }
  230. if (curTangents.size() == curVertices.size()) {
  231. curMesh->mTangents = new aiVector3D[curMesh->mNumVertices];
  232. }
  233. if (curBitangents.size() == curVertices.size()) {
  234. curMesh->mBitangents = new aiVector3D[curMesh->mNumVertices];
  235. }
  236. if (curColors.size() == curVertices.size() && useColors) {
  237. curMesh->mColors[0] = new aiColor4D[curMesh->mNumVertices];
  238. }
  239. if (curUVs.size() == curVertices.size()) {
  240. curMesh->mTextureCoords[0] = new aiVector3D[curMesh->mNumVertices];
  241. }
  242. if (curUV2s.size() == curVertices.size()) {
  243. curMesh->mTextureCoords[1] = new aiVector3D[curMesh->mNumVertices];
  244. }
  245. }
  246. //break;
  247. //case EXN_TEXT: {
  248. const char *sz = child.child_value();
  249. if (textMeaning == 1) {
  250. textMeaning = 0;
  251. // read vertices
  252. do {
  253. SkipSpacesAndLineEnd(&sz);
  254. aiVector3D temp;
  255. aiColor4D c;
  256. // Read the vertex position
  257. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  258. SkipSpaces(&sz);
  259. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  260. SkipSpaces(&sz);
  261. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  262. SkipSpaces(&sz);
  263. curVertices.push_back(temp);
  264. // Read the vertex normals
  265. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  266. SkipSpaces(&sz);
  267. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  268. SkipSpaces(&sz);
  269. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  270. SkipSpaces(&sz);
  271. curNormals.push_back(temp);
  272. // read the vertex colors
  273. uint32_t clr = strtoul16(sz, &sz);
  274. ColorFromARGBPacked(clr, c);
  275. if (!curColors.empty() && c != *(curColors.end() - 1))
  276. useColors = true;
  277. curColors.push_back(c);
  278. SkipSpaces(&sz);
  279. // read the first UV coordinate set
  280. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  281. SkipSpaces(&sz);
  282. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  283. SkipSpaces(&sz);
  284. temp.z = 0.f;
  285. temp.y = 1.f - temp.y; // DX to OGL
  286. curUVs.push_back(temp);
  287. // read the (optional) second UV coordinate set
  288. if (vertexFormat == 1) {
  289. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  290. SkipSpaces(&sz);
  291. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  292. temp.y = 1.f - temp.y; // DX to OGL
  293. curUV2s.push_back(temp);
  294. }
  295. // read optional tangent and bitangent vectors
  296. else if (vertexFormat == 2) {
  297. // tangents
  298. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  299. SkipSpaces(&sz);
  300. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  301. SkipSpaces(&sz);
  302. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  303. SkipSpaces(&sz);
  304. temp.y *= -1.0f;
  305. curTangents.push_back(temp);
  306. // bitangents
  307. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  308. SkipSpaces(&sz);
  309. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  310. SkipSpaces(&sz);
  311. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  312. SkipSpaces(&sz);
  313. temp.y *= -1.0f;
  314. curBitangents.push_back(temp);
  315. }
  316. }
  317. /* IMPORTANT: We assume that each vertex is specified in one
  318. line. So we can skip the rest of the line - unknown vertex
  319. elements are ignored.
  320. */
  321. while (SkipLine(&sz));
  322. } else if (textMeaning == 2) {
  323. textMeaning = 0;
  324. // read indices
  325. aiFace *curFace = curMesh->mFaces;
  326. aiFace *const faceEnd = curMesh->mFaces + curMesh->mNumFaces;
  327. aiVector3D *pcV = curMesh->mVertices;
  328. aiVector3D *pcN = curMesh->mNormals;
  329. aiVector3D *pcT = curMesh->mTangents;
  330. aiVector3D *pcB = curMesh->mBitangents;
  331. aiColor4D *pcC0 = curMesh->mColors[0];
  332. aiVector3D *pcT0 = curMesh->mTextureCoords[0];
  333. aiVector3D *pcT1 = curMesh->mTextureCoords[1];
  334. unsigned int curIdx = 0;
  335. unsigned int total = 0;
  336. while (SkipSpacesAndLineEnd(&sz)) {
  337. if (curFace >= faceEnd) {
  338. ASSIMP_LOG_ERROR("IRRMESH: Too many indices");
  339. break;
  340. }
  341. if (!curIdx) {
  342. curFace->mNumIndices = 3;
  343. curFace->mIndices = new unsigned int[3];
  344. }
  345. unsigned int idx = strtoul10(sz, &sz);
  346. if (idx >= curVertices.size()) {
  347. ASSIMP_LOG_ERROR("IRRMESH: Index out of range");
  348. idx = 0;
  349. }
  350. curFace->mIndices[curIdx] = total++;
  351. *pcV++ = curVertices[idx];
  352. if (pcN) *pcN++ = curNormals[idx];
  353. if (pcT) *pcT++ = curTangents[idx];
  354. if (pcB) *pcB++ = curBitangents[idx];
  355. if (pcC0) *pcC0++ = curColors[idx];
  356. if (pcT0) *pcT0++ = curUVs[idx];
  357. if (pcT1) *pcT1++ = curUV2s[idx];
  358. if (++curIdx == 3) {
  359. ++curFace;
  360. curIdx = 0;
  361. }
  362. }
  363. if (curFace != faceEnd)
  364. ASSIMP_LOG_ERROR("IRRMESH: Not enough indices");
  365. // Finish processing the mesh - do some small material workarounds
  366. if (curMatFlags & AI_IRRMESH_MAT_trans_vertex_alpha && !useColors) {
  367. // Take the opacity value of the current material
  368. // from the common vertex color alpha
  369. aiMaterial *mat = (aiMaterial *)curMat;
  370. mat->AddProperty(&curColors[0].a, 1, AI_MATKEY_OPACITY);
  371. }
  372. }
  373. }
  374. }
  375. // End of the last buffer. A material and a mesh should be there
  376. if (curMat || curMesh) {
  377. if (!curMat || !curMesh) {
  378. ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material");
  379. releaseMaterial(&curMat);
  380. releaseMesh(&curMesh);
  381. } else {
  382. materials.push_back(curMat);
  383. meshes.push_back(curMesh);
  384. }
  385. }
  386. if (materials.empty()) {
  387. throw DeadlyImportError("IRRMESH: Unable to read a mesh from this file");
  388. }
  389. // now generate the output scene
  390. pScene->mNumMeshes = (unsigned int)meshes.size();
  391. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
  392. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  393. pScene->mMeshes[i] = meshes[i];
  394. // clean this value ...
  395. pScene->mMeshes[i]->mNumUVComponents[3] = 0;
  396. }
  397. pScene->mNumMaterials = (unsigned int)materials.size();
  398. pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
  399. ::memcpy(pScene->mMaterials, &materials[0], sizeof(void *) * pScene->mNumMaterials);
  400. pScene->mRootNode = new aiNode();
  401. pScene->mRootNode->mName.Set("<IRRMesh>");
  402. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  403. pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
  404. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  405. pScene->mRootNode->mMeshes[i] = i;
  406. }
  407. }
  408. #endif // !! ASSIMP_BUILD_NO_IRRMESH_IMPORTER