Coverage Report

Created: 2025-11-11 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/Irr/IRRShared.cpp
Line
Count
Source
1
/*
2
---------------------------------------------------------------------------
3
Open Asset Import Library (assimp)
4
---------------------------------------------------------------------------
5
6
Copyright (c) 2006-2025, assimp team
7
8
All rights reserved.
9
10
Redistribution and use of this software in source and binary forms,
11
with or without modification, are permitted provided that the following
12
conditions are met:
13
14
* Redistributions of source code must retain the above
15
  copyright notice, this list of conditions and the
16
  following disclaimer.
17
18
* Redistributions in binary form must reproduce the above
19
  copyright notice, this list of conditions and the
20
  following disclaimer in the documentation and/or other
21
  materials provided with the distribution.
22
23
* Neither the name of the assimp team, nor the names of its
24
  contributors may be used to endorse or promote products
25
  derived from this software without specific prior
26
  written permission of the assimp team.
27
28
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
---------------------------------------------------------------------------
40
*/
41
42
/** @file  IRRShared.cpp
43
 *  @brief Shared utilities for the IRR and IRRMESH loaders
44
 */
45
46
// This section should be excluded only if both the Irrlicht AND the Irrlicht Mesh importers were omitted.
47
#if !(defined(ASSIMP_BUILD_NO_IRR_IMPORTER) && defined(ASSIMP_BUILD_NO_IRRMESH_IMPORTER))
48
49
#include "IRRShared.h"
50
#include <assimp/ParsingUtils.h>
51
#include <assimp/fast_atof.h>
52
#include <assimp/material.h>
53
#include <assimp/DefaultLogger.hpp>
54
55
using namespace Assimp;
56
57
// Transformation matrix to convert from Assimp to IRR space
58
const aiMatrix4x4 Assimp::AI_TO_IRR_MATRIX = aiMatrix4x4(
59
        1.0f, 0.0f, 0.0f, 0.0f,
60
        0.0f, 0.0f, 1.0f, 0.0f,
61
        0.0f, 1.0f, 0.0f, 0.0f,
62
        0.0f, 0.0f, 0.0f, 1.0f);
63
64
// ------------------------------------------------------------------------------------------------
65
// read a property in hexadecimal format (i.e. ffffffff)
66
0
void IrrlichtBase::ReadHexProperty(HexProperty &out, pugi::xml_node& hexnode) {
67
0
    for (pugi::xml_attribute attrib : hexnode.attributes()) {
68
0
        if (!ASSIMP_stricmp(attrib.name(), "name")) {
69
0
            out.name = std::string(attrib.value());
70
0
        } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
71
            // parse the hexadecimal value
72
0
            out.value = strtoul16(attrib.value());
73
0
        }
74
0
    }
75
0
}
76
77
// ------------------------------------------------------------------------------------------------
78
// read a decimal property
79
0
void IrrlichtBase::ReadIntProperty(IntProperty &out, pugi::xml_node& intnode) {
80
0
    for (pugi::xml_attribute attrib : intnode.attributes()) {
81
0
        if (!ASSIMP_stricmp(attrib.name(), "name")) {
82
0
            out.name = std::string(attrib.value());
83
0
        } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
84
            // parse the int value
85
0
            out.value = strtol10(attrib.value());
86
0
        }
87
0
    }
88
0
}
89
90
// ------------------------------------------------------------------------------------------------
91
// read a string property
92
0
void IrrlichtBase::ReadStringProperty(StringProperty &out, pugi::xml_node& stringnode) {
93
0
    for (pugi::xml_attribute attrib : stringnode.attributes()) {
94
0
        if (!ASSIMP_stricmp(attrib.name(), "name")) {
95
0
            out.name = std::string(attrib.value());
96
0
        } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
97
            // simple copy the string
98
0
            out.value = std::string(attrib.value());
99
0
        }
100
0
    }
101
0
}
102
103
// ------------------------------------------------------------------------------------------------
104
// read a boolean property
105
0
void IrrlichtBase::ReadBoolProperty(BoolProperty &out, pugi::xml_node& boolnode) {
106
0
    for (pugi::xml_attribute attrib : boolnode.attributes()) {
107
0
        if (!ASSIMP_stricmp(attrib.name(), "name")) {
108
0
            out.name = std::string(attrib.value());
109
0
        } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
110
            // true or false, case insensitive
111
0
            out.value = (ASSIMP_stricmp(attrib.value(), "true") ? false : true);
112
0
        }
113
0
    }
