SMDLoader.cpp 41 KB

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