SMDLoader.cpp 35 KB

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