SMDLoader.cpp 39 KB

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