IRRShared.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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 IRRShared.cpp
  35. * @brief Shared utilities for the IRR and IRRMESH loaders
  36. */
  37. //This section should be excluded only if both the Irrlicht AND the Irrlicht Mesh importers were omitted.
  38. #if !(defined(ASSIMP_BUILD_NO_IRR_IMPORTER) && defined(ASSIMP_BUILD_NO_IRRMESH_IMPORTER))
  39. #include "IRRShared.h"
  40. #include <assimp/ParsingUtils.h>
  41. #include <assimp/fast_atof.h>
  42. #include <assimp/DefaultLogger.hpp>
  43. #include <assimp/material.h>
  44. using namespace Assimp;
  45. using namespace irr;
  46. using namespace irr::io;
  47. // Transformation matrix to convert from Assimp to IRR space
  48. const aiMatrix4x4 Assimp::AI_TO_IRR_MATRIX = aiMatrix4x4 (
  49. 1.0f, 0.0f, 0.0f, 0.0f,
  50. 0.0f, 0.0f, 1.0f, 0.0f,
  51. 0.0f, 1.0f, 0.0f, 0.0f,
  52. 0.0f, 0.0f, 0.0f, 1.0f);
  53. // ------------------------------------------------------------------------------------------------
  54. // read a property in hexadecimal format (i.e. ffffffff)
  55. void IrrlichtBase::ReadHexProperty (HexProperty& out)
  56. {
  57. for (int i = 0; i < reader->getAttributeCount();++i)
  58. {
  59. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  60. {
  61. out.name = std::string( reader->getAttributeValue(i) );
  62. }
  63. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  64. {
  65. // parse the hexadecimal value
  66. out.value = strtoul16(reader->getAttributeValue(i));
  67. }
  68. }
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // read a decimal property
  72. void IrrlichtBase::ReadIntProperty (IntProperty& out)
  73. {
  74. for (int i = 0; i < reader->getAttributeCount();++i)
  75. {
  76. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  77. {
  78. out.name = std::string( reader->getAttributeValue(i) );
  79. }
  80. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  81. {
  82. // parse the ecimal value
  83. out.value = strtol10(reader->getAttributeValue(i));
  84. }
  85. }
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // read a string property
  89. void IrrlichtBase::ReadStringProperty (StringProperty& out)
  90. {
  91. for (int i = 0; i < reader->getAttributeCount();++i)
  92. {
  93. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  94. {
  95. out.name = std::string( reader->getAttributeValue(i) );
  96. }
  97. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  98. {
  99. // simple copy the string
  100. out.value = std::string (reader->getAttributeValue(i));
  101. }
  102. }
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // read a boolean property
  106. void IrrlichtBase::ReadBoolProperty (BoolProperty& out)
  107. {
  108. for (int i = 0; i < reader->getAttributeCount();++i)
  109. {
  110. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  111. {
  112. out.name = std::string( reader->getAttributeValue(i) );
  113. }
  114. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  115. {
  116. // true or false, case insensitive
  117. out.value = (ASSIMP_stricmp( reader->getAttributeValue(i),
  118. "true") ? false : true);
  119. }
  120. }
  121. }
  122. // ------------------------------------------------------------------------------------------------
  123. // read a float property
  124. void IrrlichtBase::ReadFloatProperty (FloatProperty& out)
  125. {
  126. for (int i = 0; i < reader->getAttributeCount();++i)
  127. {
  128. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  129. {
  130. out.name = std::string( reader->getAttributeValue(i) );
  131. }
  132. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  133. {
  134. // just parse the float
  135. out.value = fast_atof( reader->getAttributeValue(i) );
  136. }
  137. }
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. // read a vector property
  141. void IrrlichtBase::ReadVectorProperty (VectorProperty& out)
  142. {
  143. for (int i = 0; i < reader->getAttributeCount();++i)
  144. {
  145. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  146. {
  147. out.name = std::string( reader->getAttributeValue(i) );
  148. }
  149. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  150. {
  151. // three floats, separated with commas
  152. const char* ptr = reader->getAttributeValue(i);
  153. SkipSpaces(&ptr);
  154. ptr = fast_atoreal_move<float>( ptr,(float&)out.value.x );
  155. SkipSpaces(&ptr);
  156. if (',' != *ptr)
  157. {
  158. ASSIMP_LOG_ERROR("IRR(MESH): Expected comma in vector definition");
  159. }
  160. else SkipSpaces(ptr+1,&ptr);
  161. ptr = fast_atoreal_move<float>( ptr,(float&)out.value.y );
  162. SkipSpaces(&ptr);
  163. if (',' != *ptr)
  164. {
  165. ASSIMP_LOG_ERROR("IRR(MESH): Expected comma in vector definition");
  166. }
  167. else SkipSpaces(ptr+1,&ptr);
  168. ptr = fast_atoreal_move<float>( ptr,(float&)out.value.z );
  169. }
  170. }
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. // Convert a string to a proper aiMappingMode
  174. int ConvertMappingMode(const std::string& mode)
  175. {
  176. if (mode == "texture_clamp_repeat")
  177. {
  178. return aiTextureMapMode_Wrap;
  179. }
  180. else if (mode == "texture_clamp_mirror")
  181. return aiTextureMapMode_Mirror;
  182. return aiTextureMapMode_Clamp;
  183. }
  184. // ------------------------------------------------------------------------------------------------
  185. // Parse a material from the XML file
  186. aiMaterial* IrrlichtBase::ParseMaterial(unsigned int& matFlags)
  187. {
  188. aiMaterial* mat = new aiMaterial();
  189. aiColor4D clr;
  190. aiString s;
  191. matFlags = 0; // zero output flags
  192. int cnt = 0; // number of used texture channels
  193. unsigned int nd = 0;
  194. // Continue reading from the file
  195. while (reader->read())
  196. {
  197. switch (reader->getNodeType())
  198. {
  199. case EXN_ELEMENT:
  200. // Hex properties
  201. if (!ASSIMP_stricmp(reader->getNodeName(),"color"))
  202. {
  203. HexProperty prop;
  204. ReadHexProperty(prop);
  205. if (prop.name == "Diffuse")
  206. {
  207. ColorFromARGBPacked(prop.value,clr);
  208. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  209. }
  210. else if (prop.name == "Ambient")
  211. {
  212. ColorFromARGBPacked(prop.value,clr);
  213. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_AMBIENT);
  214. }
  215. else if (prop.name == "Specular")
  216. {
  217. ColorFromARGBPacked(prop.value,clr);
  218. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_SPECULAR);
  219. }
  220. // NOTE: The 'emissive' property causes problems. It is
  221. // often != 0, even if there is obviously no light
  222. // emitted by the described surface. In fact I think
  223. // IRRLICHT ignores this property, too.
  224. #if 0
  225. else if (prop.name == "Emissive")
  226. {
  227. ColorFromARGBPacked(prop.value,clr);
  228. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
  229. }
  230. #endif
  231. }
  232. // Float properties
  233. else if (!ASSIMP_stricmp(reader->getNodeName(),"float"))
  234. {
  235. FloatProperty prop;
  236. ReadFloatProperty(prop);
  237. if (prop.name == "Shininess")
  238. {
  239. mat->AddProperty(&prop.value,1,AI_MATKEY_SHININESS);
  240. }
  241. }
  242. // Bool properties
  243. else if (!ASSIMP_stricmp(reader->getNodeName(),"bool"))
  244. {
  245. BoolProperty prop;
  246. ReadBoolProperty(prop);
  247. if (prop.name == "Wireframe")
  248. {
  249. int val = (prop.value ? true : false);
  250. mat->AddProperty(&val,1,AI_MATKEY_ENABLE_WIREFRAME);
  251. }
  252. else if (prop.name == "GouraudShading")
  253. {
  254. int val = (prop.value ? aiShadingMode_Gouraud
  255. : aiShadingMode_NoShading);
  256. mat->AddProperty(&val,1,AI_MATKEY_SHADING_MODEL);
  257. }
  258. else if (prop.name == "BackfaceCulling")
  259. {
  260. int val = (!prop.value);
  261. mat->AddProperty(&val,1,AI_MATKEY_TWOSIDED);
  262. }
  263. }
  264. // String properties - textures and texture related properties
  265. else if (!ASSIMP_stricmp(reader->getNodeName(),"texture") ||
  266. !ASSIMP_stricmp(reader->getNodeName(),"enum"))
  267. {
  268. StringProperty prop;
  269. ReadStringProperty(prop);
  270. if (prop.value.length())
  271. {
  272. // material type (shader)
  273. if (prop.name == "Type")
  274. {
  275. if (prop.value == "solid")
  276. {
  277. // default material ...
  278. }
  279. else if (prop.value == "trans_vertex_alpha")
  280. {
  281. matFlags = AI_IRRMESH_MAT_trans_vertex_alpha;
  282. }
  283. else if (prop.value == "lightmap")
  284. {
  285. matFlags = AI_IRRMESH_MAT_lightmap;
  286. }
  287. else if (prop.value == "solid_2layer")
  288. {
  289. matFlags = AI_IRRMESH_MAT_solid_2layer;
  290. }
  291. else if (prop.value == "lightmap_m2")
  292. {
  293. matFlags = AI_IRRMESH_MAT_lightmap_m2;
  294. }
  295. else if (prop.value == "lightmap_m4")
  296. {
  297. matFlags = AI_IRRMESH_MAT_lightmap_m4;
  298. }
  299. else if (prop.value == "lightmap_light")
  300. {
  301. matFlags = AI_IRRMESH_MAT_lightmap_light;
  302. }
  303. else if (prop.value == "lightmap_light_m2")
  304. {
  305. matFlags = AI_IRRMESH_MAT_lightmap_light_m2;
  306. }
  307. else if (prop.value == "lightmap_light_m4")
  308. {
  309. matFlags = AI_IRRMESH_MAT_lightmap_light_m4;
  310. }
  311. else if (prop.value == "lightmap_add")
  312. {
  313. matFlags = AI_IRRMESH_MAT_lightmap_add;
  314. }
  315. // Normal and parallax maps are treated equally
  316. else if (prop.value == "normalmap_solid" ||
  317. prop.value == "parallaxmap_solid")
  318. {
  319. matFlags = AI_IRRMESH_MAT_normalmap_solid;
  320. }
  321. else if (prop.value == "normalmap_trans_vertex_alpha" ||
  322. prop.value == "parallaxmap_trans_vertex_alpha")
  323. {
  324. matFlags = AI_IRRMESH_MAT_normalmap_tva;
  325. }
  326. else if (prop.value == "normalmap_trans_add" ||
  327. prop.value == "parallaxmap_trans_add")
  328. {
  329. matFlags = AI_IRRMESH_MAT_normalmap_ta;
  330. }
  331. else {
  332. ASSIMP_LOG_WARN("IRRMat: Unrecognized material type: " + prop.value);
  333. }
  334. }
  335. // Up to 4 texture channels are supported
  336. if (prop.name == "Texture1")
  337. {
  338. // Always accept the primary texture channel
  339. ++cnt;
  340. s.Set(prop.value);
  341. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  342. }
  343. else if (prop.name == "Texture2" && cnt == 1)
  344. {
  345. // 2-layer material lightmapped?
  346. if (matFlags & AI_IRRMESH_MAT_lightmap) {
  347. ++cnt;
  348. s.Set(prop.value);
  349. mat->AddProperty(&s,AI_MATKEY_TEXTURE_LIGHTMAP(0));
  350. // set the corresponding material flag
  351. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  352. }
  353. // alternatively: normal or parallax mapping
  354. else if (matFlags & AI_IRRMESH_MAT_normalmap_solid) {
  355. ++cnt;
  356. s.Set(prop.value);
  357. mat->AddProperty(&s,AI_MATKEY_TEXTURE_NORMALS(0));
  358. // set the corresponding material flag
  359. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  360. } else if (matFlags & AI_IRRMESH_MAT_solid_2layer) {// or just as second diffuse texture
  361. ++cnt;
  362. s.Set(prop.value);
  363. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(1));
  364. ++nd;
  365. // set the corresponding material flag
  366. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  367. } else {
  368. ASSIMP_LOG_WARN("IRRmat: Skipping second texture");
  369. }
  370. } else if (prop.name == "Texture3" && cnt == 2) {
  371. // Irrlicht does not seem to use these channels.
  372. ++cnt;
  373. s.Set(prop.value);
  374. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(nd+1));
  375. } else if (prop.name == "Texture4" && cnt == 3) {
  376. // Irrlicht does not seem to use these channels.
  377. ++cnt;
  378. s.Set(prop.value);
  379. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(nd+2));
  380. }
  381. // Texture mapping options
  382. if (prop.name == "TextureWrap1" && cnt >= 1)
  383. {
  384. int map = ConvertMappingMode(prop.value);
  385. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  386. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  387. }
  388. else if (prop.name == "TextureWrap2" && cnt >= 2)
  389. {
  390. int map = ConvertMappingMode(prop.value);
  391. if (matFlags & AI_IRRMESH_MAT_lightmap) {
  392. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_LIGHTMAP(0));
  393. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_LIGHTMAP(0));
  394. }
  395. else if (matFlags & (AI_IRRMESH_MAT_normalmap_solid)) {
  396. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_NORMALS(0));
  397. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_NORMALS(0));
  398. }
  399. else if (matFlags & AI_IRRMESH_MAT_solid_2layer) {
  400. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(1));
  401. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(1));
  402. }
  403. }
  404. else if (prop.name == "TextureWrap3" && cnt >= 3)
  405. {
  406. int map = ConvertMappingMode(prop.value);
  407. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd+1));
  408. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd+1));
  409. }
  410. else if (prop.name == "TextureWrap4" && cnt >= 4)
  411. {
  412. int map = ConvertMappingMode(prop.value);
  413. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd+2));
  414. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd+2));
  415. }
  416. }
  417. }
  418. break;
  419. case EXN_ELEMENT_END:
  420. /* Assume there are no further nested nodes in <material> elements
  421. */
  422. if (/* IRRMESH */ !ASSIMP_stricmp(reader->getNodeName(),"material") ||
  423. /* IRR */ !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  424. {
  425. // Now process lightmapping flags
  426. // We should have at least one textur to do that ..
  427. if (cnt && matFlags & AI_IRRMESH_MAT_lightmap)
  428. {
  429. float f = 1.f;
  430. unsigned int unmasked = matFlags&~AI_IRRMESH_MAT_lightmap;
  431. // Additive lightmap?
  432. int op = (unmasked & AI_IRRMESH_MAT_lightmap_add
  433. ? aiTextureOp_Add : aiTextureOp_Multiply);
  434. // Handle Irrlicht's lightmapping scaling factor
  435. if (unmasked & AI_IRRMESH_MAT_lightmap_m2 ||
  436. unmasked & AI_IRRMESH_MAT_lightmap_light_m2)
  437. {
  438. f = 2.f;
  439. }
  440. else if (unmasked & AI_IRRMESH_MAT_lightmap_m4 ||
  441. unmasked & AI_IRRMESH_MAT_lightmap_light_m4)
  442. {
  443. f = 4.f;
  444. }
  445. mat->AddProperty( &f, 1, AI_MATKEY_TEXBLEND_LIGHTMAP(0));
  446. mat->AddProperty( &op,1, AI_MATKEY_TEXOP_LIGHTMAP(0));
  447. }
  448. return mat;
  449. }
  450. default:
  451. // GCC complains here ...
  452. break;
  453. }
  454. }
  455. ASSIMP_LOG_ERROR("IRRMESH: Unexpected end of file. Material is not complete");
  456. return mat;
  457. }
  458. #endif // !(defined(ASSIMP_BUILD_NO_IRR_IMPORTER) && defined(ASSIMP_BUILD_NO_IRRMESH_IMPORTER))