IRRShared.cpp 16 KB

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