/src/assimp/code/AssetLib/OpenGEX/OpenGEXImporter.cpp
Line | Count | Source |
1 | | /* |
2 | | Open Asset Import Library (assimp) |
3 | | ---------------------------------------------------------------------- |
4 | | |
5 | | Copyright (c) 2006-2026, 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 | | #ifndef ASSIMP_BUILD_NO_OPENGEX_IMPORTER |
42 | | |
43 | | #include "OpenGEXImporter.h" |
44 | | #include "PostProcessing/MakeVerboseFormat.h" |
45 | | |
46 | | #include <assimp/DefaultIOSystem.h> |
47 | | #include <assimp/StringComparison.h> |
48 | | #include <assimp/StringUtils.h> |
49 | | #include <assimp/DefaultLogger.hpp> |
50 | | #include <assimp/ai_assert.h> |
51 | | #include <assimp/importerdesc.h> |
52 | | #include <assimp/scene.h> |
53 | | #include <openddlparser/OpenDDLParser.h> |
54 | | #include <limits> |
55 | | |
56 | | static constexpr aiImporterDesc desc = { |
57 | | "Open Game Engine Exchange", |
58 | | "", |
59 | | "", |
60 | | "", |
61 | | aiImporterFlags_SupportTextFlavour, |
62 | | 0, |
63 | | 0, |
64 | | 0, |
65 | | 0, |
66 | | "ogex" |
67 | | }; |
68 | | |
69 | | namespace Grammar { |
70 | | static constexpr char MetricType[] = "Metric"; |
71 | | static constexpr char Metric_DistanceType[] = "distance"; |
72 | | static constexpr char Metric_AngleType[] = "angle"; |
73 | | static constexpr char Metric_TimeType[] = "time"; |
74 | | static constexpr char Metric_UpType[] = "up"; |
75 | | static constexpr char NameType[] = "Name"; |
76 | | static constexpr char ObjectRefType[] = "ObjectRef"; |
77 | | static constexpr char MaterialRefType[] = "MaterialRef"; |
78 | | static constexpr char MetricKeyType[] = "key"; |
79 | | static constexpr char GeometryNodeType[] = "GeometryNode"; |
80 | | static constexpr char CameraNodeType[] = "CameraNode"; |
81 | | static constexpr char LightNodeType[] = "LightNode"; |
82 | | static constexpr char GeometryObjectType[] = "GeometryObject"; |
83 | | static constexpr char CameraObjectType[] = "CameraObject"; |
84 | | static constexpr char LightObjectType[] = "LightObject"; |
85 | | static constexpr char TransformType[] = "Transform"; |
86 | | static constexpr char MeshType[] = "Mesh"; |
87 | | static constexpr char VertexArrayType[] = "VertexArray"; |
88 | | static constexpr char IndexArrayType[] = "IndexArray"; |
89 | | static constexpr char MaterialType[] = "Material"; |
90 | | static constexpr char ColorType[] = "Color"; |
91 | | static constexpr char ParamType[] = "Param"; |
92 | | static constexpr char TextureType[] = "Texture"; |
93 | | static constexpr char AttenType[] = "Atten"; |
94 | | |
95 | | static constexpr char DiffuseColorToken[] = "diffuse"; |
96 | | static constexpr char SpecularColorToken[] = "specular"; |
97 | | static constexpr char EmissionColorToken[] = "emission"; |
98 | | |
99 | | static constexpr char DiffuseTextureToken[] = "diffuse"; |
100 | | static constexpr char DiffuseSpecularTextureToken[] = "specular"; |
101 | | static constexpr char SpecularPowerTextureToken[] = "specular_power"; |
102 | | static constexpr char EmissionTextureToken[] = "emission"; |
103 | | static constexpr char OpacyTextureToken[] = "opacity"; |
104 | | static constexpr char TransparencyTextureToken[] = "transparency"; |
105 | | static constexpr char NormalTextureToken[] = "normal"; |
106 | | |
107 | | enum TokenType { |
108 | | NoneType = -1, |
109 | | MetricToken, |
110 | | NameToken, |
111 | | ObjectRefToken, |
112 | | MaterialRefToken, |
113 | | MetricKeyToken, |
114 | | GeometryNodeToken, |
115 | | CameraNodeToken, |
116 | | LightNodeToken, |
117 | | GeometryObjectToken, |
118 | | CameraObjectToken, |
119 | | LightObjectToken, |
120 | | TransformToken, |
121 | | MeshToken, |
122 | | VertexArrayToken, |
123 | | IndexArrayToken, |
124 | | MaterialToken, |
125 | | ColorToken, |
126 | | ParamToken, |
127 | | TextureToken, |
128 | | AttenToken |
129 | | }; |
130 | | |
131 | | static const std::string ValidMetricToken[4] = { |
132 | | Metric_DistanceType, |
133 | | Metric_AngleType, |
134 | | Metric_TimeType, |
135 | | Metric_UpType |
136 | | }; |
137 | | |
138 | 583 | static int isValidMetricType(const char *token) { |
139 | 583 | if (nullptr == token) { |
140 | 0 | return false; |
141 | 0 | } |
142 | | |
143 | 583 | int idx = -1; |
144 | 1.51k | for (size_t i = 0; i < 4; i++) { |
145 | 1.49k | if (ValidMetricToken[i] == token) { |
146 | 554 | idx = (int)i; |
147 | 554 | break; |
148 | 554 | } |
149 | 1.49k | } |
150 | | |
151 | 583 | return idx; |
152 | 583 | } |
153 | | |
154 | 2.54k | static TokenType matchTokenType(const char *tokenType) { |
155 | 2.54k | const size_t len = std::strlen(tokenType); |
156 | 2.54k | if (0 == strncmp(MetricType, tokenType, len)) { |
157 | 695 | return MetricToken; |
158 | 1.84k | } else if (0 == strncmp(NameType, tokenType, len)) { |
159 | 211 | return NameToken; |
160 | 1.63k | } else if (0 == strncmp(ObjectRefType, tokenType, len)) { |
161 | 185 | return ObjectRefToken; |
162 | 1.44k | } else if (0 == strncmp(MaterialRefType, tokenType, len)) { |
163 | 193 | return MaterialRefToken; |
164 | 1.25k | } else if (0 == strncmp(MetricKeyType, tokenType, len)) { |
165 | 0 | return MetricKeyToken; |
166 | 1.25k | } else if (0 == strncmp(GeometryNodeType, tokenType, len)) { |
167 | 219 | return GeometryNodeToken; |
168 | 1.03k | } else if (0 == strncmp(CameraNodeType, tokenType, len)) { |
169 | 8 | return CameraNodeToken; |
170 | 1.02k | } else if (0 == strncmp(LightNodeType, tokenType, len)) { |
171 | 9 | return LightNodeToken; |
172 | 1.02k | } else if (0 == strncmp(GeometryObjectType, tokenType, len)) { |
173 | 138 | return GeometryObjectToken; |
174 | 882 | } else if (0 == strncmp(CameraObjectType, tokenType, len)) { |
175 | 8 | return CameraObjectToken; |
176 | 874 | } else if (0 == strncmp(LightObjectType, tokenType, len)) { |
177 | 8 | return LightObjectToken; |
178 | 866 | } else if (0 == strncmp(TransformType, tokenType, len)) { |
179 | 186 | return TransformToken; |
180 | 680 | } else if (0 == strncmp(MeshType, tokenType, len)) { |
181 | 133 | return MeshToken; |
182 | 547 | } else if (0 == strncmp(VertexArrayType, tokenType, len)) { |
183 | 246 | return VertexArrayToken; |
184 | 301 | } else if (0 == strncmp(IndexArrayType, tokenType, len)) { |
185 | 29 | return IndexArrayToken; |
186 | 272 | } else if (0 == strncmp(MaterialType, tokenType, len)) { |
187 | 0 | return MaterialToken; |
188 | 272 | } else if (0 == strncmp(ColorType, tokenType, len)) { |
189 | 0 | return ColorToken; |
190 | 272 | } else if (0 == strncmp(ParamType, tokenType, len)) { |
191 | 24 | return ParamToken; |
192 | 248 | } else if (0 == strncmp(TextureType, tokenType, len)) { |
193 | 1 | return TextureToken; |
194 | 247 | } else if (0 == strncmp(AttenType, tokenType, len)) { |
195 | 0 | return AttenToken; |
196 | 0 | } |
197 | | |
198 | 247 | return NoneType; |
199 | 2.54k | } |
200 | | } // Namespace Grammar |
201 | | |
202 | | namespace Assimp { |
203 | | namespace OpenGEX { |
204 | | |
205 | | USE_ODDLPARSER_NS |
206 | | |
207 | | //------------------------------------------------------------------------------------------------ |
208 | 377 | static void propId2StdString(Property *prop, std::string &name, std::string &key) { |
209 | 377 | name = key = std::string(); |
210 | 377 | if (nullptr == prop) { |
211 | 0 | return; |
212 | 0 | } |
213 | | |
214 | 377 | if (nullptr != prop->m_key) { |
215 | | #ifdef ASSIMP_USE_HUNTER |
216 | | name = prop->m_key->m_text.m_buffer; |
217 | | #else |
218 | 377 | name = prop->m_key->m_buffer; |
219 | 377 | #endif |
220 | 377 | if (Value::ValueType::ddl_string == prop->m_value->m_type) { |
221 | 353 | key = prop->m_value->getString(); |
222 | 353 | } |
223 | 377 | } |
224 | 377 | } |
225 | | |
226 | | //------------------------------------------------------------------------------------------------ |
227 | 542 | static void logDDLParserMessage (LogSeverity severity, const std::string &rawmsg) { |
228 | 542 | std::string msg = ai_str_toprintable(rawmsg); |
229 | 542 | switch (severity) { |
230 | 0 | case ddl_debug_msg: ASSIMP_LOG_DEBUG(msg); break; |
231 | 0 | case ddl_info_msg: ASSIMP_LOG_INFO(msg); break; |
232 | 0 | case ddl_warn_msg: ASSIMP_LOG_WARN(msg); break; |
233 | 542 | case ddl_error_msg: ASSIMP_LOG_ERROR(msg); break; |
234 | 0 | default: ASSIMP_LOG_VERBOSE_DEBUG(msg); break; |
235 | 542 | } |
236 | 542 | } |
237 | | |
238 | | //------------------------------------------------------------------------------------------------ |
239 | 165k | OpenGEXImporter::VertexContainer::~VertexContainer() { |
240 | 165k | delete[] m_colors; |
241 | | |
242 | 1.32M | for (auto &texcoords : m_textureCoords) { |
243 | 1.32M | delete[] texcoords; |
244 | 1.32M | } |
245 | 165k | } |
246 | | |
247 | | //------------------------------------------------------------------------------------------------ |
248 | | OpenGEXImporter::RefInfo::RefInfo(aiNode *node, Type type, std::vector<std::string> &names) : |
249 | 335 | m_node(node), |
250 | 335 | m_type(type), |
251 | 335 | m_Names(names) { |
252 | | // empty |
253 | 335 | } |
254 | | |
255 | | //------------------------------------------------------------------------------------------------ |
256 | | OpenGEXImporter::OpenGEXImporter() : |
257 | 165k | m_root(nullptr), |
258 | 165k | m_nodeChildMap(), |
259 | 165k | m_mesh2refMap(), |
260 | 165k | m_material2refMap(), |
261 | 165k | m_ctx(nullptr), |
262 | 165k | m_metrics(), |
263 | 165k | m_currentNode(nullptr), |
264 | 165k | m_currentVertices(), |
265 | 165k | m_currentMesh(nullptr), |
266 | 165k | m_currentMaterial(nullptr), |
267 | 165k | m_currentLight(nullptr), |
268 | 165k | m_currentCamera(nullptr), |
269 | 165k | m_tokenType(Grammar::NoneType), |
270 | 165k | m_materialCache(), |
271 | 165k | m_cameraCache(), |
272 | 165k | m_lightCache(), |
273 | 165k | m_nodeStack(), |
274 | 165k | m_unresolvedRefStack() { |
275 | | // empty |
276 | 165k | } |
277 | | |
278 | | //------------------------------------------------------------------------------------------------ |
279 | 8.82k | bool OpenGEXImporter::CanRead(const std::string &file, IOSystem *pIOHandler, bool /*checkSig*/) const { |
280 | 8.82k | static const char *tokens[] = { "Metric", "GeometryNode", "VertexArray (attrib", "IndexArray" }; |
281 | 8.82k | return SearchFileHeaderForToken(pIOHandler, file, tokens, AI_COUNT_OF(tokens)); |
282 | 8.82k | } |
283 | | |
284 | | //------------------------------------------------------------------------------------------------ |
285 | 938 | void OpenGEXImporter::InternReadFile(const std::string &filename, aiScene *pScene, IOSystem *pIOHandler) { |
286 | | // open source file |
287 | 938 | std::unique_ptr<IOStream> file(pIOHandler->Open(filename, "rb")); |
288 | 938 | if (!file) { |
289 | 0 | throw DeadlyImportError("Failed to open file ", filename); |
290 | 0 | } |
291 | | |
292 | 938 | std::vector<char> buffer; |
293 | 938 | TextFileToBuffer(file.get(), buffer); |
294 | | |
295 | 938 | OpenDDLParser myParser; |
296 | 938 | myParser.setLogCallback(&logDDLParserMessage); |
297 | 938 | myParser.setBuffer(&buffer[0], buffer.size()); |
298 | 938 | bool success(myParser.parse()); |
299 | 938 | if (success) { |
300 | 357 | m_ctx = myParser.getContext(); |
301 | 357 | pScene->mRootNode = new aiNode; |
302 | 357 | pScene->mRootNode->mName.Set(filename); |
303 | 357 | handleNodes(m_ctx->m_root, pScene); |
304 | 357 | } |
305 | | |
306 | 938 | copyMeshes(pScene); |
307 | 938 | copyCameras(pScene); |
308 | | // TODO: lights only partially implemented and breaking model import |
309 | | // copyLights(pScene); |
310 | 938 | copyMaterials(pScene); |
311 | 938 | resolveReferences(); |
312 | 938 | createNodeTree(pScene); |
313 | 938 | } |
314 | | |
315 | | //------------------------------------------------------------------------------------------------ |
316 | 162k | const aiImporterDesc *OpenGEXImporter::GetInfo() const { |
317 | 162k | return &desc; |
318 | 162k | } |
319 | | |
320 | | //------------------------------------------------------------------------------------------------ |
321 | 938 | void OpenGEXImporter::SetupProperties(const Importer *pImp) { |
322 | 938 | if (nullptr == pImp) { |
323 | 0 | return; |
324 | 0 | } |
325 | 938 | } |
326 | | |
327 | | //------------------------------------------------------------------------------------------------ |
328 | 863 | void OpenGEXImporter::handleNodes(DDLNode *node, aiScene *pScene) { |
329 | 863 | if (nullptr == node) { |
330 | 0 | return; |
331 | 0 | } |
332 | | |
333 | 863 | DDLNode::DllNodeList children = node->getChildNodeList(); |
334 | 3.33k | for (DDLNode::DllNodeList::iterator it = children.begin(); it != children.end(); ++it) { |
335 | 2.54k | Grammar::TokenType tokenType(Grammar::matchTokenType((*it)->getType().c_str())); |
336 | 2.54k | switch (tokenType) { |
337 | 695 | case Grammar::MetricToken: |
338 | 695 | handleMetricNode(*it, pScene); |
339 | 695 | break; |
340 | | |
341 | 211 | case Grammar::NameToken: |
342 | 211 | handleNameNode(*it, pScene); |
343 | 211 | break; |
344 | | |
345 | 185 | case Grammar::ObjectRefToken: |
346 | 185 | handleObjectRefNode(*it, pScene); |
347 | 185 | break; |
348 | | |
349 | 193 | case Grammar::MaterialRefToken: |
350 | 193 | handleMaterialRefNode(*it, pScene); |
351 | 193 | break; |
352 | | |
353 | 0 | case Grammar::MetricKeyToken: |
354 | 0 | break; |
355 | | |
356 | 219 | case Grammar::GeometryNodeToken: |
357 | 219 | handleGeometryNode(*it, pScene); |
358 | 219 | break; |
359 | | |
360 | 8 | case Grammar::CameraNodeToken: |
361 | 8 | handleCameraNode(*it, pScene); |
362 | 8 | break; |
363 | | |
364 | 9 | case Grammar::LightNodeToken: |
365 | | // TODO: lights only partially implemented and breaking model import |
366 | | // handleLightNode(*it, pScene); |
367 | 9 | break; |
368 | | |
369 | 138 | case Grammar::GeometryObjectToken: |
370 | 138 | handleGeometryObject(*it, pScene); |
371 | 138 | break; |
372 | | |
373 | 8 | case Grammar::CameraObjectToken: |
374 | 8 | handleCameraObject(*it, pScene); |
375 | 8 | break; |
376 | | |
377 | 8 | case Grammar::LightObjectToken: |
378 | | // TODO: lights only partially implemented and breaking model import |
379 | | // handleLightObject(*it, pScene); |
380 | 8 | break; |
381 | | |
382 | 186 | case Grammar::TransformToken: |
383 | 186 | handleTransformNode(*it, pScene); |
384 | 186 | break; |
385 | | |
386 | 133 | case Grammar::MeshToken: |
387 | 133 | handleMeshNode(*it, pScene); |
388 | 133 | break; |
389 | | |
390 | 246 | case Grammar::VertexArrayToken: |
391 | 246 | handleVertexArrayNode(*it, pScene); |
392 | 246 | break; |
393 | | |
394 | 29 | case Grammar::IndexArrayToken: |
395 | 29 | handleIndexArrayNode(*it, pScene); |
396 | 29 | break; |
397 | | |
398 | 0 | case Grammar::MaterialToken: |
399 | 0 | handleMaterialNode(*it, pScene); |
400 | 0 | break; |
401 | | |
402 | 0 | case Grammar::ColorToken: |
403 | 0 | handleColorNode(*it, pScene); |
404 | 0 | break; |
405 | | |
406 | 24 | case Grammar::ParamToken: |
407 | 24 | handleParamNode(*it, pScene); |
408 | 24 | break; |
409 | | |
410 | 1 | case Grammar::TextureToken: |
411 | 1 | handleTextureNode(*it, pScene); |
412 | 1 | break; |
413 | | |
414 | 247 | default: |
415 | 247 | break; |
416 | 2.54k | } |
417 | 2.54k | } |
418 | 863 | } |
419 | | |
420 | | //------------------------------------------------------------------------------------------------ |
421 | 695 | void OpenGEXImporter::handleMetricNode(DDLNode *node, aiScene * /*pScene*/) { |
422 | 695 | if (nullptr == node || nullptr == m_ctx) { |
423 | 0 | return; |
424 | 0 | } |
425 | | |
426 | 695 | if (m_ctx->m_root != node->getParent()) { |
427 | 65 | return; |
428 | 65 | } |
429 | | |
430 | 630 | Property *prop(node->getProperties()); |
431 | 1.21k | while (nullptr != prop) { |
432 | 584 | if (nullptr != prop->m_key && nullptr != prop->m_value) { |
433 | 583 | if (Value::ValueType::ddl_string == prop->m_value->m_type) { |
434 | 583 | std::string valName((char *)prop->m_value->m_data); |
435 | 583 | int type(Grammar::isValidMetricType(valName.c_str())); |
436 | 583 | if (Grammar::NoneType != type) { |
437 | 554 | Value *val(node->getValue()); |
438 | 554 | if (nullptr != val) { |
439 | 529 | if (Value::ValueType::ddl_float == val->m_type) { |
440 | 412 | m_metrics[type].m_floatValue = val->getFloat(); |
441 | 412 | } else if (Value::ValueType::ddl_int32 == val->m_type) { |
442 | 0 | m_metrics[type].m_intValue = val->getInt32(); |
443 | 117 | } else if (Value::ValueType::ddl_string == val->m_type) { |
444 | 116 | m_metrics[type].m_stringValue = std::string(val->getString()); |
445 | 116 | } else { |
446 | 1 | throw DeadlyImportError("OpenGEX: invalid data type for Metric node."); |
447 | 1 | } |
448 | 529 | } |
449 | 554 | } |
450 | 583 | } |
451 | 583 | } |
452 | 583 | prop = prop->m_next; |
453 | 583 | } |
454 | 630 | } |
455 | | |
456 | | //------------------------------------------------------------------------------------------------ |
457 | 211 | void OpenGEXImporter::handleNameNode(DDLNode *node, aiScene * /*pScene*/) { |
458 | 211 | if (nullptr == m_currentNode) { |
459 | 0 | throw DeadlyImportError("No current node for name."); |
460 | 0 | } |
461 | | |
462 | 211 | Value *val(node->getValue()); |
463 | 211 | if (nullptr != val) { |
464 | 190 | if (Value::ValueType::ddl_string != val->m_type) { |
465 | 0 | throw DeadlyImportError("OpenGEX: invalid data type for value in node name."); |
466 | 0 | } |
467 | | |
468 | 190 | const std::string name(val->getString()); |
469 | 190 | if (m_tokenType == Grammar::GeometryNodeToken || |
470 | | // TODO: lights only partially implemented and breaking model import |
471 | | // m_tokenType == Grammar::LightNodeToken || |
472 | 190 | m_tokenType == Grammar::CameraNodeToken) { |
473 | 190 | m_currentNode->mName.Set(name.c_str()); |
474 | 190 | } else if (m_tokenType == Grammar::MaterialToken) { |
475 | 0 | aiString aiName; |
476 | 0 | aiName.Set(name); |
477 | 0 | m_currentMaterial->AddProperty(&aiName, AI_MATKEY_NAME); |
478 | 0 | m_material2refMap[name] = m_materialCache.size() - 1; |
479 | 0 | } |
480 | 190 | } |
481 | 211 | } |
482 | | |
483 | | //------------------------------------------------------------------------------------------------ |
484 | 374 | static void getRefNames(DDLNode *node, std::vector<std::string> &names) { |
485 | 374 | ai_assert(nullptr != node); |
486 | | |
487 | 374 | Reference *ref = node->getReferences(); |
488 | 374 | if (nullptr != ref) { |
489 | 686 | for (size_t i = 0; i < ref->m_numRefs; i++) { |
490 | 343 | Name *currentName(ref->m_referencedName[i]); |
491 | 343 | if (nullptr != currentName && nullptr != currentName->m_id) { |
492 | | #ifdef ASSIMP_USE_HUNTER |
493 | | const std::string name(currentName->m_id->m_text.m_buffer); |
494 | | #else |
495 | 343 | const std::string name(currentName->m_id->m_buffer); |
496 | 343 | #endif |
497 | 343 | if (!name.empty()) { |
498 | 343 | names.push_back(name); |
499 | 343 | } |
500 | 343 | } |
501 | 343 | } |
502 | 343 | } |
503 | 374 | } |
504 | | |
505 | | //------------------------------------------------------------------------------------------------ |
506 | 185 | void OpenGEXImporter::handleObjectRefNode(DDLNode *node, aiScene * /*pScene*/) { |
507 | 185 | if (nullptr == m_currentNode) { |
508 | 3 | throw DeadlyImportError("No parent node for name."); |
509 | 3 | } |
510 | | |
511 | 182 | std::vector<std::string> objRefNames; |
512 | 182 | getRefNames(node, objRefNames); |
513 | | |
514 | | // when we are dealing with a geometry node prepare the mesh cache |
515 | 182 | if (m_tokenType == Grammar::GeometryNodeToken) { |
516 | 174 | m_currentNode->mNumMeshes = static_cast<unsigned int>(objRefNames.size()); |
517 | 174 | m_currentNode->mMeshes = new unsigned int[objRefNames.size()]; |
518 | 174 | if (!objRefNames.empty()) { |
519 | 163 | m_unresolvedRefStack.push_back(std::unique_ptr<RefInfo>(new RefInfo(m_currentNode, RefInfo::MeshRef, objRefNames))); |
520 | 163 | } |
521 | 174 | } else if (m_tokenType == Grammar::LightNodeToken) { |
522 | | // TODO! |
523 | 8 | } else if (m_tokenType == Grammar::CameraNodeToken) { |
524 | | // TODO! |
525 | 8 | } |
526 | 182 | } |
527 | | |
528 | | //------------------------------------------------------------------------------------------------ |
529 | 193 | void OpenGEXImporter::handleMaterialRefNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
530 | 193 | if (nullptr == m_currentNode) { |
531 | 1 | throw DeadlyImportError("No parent node for name."); |
532 | 1 | } |
533 | | |
534 | 192 | std::vector<std::string> matRefNames; |
535 | 192 | getRefNames(node, matRefNames); |
536 | 192 | if (!matRefNames.empty()) { |
537 | 172 | m_unresolvedRefStack.push_back(std::unique_ptr<RefInfo>(new RefInfo(m_currentNode, RefInfo::MaterialRef, matRefNames))); |
538 | 172 | } |
539 | 192 | } |
540 | | |
541 | | //------------------------------------------------------------------------------------------------ |
542 | 219 | void OpenGEXImporter::handleGeometryNode(DDLNode *node, aiScene *pScene) { |
543 | 219 | aiNode *newNode = new aiNode; |
544 | 219 | pushNode(newNode, pScene); |
545 | 219 | m_tokenType = Grammar::GeometryNodeToken; |
546 | 219 | m_currentNode = newNode; |
547 | 219 | handleNodes(node, pScene); |
548 | | |
549 | 219 | popNode(); |
550 | 219 | } |
551 | | |
552 | | //------------------------------------------------------------------------------------------------ |
553 | 8 | void OpenGEXImporter::handleCameraNode(DDLNode *node, aiScene *pScene) { |
554 | 8 | aiCamera *camera = new aiCamera; |
555 | 8 | m_cameraCache.push_back(camera); |
556 | 8 | m_currentCamera = camera; |
557 | | |
558 | 8 | aiNode *newNode = new aiNode; |
559 | 8 | pushNode(newNode, pScene); |
560 | 8 | m_tokenType = Grammar::CameraNodeToken; |
561 | 8 | m_currentNode = newNode; |
562 | | |
563 | 8 | handleNodes(node, pScene); |
564 | | |
565 | 8 | popNode(); |
566 | | |
567 | 8 | m_currentCamera->mName.Set(newNode->mName.C_Str()); |
568 | 8 | } |
569 | | |
570 | | //------------------------------------------------------------------------------------------------ |
571 | 0 | void OpenGEXImporter::handleLightNode(ODDLParser::DDLNode *node, aiScene *pScene) { |
572 | 0 | aiLight *light = new aiLight; |
573 | 0 | m_lightCache.push_back(light); |
574 | 0 | m_currentLight = light; |
575 | |
|
576 | 0 | aiNode *newNode = new aiNode; |
577 | 0 | m_tokenType = Grammar::LightNodeToken; |
578 | 0 | m_currentNode = newNode; |
579 | 0 | pushNode(newNode, pScene); |
580 | |
|
581 | 0 | handleNodes(node, pScene); |
582 | |
|
583 | 0 | popNode(); |
584 | |
|
585 | 0 | m_currentLight->mName.Set(newNode->mName.C_Str()); |
586 | 0 | } |
587 | | |
588 | | //------------------------------------------------------------------------------------------------ |
589 | 138 | void OpenGEXImporter::handleGeometryObject(DDLNode *node, aiScene *pScene) { |
590 | | // parameters will be parsed normally in the tree, so just go for it |
591 | 138 | handleNodes(node, pScene); |
592 | 138 | } |
593 | | |
594 | | //------------------------------------------------------------------------------------------------ |
595 | 8 | void OpenGEXImporter::handleCameraObject(ODDLParser::DDLNode *node, aiScene *pScene) { |
596 | | // parameters will be parsed normally in the tree, so just go for it |
597 | | |
598 | 8 | handleNodes(node, pScene); |
599 | 8 | } |
600 | | |
601 | | //------------------------------------------------------------------------------------------------ |
602 | 0 | void OpenGEXImporter::handleLightObject(ODDLParser::DDLNode *node, aiScene *pScene) { |
603 | 0 | aiLight *light(new aiLight); |
604 | 0 | m_lightCache.push_back(light); |
605 | 0 | std::string objName = node->getName(); |
606 | 0 | if (!objName.empty()) { |
607 | 0 | light->mName.Set(objName); |
608 | 0 | } |
609 | 0 | m_currentLight = light; |
610 | |
|
611 | 0 | Property *prop = node->findPropertyByName("type"); |
612 | 0 | if (nullptr != prop) { |
613 | 0 | if (nullptr != prop->m_value) { |
614 | 0 | std::string typeStr(prop->m_value->getString()); |
615 | 0 | if ("point" == typeStr) { |
616 | 0 | m_currentLight->mType = aiLightSource_POINT; |
617 | 0 | } else if ("spot" == typeStr) { |
618 | 0 | m_currentLight->mType = aiLightSource_SPOT; |
619 | 0 | } else if ("infinite" == typeStr) { |
620 | 0 | m_currentLight->mType = aiLightSource_DIRECTIONAL; |
621 | 0 | } |
622 | 0 | } |
623 | 0 | } |
624 | | |
625 | | // parameters will be parsed normally in the tree, so just go for it |
626 | 0 | handleNodes(node, pScene); |
627 | 0 | } |
628 | | |
629 | | //------------------------------------------------------------------------------------------------ |
630 | 173 | static void setMatrix(aiNode *node, DataArrayList *transformData) { |
631 | 173 | ai_assert(nullptr != node); |
632 | 173 | ai_assert(nullptr != transformData); |
633 | | |
634 | 173 | float m[16]; |
635 | 173 | size_t i(1); |
636 | 173 | Value *next(transformData->m_dataList->m_next); |
637 | 173 | m[0] = transformData->m_dataList->getFloat(); |
638 | 2.76k | while (next != nullptr) { |
639 | 2.59k | m[i] = next->getFloat(); |
640 | 2.59k | next = next->m_next; |
641 | 2.59k | i++; |
642 | 2.59k | } |
643 | | |
644 | 173 | ai_assert(i == 16); |
645 | | |
646 | 173 | node->mTransformation.a1 = m[0]; |
647 | 173 | node->mTransformation.a2 = m[4]; |
648 | 173 | node->mTransformation.a3 = m[8]; |
649 | 173 | node->mTransformation.a4 = m[12]; |
650 | | |
651 | 173 | node->mTransformation.b1 = m[1]; |
652 | 173 | node->mTransformation.b2 = m[5]; |
653 | 173 | node->mTransformation.b3 = m[9]; |
654 | 173 | node->mTransformation.b4 = m[13]; |
655 | | |
656 | 173 | node->mTransformation.c1 = m[2]; |
657 | 173 | node->mTransformation.c2 = m[6]; |
658 | 173 | node->mTransformation.c3 = m[10]; |
659 | 173 | node->mTransformation.c4 = m[14]; |
660 | | |
661 | 173 | node->mTransformation.d1 = m[3]; |
662 | 173 | node->mTransformation.d2 = m[7]; |
663 | 173 | node->mTransformation.d3 = m[11]; |
664 | 173 | node->mTransformation.d4 = m[15]; |
665 | 173 | } |
666 | | |
667 | | //------------------------------------------------------------------------------------------------ |
668 | 186 | void OpenGEXImporter::handleTransformNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
669 | 186 | if (nullptr == m_currentNode) { |
670 | 2 | throw DeadlyImportError("No parent node for name."); |
671 | 2 | } |
672 | | |
673 | 184 | DataArrayList *transformData(node->getDataArrayList()); |
674 | 184 | if (nullptr != transformData) { |
675 | 183 | if (transformData->m_numItems != 16) { |
676 | 10 | throw DeadlyImportError("Invalid number of data for transform matrix."); |
677 | 10 | } |
678 | 173 | setMatrix(m_currentNode, transformData); |
679 | 173 | } |
680 | 184 | } |
681 | | |
682 | | //------------------------------------------------------------------------------------------------ |
683 | 133 | void OpenGEXImporter::handleMeshNode(ODDLParser::DDLNode *node, aiScene *pScene) { |
684 | 133 | m_currentMesh = new aiMesh; |
685 | 133 | const size_t meshidx(m_meshCache.size()); |
686 | | // ownership is transferred but a reference remains in m_currentMesh |
687 | 133 | m_meshCache.emplace_back(m_currentMesh); |
688 | | |
689 | 133 | Property *prop = node->getProperties(); |
690 | 133 | if (nullptr != prop) { |
691 | 132 | std::string propName, propKey; |
692 | 132 | propId2StdString(prop, propName, propKey); |
693 | 132 | if ("primitive" == propName) { |
694 | 129 | if ("points" == propKey) { |
695 | 0 | m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_POINT; |
696 | 129 | } else if ("lines" == propKey) { |
697 | 0 | m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_LINE; |
698 | 129 | } else if ("triangles" == propKey) { |
699 | 122 | m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE; |
700 | 122 | } else if ("quads" == propKey) { |
701 | 0 | m_currentMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON; |
702 | 7 | } else { |
703 | 7 | ASSIMP_LOG_WARN(propKey, " is not supported primitive type."); |
704 | 7 | } |
705 | 129 | } |
706 | 132 | } |
707 | | |
708 | 133 | handleNodes(node, pScene); |
709 | | |
710 | 133 | DDLNode *parent(node->getParent()); |
711 | 133 | if (nullptr != parent) { |
712 | 122 | const std::string &name = parent->getName(); |
713 | 122 | m_mesh2refMap[name] = meshidx; |
714 | 122 | } |
715 | 133 | } |
716 | | |
717 | | //------------------------------------------------------------------------------------------------ |
718 | | enum MeshAttribute { |
719 | | None, |
720 | | Position, |
721 | | Color, |
722 | | Normal, |
723 | | TexCoord |
724 | | }; |
725 | | |
726 | | constexpr auto PosToken = "position"; |
727 | | constexpr auto ColToken = "color"; |
728 | | constexpr auto NormalToken = "normal"; |
729 | | constexpr auto TexCoordToken = "texcoord"; |
730 | | |
731 | | //------------------------------------------------------------------------------------------------ |
732 | 245 | static MeshAttribute getAttributeByName(const char *attribName) { |
733 | 245 | ai_assert(nullptr != attribName); |
734 | | |
735 | 245 | if (0 == strcmp(PosToken, attribName)) { |
736 | 112 | return Position; |
737 | 133 | } else if (0 == strcmp(ColToken, attribName)) { |
738 | 1 | return Color; |
739 | 132 | } else if (0 == strcmp(NormalToken, attribName)) { |
740 | 72 | return Normal; |
741 | 72 | } else if (0 == strcmp(TexCoordToken, attribName)) { |
742 | 25 | return TexCoord; |
743 | 25 | } |
744 | | |
745 | 35 | return None; |
746 | 245 | } |
747 | | |
748 | | //------------------------------------------------------------------------------------------------ |
749 | 17.5k | static void fillVector3(aiVector3D *vec3, Value *vals) { |
750 | 17.5k | ai_assert(nullptr != vec3); |
751 | | |
752 | 17.5k | float x(0.0f), y(0.0f), z(0.0f); |
753 | | // A vector-array entry may carry fewer values than expected; missing |
754 | | // components default to 0 instead of dereferencing a null Value. The |
755 | | // previous code read y and z unconditionally and crashed on short input. |
756 | 17.5k | Value *next(vals); |
757 | 17.5k | if (nullptr != next) { |
758 | 17.5k | x = next->getFloat(); |
759 | 17.5k | next = next->m_next; |
760 | 17.5k | if (nullptr != next) { |
761 | 17.5k | y = next->getFloat(); |
762 | 17.5k | next = next->m_next; |
763 | 17.5k | if (nullptr != next) { |
764 | 16.8k | z = next->getFloat(); |
765 | 16.8k | } |
766 | 17.5k | } |
767 | 17.5k | } |
768 | | |
769 | 17.5k | vec3->Set(x, y, z); |
770 | 17.5k | } |
771 | | |
772 | | //------------------------------------------------------------------------------------------------ |
773 | 80 | static void fillColor4(aiColor4D *col4, Value *vals) { |
774 | 80 | ai_assert(nullptr != col4); |
775 | 80 | ai_assert(nullptr != vals); |
776 | | |
777 | 80 | Value *next(vals); |
778 | 80 | col4->r = next->getFloat(); |
779 | 80 | next = next->m_next; |
780 | 80 | if (!next) { |
781 | 0 | throw DeadlyImportError("OpenGEX: Not enough values to fill 4-element color, only 1"); |
782 | 0 | } |
783 | | |
784 | 80 | col4->g = next->getFloat(); |
785 | 80 | next = next->m_next; |
786 | 80 | if (!next) { |
787 | 0 | throw DeadlyImportError("OpenGEX: Not enough values to fill 4-element color, only 2"); |
788 | 0 | } |
789 | | |
790 | 80 | col4->b = next->getFloat(); |
791 | 80 | next = next->m_next; |
792 | 80 | if (!next) { |
793 | 80 | col4->a = 1.0f; |
794 | 80 | } else { |
795 | 0 | col4->a = next->getFloat(); |
796 | 0 | } |
797 | 80 | } |
798 | | |
799 | | //------------------------------------------------------------------------------------------------ |
800 | 237 | static size_t countDataArrayListItems(DataArrayList *vaList) { |
801 | 237 | size_t numItems(0); |
802 | 237 | if (nullptr == vaList) { |
803 | 0 | return numItems; |
804 | 0 | } |
805 | | |
806 | 237 | DataArrayList *next(vaList); |
807 | 31.6k | while (nullptr != next) { |
808 | 31.4k | if (nullptr != vaList->m_dataList) { |
809 | 31.4k | numItems++; |
810 | 31.4k | } |
811 | 31.4k | next = next->m_next; |
812 | 31.4k | } |
813 | | |
814 | 237 | return numItems; |
815 | 237 | } |
816 | | |
817 | | //------------------------------------------------------------------------------------------------ |
818 | 208 | static void copyVectorArray(size_t numItems, DataArrayList *vaList, aiVector3D *vectorArray) { |
819 | 17.7k | for (size_t i = 0; i < numItems && nullptr != vaList; i++) { |
820 | 17.5k | Value *next(vaList->m_dataList); |
821 | 17.5k | fillVector3(&vectorArray[i], next); |
822 | 17.5k | vaList = vaList->m_next; |
823 | 17.5k | } |
824 | 208 | } |
825 | | |
826 | | //------------------------------------------------------------------------------------------------ |
827 | 1 | static void copyColor4DArray(size_t numItems, DataArrayList *vaList, aiColor4D *colArray) { |
828 | 81 | for (size_t i = 0; i < numItems; i++) { |
829 | 80 | Value *next(vaList->m_dataList); |
830 | 80 | fillColor4(&colArray[i], next); |
831 | 80 | } |
832 | 1 | } |
833 | | |
834 | | //------------------------------------------------------------------------------------------------ |
835 | 246 | void OpenGEXImporter::handleVertexArrayNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
836 | 246 | if (nullptr == node) { |
837 | 0 | throw DeadlyImportError("No parent node for name."); |
838 | 0 | } |
839 | | |
840 | 246 | Property *prop = node->getProperties(); |
841 | 246 | if (nullptr != prop) { |
842 | 245 | std::string propName, propKey; |
843 | 245 | propId2StdString(prop, propName, propKey); |
844 | 245 | MeshAttribute attribType(getAttributeByName(propKey.c_str())); |
845 | 245 | if (None == attribType) { |
846 | 35 | return; |
847 | 35 | } |
848 | | |
849 | 210 | DataArrayList *vaList = node->getDataArrayList(); |
850 | 210 | if (nullptr == vaList) { |
851 | 1 | return; |
852 | 1 | } |
853 | | |
854 | 209 | const size_t numItems(countDataArrayListItems(vaList)); |
855 | | |
856 | 209 | if (Position == attribType) { |
857 | 112 | m_currentVertices.m_vertices.resize(numItems); |
858 | 112 | copyVectorArray(numItems, vaList, m_currentVertices.m_vertices.data()); |
859 | 112 | } else if (Color == attribType) { |
860 | 1 | m_currentVertices.m_numColors = numItems; |
861 | 1 | m_currentVertices.m_colors = new aiColor4D[numItems]; |
862 | 1 | copyColor4DArray(numItems, vaList, m_currentVertices.m_colors); |
863 | 96 | } else if (Normal == attribType) { |
864 | 71 | m_currentVertices.m_normals.resize(numItems); |
865 | 71 | copyVectorArray(numItems, vaList, m_currentVertices.m_normals.data()); |
866 | 71 | } else if (TexCoord == attribType) { |
867 | 25 | m_currentVertices.m_numUVComps[0] = numItems; |
868 | 25 | m_currentVertices.m_textureCoords[0] = new aiVector3D[numItems]; |
869 | 25 | copyVectorArray(numItems, vaList, m_currentVertices.m_textureCoords[0]); |
870 | 25 | } |
871 | 209 | } |
872 | 246 | } |
873 | | |
874 | | //------------------------------------------------------------------------------------------------ |
875 | 29 | void OpenGEXImporter::handleIndexArrayNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
876 | 29 | if (nullptr == node) { |
877 | 0 | throw DeadlyImportError("No parent node for name."); |
878 | 0 | } |
879 | | |
880 | 29 | if (nullptr == m_currentMesh) { |
881 | 0 | throw DeadlyImportError("No current mesh for index data found."); |
882 | 0 | } |
883 | | |
884 | 29 | DataArrayList *vaList = node->getDataArrayList(); |
885 | 29 | if (nullptr == vaList) { |
886 | 1 | return; |
887 | 1 | } |
888 | | |
889 | 28 | const size_t numItems(countDataArrayListItems(vaList)); |
890 | 28 | m_currentMesh->mNumFaces = static_cast<unsigned int>(numItems); |
891 | 28 | m_currentMesh->mFaces = new aiFace[numItems]; |
892 | 28 | m_currentMesh->mNumVertices = static_cast<unsigned int>(numItems * 3); |
893 | 28 | m_currentMesh->mVertices = new aiVector3D[m_currentMesh->mNumVertices]; |
894 | 28 | bool hasColors(false); |
895 | 28 | if (m_currentVertices.m_numColors > 0) { |
896 | 1 | m_currentMesh->mColors[0] = new aiColor4D[m_currentMesh->mNumVertices]; |
897 | 1 | hasColors = true; |
898 | 1 | } |
899 | 28 | bool hasNormalCoords(false); |
900 | 28 | if (!m_currentVertices.m_normals.empty()) { |
901 | 16 | m_currentMesh->mNormals = new aiVector3D[m_currentMesh->mNumVertices]; |
902 | 16 | hasNormalCoords = true; |
903 | 16 | } |
904 | 28 | bool hasTexCoords(false); |
905 | 28 | if (m_currentVertices.m_numUVComps[0] > 0) { |
906 | 21 | m_currentMesh->mTextureCoords[0] = new aiVector3D[m_currentMesh->mNumVertices]; |
907 | 21 | hasTexCoords = true; |
908 | 21 | } |
909 | | |
910 | 28 | unsigned int index(0); |
911 | 13.7k | for (size_t i = 0; i < m_currentMesh->mNumFaces; i++) { |
912 | 13.6k | aiFace ¤t(m_currentMesh->mFaces[i]); |
913 | 13.6k | current.mNumIndices = 3; |
914 | 13.6k | current.mIndices = new unsigned int[current.mNumIndices]; |
915 | | // The index array must supply one data entry per face. |
916 | 13.6k | if (nullptr == vaList) { |
917 | 0 | throw DeadlyImportError("OpenGEX: index array has fewer entries than the mesh has faces"); |
918 | 0 | } |
919 | 13.6k | Value *next(vaList->m_dataList); |
920 | 54.7k | for (size_t indices = 0; indices < current.mNumIndices; indices++) { |
921 | | // Each face needs three indices; a short entry must not walk off |
922 | | // the value list. |
923 | 41.0k | if (nullptr == next) { |
924 | 8 | throw DeadlyImportError("OpenGEX: index array entry has fewer than three indices"); |
925 | 8 | } |
926 | 41.0k | size_t idx = 0; |
927 | 41.0k | if (next->m_type == Value::ValueType::ddl_unsigned_int8) { |
928 | 0 | idx = next->getUnsignedInt8(); |
929 | 41.0k | } else if (next->m_type == Value::ValueType::ddl_unsigned_int16) { |
930 | 0 | idx = next->getUnsignedInt16(); |
931 | 41.0k | } else if (next->m_type == Value::ValueType::ddl_unsigned_int32) { |
932 | 41.0k | idx = next->getUnsignedInt32(); |
933 | 41.0k | } else if (next->m_type == Value::ValueType::ddl_unsigned_int64) { |
934 | 0 | const auto idx64 = next->getUnsignedInt64(); |
935 | 0 | if (idx64 > static_cast<decltype(idx64)>(std::numeric_limits<size_t>::max())) { |
936 | 0 | throw DeadlyImportError("OpenGEX: vertex index is too large"); |
937 | 0 | } |
938 | 0 | idx = static_cast<size_t>(idx64); |
939 | 0 | } else { |
940 | 0 | throw DeadlyImportError("OpenGEX: index array uses an unsupported index type"); |
941 | 0 | } |
942 | | |
943 | | // idx comes straight from the file. Validate it (and the output |
944 | | // cursor) at runtime -- the previous ai_assert()s are compiled out |
945 | | // of release builds, so an out-of-range index read past the end of |
946 | | // the vertex arrays (heap-buffer-overflow). |
947 | 41.0k | if (idx >= m_currentVertices.m_vertices.size()) { |
948 | 3 | throw DeadlyImportError("OpenGEX: vertex index is out of range"); |
949 | 3 | } |
950 | 41.0k | if (index >= m_currentMesh->mNumVertices) { |
951 | 0 | throw DeadlyImportError("OpenGEX: index array references more vertices than declared"); |
952 | 0 | } |
953 | 41.0k | aiVector3D &pos = (m_currentVertices.m_vertices[idx]); |
954 | 41.0k | m_currentMesh->mVertices[index].Set(pos.x, pos.y, pos.z); |
955 | 41.0k | if (hasColors && static_cast<size_t>(idx) < m_currentVertices.m_numColors) { |
956 | 156 | aiColor4D &col = m_currentVertices.m_colors[idx]; |
957 | 156 | m_currentMesh->mColors[0][index] = col; |
958 | 156 | } |
959 | 41.0k | if (hasNormalCoords && static_cast<size_t>(idx) < m_currentVertices.m_normals.size()) { |
960 | 40.8k | aiVector3D &normal = (m_currentVertices.m_normals[idx]); |
961 | 40.8k | m_currentMesh->mNormals[index].Set(normal.x, normal.y, normal.z); |
962 | 40.8k | } |
963 | 41.0k | if (hasTexCoords && static_cast<size_t>(idx) < m_currentVertices.m_numUVComps[0]) { |
964 | 587 | aiVector3D &tex = (m_currentVertices.m_textureCoords[0][idx]); |
965 | 587 | m_currentMesh->mTextureCoords[0][index].Set(tex.x, tex.y, tex.z); |
966 | 587 | } |
967 | 41.0k | current.mIndices[indices] = index; |
968 | 41.0k | index++; |
969 | | |
970 | 41.0k | next = next->m_next; |
971 | 41.0k | } |
972 | 13.6k | vaList = vaList->m_next; |
973 | 13.6k | } |
974 | 28 | } |
975 | | |
976 | | //------------------------------------------------------------------------------------------------ |
977 | 0 | static void getColorRGB3(aiColor3D *pColor, DataArrayList *colList) { |
978 | 0 | if (nullptr == pColor || nullptr == colList) { |
979 | 0 | return; |
980 | 0 | } |
981 | | |
982 | | // The data list may be shorter than three entries on malformed input; |
983 | | // stop instead of dereferencing a null Value. |
984 | 0 | Value *val(colList->m_dataList); |
985 | 0 | if (nullptr == val) return; |
986 | 0 | pColor->r = val->getFloat(); |
987 | 0 | val = val->getNext(); |
988 | 0 | if (nullptr == val) return; |
989 | 0 | pColor->g = val->getFloat(); |
990 | 0 | val = val->getNext(); |
991 | 0 | if (nullptr == val) return; |
992 | 0 | pColor->b = val->getFloat(); |
993 | 0 | } |
994 | | |
995 | | //------------------------------------------------------------------------------------------------ |
996 | 0 | static void getColorRGB4(aiColor4D *pColor, DataArrayList *colList) { |
997 | 0 | if (nullptr == pColor || nullptr == colList) { |
998 | 0 | return; |
999 | 0 | } |
1000 | | |
1001 | | // The data list may be shorter than four entries on malformed input; |
1002 | | // stop instead of dereferencing a null Value. |
1003 | 0 | Value *val(colList->m_dataList); |
1004 | 0 | if (nullptr == val) return; |
1005 | 0 | pColor->r = val->getFloat(); |
1006 | 0 | val = val->getNext(); |
1007 | 0 | if (nullptr == val) return; |
1008 | 0 | pColor->g = val->getFloat(); |
1009 | 0 | val = val->getNext(); |
1010 | 0 | if (nullptr == val) return; |
1011 | 0 | pColor->b = val->getFloat(); |
1012 | 0 | val = val->getNext(); |
1013 | 0 | if (nullptr == val) return; |
1014 | 0 | pColor->a = val->getFloat(); |
1015 | 0 | } |
1016 | | |
1017 | | //------------------------------------------------------------------------------------------------ |
1018 | | enum ColorType { |
1019 | | NoneColor = 0, |
1020 | | DiffuseColor, |
1021 | | SpecularColor, |
1022 | | EmissionColor, |
1023 | | LightColor |
1024 | | }; |
1025 | | |
1026 | | //------------------------------------------------------------------------------------------------ |
1027 | 0 | static ColorType getColorType(Text *id) { |
1028 | 0 | if (nullptr == id) { |
1029 | 0 | return NoneColor; |
1030 | 0 | } |
1031 | | |
1032 | 0 | if (*id == Grammar::DiffuseColorToken) { |
1033 | 0 | return DiffuseColor; |
1034 | 0 | } else if (*id == Grammar::SpecularColorToken) { |
1035 | 0 | return SpecularColor; |
1036 | 0 | } else if (*id == Grammar::EmissionColorToken) { |
1037 | 0 | return EmissionColor; |
1038 | 0 | } else if (*id == "light") { |
1039 | 0 | return LightColor; |
1040 | 0 | } |
1041 | | |
1042 | 0 | return NoneColor; |
1043 | 0 | } |
1044 | | |
1045 | | //------------------------------------------------------------------------------------------------ |
1046 | 0 | void OpenGEXImporter::handleMaterialNode(ODDLParser::DDLNode *node, aiScene *pScene) { |
1047 | 0 | m_currentMaterial = new aiMaterial; |
1048 | 0 | m_materialCache.push_back(m_currentMaterial); |
1049 | 0 | m_tokenType = Grammar::MaterialToken; |
1050 | 0 | handleNodes(node, pScene); |
1051 | 0 | } |
1052 | | |
1053 | | //------------------------------------------------------------------------------------------------ |
1054 | 0 | void OpenGEXImporter::handleColorNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
1055 | 0 | if (nullptr == node) { |
1056 | 0 | return; |
1057 | 0 | } |
1058 | | |
1059 | 0 | Property *prop = node->findPropertyByName("attrib"); |
1060 | 0 | if (nullptr != prop) { |
1061 | 0 | if (nullptr != prop->m_value) { |
1062 | 0 | DataArrayList *colList(node->getDataArrayList()); |
1063 | 0 | if (nullptr == colList) { |
1064 | 0 | return; |
1065 | 0 | } |
1066 | 0 | aiColor3D col; |
1067 | 0 | if (3 == colList->m_numItems) { |
1068 | 0 | aiColor3D col3; |
1069 | 0 | getColorRGB3(&col3, colList); |
1070 | 0 | col = col3; |
1071 | 0 | } else { |
1072 | 0 | aiColor4D col4; |
1073 | 0 | getColorRGB4(&col4, colList); |
1074 | 0 | col.r = col4.r; |
1075 | 0 | col.g = col4.g; |
1076 | 0 | col.b = col4.b; |
1077 | 0 | } |
1078 | | #ifdef ASSIMP_USE_HUNTER |
1079 | | const ColorType colType(getColorType(&prop->m_key->m_text)); |
1080 | | #else |
1081 | 0 | const ColorType colType(getColorType(prop->m_key)); |
1082 | 0 | #endif |
1083 | 0 | if (DiffuseColor == colType) { |
1084 | 0 | m_currentMaterial->AddProperty(&col, 1, AI_MATKEY_COLOR_DIFFUSE); |
1085 | 0 | } else if (SpecularColor == colType) { |
1086 | 0 | m_currentMaterial->AddProperty(&col, 1, AI_MATKEY_COLOR_SPECULAR); |
1087 | 0 | } else if (EmissionColor == colType) { |
1088 | 0 | m_currentMaterial->AddProperty(&col, 1, AI_MATKEY_COLOR_EMISSIVE); |
1089 | 0 | } else if (LightColor == colType) { |
1090 | 0 | m_currentLight->mColorDiffuse = col; |
1091 | 0 | } |
1092 | 0 | } |
1093 | 0 | } |
1094 | 0 | } |
1095 | | |
1096 | | //------------------------------------------------------------------------------------------------ |
1097 | 1 | void OpenGEXImporter::handleTextureNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
1098 | 1 | if (nullptr == node) { |
1099 | 0 | return; |
1100 | 0 | } |
1101 | | |
1102 | 1 | Property *prop = node->findPropertyByName("attrib"); |
1103 | 1 | if (nullptr != prop) { |
1104 | 0 | if (nullptr != prop->m_value) { |
1105 | 0 | Value *val(node->getValue()); |
1106 | 0 | if (nullptr != val) { |
1107 | 0 | aiString tex; |
1108 | 0 | tex.Set(val->getString()); |
1109 | 0 | if (prop->m_value->getString() == Grammar::DiffuseTextureToken) { |
1110 | 0 | m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_DIFFUSE(0)); |
1111 | 0 | } else if (prop->m_value->getString() == Grammar::DiffuseSpecularTextureToken) { |
1112 | 0 | m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_SPECULAR(0)); |
1113 | 0 | } else if (prop->m_value->getString() == Grammar::SpecularPowerTextureToken) { |
1114 | 0 | m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_SPECULAR(0)); |
1115 | 0 | } else if (prop->m_value->getString() == Grammar::EmissionTextureToken) { |
1116 | 0 | m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_EMISSIVE(0)); |
1117 | 0 | } else if (prop->m_value->getString() == Grammar::OpacyTextureToken) { |
1118 | 0 | m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_OPACITY(0)); |
1119 | 0 | } else if (prop->m_value->getString() == Grammar::TransparencyTextureToken) { |
1120 | | // ToDo! |
1121 | | // m_currentMaterial->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE( 0 ) ); |
1122 | 0 | } else if (prop->m_value->getString() == Grammar::NormalTextureToken) { |
1123 | 0 | m_currentMaterial->AddProperty(&tex, AI_MATKEY_TEXTURE_NORMALS(0)); |
1124 | 0 | } else { |
1125 | 0 | ai_assert(false); |
1126 | 0 | } |
1127 | 0 | } |
1128 | 0 | } |
1129 | 0 | } |
1130 | 1 | } |
1131 | | |
1132 | | //------------------------------------------------------------------------------------------------ |
1133 | 24 | void OpenGEXImporter::handleParamNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
1134 | 24 | if (nullptr == node) { |
1135 | 0 | return; |
1136 | 0 | } |
1137 | | |
1138 | 24 | Property *prop = node->findPropertyByName("attrib"); |
1139 | 24 | if (nullptr == prop) { |
1140 | 0 | return; |
1141 | 0 | } |
1142 | | |
1143 | 24 | if (nullptr != prop->m_value) { |
1144 | 24 | Value *val(node->getValue()); |
1145 | 24 | if (nullptr == val) { |
1146 | 0 | return; |
1147 | 0 | } |
1148 | | // A Param node carrying camera attributes can appear without a |
1149 | | // preceding CameraObject node, leaving m_currentCamera null. Skip it |
1150 | | // rather than dereferencing a null aiCamera. |
1151 | 24 | if (nullptr == m_currentCamera) { |
1152 | 0 | return; |
1153 | 0 | } |
1154 | 24 | const float floatVal(val->getFloat()); |
1155 | 24 | if (0 == ASSIMP_strincmp("fov", prop->m_value->getString(), 3)) { |
1156 | 8 | m_currentCamera->mHorizontalFOV = floatVal; |
1157 | 16 | } else if (0 == ASSIMP_strincmp("near", prop->m_value->getString(), 4)) { |
1158 | 8 | m_currentCamera->mClipPlaneNear = floatVal; |
1159 | 8 | } else if (0 == ASSIMP_strincmp("far", prop->m_value->getString(), 3)) { |
1160 | 8 | m_currentCamera->mClipPlaneFar = floatVal; |
1161 | 8 | } |
1162 | 24 | } |
1163 | 24 | } |
1164 | | |
1165 | | //------------------------------------------------------------------------------------------------ |
1166 | 0 | void OpenGEXImporter::handleAttenNode(ODDLParser::DDLNode *node, aiScene * /*pScene*/) { |
1167 | 0 | if (nullptr == node) { |
1168 | 0 | return; |
1169 | 0 | } |
1170 | | |
1171 | 0 | Property *prop = node->findPropertyByName("curve"); |
1172 | 0 | if (nullptr != prop) { |
1173 | 0 | if (nullptr != prop->m_value) { |
1174 | 0 | Value *val(node->getValue()); |
1175 | 0 | const float floatVal(val->getFloat()); |
1176 | 0 | if (0 == strncmp("scale", prop->m_value->getString(), strlen("scale"))) { |
1177 | 0 | m_currentLight->mAttenuationQuadratic = floatVal; |
1178 | 0 | } |
1179 | 0 | } |
1180 | 0 | } |
1181 | 0 | } |
1182 | | |
1183 | | //------------------------------------------------------------------------------------------------ |
1184 | 906 | void OpenGEXImporter::copyMeshes(aiScene *pScene) { |
1185 | 906 | ai_assert(nullptr != pScene); |
1186 | | |
1187 | 906 | if (m_meshCache.empty()) { |
1188 | 833 | return; |
1189 | 833 | } |
1190 | | |
1191 | 73 | pScene->mNumMeshes = static_cast<unsigned int>(m_meshCache.size()); |
1192 | 73 | pScene->mMeshes = new aiMesh *[pScene->mNumMeshes]; |
1193 | 195 | for (unsigned int i = 0; i < pScene->mNumMeshes; i++) { |
1194 | 122 | pScene->mMeshes[i] = m_meshCache[i].release(); |
1195 | 122 | } |
1196 | 73 | } |
1197 | | |
1198 | | //------------------------------------------------------------------------------------------------ |
1199 | 906 | void OpenGEXImporter::copyCameras(aiScene *pScene) { |
1200 | 906 | ai_assert(nullptr != pScene); |
1201 | | |
1202 | 906 | if (m_cameraCache.empty()) { |
1203 | 902 | return; |
1204 | 902 | } |
1205 | | |
1206 | 4 | pScene->mNumCameras = static_cast<unsigned int>(m_cameraCache.size()); |
1207 | 4 | pScene->mCameras = new aiCamera *[pScene->mNumCameras]; |
1208 | 4 | std::copy(m_cameraCache.begin(), m_cameraCache.end(), pScene->mCameras); |
1209 | 4 | } |
1210 | | |
1211 | | //------------------------------------------------------------------------------------------------ |
1212 | 0 | void OpenGEXImporter::copyLights(aiScene *pScene) { |
1213 | 0 | ai_assert(nullptr != pScene); |
1214 | |
|
1215 | 0 | if (m_lightCache.empty()) { |
1216 | 0 | return; |
1217 | 0 | } |
1218 | | |
1219 | 0 | pScene->mNumLights = static_cast<unsigned int>(m_lightCache.size()); |
1220 | 0 | pScene->mLights = new aiLight *[pScene->mNumLights]; |
1221 | 0 | std::copy(m_lightCache.begin(), m_lightCache.end(), pScene->mLights); |
1222 | 0 | } |
1223 | | |
1224 | | //------------------------------------------------------------------------------------------------ |
1225 | 906 | void OpenGEXImporter::copyMaterials(aiScene *pScene) { |
1226 | 906 | ai_assert(nullptr != pScene); |
1227 | | |
1228 | 906 | if (m_materialCache.empty()) { |
1229 | 906 | return; |
1230 | 906 | } |
1231 | | |
1232 | 0 | pScene->mNumMaterials = static_cast<unsigned int>(m_materialCache.size()); |
1233 | 0 | pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials]; |
1234 | 0 | std::copy(m_materialCache.begin(), m_materialCache.end(), pScene->mMaterials); |
1235 | 0 | } |
1236 | | |
1237 | | //------------------------------------------------------------------------------------------------ |
1238 | 906 | void OpenGEXImporter::resolveReferences() { |
1239 | 906 | if (m_unresolvedRefStack.empty()) { |
1240 | 837 | return; |
1241 | 837 | } |
1242 | | |
1243 | 69 | RefInfo *currentRefInfo(nullptr); |
1244 | 367 | for (auto it = m_unresolvedRefStack.begin(); it != m_unresolvedRefStack.end(); ++it) { |
1245 | 298 | currentRefInfo = it->get(); |
1246 | 298 | if (nullptr != currentRefInfo) { |
1247 | 298 | aiNode *node(currentRefInfo->m_node); |
1248 | 298 | if (RefInfo::MeshRef == currentRefInfo->m_type) { |
1249 | 290 | for (size_t i = 0; i < currentRefInfo->m_Names.size(); ++i) { |
1250 | 145 | const std::string &name(currentRefInfo->m_Names[i]); |
1251 | 145 | ReferenceMap::const_iterator curIt(m_mesh2refMap.find(name)); |
1252 | 145 | if (m_mesh2refMap.end() != curIt) { |
1253 | 108 | unsigned int meshIdx = static_cast<unsigned int>(m_mesh2refMap[name]); |
1254 | 108 | node->mMeshes[i] = meshIdx; |
1255 | 108 | } |
1256 | 145 | } |
1257 | 153 | } else if (RefInfo::MaterialRef == currentRefInfo->m_type) { |
1258 | 306 | for (size_t i = 0; i < currentRefInfo->m_Names.size(); ++i) { |
1259 | 153 | const std::string name(currentRefInfo->m_Names[i]); |
1260 | 153 | ReferenceMap::const_iterator curIt(m_material2refMap.find(name)); |
1261 | 153 | if (m_material2refMap.end() != curIt) { |
1262 | 0 | if (nullptr != m_currentMesh) { |
1263 | 0 | unsigned int matIdx = static_cast<unsigned int>(m_material2refMap[name]); |
1264 | 0 | if (m_currentMesh->mMaterialIndex != 0) { |
1265 | 0 | ASSIMP_LOG_WARN("Override of material reference in current mesh by material reference."); |
1266 | 0 | } |
1267 | 0 | m_currentMesh->mMaterialIndex = matIdx; |
1268 | 0 | } else { |
1269 | 0 | ASSIMP_LOG_WARN("Cannot resolve material reference, because no current mesh is there."); |
1270 | 0 | } |
1271 | 0 | } |
1272 | 153 | } |
1273 | 153 | } else { |
1274 | 0 | throw DeadlyImportError("Unknown reference info to resolve."); |
1275 | 0 | } |
1276 | 298 | } |
1277 | 298 | } |
1278 | 69 | } |
1279 | | |
1280 | | //------------------------------------------------------------------------------------------------ |
1281 | 906 | void OpenGEXImporter::createNodeTree(aiScene *pScene) { |
1282 | 906 | if (nullptr == m_root) { |
1283 | 813 | return; |
1284 | 813 | } |
1285 | | |
1286 | 93 | if (m_root->m_children.empty()) { |
1287 | 0 | return; |
1288 | 0 | } |
1289 | | |
1290 | 93 | pScene->mRootNode->mNumChildren = static_cast<unsigned int>(m_root->m_children.size()); |
1291 | 93 | pScene->mRootNode->mChildren = new aiNode *[pScene->mRootNode->mNumChildren]; |
1292 | 93 | std::copy(m_root->m_children.begin(), m_root->m_children.end(), pScene->mRootNode->mChildren); |
1293 | 93 | } |
1294 | | |
1295 | | //------------------------------------------------------------------------------------------------ |
1296 | 227 | void OpenGEXImporter::pushNode(aiNode *node, aiScene *pScene) { |
1297 | 227 | ai_assert(nullptr != pScene); |
1298 | | |
1299 | 227 | if (nullptr == node) { |
1300 | 0 | return; |
1301 | 0 | } |
1302 | | |
1303 | 227 | ChildInfo *info(nullptr); |
1304 | 227 | if (m_nodeStack.empty()) { |
1305 | 207 | node->mParent = pScene->mRootNode; |
1306 | 207 | NodeChildMap::iterator it(m_nodeChildMap.find(node->mParent)); |
1307 | 207 | if (m_nodeChildMap.end() == it) { |
1308 | 114 | info = new ChildInfo; |
1309 | 114 | m_root = info; |
1310 | 114 | m_nodeChildMap[node->mParent] = std::unique_ptr<ChildInfo>(info); |
1311 | 114 | } else { |
1312 | 93 | info = it->second.get(); |
1313 | 93 | } |
1314 | 207 | info->m_children.push_back(node); |
1315 | 207 | } else { |
1316 | 20 | aiNode *parent(m_nodeStack.back()); |
1317 | 20 | ai_assert(nullptr != parent); |
1318 | 20 | node->mParent = parent; |
1319 | 20 | NodeChildMap::iterator it(m_nodeChildMap.find(node->mParent)); |
1320 | 20 | if (m_nodeChildMap.end() == it) { |
1321 | 13 | info = new ChildInfo; |
1322 | 13 | m_nodeChildMap[node->mParent] = std::unique_ptr<ChildInfo>(info); |
1323 | 13 | } else { |
1324 | 7 | info = it->second.get(); |
1325 | 7 | } |
1326 | 20 | info->m_children.push_back(node); |
1327 | 20 | } |
1328 | 227 | m_nodeStack.push_back(node); |
1329 | 227 | } |
1330 | | |
1331 | | //------------------------------------------------------------------------------------------------ |
1332 | 206 | aiNode *OpenGEXImporter::popNode() { |
1333 | 206 | if (m_nodeStack.empty()) { |
1334 | 0 | return nullptr; |
1335 | 0 | } |
1336 | | |
1337 | 206 | aiNode *node(top()); |
1338 | 206 | m_nodeStack.pop_back(); |
1339 | | |
1340 | 206 | return node; |
1341 | 206 | } |
1342 | | |
1343 | | //------------------------------------------------------------------------------------------------ |
1344 | 206 | aiNode *OpenGEXImporter::top() const { |
1345 | 206 | if (m_nodeStack.empty()) { |
1346 | 0 | return nullptr; |
1347 | 0 | } |
1348 | | |
1349 | 206 | return m_nodeStack.back(); |
1350 | 206 | } |
1351 | | |
1352 | | //------------------------------------------------------------------------------------------------ |
1353 | 0 | void OpenGEXImporter::clearNodeStack() { |
1354 | 0 | m_nodeStack.clear(); |
1355 | 0 | } |
1356 | | |
1357 | | //------------------------------------------------------------------------------------------------ |
1358 | | |
1359 | | } // Namespace OpenGEX |
1360 | | } // Namespace Assimp |
1361 | | |
1362 | | #endif // ASSIMP_BUILD_NO_OPENGEX_IMPORTER |