Coverage Report

Created: 2026-01-25 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/X3D/X3DImporter_Light.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2019, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
copyright notice, this list of conditions and the
15
following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
copyright notice, this list of conditions and the
19
following disclaimer in the documentation and/or other
20
materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
contributors may be used to endorse or promote products
24
derived from this software without specific prior
25
written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
/// \file   X3DImporter_Light.cpp
42
/// \brief  Parsing data from nodes of "Lighting" set of X3D.
43
/// date   2015-2016
44
/// author smal.root@gmail.com
45
46
#ifndef ASSIMP_BUILD_NO_X3D_IMPORTER
47
48
#include "X3DImporter.hpp"
49
#include "X3DImporter_Macro.hpp"
50
#include "X3DXmlHelper.h"
51
#include <assimp/StringUtils.h>
52
53
namespace Assimp {
54
55
// <DirectionalLight
56
// DEF=""               ID
57
// USE=""               IDREF
58
// ambientIntensity="0" SFFloat [inputOutput]
59
// color="1 1 1"        SFColor [inputOutput]
60
// direction="0 0 -1"   SFVec3f [inputOutput]
61
// global="false"       SFBool  [inputOutput]
62
// intensity="1"        SFFloat [inputOutput]
63
// on="true"            SFBool  [inputOutput]
64
// />
65
0
void X3DImporter::readDirectionalLight(XmlNode &node) {
66
0
    std::string def, use;
67
0
    float ambientIntensity = 0;
68
0
    aiColor3D color(1, 1, 1);
69
0
    aiVector3D direction(0, 0, -1);
70
0
    bool global = false;
71
0
    float intensity = 1;
72
0
    bool on = true;
73
0
    X3DNodeElementBase *ne(nullptr);
74
75
0
    MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
76
0
    XmlParser::getFloatAttribute(node, "ambientIntensity", ambientIntensity);
77
0
    X3DXmlHelper::getColor3DAttribute(node, "color", color);
78
0
    X3DXmlHelper::getVector3DAttribute(node, "direction", direction);
79
0
    XmlParser::getBoolAttribute(node, "global", global);
80
0
    XmlParser::getFloatAttribute(node, "intensity", intensity);
81
0
    XmlParser::getBoolAttribute(node, "on", on);
82
83
    // if "USE" defined then find already defined element.
84
0
    if (!use.empty()) {
85
0
        ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_DirectionalLight, ne);
86
0
    } else {
87
0
        if (on) {
88
            // create and if needed - define new geometry object.
89
0
            ne = new X3DNodeElementLight(X3DElemType::ENET_DirectionalLight, mNodeElementCur);
90
0
            if (!def.empty())
91
0
                ne->ID = def;
92
0
            else
93
0
                ne->ID = "DirectionalLight_" + ai_to_string((size_t)ne); // make random name
94
95
0
            ((X3DNodeElementLight *)ne)->AmbientIntensity = ambientIntensity;
96
0
            ((X3DNodeElementLight *)ne)->Color = color;
97
0
            ((X3DNodeElementLight *)ne)->Direction = direction;
98
0
            ((X3DNodeElementLight *)ne)->Global = global;
99
0
            ((X3DNodeElementLight *)ne)->Intensity = intensity;
100
            // Assimp want a node with name similar to a light. "Why? I don't no." )
101
0
            ParseHelper_Group_Begin(false);
102
103
0
            mNodeElementCur->ID = ne->ID; // assign name to node and return to light element.
104
0
            ParseHelper_Node_Exit();
105
            // check for child nodes
106
0
            if (!isNodeEmpty(node))
107
0
                childrenReadMetadata(node, ne, "DirectionalLight");
108
0
            else
109
0
                mNodeElementCur->Children.push_back(ne); // add made object as child to current element
110
111
0
            NodeElement_List.push_back(ne); // add element to node element list because its a new object in graph
112
0
        } // if(on)
113
0
    } // if(!use.empty()) else
114
0
}
115
116
// <PointLight
117
// DEF=""               ID
118
// USE=""               IDREF
119
// ambientIntensity="0" SFFloat [inputOutput]
120
// attenuation="1 0 0"  SFVec3f [inputOutput]
121
// color="1 1 1"        SFColor [inputOutput]
122
// global="true"        SFBool  [inputOutput]
123
// intensity="1"        SFFloat [inputOutput]
124
// location="0 0 0"     SFVec3f [inputOutput]
125
// on="true"            SFBool  [inputOutput]
126
// radius="100"         SFFloat [inputOutput]
127
// />
128
0
void X3DImporter::readPointLight(XmlNode &node) {
129
0
    std::string def, use;
130
0
    float ambientIntensity = 0;
131
0
    aiVector3D attenuation(1, 0, 0);
132
0
    aiColor3D color(1, 1, 1);
133
0
    bool global = true;
134
0
    float intensity = 1;
135
0
    aiVector3D location(0, 0, 0);
136
0
    bool on = true;
137
0
    float radius = 100;
138
0
    X3DNodeElementBase *ne(nullptr);
139
140
0
    MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
141
0
    XmlParser::getFloatAttribute(node, "ambientIntensity", ambientIntensity);
142
0
    X3DXmlHelper::getVector3DAttribute(node, "attenuation", attenuation);
143
0
    X3DXmlHelper::getColor3DAttribute(node, "color", color);
144
0
    XmlParser::getBoolAttribute(node, "global", global);
145
0
    XmlParser::getFloatAttribute(node, "intensity", intensity);
146
0
    X3DXmlHelper::getVector3DAttribute(node, "location", location);
147
0
    XmlParser::getBoolAttribute(node, "on", on);
148
0
    XmlParser::getFloatAttribute(node, "radius", radius);
149
150
    // if "USE" defined then find already defined element.
151
0
    if (!use.empty()) {
152
0
        ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_PointLight, ne);
153
0
    } else {
154
0
        if (on) {
155
            // create and if needed - define new geometry object.
156
0
            ne = new X3DNodeElementLight(X3DElemType::ENET_PointLight, mNodeElementCur);
157
0
            if (!def.empty()) ne->ID = def;
158
159
0
            ((X3DNodeElementLight *)ne)->AmbientIntensity = ambientIntensity;
160
0
            ((X3DNodeElementLight *)ne)->Attenuation = attenuation;
161
0
            ((X3DNodeElementLight *)ne)->Color = color;
162
0
            ((X3DNodeElementLight *)ne)->Global = global;
163
0
            ((X3DNodeElementLight *)ne)->Intensity = intensity;
164
0
            ((X3DNodeElementLight *)ne)->Location = location;
165
0
            ((X3DNodeElementLight *)ne)->Radius = radius;
166
            // Assimp want a node with name similar to a light. "Why? I don't no." )
167
0
            ParseHelper_Group_Begin(false);
168
            // make random name
169
0
            if (ne->ID.empty()) ne->ID = "PointLight_" + ai_to_string((size_t)ne);
170
171
0
            mNodeElementCur->ID = ne->ID; // assign name to node and return to light element.
172
0
            ParseHelper_Node_Exit();
173
            // check for child nodes
174
0
            if (!isNodeEmpty(node))
175
0
                childrenReadMetadata(node, ne, "PointLight");
176
0
            else
177
0
                mNodeElementCur->Children.push_back(ne); // add made object as child to current element
178
179
0
            NodeElement_List.push_back(ne); // add element to node element list because its a new object in graph
180
0
        } // if(on)
181
0
    } // if(!use.empty()) else
