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