114
0
}
115
116
// ------------------------------------------------------------------------------------------------
117
// read a float property
118
0
void IrrlichtBase::ReadFloatProperty(FloatProperty &out, pugi::xml_node &floatnode) {
119
0
    for (pugi::xml_attribute attrib : floatnode.attributes()) {
120
0
        if (!ASSIMP_stricmp(attrib.name(), "name")) {
121
0
            out.name = std::string(attrib.value());
122
0
        } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
123
            // just parse the float
124
0
            out.value = fast_atof(attrib.value());
125
0
        }
126
0
    }
127
0
}
128
129
// ------------------------------------------------------------------------------------------------
130
// read a vector property
131
0
void IrrlichtBase::ReadVectorProperty(VectorProperty &out, pugi::xml_node& vectornode) {
132
0
    for (pugi::xml_attribute attrib : vectornode.attributes()) {
133
0
        if (!ASSIMP_stricmp(attrib.name(), "name")) {
134
0
            out.name = std::string(attrib.value());
135
0
        } else if (!ASSIMP_stricmp(attrib.name(), "value")) {
136
            // three floats, separated with commas
137
0
            const char *ptr = attrib.value();
138
0
            size_t len = std::strlen(ptr);
139
0
            const char *end = ptr + len;
140
141
0
            SkipSpaces(&ptr, end);
142
0
            ptr = fast_atoreal_move(ptr, out.value.x);
143
0
            SkipSpaces(&ptr, end);
144
0
            if (',' != *ptr) {
145
0
                ASSIMP_LOG_ERROR("IRR(MESH): Expected comma in vector definition");
146
0
            } else {
147
0
                SkipSpaces(ptr + 1, &ptr, end);
148
0
            }
149
0
            ptr = fast_atoreal_move(ptr, out.value.y);
150
0
            SkipSpaces(&ptr, end);
151
0
            if (',' != *ptr) {
152
0
                ASSIMP_LOG_ERROR("IRR(MESH): Expected comma in vector definition");
153
0
            } else {
154
0
                SkipSpaces(ptr + 1, &ptr, end);
155
0
            }
156
0
            ptr = fast_atoreal_move(ptr, out.value.z);
157
0
        }
158
0
    }
159
0
}
160
161
// ------------------------------------------------------------------------------------------------
162
// Convert a string to a proper aiMappingMode
163
0
int ConvertMappingMode(const std::string &mode) {
164
0
    if (mode == "texture_clamp_repeat") {
165
0
        return aiTextureMapMode_Wrap;
166
0
    } else if (mode == "texture_clamp_mirror") {
167
0
        return aiTextureMapMode_Mirror;
168
0
    }
169
170
0
    return aiTextureMapMode_Clamp;
171
0
}
172
173
// ------------------------------------------------------------------------------------------------
174
// Parse a material from the XML file
175
0
aiMaterial *IrrlichtBase::ParseMaterial(pugi::xml_node& materialNode, unsigned int &matFlags) {
176
0
    aiMaterial *mat = new aiMaterial();
177
0
    aiColor4D clr;
178
0
    aiString s;
179
180
0
    matFlags = 0; // zero output flags
181
0
    int cnt = 0; // number of used texture channels
182
0
    unsigned int nd = 0;
183
184
0
    for (pugi::xml_node child : materialNode.children()) {
185
0
        if (!ASSIMP_stricmp(child.name(), "color")) { // Hex properties
186
0
            HexProperty prop;
187
0
            ReadHexProperty(prop, child);
188
0
            if (prop.name == "Diffuse") {
189
0
                ColorFromARGBPacked(prop.value, clr);
190
0
                mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
191
0
            } else if (prop.name == "Ambient") {
192
0
                ColorFromARGBPacked(prop.value, clr);
193
0
                mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_AMBIENT);
194
0
            } else if (prop.name == "Specular") {
195
0
                ColorFromARGBPacked(prop.value, clr);
196
0
                mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
197
0
            }
198
199
            // NOTE: The 'emissive' property causes problems. It is
200
            // often != 0, even if there is obviously no light
201
            // emitted by the described surface. In fact I think
202
            // IRRLICHT ignores this property, too.
203
#if 0
204
            else if (prop.name == "Emissive") {
205
                ColorFromARGBPacked(prop.value,clr);
206
                mat->AddProperty(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
207
            }
208
#endif
209
0
        } else if (!ASSIMP_stricmp(child.name(), "float")) { // Float properties
210
0
            FloatProperty prop;
211
0
            ReadFloatProperty(prop, child);
212
0
            if (prop.name == "Shininess") {
213
0
                mat->AddProperty(&prop.value, 1, AI_MATKEY_SHININESS);
214
0
            }
215
0
        } else if (!ASSIMP_stricmp(child.name(), "bool")) { // Bool properties
216
0
            BoolProperty prop;
217
0
            ReadBoolProperty(prop, child);
218
0
            if (prop.name == "Wireframe") {
219
0
                int val = (prop.value ? true : false);
220
0
                mat->AddProperty(&val, 1, AI_MATKEY_ENABLE_WIREFRAME);
221
0
            } else if (prop.name == "GouraudShading") {
222
0
                int val = (prop.value ? aiShadingMode_Gouraud : aiShadingMode_NoShading);
223
0
                mat->AddProperty(&val, 1, AI_MATKEY_SHADING_MODEL);
224
0
            } else if (prop.name == "BackfaceCulling") {
225
0
                int val = (!prop.value);
226
0
                mat->AddProperty(&val, 1, AI_MATKEY_TWOSIDED);
227
0
            }
228
0
        } else if (!ASSIMP_stricmp(child.name(), "texture") ||
229
0
                   !ASSIMP_stricmp(child.name(), "enum")) { // String properties - textures and texture related properties
230
0
            StringProperty prop;
231
0
            ReadStringProperty(prop, child);
232
0
            if (prop.value.length()) {
233
                // material type (shader)
234
0
                if (prop.name == "Type") {
235
0
                    if (prop.value == "solid") {
236
                        // default material ...
237
0
                    } else if (prop.value == "trans_vertex_alpha") {
238
0
                        matFlags = AI_IRRMESH_MAT_trans_vertex_alpha;
239
0
                    } else if (prop.value == "lightmap") {
240
0
                        matFlags = AI_IRRMESH_MAT_lightmap;
241
0
                    } else if (prop.value == "solid_2layer") {
242
0
                        matFlags = AI_IRRMESH_MAT_solid_2layer;
243
0
                    } else if (prop.value == "lightmap_m2") {
244
0
                        matFlags = AI_IRRMESH_MAT_lightmap_m2;
245
0
                    } else if (prop.value == "lightmap_m4") {
246
0
                        matFlags = AI_IRRMESH_MAT_lightmap_m4;
247
0
                    } else if (prop.value == "lightmap_light") {
248
0
                        matFlags = AI_IRRMESH_MAT_lightmap_light;
249
0
                    } else if (prop.value == "lightmap_light_m2") {
250
0
                        matFlags = AI_IRRMESH_MAT_lightmap_light_m2;
251
0
                    } else if (prop.value == "lightmap_light_m4") {
252
0
                        matFlags = AI_IRRMESH_MAT_lightmap_light_m4;
253
0
                    } else if (prop.value == "lightmap_add") {
254
0
                        matFlags = AI_IRRMESH_MAT_lightmap_add;
255
0
                    } else if (prop.value == "normalmap_solid" ||
256
0
                               prop.value == "parallaxmap_solid") { // Normal and parallax maps are treated equally
257
0
                        matFlags = AI_IRRMESH_MAT_normalmap_solid;
258
0
                    } else if (prop.value == "normalmap_trans_vertex_alpha" ||
259
0
                               prop.value == "parallaxmap_trans_vertex_alpha") {
260
0
                        matFlags = AI_IRRMESH_MAT_normalmap_tva;
261
0
                    } else if (prop.value == "normalmap_trans_add" ||
262
0
                               prop.value == "parallaxmap_trans_add") {
263
0
                        matFlags = AI_IRRMESH_MAT_normalmap_ta;
264
0
                    } else {
265
0
                        ASSIMP_LOG_WARN("IRRMat: Unrecognized material type: ", prop.value);
266
0
                    }
267
0
                }
268
269
                // Up to 4 texture channels are supported
270
0
                if (prop.name == "Texture1") {
271
                    // Always accept the primary texture channel
272
0
                    ++cnt;
273
0
                    s.Set(prop.value);
274
0
                    mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
275
0
                } else if (prop.name == "Texture2" && cnt == 1) {
276
                    // 2-layer material lightmapped?
277
0
                    if (matFlags & AI_IRRMESH_MAT_lightmap) {
278
0
                        ++cnt;
279
0
                        s.Set(prop.value);
280
0
                        mat->AddProperty(&s, AI_MATKEY_TEXTURE_LIGHTMAP(0));
281
282
                        // set the corresponding material flag
283
0
                        matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
284
0
                    } else if (matFlags & AI_IRRMESH_MAT_normalmap_solid) { // alternatively: normal or parallax mapping
285
0
                        ++cnt;
286
0
                        s.Set(prop.value);
287
0
                        mat->AddProperty(&s, AI_MATKEY_TEXTURE_NORMALS(0));
288
289
                        // set the corresponding material flag
290
0
                        matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
291
0
                    } else if (matFlags & AI_IRRMESH_MAT_solid_2layer) { // or just as second diffuse texture
292
0
                        ++cnt;
293
0
                        s.Set(prop.value);
294
0
                        mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(1));
295
0
                        ++nd;
296
297
                        // set the corresponding material flag
298
0
                        matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
299
0
                    } else {
300
0
                        ASSIMP_LOG_WARN("IRRmat: Skipping second texture");
301
0
                    }
302
0
                } else if (prop.name == "Texture3" && cnt == 2) {
303
                    // Irrlicht does not seem to use these channels.
304
0
                    ++cnt;
305
0
                    s.Set(prop.value);
306
0
                    mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(nd + 1));
307
0
                } else if (prop.name == "Texture4" && cnt == 3) {
308
                    // Irrlicht does not seem to use these channels.
309
0
                    ++cnt;
310
0
                    s.Set(prop.value);
311
0
                    mat->AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(nd + 2));
312
0
                }
313
314
                // Texture mapping options
315
0
                if (prop.name == "TextureWrap1" && cnt >= 1) {
316
0
                    int map = ConvertMappingMode(prop.value);
317
0
                    mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
318
0
                    mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
319
0
                } else if (prop.name == "TextureWrap2" && cnt >= 2) {
320
0
                    int map = ConvertMappingMode(prop.value);
321
0
                    if (matFlags & AI_IRRMESH_MAT_lightmap) {
322
0
                        mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_LIGHTMAP(0));
323
0
                        mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_LIGHTMAP(0));
324
0
                    } else if (matFlags & (AI_IRRMESH_MAT_normalmap_solid)) {
325
0
                        mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_NORMALS(0));
