SMDLoader.cpp 37 KB

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