SMDLoader.cpp 37 KB

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