/src/assimp/code/AssetLib/Ogre/OgreXmlSerializer.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 | | |
42 | | #include "OgreXmlSerializer.h" |
43 | | #include "OgreBinarySerializer.h" |
44 | | #include "OgreParsingUtils.h" |
45 | | |
46 | | #include <assimp/TinyFormatter.h> |
47 | | #include <assimp/DefaultLogger.hpp> |
48 | | |
49 | | #include <memory> |
50 | | |
51 | | #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER |
52 | | |
53 | | // Define as 1 to get verbose logging. |
54 | | #define OGRE_XML_SERIALIZER_DEBUG 0 |
55 | | |
56 | | namespace Assimp { |
57 | | namespace Ogre { |
58 | | |
59 | | //AI_WONT_RETURN void ThrowAttibuteError(const XmlParser *reader, const std::string &name, const std::string &error = "") AI_WONT_RETURN_SUFFIX; |
60 | | AI_WONT_RETURN void ThrowAttibuteError(const std::string &nodeName, const std::string &name, const std::string &error) AI_WONT_RETURN_SUFFIX; |
61 | 0 | AI_WONT_RETURN void ThrowAttibuteError(const std::string &nodeName, const std::string &name, const std::string &error) { |
62 | 0 | if (!error.empty()) { |
63 | 0 | throw DeadlyImportError(error, " in node '", nodeName, "' and attribute '", name, "'"); |
64 | 0 | } else { |
65 | 0 | throw DeadlyImportError("Attribute '", name, "' does not exist in node '", nodeName, "'"); |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | template <> |
70 | 0 | int32_t OgreXmlSerializer::ReadAttribute<int32_t>(XmlNode &xmlNode, const char *name) const { |
71 | 0 | if (!XmlParser::hasAttribute(xmlNode, name)) { |
72 | 0 | ThrowAttibuteError(xmlNode.name(), name, "Not found"); |
73 | 0 | } |
74 | 0 | pugi::xml_attribute attr = xmlNode.attribute(name); |
75 | 0 | return static_cast<int32_t>(attr.as_int()); |
76 | 0 | } |
77 | | |
78 | | template <> |
79 | 0 | uint32_t OgreXmlSerializer::ReadAttribute<uint32_t>(XmlNode &xmlNode, const char *name) const { |
80 | 0 | if (!XmlParser::hasAttribute(xmlNode, name)) { |
81 | 0 | ThrowAttibuteError(xmlNode.name(), name, "Not found"); |
82 | 0 | } |
83 | | |
84 | | // @note This is hackish. But we are never expecting unsigned values that go outside the |
85 | | // int32_t range. Just monitor for negative numbers and kill the import. |
86 | 0 | int32_t temp = ReadAttribute<int32_t>(xmlNode, name); |
87 | 0 | if (temp < 0) { |
88 | 0 | ThrowAttibuteError(xmlNode.name(), name, "Found a negative number value where expecting a uint32_t value"); |
89 | 0 | } |
90 | | |
91 | 0 | return static_cast<uint32_t>(temp); |
92 | 0 | } |
93 | | |
94 | | template <> |
95 | 0 | uint16_t OgreXmlSerializer::ReadAttribute<uint16_t>(XmlNode &xmlNode, const char *name) const { |
96 | 0 | if (!XmlParser::hasAttribute(xmlNode, name)) { |
97 | 0 | ThrowAttibuteError(xmlNode.name(), name, "Not found"); |
98 | 0 | } |
99 | | |
100 | 0 | return static_cast<uint16_t>(xmlNode.attribute(name).as_int()); |
101 | 0 | } |
102 | | |
103 | | template <> |
104 | 0 | float OgreXmlSerializer::ReadAttribute<float>(XmlNode &xmlNode, const char *name) const { |
105 | 0 | if (!XmlParser::hasAttribute(xmlNode, name)) { |
106 | 0 | ThrowAttibuteError(xmlNode.name(), name, "Not found"); |
107 | 0 | } |
108 | | |
109 | 0 | return xmlNode.attribute(name).as_float(); |
110 | 0 | } |
111 | | |
112 | | template <> |
113 | 0 | std::string OgreXmlSerializer::ReadAttribute<std::string>(XmlNode &xmlNode, const char *name) const { |
114 | 0 | if (!XmlParser::hasAttribute(xmlNode, name)) { |
115 | 0 | ThrowAttibuteError(xmlNode.name(), name, "Not found"); |
116 | 0 | } |
117 | | |
118 | 0 | return xmlNode.attribute(name).as_string(); |
119 | 0 | } |
120 | | |
121 | | template <> |
122 | 0 | bool OgreXmlSerializer::ReadAttribute<bool>(XmlNode &xmlNode, const char *name) const { |
123 | 0 | std::string value = ai_tolower(ReadAttribute<std::string>(xmlNode, name)); |
124 | 0 | if (ASSIMP_stricmp(value, "true") == 0) { |
125 | 0 | return true; |
126 | 0 | } else if (ASSIMP_stricmp(value, "false") == 0) { |
127 | 0 | return false; |
128 | 0 | } |
129 | | |
130 | 0 | ThrowAttibuteError(xmlNode.name(), name, "Boolean value is expected to be 'true' or 'false', encountered '" + value + "'"); |
131 | 0 | } |
132 | | |
133 | | // Mesh XML constants |
134 | | |
135 | | // <mesh> |
136 | | static const char *nnMesh = "mesh"; |
137 | | static const char *nnSharedGeometry = "sharedgeometry"; |
138 | | static const char *nnSubMeshes = "submeshes"; |
139 | | static const char *nnSubMesh = "submesh"; |
140 | | //static const char *nnSubMeshNames = "submeshnames"; |
141 | | static const char *nnSkeletonLink = "skeletonlink"; |
142 | | //static const char *nnLOD = "levelofdetail"; |
143 | | //static const char *nnExtremes = "extremes"; |
144 | | //static const char *nnPoses = "poses"; |
145 | | static const char *nnAnimations = "animations"; |
146 | | |
147 | | // <submesh> |
148 | | static const char *nnFaces = "faces"; |
149 | | static const char *nnFace = "face"; |
150 | | static const char *nnGeometry = "geometry"; |
151 | | //static const char *nnTextures = "textures"; |
152 | | |
153 | | // <mesh/submesh> |
154 | | static const char *nnBoneAssignments = "boneassignments"; |
155 | | |
156 | | // <sharedgeometry/geometry> |
157 | | static const char *nnVertexBuffer = "vertexbuffer"; |
158 | | |
159 | | // <vertexbuffer> |
160 | | //static const char *nnVertex = "vertex"; |
161 | | static const char *nnPosition = "position"; |
162 | | static const char *nnNormal = "normal"; |
163 | | static const char *nnTangent = "tangent"; |
164 | | //static const char *nnBinormal = "binormal"; |
165 | | static const char *nnTexCoord = "texcoord"; |
166 | | //static const char *nnColorDiffuse = "colour_diffuse"; |
167 | | //static const char *nnColorSpecular = "colour_specular"; |
168 | | |
169 | | // <boneassignments> |
170 | | static const char *nnVertexBoneAssignment = "vertexboneassignment"; |
171 | | |
172 | | // Skeleton XML constants |
173 | | |
174 | | // <skeleton> |
175 | | static const char *nnSkeleton = "skeleton"; |
176 | | static const char *nnBones = "bones"; |
177 | | static const char *nnBoneHierarchy = "bonehierarchy"; |
178 | | //static const char *nnAnimationLinks = "animationlinks"; |
179 | | |
180 | | // <bones> |
181 | | static const char *nnBone = "bone"; |
182 | | static const char *nnRotation = "rotation"; |
183 | | static const char *nnAxis = "axis"; |
184 | | static const char *nnScale = "scale"; |
185 | | |
186 | | // <bonehierarchy> |
187 | | static const char *nnBoneParent = "boneparent"; |
188 | | |
189 | | // <animations> |
190 | | static const char *nnAnimation = "animation"; |
191 | | static const char *nnTracks = "tracks"; |
192 | | |
193 | | // <tracks> |
194 | | static const char *nnTrack = "track"; |
195 | | static const char *nnKeyFrames = "keyframes"; |
196 | | static const char *nnKeyFrame = "keyframe"; |
197 | | static const char *nnTranslate = "translate"; |
198 | | static const char *nnRotate = "rotate"; |
199 | | |
200 | | // Common XML constants |
201 | | static const char *anX = "x"; |
202 | | static const char *anY = "y"; |
203 | | static const char *anZ = "z"; |
204 | | |
205 | | // Mesh |
206 | | |
207 | | OgreXmlSerializer::OgreXmlSerializer(XmlParser *parser) : |
208 | 0 | mParser(parser) { |
209 | | // empty |
210 | 0 | } |
211 | | |
212 | 0 | MeshXml *OgreXmlSerializer::ImportMesh(XmlParser *parser) { |
213 | 0 | if (nullptr == parser) { |
214 | 0 | return nullptr; |
215 | 0 | } |
216 | | |
217 | 0 | OgreXmlSerializer serializer(parser); |
218 | |
|
219 | 0 | MeshXml *mesh = new MeshXml(); |
220 | 0 | serializer.ReadMesh(mesh); |
221 | |
|
222 | 0 | return mesh; |
223 | 0 | } |
224 | | |
225 | 0 | void OgreXmlSerializer::ReadMesh(MeshXml *mesh) { |
226 | 0 | XmlNode root = mParser->getRootNode(); |
227 | 0 | if (nullptr == root) { |
228 | 0 | throw DeadlyImportError("Root node is <" + std::string(root.name()) + "> expecting <mesh>"); |
229 | 0 | } |
230 | | |
231 | 0 | XmlNode startNode = root.child(nnMesh); |
232 | 0 | if (startNode.empty()) { |
233 | 0 | throw DeadlyImportError("Root node is <" + std::string(root.name()) + "> expecting <mesh>"); |
234 | 0 | } |
235 | 0 | for (XmlNode currentNode : startNode.children()) { |
236 | 0 | const std::string currentName = currentNode.name(); |
237 | 0 | if (currentName == nnSharedGeometry) { |
238 | 0 | mesh->sharedVertexData = new VertexDataXml(); |
239 | 0 | ReadGeometry(currentNode, mesh->sharedVertexData); |
240 | 0 | } else if (currentName == nnSubMeshes) { |
241 | 0 | for (XmlNode &subMeshesNode : currentNode.children()) { |
242 | 0 | const std::string ¤tSMName = subMeshesNode.name(); |
243 | 0 | if (currentSMName == nnSubMesh) { |
244 | 0 | ReadSubMesh(subMeshesNode, mesh); |
245 | 0 | } |
246 | 0 | } |
247 | 0 | } else if (currentName == nnBoneAssignments) { |
248 | 0 | ReadBoneAssignments(currentNode, mesh->sharedVertexData); |
249 | 0 | } else if (currentName == nnSkeletonLink) { |
250 | 0 | mesh->skeletonRef = currentNode.attribute("name").as_string(); |
251 | 0 | } |
252 | 0 | } |
253 | |
|
254 | 0 | ASSIMP_LOG_VERBOSE_DEBUG("Reading Mesh"); |
255 | 0 | } |
256 | | |
257 | 0 | void OgreXmlSerializer::ReadGeometry(XmlNode &node, VertexDataXml *dest) { |
258 | 0 | dest->count = ReadAttribute<uint32_t>(node, "vertexcount"); |
259 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Reading geometry of ", dest->count, " vertices"); |
260 | |
|
261 | 0 | for (XmlNode currentNode : node.children()) { |
262 | 0 | const std::string ¤tName = currentNode.name(); |
263 | 0 | if (currentName == nnVertexBuffer) { |
264 | 0 | ReadGeometryVertexBuffer(currentNode, dest); |
265 | 0 | } |
266 | 0 | } |
267 | 0 | } |
268 | | |
269 | 0 | void OgreXmlSerializer::ReadGeometryVertexBuffer(XmlNode &node, VertexDataXml *dest) { |
270 | 0 | bool positions = (XmlParser::hasAttribute(node, "positions") && ReadAttribute<bool>(node, "positions")); |
271 | 0 | bool normals = (XmlParser::hasAttribute(node, "normals") && ReadAttribute<bool>(node, "normals")); |
272 | 0 | bool tangents = (XmlParser::hasAttribute(node, "tangents") && ReadAttribute<bool>(node, "tangents")); |
273 | 0 | uint32_t uvs = (XmlParser::hasAttribute(node, "texture_coords") ? ReadAttribute<uint32_t>(node, "texture_coords") : 0); |
274 | | |
275 | | // Not having positions is a error only if a previous vertex buffer did not have them. |
276 | 0 | if (!positions && !dest->HasPositions()) { |
277 | 0 | throw DeadlyImportError("Vertex buffer does not contain positions!"); |
278 | 0 | } |
279 | | |
280 | 0 | if (positions) { |
281 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Contains positions"); |
282 | 0 | dest->positions.reserve(dest->count); |
283 | 0 | } |
284 | 0 | if (normals) { |
285 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Contains normals"); |
286 | 0 | dest->normals.reserve(dest->count); |
287 | 0 | } |
288 | 0 | if (tangents) { |
289 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Contains tangents"); |
290 | 0 | dest->tangents.reserve(dest->count); |
291 | 0 | } |
292 | 0 | if (uvs > 0) { |
293 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Contains ", uvs, " texture coords"); |
294 | 0 | dest->uvs.resize(uvs); |
295 | 0 | for (size_t i = 0, len = dest->uvs.size(); i < len; ++i) { |
296 | 0 | dest->uvs[i].reserve(dest->count); |
297 | 0 | } |
298 | 0 | } |
299 | |
|
300 | 0 | for (XmlNode currentNode : node.children("vertex")) { |
301 | 0 | for (XmlNode vertexNode : currentNode.children()) { |
302 | 0 | const std::string ¤tName = vertexNode.name(); |
303 | 0 | if (positions && currentName == nnPosition) { |
304 | 0 | aiVector3D pos; |
305 | 0 | pos.x = ReadAttribute<float>(vertexNode, anX); |
306 | 0 | pos.y = ReadAttribute<float>(vertexNode, anY); |
307 | 0 | pos.z = ReadAttribute<float>(vertexNode, anZ); |
308 | 0 | dest->positions.push_back(pos); |
309 | 0 | } else if (normals && currentName == nnNormal) { |
310 | 0 | aiVector3D normal; |
311 | 0 | normal.x = ReadAttribute<float>(vertexNode, anX); |
312 | 0 | normal.y = ReadAttribute<float>(vertexNode, anY); |
313 | 0 | normal.z = ReadAttribute<float>(vertexNode, anZ); |
314 | 0 | dest->normals.push_back(normal); |
315 | 0 | } else if (tangents && currentName == nnTangent) { |
316 | 0 | aiVector3D tangent; |
317 | 0 | tangent.x = ReadAttribute<float>(vertexNode, anX); |
318 | 0 | tangent.y = ReadAttribute<float>(vertexNode, anY); |
319 | 0 | tangent.z = ReadAttribute<float>(vertexNode, anZ); |
320 | 0 | dest->tangents.push_back(tangent); |
321 | 0 | } else if (uvs > 0 && currentName == nnTexCoord) { |
322 | 0 | for (auto &curUvs : dest->uvs) { |
323 | 0 | aiVector3D uv; |
324 | 0 | uv.x = ReadAttribute<float>(vertexNode, "u"); |
325 | 0 | uv.y = (ReadAttribute<float>(vertexNode, "v") * -1) + 1; // Flip UV from Ogre to Assimp form |
326 | 0 | curUvs.push_back(uv); |
327 | 0 | } |
328 | 0 | } |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | | // Sanity checks |
333 | 0 | if (dest->positions.size() != dest->count) { |
334 | 0 | throw DeadlyImportError("Read only ", dest->positions.size(), " positions when should have read ", dest->count); |
335 | 0 | } |
336 | 0 | if (normals && dest->normals.size() != dest->count) { |
337 | 0 | throw DeadlyImportError("Read only ", dest->normals.size(), " normals when should have read ", dest->count); |
338 | 0 | } |
339 | 0 | if (tangents && dest->tangents.size() != dest->count) { |
340 | 0 | throw DeadlyImportError("Read only ", dest->tangents.size(), " tangents when should have read ", dest->count); |
341 | 0 | } |
342 | 0 | for (unsigned int i = 0; i < dest->uvs.size(); ++i) { |
343 | 0 | if (dest->uvs[i].size() != dest->count) { |
344 | 0 | throw DeadlyImportError("Read only ", dest->uvs[i].size(), |
345 | 0 | " uvs for uv index ", i, " when should have read ", dest->count); |
346 | 0 | } |
347 | 0 | } |
348 | 0 | } |
349 | | |
350 | 0 | void OgreXmlSerializer::ReadSubMesh(XmlNode &node, MeshXml *mesh) { |
351 | 0 | static const char *anMaterial = "material"; |
352 | 0 | static const char *anUseSharedVertices = "usesharedvertices"; |
353 | 0 | static const char *anCount = "count"; |
354 | 0 | static const char *anV1 = "v1"; |
355 | 0 | static const char *anV2 = "v2"; |
356 | 0 | static const char *anV3 = "v3"; |
357 | 0 | static const char *anV4 = "v4"; |
358 | |
|
359 | 0 | SubMeshXml *submesh = new SubMeshXml(); |
360 | |
|
361 | 0 | if (XmlParser::hasAttribute(node, anMaterial)) { |
362 | 0 | submesh->materialRef = ReadAttribute<std::string>(node, anMaterial); |
363 | 0 | } |
364 | 0 | if (XmlParser::hasAttribute(node, anUseSharedVertices)) { |
365 | 0 | submesh->usesSharedVertexData = ReadAttribute<bool>(node, anUseSharedVertices); |
366 | 0 | } |
367 | |
|
368 | 0 | ASSIMP_LOG_VERBOSE_DEBUG("Reading SubMesh ", mesh->subMeshes.size()); |
369 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Material: '", submesh->materialRef, "'"); |
370 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Uses shared geometry: ", (submesh->usesSharedVertexData ? "true" : "false")); |
371 | | |
372 | | // TODO: maybe we have always just 1 faces and 1 geometry and always in this order. this loop will only work correct, when the order |
373 | | // of faces and geometry changed, and not if we have more than one of one |
374 | | /// @todo Fix above comment with better read logic below |
375 | |
|
376 | 0 | bool quadWarned = false; |
377 | |
|
378 | 0 | for (XmlNode ¤tNode : node.children()) { |
379 | 0 | const std::string ¤tName = currentNode.name(); |
380 | 0 | if (currentName == nnFaces) { |
381 | 0 | submesh->indexData->faceCount = ReadAttribute<uint32_t>(currentNode, anCount); |
382 | 0 | submesh->indexData->faces.reserve(submesh->indexData->faceCount); |
383 | 0 | for (XmlNode currentChildNode : currentNode.children()) { |
384 | 0 | const std::string ¤tChildName = currentChildNode.name(); |
385 | 0 | if (currentChildName == nnFace) { |
386 | 0 | aiFace face; |
387 | 0 | face.mNumIndices = 3; |
388 | 0 | face.mIndices = new unsigned int[3]; |
389 | 0 | face.mIndices[0] = ReadAttribute<uint32_t>(currentChildNode, anV1); |
390 | 0 | face.mIndices[1] = ReadAttribute<uint32_t>(currentChildNode, anV2); |
391 | 0 | face.mIndices[2] = ReadAttribute<uint32_t>(currentChildNode, anV3); |
392 | | /// @todo Support quads if Ogre even supports them in XML (I'm not sure but I doubt it) |
393 | 0 | if (!quadWarned && XmlParser::hasAttribute(currentChildNode, anV4)) { |
394 | 0 | ASSIMP_LOG_WARN("Submesh <face> has quads with <v4>, only triangles are supported at the moment!"); |
395 | 0 | quadWarned = true; |
396 | 0 | } |
397 | 0 | submesh->indexData->faces.push_back(face); |
398 | 0 | } |
399 | 0 | } |
400 | 0 | if (submesh->indexData->faces.size() == submesh->indexData->faceCount) { |
401 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Faces ", submesh->indexData->faceCount); |
402 | 0 | } else { |
403 | 0 | throw DeadlyImportError("Read only ", submesh->indexData->faces.size(), " faces when should have read ", submesh->indexData->faceCount); |
404 | 0 | } |
405 | 0 | } else if (currentName == nnGeometry) { |
406 | 0 | if (submesh->usesSharedVertexData) { |
407 | 0 | throw DeadlyImportError("Found <geometry> in <submesh> when use shared geometry is true. Invalid mesh file."); |
408 | 0 | } |
409 | | |
410 | 0 | submesh->vertexData = new VertexDataXml(); |
411 | 0 | ReadGeometry(currentNode, submesh->vertexData); |
412 | 0 | } else if (currentName == nnBoneAssignments) { |
413 | 0 | ReadBoneAssignments(currentNode, submesh->vertexData); |
414 | 0 | } |
415 | 0 | } |
416 | | |
417 | 0 | submesh->index = static_cast<unsigned int>(mesh->subMeshes.size()); |
418 | 0 | mesh->subMeshes.push_back(submesh); |
419 | 0 | } |
420 | | |
421 | 0 | void OgreXmlSerializer::ReadBoneAssignments(XmlNode &node, VertexDataXml *dest) { |
422 | 0 | if (!dest) { |
423 | 0 | throw DeadlyImportError("Cannot read bone assignments, vertex data is null."); |
424 | 0 | } |
425 | | |
426 | 0 | static const char *anVertexIndex = "vertexindex"; |
427 | 0 | static const char *anBoneIndex = "boneindex"; |
428 | 0 | static const char *anWeight = "weight"; |
429 | |
|
430 | 0 | std::set<uint32_t> influencedVertices; |
431 | 0 | for (XmlNode ¤tNode : node.children()) { |
432 | 0 | const std::string ¤tName = currentNode.name(); |
433 | 0 | if (currentName == nnVertexBoneAssignment) { |
434 | 0 | VertexBoneAssignment ba; |
435 | 0 | ba.vertexIndex = ReadAttribute<uint32_t>(currentNode, anVertexIndex); |
436 | 0 | ba.boneIndex = ReadAttribute<uint16_t>(currentNode, anBoneIndex); |
437 | 0 | ba.weight = ReadAttribute<float>(currentNode, anWeight); |
438 | |
|
439 | 0 | dest->boneAssignments.push_back(ba); |
440 | 0 | influencedVertices.insert(ba.vertexIndex); |
441 | 0 | } |
442 | 0 | } |
443 | | |
444 | | /** Normalize bone weights. |
445 | | Some exporters won't care if the sum of all bone weights |
446 | | for a single vertex equals 1 or not, so validate here. */ |
447 | 0 | const float epsilon = 0.05f; |
448 | 0 | for (const uint32_t vertexIndex : influencedVertices) { |
449 | 0 | float sum = 0.0f; |
450 | 0 | for (VertexBoneAssignmentList::const_iterator baIter = dest->boneAssignments.begin(), baEnd = dest->boneAssignments.end(); baIter != baEnd; ++baIter) { |
451 | 0 | if (baIter->vertexIndex == vertexIndex) |
452 | 0 | sum += baIter->weight; |
453 | 0 | } |
454 | 0 | if ((sum < (1.0f - epsilon)) || (sum > (1.0f + epsilon))) { |
455 | 0 | for (auto &boneAssign : dest->boneAssignments) { |
456 | 0 | if (boneAssign.vertexIndex == vertexIndex) |
457 | 0 | boneAssign.weight /= sum; |
458 | 0 | } |
459 | 0 | } |
460 | 0 | } |
461 | |
|
462 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - ", dest->boneAssignments.size(), " bone assignments"); |
463 | 0 | } |
464 | | |
465 | | // Skeleton |
466 | | |
467 | 0 | bool OgreXmlSerializer::ImportSkeleton(Assimp::IOSystem *pIOHandler, MeshXml *mesh) { |
468 | 0 | if (!mesh || mesh->skeletonRef.empty()) |
469 | 0 | return false; |
470 | | |
471 | | // Highly unusual to see in read world cases but support |
472 | | // XML mesh referencing a binary skeleton file. |
473 | 0 | if (EndsWith(mesh->skeletonRef, ".skeleton", false)) { |
474 | 0 | if (OgreBinarySerializer::ImportSkeleton(pIOHandler, mesh)) |
475 | 0 | return true; |
476 | | |
477 | | /** Last fallback if .skeleton failed to be read. Try reading from |
478 | | .skeleton.xml even if the XML file referenced a binary skeleton. |
479 | | @note This logic was in the previous version and I don't want to break |
480 | | old code that might depends on it. */ |
481 | 0 | mesh->skeletonRef = mesh->skeletonRef + ".xml"; |
482 | 0 | } |
483 | | |
484 | 0 | XmlParserPtr xmlParser = OpenXmlParser(pIOHandler, mesh->skeletonRef); |
485 | 0 | if (!xmlParser.get()) |
486 | 0 | return false; |
487 | | |
488 | 0 | Skeleton *skeleton = new Skeleton(); |
489 | 0 | OgreXmlSerializer serializer(xmlParser.get()); |
490 | 0 | XmlNode root = xmlParser->getRootNode(); |
491 | 0 | if (std::string(root.name()) != nnSkeleton) { |
492 | 0 | ASSIMP_LOG_VERBOSE_DEBUG("nSkeleton is not a valid root: ", root.name(), "."); |
493 | 0 | for (auto &a : root.children()) { |
494 | 0 | if (std::string(a.name()) == nnSkeleton) { |
495 | 0 | root = a; |
496 | 0 | break; |
497 | 0 | } |
498 | 0 | } |
499 | 0 | } |
500 | 0 | serializer.ReadSkeleton(root, skeleton); |
501 | 0 | mesh->skeleton = skeleton; |
502 | 0 | return true; |
503 | 0 | } |
504 | | |
505 | 0 | bool OgreXmlSerializer::ImportSkeleton(Assimp::IOSystem *pIOHandler, Mesh *mesh) { |
506 | 0 | if (!mesh || mesh->skeletonRef.empty()) { |
507 | 0 | return false; |
508 | 0 | } |
509 | | |
510 | 0 | XmlParserPtr xmlParser = OpenXmlParser(pIOHandler, mesh->skeletonRef); |
511 | 0 | if (!xmlParser.get()) { |
512 | 0 | return false; |
513 | 0 | } |
514 | | |
515 | 0 | Skeleton *skeleton = new Skeleton(); |
516 | 0 | OgreXmlSerializer serializer(xmlParser.get()); |
517 | 0 | XmlNode root = xmlParser->getRootNode(); |
518 | |
|
519 | 0 | serializer.ReadSkeleton(root, skeleton); |
520 | 0 | mesh->skeleton = skeleton; |
521 | |
|
522 | 0 | return true; |
523 | 0 | } |
524 | | |
525 | 0 | XmlParserPtr OgreXmlSerializer::OpenXmlParser(Assimp::IOSystem *pIOHandler, const std::string &filename) { |
526 | 0 | if (!EndsWith(filename, ".skeleton.xml", false)) { |
527 | 0 | ASSIMP_LOG_ERROR("Imported Mesh is referencing to unsupported '", filename, "' skeleton file."); |
528 | 0 | return XmlParserPtr(); |
529 | 0 | } |
530 | | |
531 | 0 | if (!pIOHandler->Exists(filename)) { |
532 | 0 | ASSIMP_LOG_ERROR("Failed to find skeleton file '", filename, "' that is referenced by imported Mesh."); |
533 | 0 | return XmlParserPtr(); |
534 | 0 | } |
535 | | |
536 | 0 | std::unique_ptr<IOStream> file(pIOHandler->Open(filename)); |
537 | 0 | if (!file) { |
538 | 0 | throw DeadlyImportError("Failed to open skeleton file ", filename); |
539 | 0 | } |
540 | | |
541 | 0 | XmlParserPtr xmlParser = std::make_shared<XmlParser>(); |
542 | 0 | if (!xmlParser->parse(file.get())) { |
543 | 0 | throw DeadlyImportError("Failed to create XML reader for skeleton file " + filename); |
544 | 0 | } |
545 | 0 | return xmlParser; |
546 | 0 | } |
547 | | |
548 | 0 | void OgreXmlSerializer::ReadSkeleton(XmlNode &node, Skeleton *skeleton) { |
549 | 0 | if (std::string(node.name()) != nnSkeleton) { |
550 | 0 | throw DeadlyImportError("Root node is <" + std::string(node.name()) + "> expecting <skeleton>"); |
551 | 0 | } |
552 | | |
553 | 0 | ASSIMP_LOG_VERBOSE_DEBUG("Reading Skeleton"); |
554 | | |
555 | | // Optional blend mode from root node |
556 | 0 | if (XmlParser::hasAttribute(node, "blendmode")) { |
557 | 0 | skeleton->blendMode = (ai_tolower(ReadAttribute<std::string>(node, "blendmode")) == "cumulative" ? Skeleton::ANIMBLEND_CUMULATIVE : Skeleton::ANIMBLEND_AVERAGE); |
558 | 0 | } |
559 | |
|
560 | 0 | for (XmlNode ¤tNode : node.children()) { |
561 | 0 | const std::string currentName = currentNode.name(); |
562 | 0 | if (currentName == nnBones) { |
563 | 0 | ReadBones(currentNode, skeleton); |
564 | 0 | } else if (currentName == nnBoneHierarchy) { |
565 | 0 | ReadBoneHierarchy(currentNode, skeleton); |
566 | 0 | } else if (currentName == nnAnimations) { |
567 | 0 | ReadAnimations(currentNode, skeleton); |
568 | 0 | } |
569 | 0 | } |
570 | 0 | } |
571 | | |
572 | 0 | void OgreXmlSerializer::ReadAnimations(XmlNode &node, Skeleton *skeleton) { |
573 | 0 | if (skeleton->bones.empty()) { |
574 | 0 | throw DeadlyImportError("Cannot read <animations> for a Skeleton without bones"); |
575 | 0 | } |
576 | | |
577 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Animations"); |
578 | |
|
579 | 0 | for (XmlNode ¤tNode : node.children()) { |
580 | 0 | const std::string currentName = currentNode.name(); |
581 | 0 | if (currentName == nnAnimation) { |
582 | 0 | Animation *anim = new Animation(skeleton); |
583 | 0 | anim->name = ReadAttribute<std::string>(currentNode, "name"); |
584 | 0 | anim->length = ReadAttribute<float>(currentNode, "length"); |
585 | 0 | for (XmlNode ¤tChildNode : currentNode.children()) { |
586 | 0 | const std::string currentChildName = currentChildNode.name(); |
587 | 0 | if (currentChildName == nnTracks) { |
588 | 0 | ReadAnimationTracks(currentChildNode, anim); |
589 | 0 | } else { |
590 | 0 | throw DeadlyImportError("No <tracks> found in <animation> ", anim->name); |
591 | 0 | } |
592 | 0 | } |
593 | 0 | skeleton->animations.push_back(anim); |
594 | 0 | } |
595 | 0 | } |
596 | 0 | } |
597 | | |
598 | 0 | void OgreXmlSerializer::ReadAnimationTracks(XmlNode &node, Animation *dest) { |
599 | 0 | for (XmlNode ¤tNode : node.children()) { |
600 | 0 | const std::string currentName = currentNode.name(); |
601 | 0 | if (currentName == nnTrack) { |
602 | 0 | VertexAnimationTrack track; |
603 | 0 | track.type = VertexAnimationTrack::VAT_TRANSFORM; |
604 | 0 | track.boneName = ReadAttribute<std::string>(currentNode, "bone"); |
605 | 0 | for (XmlNode ¤tChildNode : currentNode.children()) { |
606 | 0 | const std::string currentChildName = currentChildNode.name(); |
607 | 0 | if (currentChildName == nnKeyFrames) { |
608 | 0 | ReadAnimationKeyFrames(currentChildNode, dest, &track); |
609 | 0 | } else { |
610 | 0 | throw DeadlyImportError("No <keyframes> found in <track> ", dest->name); |
611 | 0 | } |
612 | 0 | } |
613 | 0 | dest->tracks.push_back(track); |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | | |
618 | 0 | void OgreXmlSerializer::ReadAnimationKeyFrames(XmlNode &node, Animation *anim, VertexAnimationTrack *dest) { |
619 | 0 | const aiVector3D zeroVec(0.f, 0.f, 0.f); |
620 | 0 | for (XmlNode ¤tNode : node.children()) { |
621 | 0 | TransformKeyFrame keyframe; |
622 | 0 | const std::string currentName = currentNode.name(); |
623 | 0 | if (currentName == nnKeyFrame) { |
624 | 0 | keyframe.timePos = ReadAttribute<float>(currentNode, "time"); |
625 | 0 | for (XmlNode ¤tChildNode : currentNode.children()) { |
626 | 0 | const std::string currentChildName = currentChildNode.name(); |
627 | 0 | if (currentChildName == nnTranslate) { |
628 | 0 | keyframe.position.x = ReadAttribute<float>(currentChildNode, anX); |
629 | 0 | keyframe.position.y = ReadAttribute<float>(currentChildNode, anY); |
630 | 0 | keyframe.position.z = ReadAttribute<float>(currentChildNode, anZ); |
631 | 0 | } else if (currentChildName == nnRotate) { |
632 | 0 | float angle = ReadAttribute<float>(currentChildNode, "angle"); |
633 | 0 | for (XmlNode ¤tChildChildNode : currentChildNode.children()) { |
634 | 0 | const std::string currentChildChildName = currentChildChildNode.name(); |
635 | 0 | if (currentChildChildName == nnAxis) { |
636 | 0 | aiVector3D axis; |
637 | 0 | axis.x = ReadAttribute<float>(currentChildChildNode, anX); |
638 | 0 | axis.y = ReadAttribute<float>(currentChildChildNode, anY); |
639 | 0 | axis.z = ReadAttribute<float>(currentChildChildNode, anZ); |
640 | 0 | if (axis.Equal(zeroVec)) { |
641 | 0 | axis.x = 1.0f; |
642 | 0 | if (angle != 0) { |
643 | 0 | ASSIMP_LOG_WARN("Found invalid a key frame with a zero rotation axis in animation: ", anim->name); |
644 | 0 | } |
645 | 0 | } |
646 | 0 | keyframe.rotation = aiQuaternion(axis, angle); |
647 | 0 | } |
648 | 0 | } |
649 | 0 | } else if (currentChildName == nnScale) { |
650 | 0 | keyframe.scale.x = ReadAttribute<float>(currentChildNode, anX); |
651 | 0 | keyframe.scale.y = ReadAttribute<float>(currentChildNode, anY); |
652 | 0 | keyframe.scale.z = ReadAttribute<float>(currentChildNode, anZ); |
653 | 0 | } |
654 | 0 | } |
655 | 0 | } |
656 | 0 | dest->transformKeyFrames.push_back(keyframe); |
657 | 0 | } |
658 | 0 | } |
659 | | |
660 | 0 | void OgreXmlSerializer::ReadBoneHierarchy(XmlNode &node, Skeleton *skeleton) { |
661 | 0 | if (skeleton->bones.empty()) { |
662 | 0 | throw DeadlyImportError("Cannot read <bonehierarchy> for a Skeleton without bones"); |
663 | 0 | } |
664 | | |
665 | 0 | for (XmlNode ¤tNode : node.children()) { |
666 | 0 | const std::string currentName = currentNode.name(); |
667 | 0 | if (currentName == nnBoneParent) { |
668 | 0 | const std::string name = ReadAttribute<std::string>(currentNode, "bone"); |
669 | 0 | const std::string parentName = ReadAttribute<std::string>(currentNode, "parent"); |
670 | |
|
671 | 0 | Bone *bone = skeleton->BoneByName(name); |
672 | 0 | Bone *parent = skeleton->BoneByName(parentName); |
673 | |
|
674 | 0 | if (bone && parent) { |
675 | 0 | parent->AddChild(bone); |
676 | 0 | } else { |
677 | 0 | throw DeadlyImportError("Failed to find bones for parenting: Child ", name, " for parent ", parentName); |
678 | 0 | } |
679 | 0 | } |
680 | 0 | } |
681 | | |
682 | | // Calculate bone matrices for root bones. Recursively calculates their children. |
683 | 0 | for (size_t i = 0, len = skeleton->bones.size(); i < len; ++i) { |
684 | 0 | Bone *bone = skeleton->bones[i]; |
685 | 0 | if (!bone->IsParented()) |
686 | 0 | bone->CalculateWorldMatrixAndDefaultPose(skeleton); |
687 | 0 | } |
688 | 0 | } |
689 | | |
690 | 0 | static bool BoneCompare(Bone *a, Bone *b) { |
691 | 0 | ai_assert(nullptr != a); |
692 | 0 | ai_assert(nullptr != b); |
693 | |
|
694 | 0 | return (a->id < b->id); |
695 | 0 | } |
696 | | |
697 | 0 | void OgreXmlSerializer::ReadBones(XmlNode &node, Skeleton *skeleton) { |
698 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" - Bones"); |
699 | |
|
700 | 0 | for (XmlNode ¤tNode : node.children()) { |
701 | 0 | const std::string currentName = currentNode.name(); |
702 | 0 | if (currentName == nnBone) { |
703 | 0 | Bone *bone = new Bone(); |
704 | 0 | bone->id = ReadAttribute<uint16_t>(currentNode, "id"); |
705 | 0 | bone->name = ReadAttribute<std::string>(currentNode, "name"); |
706 | 0 | for (XmlNode ¤tChildNode : currentNode.children()) { |
707 | 0 | const std::string currentChildName = currentChildNode.name(); |
708 | 0 | if (currentChildName == nnPosition) { |
709 | 0 | bone->position.x = ReadAttribute<float>(currentChildNode, anX); |
710 | 0 | bone->position.y = ReadAttribute<float>(currentChildNode, anY); |
711 | 0 | bone->position.z = ReadAttribute<float>(currentChildNode, anZ); |
712 | 0 | } else if (currentChildName == nnRotation) { |
713 | 0 | float angle = ReadAttribute<float>(currentChildNode, "angle"); |
714 | 0 | for (XmlNode currentChildChildNode : currentChildNode.children()) { |
715 | 0 | const std::string ¤tChildChildName = currentChildChildNode.name(); |
716 | 0 | if (currentChildChildName == nnAxis) { |
717 | 0 | aiVector3D axis; |
718 | 0 | axis.x = ReadAttribute<float>(currentChildChildNode, anX); |
719 | 0 | axis.y = ReadAttribute<float>(currentChildChildNode, anY); |
720 | 0 | axis.z = ReadAttribute<float>(currentChildChildNode, anZ); |
721 | |
|
722 | 0 | bone->rotation = aiQuaternion(axis, angle); |
723 | 0 | } else { |
724 | 0 | throw DeadlyImportError("No axis specified for bone rotation in bone ", bone->id); |
725 | 0 | } |
726 | 0 | } |
727 | 0 | } else if (currentChildName == nnScale) { |
728 | 0 | if (XmlParser::hasAttribute(currentChildNode, "factor")) { |
729 | 0 | float factor = ReadAttribute<float>(currentChildNode, "factor"); |
730 | 0 | bone->scale.Set(factor, factor, factor); |
731 | 0 | } else { |
732 | 0 | if (XmlParser::hasAttribute(currentChildNode, anX)) |
733 | 0 | bone->scale.x = ReadAttribute<float>(currentChildNode, anX); |
734 | 0 | if (XmlParser::hasAttribute(currentChildNode, anY)) |
735 | 0 | bone->scale.y = ReadAttribute<float>(currentChildNode, anY); |
736 | 0 | if (XmlParser::hasAttribute(currentChildNode, anZ)) |
737 | 0 | bone->scale.z = ReadAttribute<float>(currentChildNode, anZ); |
738 | 0 | } |
739 | 0 | } |
740 | 0 | } |
741 | 0 | skeleton->bones.push_back(bone); |
742 | 0 | } |
743 | 0 | } |
744 | | |
745 | | // Order bones by Id |
746 | 0 | std::sort(skeleton->bones.begin(), skeleton->bones.end(), BoneCompare); |
747 | | |
748 | | // Validate that bone indexes are not skipped. |
749 | | /** @note Left this from original authors code, but not sure if this is strictly necessary |
750 | | as per the Ogre skeleton spec. It might be more that other (later) code in this imported does not break. */ |
751 | 0 | for (size_t i = 0, len = skeleton->bones.size(); i < len; ++i) { |
752 | 0 | Bone *b = skeleton->bones[i]; |
753 | 0 | ASSIMP_LOG_VERBOSE_DEBUG(" ", b->id, " ", b->name); |
754 | |
|
755 | 0 | if (b->id != static_cast<uint16_t>(i)) { |
756 | 0 | throw DeadlyImportError("Bone ids are not in sequence starting from 0. Missing index ", i); |
757 | 0 | } |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | | } // namespace Ogre |
762 | | } // namespace Assimp |
763 | | |
764 | | #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER |