IRRMeshLoader.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. // Constructor to be privately used by Importer
  61. IRRMeshImporter::IRRMeshImporter() :
  62. BaseImporter(),
  63. IrrlichtBase() {
  64. // empty
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Destructor, private as well
  68. IRRMeshImporter::~IRRMeshImporter() {}
  69. // ------------------------------------------------------------------------------------------------
  70. // Returns whether the class can handle the format of the given file.
  71. bool IRRMeshImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
  72. /* NOTE: A simple check for the file extension is not enough
  73. * here. Irrmesh and irr are easy, but xml is too generic
  74. * and could be collada, too. So we need to open the file and
  75. * search for typical tokens.
  76. */
  77. static const char *tokens[] = { "irrmesh" };
  78. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Get a list of all file extensions which are handled by this class
  82. const aiImporterDesc *IRRMeshImporter::GetInfo() const {
  83. return &desc;
  84. }
  85. static void releaseMaterial(aiMaterial **mat) {
  86. if (*mat != nullptr) {
  87. delete *mat;
  88. *mat = nullptr;
  89. }
  90. }
  91. static void releaseMesh(aiMesh **mesh) {
  92. if (*mesh != nullptr) {
  93. delete *mesh;
  94. *mesh = nullptr;
  95. }
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // Imports the given file into the given scene structure.
  99. void IRRMeshImporter::InternReadFile(const std::string &pFile,
  100. aiScene *pScene, IOSystem *pIOHandler) {
  101. std::unique_ptr<IOStream> file(pIOHandler->Open(pFile));
  102. // Check whether we can read from the file
  103. if (file.get() == NULL)
  104. throw DeadlyImportError("Failed to open IRRMESH file ", pFile);
  105. // Construct the irrXML parser
  106. XmlParser parser;
  107. if (!parser.parse( file.get() )) {
  108. throw DeadlyImportError("XML parse error while loading IRRMESH file ", pFile);
  109. }
  110. XmlNode root = parser.getRootNode();
  111. // final data
  112. std::vector<aiMaterial *> materials;
  113. std::vector<aiMesh *> meshes;
  114. materials.reserve(5);
  115. meshes.reserve(5);
  116. // temporary data - current mesh buffer
  117. aiMaterial *curMat = nullptr;
  118. aiMesh *curMesh = nullptr;
  119. unsigned int curMatFlags = 0;
  120. std::vector<aiVector3D> curVertices, curNormals, curTangents, curBitangents;
  121. std::vector<aiColor4D> curColors;
  122. std::vector<aiVector3D> curUVs, curUV2s;
  123. // some temporary variables
  124. int textMeaning = 0;
  125. int vertexFormat = 0; // 0 = normal; 1 = 2 tcoords, 2 = tangents
  126. bool useColors = false;
  127. // Parse the XML file
  128. for (pugi::xml_node child : root.children()) {
  129. if (child.type() == pugi::node_element) {
  130. if (!ASSIMP_stricmp(child.name(), "buffer") && (curMat || curMesh)) {
  131. // end of previous buffer. A material and a mesh should be there
  132. if (!curMat || !curMesh) {
  133. ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material");
  134. releaseMaterial(&curMat);
  135. releaseMesh(&curMesh);
  136. } else {
  137. materials.push_back(curMat);
  138. meshes.push_back(curMesh);
  139. }
  140. curMat = nullptr;
  141. curMesh = nullptr;
  142. curVertices.clear();
  143. curColors.clear();
  144. curNormals.clear();
  145. curUV2s.clear();
  146. curUVs.clear();
  147. curTangents.clear();
  148. curBitangents.clear();
  149. }
  150. if (!ASSIMP_stricmp(child.name(), "material")) {
  151. if (curMat) {
  152. ASSIMP_LOG_WARN("IRRMESH: Only one material description per buffer, please");
  153. releaseMaterial(&curMat);
  154. }
  155. curMat = ParseMaterial(curMatFlags);
  156. }
  157. /* no else here! */ if (!ASSIMP_stricmp(child.name(), "vertices")) {
  158. pugi::xml_attribute attr = child.attribute("vertexCount");
  159. int num = attr.as_int();
  160. //int num = reader->getAttributeValueAsInt("vertexCount");
  161. if (!num) {
  162. // This is possible ... remove the mesh from the list and skip further reading
  163. ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero vertices");
  164. releaseMaterial(&curMat);
  165. releaseMesh(&curMesh);
  166. textMeaning = 0;
  167. continue;
  168. }
  169. curVertices.reserve(num);
  170. curNormals.reserve(num);
  171. curColors.reserve(num);
  172. curUVs.reserve(num);
  173. // Determine the file format
  174. //const char *t = reader->getAttributeValueSafe("type");
  175. pugi::xml_attribute t = child.attribute("type");
  176. if (!ASSIMP_stricmp("2tcoords", t.name())) {
  177. curUV2s.reserve(num);
  178. vertexFormat = 1;
  179. if (curMatFlags & AI_IRRMESH_EXTRA_2ND_TEXTURE) {
  180. // *********************************************************
  181. // We have a second texture! So use this UV channel
  182. // for it. The 2nd texture can be either a normal
  183. // texture (solid_2layer or lightmap_xxx) or a normal
  184. // map (normal_..., parallax_...)
  185. // *********************************************************
  186. int idx = 1;
  187. aiMaterial *mat = (aiMaterial *)curMat;
  188. if (curMatFlags & AI_IRRMESH_MAT_lightmap) {
  189. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_LIGHTMAP(0));
  190. } else if (curMatFlags & AI_IRRMESH_MAT_normalmap_solid) {
  191. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_NORMALS(0));
  192. } else if (curMatFlags & AI_IRRMESH_MAT_solid_2layer) {
  193. mat->AddProperty(&idx, 1, AI_MATKEY_UVWSRC_DIFFUSE(1));
  194. }
  195. }
  196. } else if (!ASSIMP_stricmp("tangents", t.name())) {
  197. curTangents.reserve(num);
  198. curBitangents.reserve(num);
  199. vertexFormat = 2;
  200. } else if (ASSIMP_stricmp("standard", t.name())) {
  201. releaseMaterial(&curMat);
  202. ASSIMP_LOG_WARN("IRRMESH: Unknown vertex format");
  203. } else
  204. vertexFormat = 0;
  205. textMeaning = 1;
  206. } else if (!ASSIMP_stricmp(child.name(), "indices")) {
  207. if (curVertices.empty() && curMat) {
  208. releaseMaterial(&curMat);
  209. throw DeadlyImportError("IRRMESH: indices must come after vertices");
  210. }
  211. textMeaning = 2;
  212. // start a new mesh
  213. curMesh = new aiMesh();
  214. // allocate storage for all faces
  215. pugi::xml_attribute attr = child.attribute("indexCount");
  216. curMesh->mNumVertices = attr.as_int();
  217. if (!curMesh->mNumVertices) {
  218. // This is possible ... remove the mesh from the list and skip further reading
  219. ASSIMP_LOG_WARN("IRRMESH: Found mesh with zero indices");
  220. // mesh - away
  221. releaseMesh(&curMesh);
  222. // material - away
  223. releaseMaterial(&curMat);
  224. textMeaning = 0;
  225. continue;
  226. }
  227. if (curMesh->mNumVertices % 3) {
  228. ASSIMP_LOG_WARN("IRRMESH: Number if indices isn't divisible by 3");
  229. }
  230. curMesh->mNumFaces = curMesh->mNumVertices / 3;
  231. curMesh->mFaces = new aiFace[curMesh->mNumFaces];
  232. // setup some members
  233. curMesh->mMaterialIndex = (unsigned int)materials.size();
  234. curMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  235. // allocate storage for all vertices
  236. curMesh->mVertices = new aiVector3D[curMesh->mNumVertices];
  237. if (curNormals.size() == curVertices.size()) {
  238. curMesh->mNormals = new aiVector3D[curMesh->mNumVertices];
  239. }
  240. if (curTangents.size() == curVertices.size()) {
  241. curMesh->mTangents = new aiVector3D[curMesh->mNumVertices];
  242. }
  243. if (curBitangents.size() == curVertices.size()) {
  244. curMesh->mBitangents = new aiVector3D[curMesh->mNumVertices];
  245. }
  246. if (curColors.size() == curVertices.size() && useColors) {
  247. curMesh->mColors[0] = new aiColor4D[curMesh->mNumVertices];
  248. }
  249. if (curUVs.size() == curVertices.size()) {
  250. curMesh->mTextureCoords[0] = new aiVector3D[curMesh->mNumVertices];
  251. }
  252. if (curUV2s.size() == curVertices.size()) {
  253. curMesh->mTextureCoords[1] = new aiVector3D[curMesh->mNumVertices];
  254. }
  255. }
  256. //break;
  257. //case EXN_TEXT: {
  258. const char *sz = child.child_value();
  259. if (textMeaning == 1) {
  260. textMeaning = 0;
  261. // read vertices
  262. do {
  263. SkipSpacesAndLineEnd(&sz);
  264. aiVector3D temp;
  265. aiColor4D c;
  266. // Read the vertex position
  267. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  268. SkipSpaces(&sz);
  269. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  270. SkipSpaces(&sz);
  271. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  272. SkipSpaces(&sz);
  273. curVertices.push_back(temp);
  274. // Read the vertex normals
  275. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  276. SkipSpaces(&sz);
  277. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  278. SkipSpaces(&sz);
  279. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  280. SkipSpaces(&sz);
  281. curNormals.push_back(temp);
  282. // read the vertex colors
  283. uint32_t clr = strtoul16(sz, &sz);
  284. ColorFromARGBPacked(clr, c);
  285. if (!curColors.empty() && c != *(curColors.end() - 1))
  286. useColors = true;
  287. curColors.push_back(c);
  288. SkipSpaces(&sz);
  289. // read the first UV coordinate set
  290. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  291. SkipSpaces(&sz);
  292. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  293. SkipSpaces(&sz);
  294. temp.z = 0.f;
  295. temp.y = 1.f - temp.y; // DX to OGL
  296. curUVs.push_back(temp);
  297. // read the (optional) second UV coordinate set
  298. if (vertexFormat == 1) {
  299. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  300. SkipSpaces(&sz);
  301. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  302. temp.y = 1.f - temp.y; // DX to OGL
  303. curUV2s.push_back(temp);
  304. }
  305. // read optional tangent and bitangent vectors
  306. else if (vertexFormat == 2) {
  307. // tangents
  308. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  309. SkipSpaces(&sz);
  310. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  311. SkipSpaces(&sz);
  312. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  313. SkipSpaces(&sz);
  314. temp.y *= -1.0f;
  315. curTangents.push_back(temp);
  316. // bitangents
  317. sz = fast_atoreal_move<float>(sz, (float &)temp.x);
  318. SkipSpaces(&sz);
  319. sz = fast_atoreal_move<float>(sz, (float &)temp.z);
  320. SkipSpaces(&sz);
  321. sz = fast_atoreal_move<float>(sz, (float &)temp.y);
  322. SkipSpaces(&sz);
  323. temp.y *= -1.0f;
  324. curBitangents.push_back(temp);
  325. }
  326. }
  327. /* IMPORTANT: We assume that each vertex is specified in one
  328. line. So we can skip the rest of the line - unknown vertex
  329. elements are ignored.
  330. */
  331. while (SkipLine(&sz));
  332. } else if (textMeaning == 2) {
  333. textMeaning = 0;
  334. // read indices
  335. aiFace *curFace = curMesh->mFaces;
  336. aiFace *const faceEnd = curMesh->mFaces + curMesh->mNumFaces;
  337. aiVector3D *pcV = curMesh->mVertices;
  338. aiVector3D *pcN = curMesh->mNormals;
  339. aiVector3D *pcT = curMesh->mTangents;
  340. aiVector3D *pcB = curMesh->mBitangents;
  341. aiColor4D *pcC0 = curMesh->mColors[0];
  342. aiVector3D *pcT0 = curMesh->mTextureCoords[0];
  343. aiVector3D *pcT1 = curMesh->mTextureCoords[1];
  344. unsigned int curIdx = 0;
  345. unsigned int total = 0;
  346. while (SkipSpacesAndLineEnd(&sz)) {
  347. if (curFace >= faceEnd) {
  348. ASSIMP_LOG_ERROR("IRRMESH: Too many indices");
  349. break;
  350. }
  351. if (!curIdx) {
  352. curFace->mNumIndices = 3;
  353. curFace->mIndices = new unsigned int[3];
  354. }
  355. unsigned int idx = strtoul10(sz, &sz);
  356. if (idx >= curVertices.size()) {
  357. ASSIMP_LOG_ERROR("IRRMESH: Index out of range");
  358. idx = 0;
  359. }
  360. curFace->mIndices[curIdx] = total++;
  361. *pcV++ = curVertices[idx];
  362. if (pcN) *pcN++ = curNormals[idx];
  363. if (pcT) *pcT++ = curTangents[idx];
  364. if (pcB) *pcB++ = curBitangents[idx];
  365. if (pcC0) *pcC0++ = curColors[idx];
  366. if (pcT0) *pcT0++ = curUVs[idx];
  367. if (pcT1) *pcT1++ = curUV2s[idx];
  368. if (++curIdx == 3) {
  369. ++curFace;
  370. curIdx = 0;
  371. }
  372. }
  373. if (curFace != faceEnd)
  374. ASSIMP_LOG_ERROR("IRRMESH: Not enough indices");
  375. // Finish processing the mesh - do some small material workarounds
  376. if (curMatFlags & AI_IRRMESH_MAT_trans_vertex_alpha && !useColors) {
  377. // Take the opacity value of the current material
  378. // from the common vertex color alpha
  379. aiMaterial *mat = (aiMaterial *)curMat;
  380. mat->AddProperty(&curColors[0].a, 1, AI_MATKEY_OPACITY);
  381. }
  382. }
  383. }
  384. }
  385. // End of the last buffer. A material and a mesh should be there
  386. if (curMat || curMesh) {
  387. if (!curMat || !curMesh) {
  388. ASSIMP_LOG_ERROR("IRRMESH: A buffer must contain a mesh and a material");
  389. releaseMaterial(&curMat);
  390. releaseMesh(&curMesh);
  391. } else {
  392. materials.push_back(curMat);
  393. meshes.push_back(curMesh);
  394. }
  395. }
  396. if (materials.empty()) {
  397. throw DeadlyImportError("IRRMESH: Unable to read a mesh from this file");
  398. }
  399. // now generate the output scene
  400. pScene->mNumMeshes = (unsigned int)meshes.size();
  401. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
  402. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  403. pScene->mMeshes[i] = meshes[i];
  404. // clean this value ...
  405. pScene->mMeshes[i]->mNumUVComponents[3] = 0;
  406. }
  407. pScene->mNumMaterials = (unsigned int)materials.size();
  408. pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
  409. ::memcpy(pScene->mMaterials, &materials[0], sizeof(void *) * pScene->mNumMaterials);
  410. pScene->mRootNode = new aiNode();
  411. pScene->mRootNode->mName.Set("<IRRMesh>");
  412. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  413. pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
  414. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  415. pScene->mRootNode->mMeshes[i] = i;
  416. }
  417. }
  418. #endif // !! ASSIMP_BUILD_NO_IRRMESH_IMPORTER