IRRShared.cpp 15 KB

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