326
0
                        mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_NORMALS(0));
327
0
                    } else if (matFlags & AI_IRRMESH_MAT_solid_2layer) {
328
0
                        mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(1));
329
0
                        mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(1));
330
0
                    }
331
0
                } else if (prop.name == "TextureWrap3" && cnt >= 3) {
332
0
                    int map = ConvertMappingMode(prop.value);
333
0
                    mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd + 1));
334
0
                    mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd + 1));
335
0
                } else if (prop.name == "TextureWrap4" && cnt >= 4) {
336
0
                    int map = ConvertMappingMode(prop.value);
337
0
                    mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_U_DIFFUSE(nd + 2));
338
0
                    mat->AddProperty(&map, 1, AI_MATKEY_MAPPINGMODE_V_DIFFUSE(nd + 2));
339
0
                }
340
0
            }
341
0
        }
342
        // break;
343
        /*case EXN_ELEMENT_END:
344
345
                // Assume there are no further nested nodes in <material> elements
346
                if ( !ASSIMP_stricmp(reader->getNodeName(),"material") ||
347
                     !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
348
                {
349
                    // Now process lightmapping flags
350
                    // We should have at least one textur to do that ..
351
                    if (cnt && matFlags & AI_IRRMESH_MAT_lightmap)
352
                    {
353
                        float f = 1.f;
354
                        unsigned int unmasked = matFlags&~AI_IRRMESH_MAT_lightmap;
355
356
                        // Additive lightmap?
357
                        int op = (unmasked & AI_IRRMESH_MAT_lightmap_add
358
                            ? aiTextureOp_Add : aiTextureOp_Multiply);
359
360
                        // Handle Irrlicht's lightmapping scaling factor
361
                        if (unmasked & AI_IRRMESH_MAT_lightmap_m2 ||
362
                            unmasked & AI_IRRMESH_MAT_lightmap_light_m2)
363
                        {
364
                            f = 2.f;
365
                        }
366
                        else if (unmasked & AI_IRRMESH_MAT_lightmap_m4 ||
367
                            unmasked & AI_IRRMESH_MAT_lightmap_light_m4)
368
                        {
369
                            f = 4.f;
370
                        }
371
                        mat->AddProperty( &f, 1, AI_MATKEY_TEXBLEND_LIGHTMAP(0));
372
                        mat->AddProperty( &op,1, AI_MATKEY_TEXOP_LIGHTMAP(0));
373
                    }
374
375
                    return mat;
376
                }
377
            default:
378
379
                // GCC complains here ...
380
                break;
381
        }
382
    }*/
383
0
    }
384
    //ASSIMP_LOG_ERROR("IRRMESH: Unexpected end of file. Material is not complete");
385
386
0
    return mat;
387
0
}
388
389
#endif // !(defined(ASSIMP_BUILD_NO_IRR_IMPORTER) && defined(ASSIMP_BUILD_NO_IRRMESH_IMPORTER))