/src/assimp/code/AssetLib/SMD/SMDLoader.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | --------------------------------------------------------------------------- |
3 | | Open Asset Import Library (assimp) |
4 | | --------------------------------------------------------------------------- |
5 | | |
6 | | Copyright (c) 2006-2025, assimp team |
7 | | |
8 | | All rights reserved. |
9 | | |
10 | | Redistribution and use of this software in source and binary forms, |
11 | | with or without modification, are permitted provided that the following |
12 | | conditions are met: |
13 | | |
14 | | * Redistributions of source code must retain the above |
15 | | copyright notice, this list of conditions and the |
16 | | following disclaimer. |
17 | | |
18 | | * Redistributions in binary form must reproduce the above |
19 | | copyright notice, this list of conditions and the |
20 | | following disclaimer in the documentation and/or other |
21 | | materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the assimp team, nor the names of its |
24 | | contributors may be used to endorse or promote products |
25 | | derived from this software without specific prior |
26 | | written permission of the assimp team. |
27 | | |
28 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
29 | | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
31 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
32 | | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
33 | | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
34 | | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
35 | | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
36 | | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
37 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
38 | | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | | --------------------------------------------------------------------------- |
40 | | */ |
41 | | |
42 | | /** @file SMDLoader.cpp |
43 | | * @brief Implementation of the SMD importer class |
44 | | */ |
45 | | |
46 | | |
47 | | #ifndef ASSIMP_BUILD_NO_SMD_IMPORTER |
48 | | |
49 | | #include <assimp/fast_atof.h> |
50 | | #include <assimp/SkeletonMeshBuilder.h> |
51 | | #include <assimp/Importer.hpp> |
52 | | #include <assimp/IOSystem.hpp> |
53 | | #include <assimp/scene.h> |
54 | | #include <assimp/DefaultLogger.hpp> |
55 | | #include <assimp/importerdesc.h> |
56 | | #include <memory> |
57 | | #include <assimp/DefaultIOSystem.h> |
58 | | #include <tuple> |
59 | | |
60 | | // internal headers |
61 | | #include "SMDLoader.h" |
62 | | |
63 | | #ifndef _MSC_VER |
64 | 6.40k | #define strtok_s strtok_r |
65 | | #endif |
66 | | |
67 | | namespace Assimp { |
68 | | |
69 | | static constexpr aiImporterDesc desc = { |
70 | | "Valve SMD Importer", |
71 | | "", |
72 | | "", |
73 | | "", |
74 | | aiImporterFlags_SupportTextFlavour, |
75 | | 0, |
76 | | 0, |
77 | | 0, |
78 | | 0, |
79 | | "smd vta" |
80 | | }; |
81 | | |
82 | | // ------------------------------------------------------------------------------------------------ |
83 | | // Constructor to be privately used by Importer |
84 | | SMDImporter::SMDImporter() : |
85 | | configFrameID(), |
86 | 220 | mBuffer(), |
87 | 220 | mEnd(nullptr), |
88 | 220 | pScene(nullptr), |
89 | 220 | iFileSize( 0 ), |
90 | | iSmallestFrame( INT_MAX ), |
91 | 220 | dLengthOfAnim( 0.0 ), |
92 | 220 | bHasUVs(false ), |
93 | 220 | iLineNumber((unsigned int)-1) { |
94 | | // empty |
95 | 220 | } |
96 | | |
97 | | |
98 | | // ------------------------------------------------------------------------------------------------ |
99 | | // Returns whether the class can handle the format of the given file. |
100 | 137 | bool SMDImporter::CanRead( const std::string& filename, IOSystem* /*pIOHandler*/, bool) const { |
101 | 137 | return SimpleExtensionCheck(filename, "smd", "vta"); |
102 | 137 | } |
103 | | |
104 | | // ------------------------------------------------------------------------------------------------ |
105 | | // Get a list of all supported file extensions |
106 | 214 | const aiImporterDesc* SMDImporter::GetInfo () const { |
107 | 214 | return &desc; |
108 | 214 | } |
109 | | |
110 | | // ------------------------------------------------------------------------------------------------ |
111 | | // Setup configuration properties |
112 | 4 | void SMDImporter::SetupProperties(const Importer* pImp) { |
113 | | // The |
114 | | // AI_CONFIG_IMPORT_SMD_KEYFRAME option overrides the |
115 | | // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option. |
116 | 4 | configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_SMD_KEYFRAME,-1); |
117 | 4 | if(static_cast<unsigned int>(-1) == configFrameID) { |
118 | 4 | configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0); |
119 | 4 | } |
120 | | |
121 | 4 | bLoadAnimationList = pImp->GetPropertyBool(AI_CONFIG_IMPORT_SMD_LOAD_ANIMATION_LIST, true); |
122 | 4 | noSkeletonMesh = pImp->GetPropertyBool(AI_CONFIG_IMPORT_NO_SKELETON_MESHES, false); |
123 | 4 | } |
124 | | |
125 | | // ------------------------------------------------------------------------------------------------ |
126 | | // Imports the given file into the given scene structure. |
127 | 4 | void SMDImporter::InternReadFile( const std::string& pFile, aiScene* scene, IOSystem* pIOHandler) { |
128 | 4 | this->pScene = scene; |
129 | 4 | ReadSmd(pFile, pIOHandler); |
130 | | |
131 | | // If there are no triangles it seems to be an animation SMD, |
132 | | // containing only the animation skeleton. |
133 | 4 | if (asTriangles.empty()) { |
134 | 0 | if (asBones.empty()) { |
135 | 0 | throw DeadlyImportError("SMD: No triangles and no bones have " |
136 | 0 | "been found in the file. This file seems to be invalid."); |
137 | 0 | } |
138 | | |
139 | | // Set the flag in the scene structure which indicates |
140 | | // that there is nothing than an animation skeleton |
141 | 0 | pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE; |
142 | 0 | } |
143 | | |
144 | 4 | if (!asBones.empty()) { |
145 | | // Check whether all bones have been initialized |
146 | 7 | for (const auto &asBone : asBones) { |
147 | 7 | if (!asBone.mName.length()) { |
148 | 3 | ASSIMP_LOG_WARN("SMD: Not all bones have been initialized"); |
149 | 3 | break; |
150 | 3 | } |
151 | 7 | } |
152 | | |
153 | | // now fix invalid time values and make sure the animation starts at frame 0 |
154 | 4 | FixTimeValues(); |
155 | 4 | } |
156 | | |
157 | | // build output nodes (bones are added as empty dummy nodes) |
158 | 4 | CreateOutputNodes(); |
159 | | |
160 | 4 | if (!(pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)) { |
161 | | // create output meshes |
162 | 4 | CreateOutputMeshes(); |
163 | | |
164 | | // build an output material list |
165 | 4 | CreateOutputMaterials(); |
166 | | |
167 | | // use root node that renders all meshes |
168 | 4 | pScene->mRootNode->mNumMeshes = pScene->mNumMeshes; |
169 | 4 | pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes]; |
170 | 8 | for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { |
171 | 4 | pScene->mRootNode->mMeshes[i] = i; |
172 | 4 | } |
173 | 4 | } |
174 | | |
175 | | // build the output animation |
176 | 4 | CreateOutputAnimations(pFile, pIOHandler); |
177 | | |
178 | 4 | if ((pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) && !noSkeletonMesh) { |
179 | 0 | SkeletonMeshBuilder skeleton(pScene); |
180 | 0 | } |
181 | 4 | } |
182 | | |
183 | | // ------------------------------------------------------------------------------------------------ |
184 | | // Write an error message with line number to the log file |
185 | 14.2k | void SMDImporter::LogErrorNoThrow(const char* msg) { |
186 | 14.2k | const size_t _BufferSize = 1024; |
187 | 14.2k | char szTemp[_BufferSize]; |
188 | 14.2k | ai_snprintf(szTemp,_BufferSize,"Line %u: %s",iLineNumber,msg); |
189 | 14.2k | DefaultLogger::get()->error(szTemp); |
190 | 14.2k | } |
191 | | |
192 | | // ------------------------------------------------------------------------------------------------ |
193 | | // Write a warning with line number to the log file |
194 | 8.84k | void SMDImporter::LogWarning(const char* msg) { |
195 | 8.84k | const size_t _BufferSize = 1024; |
196 | 8.84k | char szTemp[_BufferSize]; |
197 | 8.84k | ai_assert(strlen(msg) < 1000); |
198 | 8.84k | ai_snprintf(szTemp,_BufferSize,"Line %u: %s",iLineNumber,msg); |
199 | 8.84k | ASSIMP_LOG_WARN(szTemp); |
200 | 8.84k | } |
201 | | |
202 | | // ------------------------------------------------------------------------------------------------ |
203 | | // Fix invalid time values in the file |
204 | 2.10k | void SMDImporter::FixTimeValues() { |
205 | 2.10k | double dDelta = (double)iSmallestFrame; |
206 | 2.10k | double dMax = 0.0f; |
207 | 4.69k | for (auto &asBone : asBones) { |
208 | 4.69k | for (auto &asKey : asBone.sAnim.asKeys) { |
209 | 0 | asKey.dTime -= dDelta; |
210 | 0 | dMax = std::max(dMax, asKey.dTime); |
211 | 0 | } |
212 | 4.69k | } |
213 | 2.10k | dLengthOfAnim = dMax; |
214 | 2.10k | } |
215 | | |
216 | | // ------------------------------------------------------------------------------------------------ |
217 | | // create output meshes |
218 | 4 | void SMDImporter::CreateOutputMeshes() { |
219 | 4 | if (aszTextures.empty()) { |
220 | 0 | aszTextures.emplace_back(); |
221 | 0 | } |
222 | | |
223 | | // we need to sort all faces by their material index |
224 | | // in opposition to other loaders we can be sure that each |
225 | | // material is at least used once. |
226 | 4 | pScene->mNumMeshes = (unsigned int) aszTextures.size(); |
227 | 4 | pScene->mMeshes = new aiMesh*[pScene->mNumMeshes]; |
228 | | |
229 | 4 | typedef std::vector<unsigned int> FaceList; |
230 | 4 | std::unique_ptr<FaceList[]> aaiFaces(new FaceList[pScene->mNumMeshes]); |
231 | | |
232 | | // approximate the space that will be required |
233 | 4 | unsigned int iNum = (unsigned int)asTriangles.size() / pScene->mNumMeshes; |
234 | 4 | iNum += iNum >> 1; |
235 | 8 | for (unsigned int i = 0; i < pScene->mNumMeshes;++i) { |
236 | 4 | aaiFaces[i].reserve(iNum); |
237 | 4 | } |
238 | | |
239 | | // collect all faces |
240 | 4 | iNum = 0; |
241 | 4 | for (const auto &asTriangle : asTriangles) { |
242 | 4 | if (asTriangle.iTexture >= aszTextures.size()) { |
243 | 0 | ASSIMP_LOG_INFO("[SMD/VTA] Material index overflow in face"); |
244 | 0 | aaiFaces[asTriangle.iTexture].push_back((unsigned int)aszTextures.size()-1); |
245 | 4 | } else { |
246 | 4 | aaiFaces[asTriangle.iTexture].push_back(iNum); |
247 | 4 | } |
248 | 4 | ++iNum; |
249 | 4 | } |
250 | | |
251 | | // now create the output meshes |
252 | 8 | for (unsigned int i = 0; i < pScene->mNumMeshes;++i) { |
253 | 4 | aiMesh*& pcMesh = pScene->mMeshes[i] = new aiMesh(); |
254 | 4 | ai_assert(!aaiFaces[i].empty()); // should not be empty ... |
255 | | |
256 | 4 | pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE; |
257 | 4 | pcMesh->mNumVertices = (unsigned int)aaiFaces[i].size()*3; |
258 | 4 | pcMesh->mNumFaces = (unsigned int)aaiFaces[i].size(); |
259 | 4 | pcMesh->mMaterialIndex = i; |
260 | | |
261 | | // storage for bones |
262 | 4 | typedef std::pair<unsigned int,float> TempWeightListEntry; |
263 | 4 | typedef std::vector< TempWeightListEntry > TempBoneWeightList; |
264 | | |
265 | 4 | std::unique_ptr<TempBoneWeightList[]> aaiBones(new TempBoneWeightList[asBones.size()]()); |
266 | | |
267 | | // try to reserve enough memory without wasting too much |
268 | 44 | for (unsigned int iBone = 0; iBone < asBones.size();++iBone) { |
269 | 40 | aaiBones[iBone].reserve(pcMesh->mNumVertices/asBones.size()); |
270 | 40 | } |
271 | | |
272 | | // allocate storage |
273 | 4 | pcMesh->mFaces = new aiFace[pcMesh->mNumFaces]; |
274 | 4 | aiVector3D* pcNormals = pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices]; |
275 | 4 | aiVector3D* pcVerts = pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices]; |
276 | | |
277 | 4 | aiVector3D* pcUVs = nullptr; |
278 | 4 | if (bHasUVs) { |
279 | 4 | pcUVs = pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices]; |
280 | 4 | pcMesh->mNumUVComponents[0] = 2; |
281 | 4 | } |
282 | | |
283 | 4 | iNum = 0; |
284 | 8 | for (unsigned int iFace = 0; iFace < pcMesh->mNumFaces;++iFace) { |
285 | 4 | pcMesh->mFaces[iFace].mIndices = new unsigned int[3]; |
286 | 4 | pcMesh->mFaces[iFace].mNumIndices = 3; |
287 | | |
288 | | // fill the vertices |
289 | 4 | unsigned int iSrcFace = aaiFaces[i][iFace]; |
290 | 4 | SMD::Face& face = asTriangles[iSrcFace]; |
291 | | |
292 | 4 | *pcVerts++ = face.avVertices[0].pos; |
293 | 4 | *pcVerts++ = face.avVertices[1].pos; |
294 | 4 | *pcVerts++ = face.avVertices[2].pos; |
295 | | |
296 | | // fill the normals |
297 | 4 | *pcNormals++ = face.avVertices[0].nor; |
298 | 4 | *pcNormals++ = face.avVertices[1].nor; |
299 | 4 | *pcNormals++ = face.avVertices[2].nor; |
300 | | |
301 | | // fill the texture coordinates |
302 | 4 | if (pcUVs) { |
303 | 4 | *pcUVs++ = face.avVertices[0].uv; |
304 | 4 | *pcUVs++ = face.avVertices[1].uv; |
305 | 4 | *pcUVs++ = face.avVertices[2].uv; |
306 | 4 | } |
307 | | |
308 | 16 | for (unsigned int iVert = 0; iVert < 3;++iVert) { |
309 | 12 | float fSum = 0.0f; |
310 | 12 | for (unsigned int iBone = 0;iBone < face.avVertices[iVert].aiBoneLinks.size();++iBone) { |
311 | 0 | TempWeightListEntry& pairval = face.avVertices[iVert].aiBoneLinks[iBone]; |
312 | | |
313 | | // FIX: The second check is here just to make sure we won't |
314 | | // assign more than one weight to a single vertex index |
315 | 0 | if (pairval.first >= asBones.size() || pairval.first == face.avVertices[iVert].iParentNode) { |
316 | 0 | ASSIMP_LOG_ERROR("[SMD/VTA] Bone index overflow. " |
317 | 0 | "The bone index will be ignored, the weight will be assigned " |
318 | 0 | "to the vertex' parent node"); |
319 | 0 | continue; |
320 | 0 | } |
321 | 0 | aaiBones[pairval.first].emplace_back(iNum,pairval.second); |
322 | 0 | fSum += pairval.second; |
323 | 0 | } |
324 | | // ****************************************************************** |
325 | | // If the sum of all vertex weights is not 1.0 we must assign |
326 | | // the rest to the vertex' parent node. Well, at least the doc says |
327 | | // we should ... |
328 | | // FIX: We use 0.975 as limit, floating-point inaccuracies seem to |
329 | | // be very strong in some SMD exporters. Furthermore it is possible |
330 | | // that the parent of a vertex is 0xffffffff (if the corresponding |
331 | | // entry in the file was unreadable) |
332 | | // ****************************************************************** |
333 | 12 | if (fSum < 0.975f && face.avVertices[iVert].iParentNode != UINT_MAX) { |
334 | 10 | if (face.avVertices[iVert].iParentNode >= asBones.size()) { |
335 | 7 | ASSIMP_LOG_ERROR("[SMD/VTA] Bone index overflow. " |
336 | 7 | "The index of the vertex parent bone is invalid. " |
337 | 7 | "The remaining weights will be normalized to 1.0"); |
338 | | |
339 | 7 | if (fSum) { |
340 | 0 | fSum = 1 / fSum; |
341 | 0 | for (auto &pairval : face.avVertices[iVert].aiBoneLinks) { |
342 | 0 | if (pairval.first >= asBones.size()) { |
343 | 0 | continue; |
344 | 0 | } |
345 | 0 | aaiBones[pairval.first].back().second *= fSum; |
346 | 0 | } |
347 | 0 | } |
348 | 7 | } else { |
349 | 3 | aaiBones[face.avVertices[iVert].iParentNode].emplace_back(iNum,1.0f-fSum); |
350 | 3 | } |
351 | 10 | } |
352 | 12 | pcMesh->mFaces[iFace].mIndices[iVert] = iNum++; |
353 | 12 | } |
354 | 4 | } |
355 | | |
356 | | // now build all bones of the mesh |
357 | 4 | iNum = 0; |
358 | 44 | for (unsigned int iBone = 0; iBone < asBones.size();++iBone) { |
359 | 40 | if (!aaiBones[iBone].empty())++iNum; |
360 | 40 | } |
361 | | |
362 | 4 | if (iNum) { |
363 | 3 | pcMesh->mNumBones = iNum; |
364 | 3 | pcMesh->mBones = new aiBone*[pcMesh->mNumBones]; |
365 | 3 | iNum = 0; |
366 | 42 | for (unsigned int iBone = 0; iBone < asBones.size();++iBone) { |
367 | 39 | if (aaiBones[iBone].empty()) { |
368 | 36 | continue; |
369 | 36 | } |
370 | 3 | aiBone*& bone = pcMesh->mBones[iNum] = new aiBone(); |
371 | | |
372 | 3 | bone->mNumWeights = (unsigned int)aaiBones[iBone].size(); |
373 | 3 | bone->mWeights = new aiVertexWeight[bone->mNumWeights]; |
374 | 3 | bone->mOffsetMatrix = asBones[iBone].mOffsetMatrix; |
375 | 3 | bone->mName.Set( asBones[iBone].mName ); |
376 | | |
377 | 3 | asBones[iBone].bIsUsed = true; |
378 | | |
379 | 6 | for (unsigned int iWeight = 0; iWeight < bone->mNumWeights;++iWeight) { |
380 | 3 | bone->mWeights[iWeight].mVertexId = aaiBones[iBone][iWeight].first; |
381 | 3 | bone->mWeights[iWeight].mWeight = aaiBones[iBone][iWeight].second; |
382 | 3 | } |
383 | 3 | ++iNum; |
384 | 3 | } |
385 | 3 | } |
386 | 4 | } |
387 | 4 | } |
388 | | |
389 | | // ------------------------------------------------------------------------------------------------ |
390 | | // add bone child nodes |
391 | 38 | void SMDImporter::AddBoneChildren(aiNode* pcNode, uint32_t iParent) { |
392 | 38 | ai_assert( nullptr != pcNode ); |
393 | 38 | ai_assert( 0 == pcNode->mNumChildren ); |
394 | 38 | ai_assert( nullptr == pcNode->mChildren); |
395 | | |
396 | | // first count ... |
397 | 470 | for (auto &bone : asBones) { |
398 | 470 | if (bone.iParent == iParent) { |
399 | 34 | ++pcNode->mNumChildren; |
400 | 34 | } |
401 | 470 | } |
402 | | |
403 | | // nothing to do |
404 | 38 | if (pcNode->mNumChildren == 0) |
405 | 34 | return; |
406 | | |
407 | | // now allocate the output array |
408 | 4 | pcNode->mChildren = new aiNode *[pcNode->mNumChildren]; |
409 | | |
410 | | // and fill all subnodes |
411 | 4 | unsigned int qq( 0 ); |
412 | 44 | for (unsigned int i = 0; i < asBones.size();++i) { |
413 | 40 | SMD::Bone& bone = asBones[i]; |
414 | 40 | if (bone.iParent != iParent) { |
415 | 6 | continue; |
416 | 6 | } |
417 | | |
418 | 34 | aiNode* pc = pcNode->mChildren[qq++] = new aiNode(); |
419 | 34 | pc->mName.Set(bone.mName); |
420 | | |
421 | | // store the local transformation matrix of the bind pose |
422 | 34 | if (bone.sAnim.asKeys.size()) { |
423 | 0 | pc->mTransformation = bone.sAnim.asKeys[0].matrix; |
424 | 0 | } |
425 | | |
426 | 34 | if (bone.iParent == static_cast<uint32_t>(-1)) { |
427 | 34 | bone.mOffsetMatrix = pc->mTransformation; |
428 | 34 | } else { |
429 | 0 | bone.mOffsetMatrix = asBones[bone.iParent].mOffsetMatrix * pc->mTransformation; |
430 | 0 | } |
431 | | |
432 | 34 | pc->mParent = pcNode; |
433 | | |
434 | | // add children to this node, too |
435 | 34 | AddBoneChildren(pc,i); |
436 | 34 | } |
437 | 4 | } |
438 | | |
439 | | // ------------------------------------------------------------------------------------------------ |
440 | | // create output nodes |
441 | 4 | void SMDImporter::CreateOutputNodes() { |
442 | 4 | pScene->mRootNode = new aiNode(); |
443 | | |
444 | | // now add all bones as dummy sub nodes to the graph |
445 | 4 | AddBoneChildren(pScene->mRootNode,(uint32_t)-1); |
446 | 40 | for (auto &bone : asBones) { |
447 | 40 | bone.mOffsetMatrix.Inverse(); |
448 | 40 | } |
449 | | |
450 | | // if we have only one bone we can even remove the root node |
451 | 4 | if (pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE && 1 == pScene->mRootNode->mNumChildren) { |
452 | 0 | aiNode* pcOldRoot = pScene->mRootNode; |
453 | 0 | pScene->mRootNode = pcOldRoot->mChildren[0]; |
454 | 0 | pcOldRoot->mChildren[0] = nullptr; |
455 | 0 | delete pcOldRoot; |
456 | |
|
457 | 0 | pScene->mRootNode->mParent = nullptr; |
458 | 4 | } else { |
459 | 4 | static constexpr char rootName[11] = "<SMD_root>"; |
460 | 4 | pScene->mRootNode->mName.length = 10; |
461 | 4 | ::strncpy(pScene->mRootNode->mName.data, rootName, pScene->mRootNode->mName.length); |
462 | 4 | } |
463 | 4 | } |
464 | | |
465 | | // ------------------------------------------------------------------------------------------------ |
466 | | // create output animations |
467 | 4 | void SMDImporter::CreateOutputAnimations(const std::string &pFile, IOSystem* pIOHandler) { |
468 | 4 | std::vector<std::tuple<std::string, std::string>> animFileList; |
469 | | |
470 | 4 | if (bLoadAnimationList) { |
471 | 4 | GetAnimationFileList(pFile, pIOHandler, animFileList); |
472 | 4 | } |
473 | 4 | int animCount = static_cast<int>( animFileList.size() + 1u ); |
474 | 4 | pScene->mNumAnimations = 1; |
475 | 4 | pScene->mAnimations = new aiAnimation*[animCount]; |
476 | 4 | memset(pScene->mAnimations, 0, sizeof(aiAnimation*)*animCount); |
477 | 4 | CreateOutputAnimation(0, ""); |
478 | | |
479 | 2.09k | for (auto &animFile : animFileList) { |
480 | 2.09k | ReadSmd(std::get<1>(animFile), pIOHandler); |
481 | 2.09k | if (asBones.empty()) { |
482 | 0 | continue; |
483 | 0 | } |
484 | | |
485 | 2.09k | FixTimeValues(); |
486 | 2.09k | CreateOutputAnimation(pScene->mNumAnimations++, std::get<0>(animFile)); |
487 | 2.09k | } |
488 | 4 | } |
489 | | |
490 | 2.10k | void SMDImporter::CreateOutputAnimation(int index, const std::string &name) { |
491 | 2.10k | aiAnimation*& anim = pScene->mAnimations[index] = new aiAnimation(); |
492 | | |
493 | 2.10k | if (name.length()) { |
494 | 2.09k | anim->mName.Set(name.c_str()); |
495 | 2.09k | } |
496 | 2.10k | anim->mDuration = dLengthOfAnim; |
497 | 2.10k | anim->mNumChannels = static_cast<unsigned int>( asBones.size() ); |
498 | 2.10k | anim->mTicksPerSecond = 25.0; // FIXME: is this correct? |
499 | | |
500 | 2.10k | aiNodeAnim** pp = anim->mChannels = new aiNodeAnim*[anim->mNumChannels]; |
501 | | |
502 | | // now build valid keys |
503 | 2.10k | unsigned int a = 0; |
504 | 4.69k | for (const auto &asBone : asBones) { |
505 | 4.69k | aiNodeAnim* p = pp[a] = new aiNodeAnim(); |
506 | | |
507 | | // copy the name of the bone |
508 | 4.69k | p->mNodeName.Set(asBone.mName); |
509 | | |
510 | 4.69k | p->mNumRotationKeys = (unsigned int)asBone.sAnim.asKeys.size(); |
511 | 4.69k | if (p->mNumRotationKeys){ |
512 | 0 | p->mNumPositionKeys = p->mNumRotationKeys; |
513 | 0 | aiVectorKey* pVecKeys = p->mPositionKeys = new aiVectorKey[p->mNumRotationKeys]; |
514 | 0 | aiQuatKey* pRotKeys = p->mRotationKeys = new aiQuatKey[p->mNumRotationKeys]; |
515 | |
|
516 | 0 | for (const auto &asKey : asBone.sAnim.asKeys) { |
517 | 0 | pRotKeys->mTime = pVecKeys->mTime = asKey.dTime; |
518 | | |
519 | | // compute the rotation quaternion from the euler angles |
520 | | // aiQuaternion: The order of the parameters is yzx? |
521 | 0 | pRotKeys->mValue = aiQuaternion(asKey.vRot.y, asKey.vRot.z, asKey.vRot.x); |
522 | 0 | pVecKeys->mValue = asKey.vPos; |
523 | |
|
524 | 0 | ++pVecKeys; ++pRotKeys; |
525 | 0 | } |
526 | 0 | } |
527 | 4.69k | ++a; |
528 | | |
529 | | // there are no scaling keys ... |
530 | 4.69k | } |
531 | 2.10k | } |
532 | | |
533 | 4 | void SMDImporter::GetAnimationFileList(const std::string &pFile, IOSystem* pIOHandler, std::vector<std::tuple<std::string, std::string>>& outList) { |
534 | 4 | auto base = DefaultIOSystem::absolutePath(pFile); |
535 | 4 | auto name = DefaultIOSystem::completeBaseName(pFile); |
536 | 4 | auto path = base + "/" + name + "_animation.txt"; |
537 | | |
538 | 4 | std::unique_ptr<IOStream> file(pIOHandler->Open(path.c_str(), "rb")); |
539 | 4 | if (file == nullptr) { |
540 | 0 | return; |
541 | 0 | } |
542 | | |
543 | | // Allocate storage and copy the contents of the file to a memory buffer |
544 | 4 | std::vector<char> buf; |
545 | 4 | size_t fileSize = file->FileSize(); |
546 | 4 | buf.resize(fileSize + 1); |
547 | 4 | TextFileToBuffer(file.get(), buf); |
548 | | |
549 | | /* |
550 | | *_animation.txt format: |
551 | | name path |
552 | | idle idle.smd |
553 | | jump anim/jump.smd |
554 | | walk.smd |
555 | | ... |
556 | | */ |
557 | 4 | std::string animName, animPath; |
558 | 4 | char *tok1, *tok2; |
559 | 4 | char *context1, *context2; |
560 | | |
561 | 4 | tok1 = strtok_s(&buf[0], "\r\n", &context1); |
562 | 2.15k | while (tok1 != nullptr) { |
563 | 2.15k | tok2 = strtok_s(tok1, " \t", &context2); |
564 | 2.15k | if (tok2) { |
565 | 2.09k | char *p = tok2; |
566 | 2.09k | tok2 = strtok_s(nullptr, " \t", &context2); |
567 | 2.09k | if (tok2) { |
568 | 465 | animPath = tok2; |
569 | 465 | animName = p; |
570 | 1.63k | } else { |
571 | | // No name |
572 | 1.63k | animPath = p; |
573 | 1.63k | animName = DefaultIOSystem::completeBaseName(animPath); |
574 | 1.63k | } |
575 | 2.09k | outList.emplace_back(animName, base + "/" + animPath); |
576 | 2.09k | } |
577 | 2.15k | tok1 = strtok_s(nullptr, "\r\n", &context1); |
578 | 2.15k | } |
579 | 4 | } |
580 | | |
581 | | // ------------------------------------------------------------------------------------------------ |
582 | | // create output materials |
583 | 4 | void SMDImporter::CreateOutputMaterials() { |
584 | 4 | ai_assert( nullptr != pScene ); |
585 | | |
586 | 4 | pScene->mNumMaterials = (unsigned int)aszTextures.size(); |
587 | 4 | pScene->mMaterials = new aiMaterial*[std::max(1u, pScene->mNumMaterials)]; |
588 | | |
589 | 8 | for (unsigned int iMat = 0; iMat < pScene->mNumMaterials; ++iMat) { |
590 | 4 | aiMaterial* pcMat = new aiMaterial(); |
591 | 4 | ai_assert( nullptr != pcMat ); |
592 | 4 | pScene->mMaterials[iMat] = pcMat; |
593 | | |
594 | 4 | aiString szName; |
595 | 4 | szName.length = static_cast<ai_uint32>(ai_snprintf(szName.data, AI_MAXLEN, "Texture_%u", iMat)); |
596 | 4 | pcMat->AddProperty(&szName,AI_MATKEY_NAME); |
597 | | |
598 | 4 | if (aszTextures[iMat].length()) |
599 | 4 | { |
600 | 4 | ::strncpy(szName.data, aszTextures[iMat].c_str(), AI_MAXLEN - 1); |
601 | 4 | szName.length = static_cast<ai_uint32>( aszTextures[iMat].length() ); |
602 | 4 | pcMat->AddProperty(&szName,AI_MATKEY_TEXTURE_DIFFUSE(0)); |
603 | 4 | } |
604 | 4 | } |
605 | | |
606 | | // create a default material if necessary |
607 | 4 | if (0 == pScene->mNumMaterials) { |
608 | 0 | pScene->mNumMaterials = 1; |
609 | |
|
610 | 0 | aiMaterial* pcHelper = new aiMaterial(); |
611 | 0 | pScene->mMaterials[0] = pcHelper; |
612 | |
|
613 | 0 | int iMode = static_cast<int>(aiShadingMode_Gouraud); |
614 | 0 | pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL); |
615 | |
|
616 | 0 | aiColor3D clr; |
617 | 0 | clr.b = clr.g = clr.r = 0.7f; |
618 | 0 | pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE); |
619 | 0 | pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR); |
620 | |
|
621 | 0 | clr.b = clr.g = clr.r = 0.05f; |
622 | 0 | pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT); |
623 | |
|
624 | 0 | aiString szName; |
625 | 0 | szName.Set(AI_DEFAULT_MATERIAL_NAME); |
626 | 0 | pcHelper->AddProperty(&szName,AI_MATKEY_NAME); |
627 | 0 | } |
628 | 4 | } |
629 | | |
630 | | // ------------------------------------------------------------------------------------------------ |
631 | | // Parse the file |
632 | 2.10k | void SMDImporter::ParseFile() { |
633 | 2.10k | const char* szCurrent = &mBuffer[0]; |
634 | | |
635 | | // read line per line ... |
636 | 3.56M | for ( ;; ) { |
637 | 3.56M | if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent, mEnd)) { |
638 | 2.10k | break; |
639 | 2.10k | } |
640 | | |
641 | | // "version <n> \n", <n> should be 1 for hl and hl2 SMD files |
642 | 3.56M | if (TokenMatch(szCurrent,"version",7)) { |
643 | 0 | if(!SkipSpaces(szCurrent,&szCurrent, mEnd)) break; |
644 | 0 | if (1 != strtoul10(szCurrent,&szCurrent)) { |
645 | 0 | ASSIMP_LOG_WARN("SMD.version is not 1. This " |
646 | 0 | "file format is not known. Continuing happily ..."); |
647 | 0 | } |
648 | 0 | continue; |
649 | 0 | } |
650 | | // "nodes\n" - Starts the node section |
651 | 3.56M | if (TokenMatch(szCurrent,"nodes",5)) { |
652 | 4.42k | ParseNodesSection(szCurrent, &szCurrent, mEnd); |
653 | 4.42k | continue; |
654 | 4.42k | } |
655 | | // "triangles\n" - Starts the triangle section |
656 | 3.55M | if (TokenMatch(szCurrent,"triangles",9)) { |
657 | 2.10k | ParseTrianglesSection(szCurrent, &szCurrent, mEnd); |
658 | 2.10k | continue; |
659 | 2.10k | } |
660 | | // "vertexanimation\n" - Starts the vertex animation section |
661 | 3.55M | if (TokenMatch(szCurrent,"vertexanimation",15)) { |
662 | 0 | bHasUVs = false; |
663 | 0 | ParseVASection(szCurrent, &szCurrent, mEnd); |
664 | 0 | continue; |
665 | 0 | } |
666 | | // "skeleton\n" - Starts the skeleton section |
667 | 3.55M | if (TokenMatch(szCurrent,"skeleton",8)) { |
668 | 0 | ParseSkeletonSection(szCurrent, &szCurrent, mEnd); |
669 | 0 | continue; |
670 | 0 | } |
671 | 3.55M | SkipLine(szCurrent, &szCurrent, mEnd); |
672 | 3.55M | } |
673 | 2.10k | } |
674 | | |
675 | 2.10k | void SMDImporter::ReadSmd(const std::string &pFile, IOSystem* pIOHandler) { |
676 | 2.10k | std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb")); |
677 | | |
678 | | // Check whether we can read from the file |
679 | 2.10k | if (file == nullptr) { |
680 | 0 | throw DeadlyImportError("Failed to open SMD/VTA file ", pFile, "."); |
681 | 0 | } |
682 | | |
683 | 2.10k | iFileSize = (unsigned int)file->FileSize(); |
684 | | |
685 | | // Allocate storage and copy the contents of the file to a memory buffer |
686 | 2.10k | mBuffer.resize(iFileSize + 1); |
687 | 2.10k | TextFileToBuffer(file.get(), mBuffer); |
688 | 2.10k | mEnd = &mBuffer[mBuffer.size() - 1] + 1; |
689 | | |
690 | 2.10k | iSmallestFrame = INT_MAX; |
691 | 2.10k | bHasUVs = true; |
692 | 2.10k | iLineNumber = 1; |
693 | | |
694 | | // Reserve enough space for ... hm ... 10 textures |
695 | 2.10k | aszTextures.reserve(10); |
696 | | |
697 | | // Reserve enough space for ... hm ... 1000 triangles |
698 | 2.10k | asTriangles.reserve(1000); |
699 | | |
700 | | // Reserve enough space for ... hm ... 20 bones |
701 | 2.10k | asBones.reserve(20); |
702 | | |
703 | 2.10k | aszTextures.clear(); |
704 | 2.10k | asTriangles.clear(); |
705 | 2.10k | asBones.clear(); |
706 | | |
707 | | // parse the file ... |
708 | 2.10k | ParseFile(); |
709 | 2.10k | } |
710 | | |
711 | | // ------------------------------------------------------------------------------------------------ |
712 | 2.10k | unsigned int SMDImporter::GetTextureIndex(const std::string& filename) { |
713 | 2.10k | unsigned int iIndex = 0; |
714 | 2.10k | for (std::vector<std::string>::const_iterator |
715 | 2.10k | i = aszTextures.begin(); |
716 | 2.10k | i != aszTextures.end();++i,++iIndex) { |
717 | | // case-insensitive ... it's a path |
718 | 0 | if (0 == ASSIMP_stricmp ( filename.c_str(),(*i).c_str())) { |
719 | 0 | return iIndex; |
720 | 0 | } |
721 | 0 | } |
722 | 2.10k | iIndex = (unsigned int)aszTextures.size(); |
723 | 2.10k | aszTextures.push_back(filename); |
724 | 2.10k | return iIndex; |
725 | 2.10k | } |
726 | | |
727 | | // ------------------------------------------------------------------------------------------------ |
728 | | // Parse the nodes section of the file |
729 | 4.42k | void SMDImporter::ParseNodesSection(const char* szCurrent, const char** szCurrentOut, const char *end) { |
730 | 13.2k | for ( ;; ) { |
731 | | // "end\n" - Ends the nodes section |
732 | 13.2k | if (0 == ASSIMP_strincmp(szCurrent, "end", 3) && IsSpaceOrNewLine(*(szCurrent+3))) { |
733 | 4.42k | szCurrent += 4; |
734 | 4.42k | break; |
735 | 4.42k | } |
736 | 8.84k | ParseNodeInfo(szCurrent,&szCurrent, end); |
737 | 8.84k | } |
738 | 4.42k | SkipSpacesAndLineEnd(szCurrent, &szCurrent, end); |
739 | 4.42k | *szCurrentOut = szCurrent; |
740 | 4.42k | } |
741 | | |
742 | | // ------------------------------------------------------------------------------------------------ |
743 | | // Parse the triangles section of the file |
744 | 2.10k | void SMDImporter::ParseTrianglesSection(const char *szCurrent, const char **szCurrentOut, const char *end) { |
745 | | // Parse a triangle, parse another triangle, parse the next triangle ... |
746 | | // and so on until we reach a token that looks quite similar to "end" |
747 | 4.20k | for ( ;; ) { |
748 | 4.20k | if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent, end)) { |
749 | 2.10k | break; |
750 | 2.10k | } |
751 | | |
752 | | // "end\n" - Ends the triangles section |
753 | 2.10k | if (TokenMatch(szCurrent,"end",3)) { |
754 | 0 | break; |
755 | 0 | } |
756 | 2.10k | ParseTriangle(szCurrent,&szCurrent, end); |
757 | 2.10k | } |
758 | 2.10k | SkipSpacesAndLineEnd(szCurrent,&szCurrent, end); |
759 | 2.10k | *szCurrentOut = szCurrent; |
760 | 2.10k | } |
761 | | // ------------------------------------------------------------------------------------------------ |
762 | | // Parse the vertex animation section of the file |
763 | 0 | void SMDImporter::ParseVASection(const char *szCurrent, const char **szCurrentOut, const char *end) { |
764 | 0 | unsigned int iCurIndex = 0; |
765 | 0 | for ( ;; ) { |
766 | 0 | if (!SkipSpacesAndLineEnd(szCurrent,&szCurrent, end)) { |
767 | 0 | break; |
768 | 0 | } |
769 | | |
770 | | // "end\n" - Ends the "vertexanimation" section |
771 | 0 | if (TokenMatch(szCurrent,"end",3)) { |
772 | 0 | break; |
773 | 0 | } |
774 | | |
775 | | // "time <n>\n" |
776 | 0 | if (TokenMatch(szCurrent,"time",4)) { |
777 | | // NOTE: The doc says that time values COULD be negative ... |
778 | | // NOTE2: this is the shape key -> valve docs |
779 | 0 | int iTime = 0; |
780 | 0 | if (!ParseSignedInt(szCurrent, &szCurrent, end, iTime) || configFrameID != (unsigned int)iTime) { |
781 | 0 | break; |
782 | 0 | } |
783 | 0 | SkipLine(szCurrent,&szCurrent, end); |
784 | 0 | } else { |
785 | 0 | if(0 == iCurIndex) { |
786 | 0 | asTriangles.emplace_back(); |
787 | 0 | } |
788 | 0 | if (++iCurIndex == 3) { |
789 | 0 | iCurIndex = 0; |
790 | 0 | } |
791 | 0 | ParseVertex(szCurrent,&szCurrent, end, asTriangles.back().avVertices[iCurIndex],true); |
792 | 0 | } |
793 | 0 | } |
794 | |
|
795 | 0 | if (iCurIndex != 2 && !asTriangles.empty()) { |
796 | | // we want to no degenerates, so throw this triangle away |
797 | 0 | asTriangles.pop_back(); |
798 | 0 | } |
799 | |
|
800 | 0 | SkipSpacesAndLineEnd(szCurrent,&szCurrent, end); |
801 | 0 | *szCurrentOut = szCurrent; |
802 | 0 | } |
803 | | |
804 | | // ------------------------------------------------------------------------------------------------ |
805 | | // Parse the skeleton section of the file |
806 | 0 | void SMDImporter::ParseSkeletonSection(const char *szCurrent, const char **szCurrentOut, const char *end) { |
807 | 0 | int iTime = 0; |
808 | 0 | for ( ;; ) { |
809 | 0 | if (!SkipSpacesAndLineEnd(szCurrent,&szCurrent, end)) { |
810 | 0 | break; |
811 | 0 | } |
812 | | |
813 | | // "end\n" - Ends the skeleton section |
814 | 0 | if (TokenMatch(szCurrent,"end",3)) { |
815 | 0 | break; |
816 | 0 | } else if (TokenMatch(szCurrent,"time",4)) { |
817 | | // "time <n>\n" - Specifies the current animation frame |
818 | 0 | if (!ParseSignedInt(szCurrent, &szCurrent, end, iTime)) { |
819 | 0 | break; |
820 | 0 | } |
821 | | |
822 | 0 | iSmallestFrame = std::min(iSmallestFrame,iTime); |
823 | 0 | SkipLine(szCurrent, &szCurrent, end); |
824 | 0 | } else { |
825 | 0 | ParseSkeletonElement(szCurrent, &szCurrent, end, iTime); |
826 | 0 | } |
827 | 0 | } |
828 | 0 | *szCurrentOut = szCurrent; |
829 | 0 | } |
830 | | |
831 | | // ------------------------------------------------------------------------------------------------ |
832 | 864 | #define SMDI_PARSE_RETURN { \ |
833 | 864 | SkipLine(szCurrent,&szCurrent, end); \ |
834 | 864 | *szCurrentOut = szCurrent; \ |
835 | 864 | return; \ |
836 | 15.1k | } |
837 | | // ------------------------------------------------------------------------------------------------ |
838 | | // Parse a node line |
839 | 8.84k | void SMDImporter::ParseNodeInfo(const char *szCurrent, const char **szCurrentOut, const char *end) { |
840 | 8.84k | unsigned int iBone = 0; |
841 | 8.84k | SkipSpacesAndLineEnd(szCurrent, &szCurrent, end); |
842 | 8.84k | if ( !ParseUnsignedInt(szCurrent, &szCurrent, end, iBone) || !SkipSpaces(szCurrent,&szCurrent, end)) { |
843 | 0 | throw DeadlyImportError("Unexpected EOF/EOL while parsing bone index"); |
844 | 0 | } |
845 | 8.84k | if (iBone == UINT_MAX) { |
846 | 0 | LogErrorNoThrow("Invalid bone number while parsing bone index"); |
847 | 0 | SMDI_PARSE_RETURN; |
848 | 0 | } |
849 | | // add our bone to the list |
850 | 8.84k | if (iBone >= asBones.size()) { |
851 | 2.31k | asBones.resize(iBone+1); |
852 | 2.31k | } |
853 | 8.84k | SMD::Bone& bone = asBones[iBone]; |
854 | | |
855 | 8.84k | bool bQuota = true; |
856 | 8.84k | if ('\"' != *szCurrent) { |
857 | 8.84k | LogWarning("Bone name is expected to be enclosed in " |
858 | 8.84k | "double quotation marks. "); |
859 | 8.84k | bQuota = false; |
860 | 8.84k | } else { |
861 | 0 | ++szCurrent; |
862 | 0 | } |
863 | | |
864 | 8.84k | const char* szEnd = szCurrent; |
865 | 33.7k | for ( ;; ) { |
866 | 33.7k | if (bQuota && '\"' == *szEnd) { |
867 | 0 | iBone = (unsigned int)(szEnd - szCurrent); |
868 | 0 | ++szEnd; |
869 | 0 | break; |
870 | 33.7k | } else if (!bQuota && IsSpaceOrNewLine(*szEnd)) { |
871 | 8.84k | iBone = (unsigned int)(szEnd - szCurrent); |
872 | 8.84k | break; |
873 | 24.9k | } else if (!(*szEnd)) { |
874 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone name"); |
875 | 0 | SMDI_PARSE_RETURN; |
876 | 0 | } |
877 | 24.9k | ++szEnd; |
878 | 24.9k | } |
879 | 8.84k | bone.mName = std::string(szCurrent,iBone); |
880 | 8.84k | szCurrent = szEnd; |
881 | | |
882 | | // the only negative bone parent index that could occur is -1 AFAIK |
883 | 8.84k | if(!ParseSignedInt(szCurrent, &szCurrent, end, (int&)bone.iParent)) { |
884 | 8.19k | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone parent index. Assuming -1"); |
885 | 8.19k | SMDI_PARSE_RETURN; |
886 | 0 | } |
887 | | |
888 | | // go to the beginning of the next line |
889 | 648 | SMDI_PARSE_RETURN; |
890 | 0 | } |
891 | | |
892 | | // ------------------------------------------------------------------------------------------------ |
893 | | // Parse a skeleton element |
894 | 0 | void SMDImporter::ParseSkeletonElement(const char *szCurrent, const char **szCurrentOut, const char *end, int iTime) { |
895 | 0 | aiVector3D vPos; |
896 | 0 | aiVector3D vRot; |
897 | |
|
898 | 0 | unsigned int iBone = 0; |
899 | 0 | if (!ParseUnsignedInt(szCurrent, &szCurrent, end, iBone)) { |
900 | 0 | ASSIMP_LOG_ERROR("Unexpected EOF/EOL while parsing bone index"); |
901 | 0 | SMDI_PARSE_RETURN; |
902 | 0 | } |
903 | 0 | if (iBone >= asBones.size()) { |
904 | 0 | LogErrorNoThrow("Bone index in skeleton section is out of range"); |
905 | 0 | SMDI_PARSE_RETURN; |
906 | 0 | } |
907 | 0 | SMD::Bone& bone = asBones[iBone]; |
908 | |
|
909 | 0 | bone.sAnim.asKeys.emplace_back(); |
910 | 0 | SMD::Bone::Animation::MatrixKey& key = bone.sAnim.asKeys.back(); |
911 | |
|
912 | 0 | key.dTime = (double)iTime; |
913 | 0 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vPos.x)) { |
914 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.x"); |
915 | 0 | SMDI_PARSE_RETURN; |
916 | 0 | } |
917 | 0 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vPos.y)) { |
918 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.y"); |
919 | 0 | SMDI_PARSE_RETURN; |
920 | 0 | } |
921 | 0 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vPos.z)) { |
922 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.z"); |
923 | 0 | SMDI_PARSE_RETURN; |
924 | 0 | } |
925 | 0 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vRot.x)) { |
926 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.x"); |
927 | 0 | SMDI_PARSE_RETURN; |
928 | 0 | } |
929 | 0 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vRot.y)) { |
930 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.y"); |
931 | 0 | SMDI_PARSE_RETURN; |
932 | 0 | } |
933 | 0 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vRot.z)) { |
934 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.z"); |
935 | 0 | SMDI_PARSE_RETURN; |
936 | 0 | } |
937 | | // build the transformation matrix of the key |
938 | 0 | key.matrix.FromEulerAnglesXYZ(vRot.x,vRot.y,vRot.z); { |
939 | 0 | aiMatrix4x4 mTemp; |
940 | 0 | mTemp.a4 = vPos.x; |
941 | 0 | mTemp.b4 = vPos.y; |
942 | 0 | mTemp.c4 = vPos.z; |
943 | 0 | key.matrix = mTemp * key.matrix; |
944 | 0 | } |
945 | 0 | key.vPos = vPos; |
946 | 0 | key.vRot = vRot; |
947 | | // go to the beginning of the next line |
948 | 0 | SMDI_PARSE_RETURN; |
949 | 0 | } |
950 | | |
951 | | // ------------------------------------------------------------------------------------------------ |
952 | | // Parse a triangle |
953 | 2.10k | void SMDImporter::ParseTriangle(const char *szCurrent, const char **szCurrentOut, const char *end) { |
954 | 2.10k | asTriangles.emplace_back(); |
955 | 2.10k | SMD::Face& face = asTriangles.back(); |
956 | | |
957 | 2.10k | if(!SkipSpaces(szCurrent, &szCurrent, end)) { |
958 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing a triangle"); |
959 | 0 | return; |
960 | 0 | } |
961 | | |
962 | | // read the texture file name |
963 | 2.10k | const char* szLast = szCurrent; |
964 | 104k | while (!IsSpaceOrNewLine(*++szCurrent)); |
965 | | |
966 | | // ... and get the index that belongs to this file name |
967 | 2.10k | face.iTexture = GetTextureIndex(std::string(szLast,(uintptr_t)szCurrent-(uintptr_t)szLast)); |
968 | | |
969 | 2.10k | SkipSpacesAndLineEnd(szCurrent, &szCurrent, end); |
970 | | |
971 | | // load three vertices |
972 | 6.30k | for (auto &avVertex : face.avVertices) { |
973 | 6.30k | ParseVertex(szCurrent, &szCurrent, end, avVertex); |
974 | 6.30k | } |
975 | 2.10k | *szCurrentOut = szCurrent; |
976 | 2.10k | } |
977 | | |
978 | | // ------------------------------------------------------------------------------------------------ |
979 | | // Parse a float |
980 | 12.8k | bool SMDImporter::ParseFloat(const char *szCurrent, const char **szCurrentOut, const char *end, float &out) { |
981 | 12.8k | if (!SkipSpaces(&szCurrent, end)) { |
982 | 2.31k | return false; |
983 | 2.31k | } |
984 | | |
985 | 10.5k | *szCurrentOut = fast_atoreal_move(szCurrent,out); |
986 | 10.5k | return true; |
987 | 12.8k | } |
988 | | |
989 | | // ------------------------------------------------------------------------------------------------ |
990 | | // Parse an unsigned int |
991 | 9.06k | bool SMDImporter::ParseUnsignedInt(const char *szCurrent, const char **szCurrentOut, const char *end, unsigned int &out) { |
992 | 9.06k | if(!SkipSpaces(&szCurrent, end)) { |
993 | 0 | return false; |
994 | 0 | } |
995 | | |
996 | 9.06k | out = strtoul10(szCurrent,szCurrentOut); |
997 | 9.06k | return true; |
998 | 9.06k | } |
999 | | |
1000 | | // ------------------------------------------------------------------------------------------------ |
1001 | | // Parse a signed int |
1002 | 15.1k | bool SMDImporter::ParseSignedInt(const char *szCurrent, const char **szCurrentOut, const char *end, int &out) { |
1003 | 15.1k | if(!SkipSpaces(&szCurrent, end)) { |
1004 | 11.9k | return false; |
1005 | 11.9k | } |
1006 | | |
1007 | 3.18k | out = strtol10(szCurrent,szCurrentOut); |
1008 | 3.18k | return true; |
1009 | 15.1k | } |
1010 | | |
1011 | | // ------------------------------------------------------------------------------------------------ |
1012 | | // Parse a vertex |
1013 | | void SMDImporter::ParseVertex(const char* szCurrent, |
1014 | | const char **szCurrentOut, const char *end, SMD::Vertex &vertex, |
1015 | 6.30k | bool bVASection /*= false*/) { |
1016 | 6.30k | if (SkipSpaces(&szCurrent, end) && IsLineEnd(*szCurrent)) { |
1017 | 0 | SkipSpacesAndLineEnd(szCurrent,&szCurrent, end); |
1018 | 0 | return ParseVertex(szCurrent, szCurrentOut, end, vertex, bVASection); |
1019 | 0 | } |
1020 | 6.30k | if(!ParseSignedInt(szCurrent, &szCurrent, end, (int&)vertex.iParentNode)) { |
1021 | 3.77k | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.parent"); |
1022 | 3.77k | SMDI_PARSE_RETURN; |
1023 | 0 | } |
1024 | 2.53k | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vertex.pos.x)) { |
1025 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.x"); |
1026 | 0 | SMDI_PARSE_RETURN; |
1027 | 0 | } |
1028 | 2.53k | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vertex.pos.y)) { |
1029 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.y"); |
1030 | 0 | SMDI_PARSE_RETURN; |
1031 | 0 | } |
1032 | 2.53k | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vertex.pos.z)) { |
1033 | 216 | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.z"); |
1034 | 216 | SMDI_PARSE_RETURN; |
1035 | 0 | } |
1036 | 2.31k | if(!ParseFloat(szCurrent,&szCurrent,end, (float&)vertex.nor.x)) { |
1037 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.x"); |
1038 | 0 | SMDI_PARSE_RETURN; |
1039 | 0 | } |
1040 | 2.31k | if(!ParseFloat(szCurrent,&szCurrent, end, (float&)vertex.nor.y)) { |
1041 | 2.10k | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.y"); |
1042 | 2.10k | SMDI_PARSE_RETURN; |
1043 | 0 | } |
1044 | 216 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vertex.nor.z)) { |
1045 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.z"); |
1046 | 0 | SMDI_PARSE_RETURN; |
1047 | 0 | } |
1048 | | |
1049 | 216 | if (bVASection) { |
1050 | 0 | SMDI_PARSE_RETURN; |
1051 | 0 | } |
1052 | | |
1053 | 216 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vertex.uv.x)) { |
1054 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.uv.x"); |
1055 | 0 | SMDI_PARSE_RETURN; |
1056 | 0 | } |
1057 | 216 | if(!ParseFloat(szCurrent, &szCurrent, end, (float&)vertex.uv.y)) { |
1058 | 0 | LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.uv.y"); |
1059 | 0 | SMDI_PARSE_RETURN; |
1060 | 0 | } |
1061 | | |
1062 | | // now read the number of bones affecting this vertex |
1063 | | // all elements from now are fully optional, we don't need them |
1064 | 216 | unsigned int iSize = 0; |
1065 | 216 | if(!ParseUnsignedInt(szCurrent, &szCurrent, end, iSize)) { |
1066 | 0 | SMDI_PARSE_RETURN; |
1067 | 0 | } |
1068 | 216 | vertex.aiBoneLinks.resize(iSize,std::pair<unsigned int, float>(0,0.0f)); |
1069 | | |
1070 | 216 | for (auto &aiBoneLink : vertex.aiBoneLinks) { |
1071 | 0 | if(!ParseUnsignedInt(szCurrent, &szCurrent, end, aiBoneLink.first)) { |
1072 | 0 | SMDI_PARSE_RETURN; |
1073 | 0 | } |
1074 | 0 | if(!ParseFloat(szCurrent, &szCurrent, end, aiBoneLink.second)) { |
1075 | 0 | SMDI_PARSE_RETURN; |
1076 | 0 | } |
1077 | 0 | } |
1078 | | |
1079 | | // go to the beginning of the next line |
1080 | 216 | SMDI_PARSE_RETURN; |
1081 | 0 | } |
1082 | | |
1083 | | } // namespace Assimp |
1084 | | |
1085 | | #endif // !! ASSIMP_BUILD_NO_SMD_IMPORTER |