IRRShared.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, 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/material.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. using namespace Assimp;
  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, pugi::xml_node& hexnode) {
  54. for (pugi::xml_attribute attrib : hexnode.attributes()) {
  55. if (!ASSIMP_stricmp(attrib.name(), "name")) {
  56. out.name = std::string(attrib.value());
  57. } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
  58. // parse the hexadecimal value
  59. out.value = strtoul16(attrib.value());
  60. }
  61. }
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // read a decimal property
  65. void IrrlichtBase::ReadIntProperty(IntProperty &out, pugi::xml_node& intnode) {
  66. for (pugi::xml_attribute attrib : intnode.attributes()) {
  67. if (!ASSIMP_stricmp(attrib.name(), "name")) {
  68. out.name = std::string(attrib.value());
  69. } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
  70. // parse the int value
  71. out.value = strtol10(attrib.value());
  72. }
  73. }
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. // read a string property
  77. void IrrlichtBase::ReadStringProperty(StringProperty &out, pugi::xml_node& stringnode) {
  78. for (pugi::xml_attribute attrib : stringnode.attributes()) {
  79. if (!ASSIMP_stricmp(attrib.name(), "name")) {
  80. out.name = std::string(attrib.value());
  81. } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
  82. // simple copy the string
  83. out.value = std::string(attrib.value());
  84. }
  85. }
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // read a boolean property
  89. void IrrlichtBase::ReadBoolProperty(BoolProperty &out, pugi::xml_node& boolnode) {
  90. for (pugi::xml_attribute attrib : boolnode.attributes()) {
  91. if (!ASSIMP_stricmp(attrib.name(), "name")) {
  92. out.name = std::string(attrib.value());
  93. } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
  94. // true or false, case insensitive
  95. out.value = (ASSIMP_stricmp(attrib.value(), "true") ? false : true);
  96. }
  97. }
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. // read a float property
  101. void IrrlichtBase::ReadFloatProperty(FloatProperty &out, pugi::xml_node &floatnode) {
  102. for (pugi::xml_attribute attrib : floatnode.attributes()) {
  103. if (!ASSIMP_stricmp(attrib.name(), "name")) {
  104. out.name = std::string(attrib.value());
  105. } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
  106. // just parse the float
  107. out.value = fast_atof(attrib.value());
  108. }
  109. }
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. // read a vector property
  113. void IrrlichtBase::ReadVectorProperty(VectorProperty &out, pugi::xml_node& vectornode) {
  114. for (pugi::xml_attribute attrib : vectornode.attributes()) {
  115. if (!ASSIMP_stricmp(attrib.name(), "name")) {
  116. out.name = std::string(attrib.value());
  117. } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
  118. // three floats, separated with commas
  119. const char *ptr = attrib.value();
  120. size_t len = std::strlen(ptr);
  121. const char *end = ptr + len;
  122. SkipSpaces(&ptr, end);
  123. ptr = fast_atoreal_move<float>(ptr, (float &)out.value.x);
  124. SkipSpaces(&ptr, end);
  125. if (',' != *ptr) {
  126. ASSIMP_LOG_ERROR("IRR(MESH): Expected comma in vector definition");
  127. } else {
  128. SkipSpaces(ptr + 1, &ptr, end);
  129. }
  130. ptr = fast_atoreal_move<float>(ptr, (float &)out.value.y);
  131. SkipSpaces(&ptr, end);
  132. if (',' != *ptr) {
  133. ASSIMP_LOG_ERROR("IRR(MESH): Expected comma in vector definition");
  134. } else {
  135. SkipSpaces(ptr + 1, &ptr, end);
  136. }
  137. ptr = fast_atoreal_move<float>(ptr, (float &)out.value.z);
  138. }
  139. }
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. // Convert a string to a proper aiMappingMode
  143. int ConvertMappingMode(const std::string &mode) {
  144. if (mode == "texture_clamp_repeat") {
  145. return aiTextureMapMode_Wrap;
  146. } else if (mode == "texture_clamp_mirror") {
  147. return aiTextureMapMode_Mirror;
  148. }
  149. return aiTextureMapMode_Clamp;
  150. }
  151. // ------------------------------------------------------------------------------------------------
  152. // Parse a material from the XML file
  153. aiMaterial *IrrlichtBase::ParseMaterial(pugi::xml_node& materialNode, unsigned int &matFlags) {
  154. aiMaterial *mat = new aiMaterial();
  155. aiColor4D clr;
  156. aiString s;
  157. matFlags = 0; // zero output flags
  158. int cnt = 0; // number of used texture channels
  159. unsigned int nd = 0;
  160. for (pugi::xml_node child : materialNode.children()) {
  161. if (!ASSIMP_stricmp(child.name(), "color")) { // Hex properties
  162. HexProperty prop;
  163. ReadHexProperty(prop, child);
  164. if (prop.name == "Diffuse") {
  165. ColorFromARGBPacked(prop.value, clr);
  166. mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
  167. } else if (prop.name == "Ambient") {
  168. ColorFromARGBPacked(prop.value, clr);
  169. mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_AMBIENT);
  170. } else if (prop.name == "Specular") {
  171. ColorFromARGBPacked(prop.value, clr);
  172. mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
  173. }
  174. // NOTE: The 'emissive' property causes problems. It is
  175. // often != 0, even if there is obviously no light
  176. // emitted by the described surface. In fact I think
  177. // IRRLICHT ignores this property, too.
  178. #if 0
  179. else if (prop.name == "Emissive") {
  180. ColorFromARGBPacked(prop.value,clr);
  181. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
  182. }
  183. #endif
  184. } else if (!ASSIMP_stricmp(child.name(), "float")) { // Float properties
  185. FloatProperty prop;
  186. ReadFloatProperty(prop, child);
  187. if (prop.name == "Shininess") {
  188. mat->AddProperty(&prop.value, 1, AI_MATKEY_SHININESS);
  189. }
  190. } else if (!ASSIMP_stricmp(child.name(), "bool")) { // Bool properties
  191. BoolProperty prop;
  192. ReadBoolProperty(prop, child);
  193. if (prop.name == "Wireframe") {
  194. int val = (prop.value ? true : false);
  195. mat->AddProperty(&val, 1, AI_MATKEY_ENABLE_WIREFRAME);
  196. } else if (prop.name == "GouraudShading") {
  197. int val = (prop.value ? aiShadingMode_Gouraud : aiShadingMode_NoShading);
  198. mat->AddProperty(&val, 1, AI_MATKEY_SHADING_MODEL);
  199. } else if (prop.name == "BackfaceCulling") {
  200. int val = (!prop.value);
  201. mat->AddProperty(&val, 1, AI_MATKEY_TWOSIDED);
  202. }
  203. } else if (!ASSIMP_stricmp(child.name(), "texture") ||
  204. !ASSIMP_stricmp(child.name(), "enum")) { // String properties - textures and texture related properties
  205. StringProperty prop;
  206. ReadStringProperty(prop, child);
  207. if (prop.value.length()) {
  208. // material type (shader)
  209. if (prop.name == "Type") {
  210. if (prop.value == "solid") {
  211. // default material ...
  212. } else if (prop.value == "trans_vertex_alpha") {
  213. matFlags = AI_IRRMESH_MAT_trans_vertex_alpha;
  214. } else if (prop.value == "lightmap") {
  215. matFlags = AI_IRRMESH_MAT_lightmap;
  216. } else if (prop.value == "solid_2layer") {
  217. matFlags = AI_IRRMESH_MAT_solid_2layer;
  218. } else if (prop.value == "lightmap_m2") {
  219. matFlags = AI_IRRMESH_MAT_lightmap_m2;
  220. } else if (prop.value == "lightmap_m4") {
  221. matFlags = AI_IRRMESH_MAT_lightmap_m4;
  222. } else if (prop.value == "lightmap_light") {
  223. matFlags = AI_IRRMESH_MAT_lightmap_light;
  224. } else if (prop.value == "lightmap_light_m2") {
  225. matFlags = AI_IRRMESH_MAT_lightmap_light_m2;
  226. } else if (prop.value == "lightmap_light_m4") {
  227. matFlags = AI_IRRMESH_MAT_lightmap_light_m4;
  228. } else if (prop.value == "lightmap_add") {
  229. matFlags = AI_IRRMESH_MAT_lightmap_add;
  230. } else if (prop.value == "normalmap_solid" ||
  231. prop.value == "parallaxmap_solid") { // Normal and parallax maps are treated equally
  232. matFlags = AI_IRRMESH_MAT_normalmap_solid;
  233. } else if (prop.value == "normalmap_trans_vertex_alpha" ||
  234. prop.value == "parallaxmap_trans_vertex_alpha") {
  235. matFlags = AI_IRRMESH_MAT_normalmap_tva;
  236. } else if (prop.value == "normalmap_trans_add" ||
  237. prop.value == "parallaxmap_trans_add") {
  238. matFlags = AI_IRRMESH_MAT_normalmap_ta;
  239. } else {
  240. ASSIMP_LOG_WARN("IRRMat: Unrecognized material type: ", prop.value);
  241. }
  242. }
  243. // Up to 4 texture channels are supported
  244. if (prop.name == "Texture1") {
  245. // Always accept the primary texture channel
  246. ++cnt;
  247. s.Set(prop.value);
  248. mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
  249. } else if (prop.name == "Texture2" && cnt == 1) {
  250. // 2-layer material lightmapped?
  251. if (matFlags & AI_IRRMESH_MAT_lightmap) {
  252. ++cnt;
  253. s.Set(prop.value);
  254. mat->AddProperty(&s, AI_MATKEY_TEXTURE_LIGHTMAP(0));
  255. // set the corresponding material flag
  256. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  257. } else if (matFlags & AI_IRRMESH_MAT_normalmap_solid) { // alternatively: normal or parallax mapping
  258. ++cnt;
  259. s.Set(prop.value);
  260. mat->AddProperty(&s, AI_MATKEY_TEXTURE_NORMALS(0));
  261. // set the corresponding material flag
  262. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  263. } else if (matFlags & AI_IRRMESH_MAT_solid_2layer) { // or just as second diffuse texture
  264. ++cnt;
  265. s.Set(prop.value);
  266. mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(1));
  267. ++nd;
  268. // set the corresponding material flag
  269. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  270. } else {
  271. ASSIMP_LOG_WARN("IRRmat: Skipping second texture");
  272. }
  273. } else if (prop.name == "Texture3" && cnt == 2) {
  274. // Irrlicht does not seem to use these channels.
  275. ++cnt;
  276. s.Set(prop.value);
  277. mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(nd + 1));
  278. } else if (prop.name == "Texture4" && cnt == 3) {
  279. // Irrlicht does not seem to use these channels.
  280. ++cnt;
  281. s.Set(prop.value);
  282. mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(nd + 2));
  283. }
  284. // Texture mapping options
  285. if (prop.name == "TextureWrap1" && cnt >= 1) {
  286. int map = ConvertMappingMode(prop.value);
  287. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  288. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  289. } else if (prop.name == "TextureWrap2" && cnt >= 2) {
  290. int map = ConvertMappingMode(prop.value);
  291. if (matFlags & AI_IRRMESH_MAT_lightmap) {
  292. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_LIGHTMAP(0));
  293. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_LIGHTMAP(0));
  294. } else if (matFlags & (AI_IRRMESH_MAT_normalmap_solid)) {
  295. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_NORMALS(0));
  296. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_NORMALS(0));
  297. } else if (matFlags & AI_IRRMESH_MAT_solid_2layer) {
  298. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(1));
  299. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(1));
  300. }
  301. } else if (prop.name == "TextureWrap3" && cnt >= 3) {
  302. int map = ConvertMappingMode(prop.value);
  303. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd + 1));
  304. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd + 1));
  305. } else if (prop.name == "TextureWrap4" && cnt >= 4) {
  306. int map = ConvertMappingMode(prop.value);
  307. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd + 2));
  308. mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd + 2));
  309. }
  310. }
  311. }
  312. // break;
  313. /*case EXN_ELEMENT_END:
  314. // Assume there are no further nested nodes in <material> elements
  315. if ( !ASSIMP_stricmp(reader->getNodeName(),"material") ||
  316. !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  317. {
  318. // Now process lightmapping flags
  319. // We should have at least one textur to do that ..
  320. if (cnt && matFlags & AI_IRRMESH_MAT_lightmap)
  321. {
  322. float f = 1.f;
  323. unsigned int unmasked = matFlags&~AI_IRRMESH_MAT_lightmap;
  324. // Additive lightmap?
  325. int op = (unmasked & AI_IRRMESH_MAT_lightmap_add
  326. ? aiTextureOp_Add : aiTextureOp_Multiply);
  327. // Handle Irrlicht's lightmapping scaling factor
  328. if (unmasked & AI_IRRMESH_MAT_lightmap_m2 ||
  329. unmasked & AI_IRRMESH_MAT_lightmap_light_m2)
  330. {
  331. f = 2.f;
  332. }
  333. else if (unmasked & AI_IRRMESH_MAT_lightmap_m4 ||
  334. unmasked & AI_IRRMESH_MAT_lightmap_light_m4)
  335. {
  336. f = 4.f;
  337. }
  338. mat->AddProperty( &f, 1, AI_MATKEY_TEXBLEND_LIGHTMAP(0));
  339. mat->AddProperty( &op,1, AI_MATKEY_TEXOP_LIGHTMAP(0));
  340. }
  341. return mat;
  342. }
  343. default:
  344. // GCC complains here ...
  345. break;
  346. }
  347. }*/
  348. }
  349. //ASSIMP_LOG_ERROR("IRRMESH: Unexpected end of file. Material is not complete");
  350. return mat;
  351. }
  352. #endif // !(defined(ASSIMP_BUILD_NO_IRR_IMPORTER) && defined(ASSIMP_BUILD_NO_IRRMESH_IMPORTER))