182
0
}
183
184
// <SpotLight
185
// DEF=""                 ID
186
// USE=""                 IDREF
187
// ambientIntensity="0"   SFFloat [inputOutput]
188
// attenuation="1 0 0"    SFVec3f [inputOutput]
189
// beamWidth="0.7854"     SFFloat [inputOutput]
190
// color="1 1 1"          SFColor [inputOutput]
191
// cutOffAngle="1.570796" SFFloat [inputOutput]
192
// direction="0 0 -1"     SFVec3f [inputOutput]
193
// global="true"          SFBool  [inputOutput]
194
// intensity="1"          SFFloat [inputOutput]
195
// location="0 0 0"       SFVec3f [inputOutput]
196
// on="true"              SFBool  [inputOutput]
197
// radius="100"           SFFloat [inputOutput]
198
// />
199
0
void X3DImporter::readSpotLight(XmlNode &node) {
200
0
    std::string def, use;
201
0
    float ambientIntensity = 0;
202
0
    aiVector3D attenuation(1, 0, 0);
203
0
    float beamWidth = 0.7854f;
204
0
    aiColor3D color(1, 1, 1);
205
0
    float cutOffAngle = 1.570796f;
206
0
    aiVector3D direction(0, 0, -1);
207
0
    bool global = true;
208
0
    float intensity = 1;
209
0
    aiVector3D location(0, 0, 0);
210
0
    bool on = true;
211
0
    float radius = 100;
212
0
    X3DNodeElementBase *ne(nullptr);
213
214
0
    MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
215
0
    XmlParser::getFloatAttribute(node, "ambientIntensity", ambientIntensity);
216
0
    X3DXmlHelper::getVector3DAttribute(node, "attenuation", attenuation);
217
0
    XmlParser::getFloatAttribute(node, "beamWidth", beamWidth);
218
0
    X3DXmlHelper::getColor3DAttribute(node, "color", color);
219
0
    XmlParser::getFloatAttribute(node, "cutOffAngle", cutOffAngle);
220
0
    X3DXmlHelper::getVector3DAttribute(node, "direction", direction);
221
0
    XmlParser::getBoolAttribute(node, "global", global);
222
0
    XmlParser::getFloatAttribute(node, "intensity", intensity);
223
0
    X3DXmlHelper::getVector3DAttribute(node, "location", location);
224
0
    XmlParser::getBoolAttribute(node, "on", on);
225
0
    XmlParser::getFloatAttribute(node, "radius", radius);
226
227
    // if "USE" defined then find already defined element.
228
0
    if (!use.empty()) {
229
0
        ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_SpotLight, ne);
230
0
    } else {
231
0
        if (on) {
232
            // create and if needed - define new geometry object.
233
0
            ne = new X3DNodeElementLight(X3DElemType::ENET_SpotLight, mNodeElementCur);
234
0
            if (!def.empty()) ne->ID = def;
235
236
0
            if (beamWidth > cutOffAngle) beamWidth = cutOffAngle;
237
238
0
            ((X3DNodeElementLight *)ne)->AmbientIntensity = ambientIntensity;
239
0
            ((X3DNodeElementLight *)ne)->Attenuation = attenuation;
240
0
            ((X3DNodeElementLight *)ne)->BeamWidth = beamWidth;
241
0
            ((X3DNodeElementLight *)ne)->Color = color;
242
0
            ((X3DNodeElementLight *)ne)->CutOffAngle = cutOffAngle;
243
0
            ((X3DNodeElementLight *)ne)->Direction = direction;
244
0
            ((X3DNodeElementLight *)ne)->Global = global;
245
0
            ((X3DNodeElementLight *)ne)->Intensity = intensity;
246
0
            ((X3DNodeElementLight *)ne)->Location = location;
247
0
            ((X3DNodeElementLight *)ne)->Radius = radius;
248
249
            // Assimp want a node with name similar to a light. "Why? I don't no." )
250
0
            ParseHelper_Group_Begin(false);
251
            // make random name
252
0
            if (ne->ID.empty()) ne->ID = "SpotLight_" + ai_to_string((size_t)ne);
253
254
0
            mNodeElementCur->ID = ne->ID; // assign name to node and return to light element.
255
0
            ParseHelper_Node_Exit();
256
            // check for child nodes
257
0
            if (!isNodeEmpty(node))
258
0
                childrenReadMetadata(node, ne, "SpotLight");
259
0
            else
260
0
                mNodeElementCur->Children.push_back(ne); // add made object as child to current element
261
262
0
            NodeElement_List.push_back(ne); // add element to node element list because its a new object in graph
263
0
        } // if(on)
264
0
    } // if(!use.empty()) else
265
0
}
266
267
} // namespace Assimp
268
269
#endif // !ASSIMP_BUILD_NO_X3D_IMPORTER