MS3DLoader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 MS3DLoader.cpp
  35. * @brief Implementation of the Ms3D importer class.
  36. * Written against http://chumbalum.swissquake.ch/ms3d/ms3dspec.txt
  37. */
  38. #include "AssimpPCH.h"
  39. #ifndef ASSIMP_BUILD_NO_MS3D_IMPORTER
  40. // internal headers
  41. #include "MS3DLoader.h"
  42. #include "StreamReader.h"
  43. using namespace Assimp;
  44. // ------------------------------------------------------------------------------------------------
  45. // Constructor to be privately used by Importer
  46. MS3DImporter::MS3DImporter()
  47. {}
  48. // ------------------------------------------------------------------------------------------------
  49. // Destructor, private as well
  50. MS3DImporter::~MS3DImporter()
  51. {}
  52. // ------------------------------------------------------------------------------------------------
  53. // Returns whether the class can handle the format of the given file.
  54. bool MS3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  55. {
  56. // first call - simple extension check
  57. const std::string extension = GetExtension(pFile);
  58. if (extension == "ms3d") {
  59. return true;
  60. }
  61. // second call - check for magic identifiers
  62. else if (!extension.length() || checkSig) {
  63. if (!pIOHandler) {
  64. return true;
  65. }
  66. const char* tokens[] = {"MS3D000000"};
  67. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  68. }
  69. return false;
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. void MS3DImporter::GetExtensionList(std::set<std::string>& extensions)
  73. {
  74. extensions.insert("ms3d");
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. void ReadColor(StreamReaderLE& stream, aiColor4D& ambient)
  78. {
  79. // aiColor4D is packed on gcc, implicit binding to float& fails therefore.
  80. // But I guess casting is fine (it could cause alignment faults on some
  81. // architectures in general, but we're not touched because aiColor4D
  82. // should be properly aligned & packed due to its uniform structure)
  83. stream >> (float&)ambient.r >> (float&)ambient.g >> (float&)ambient.b >> (float&)ambient.a;
  84. }
  85. // ------------------------------------------------------------------------------------------------
  86. void ReadVector(StreamReaderLE& stream, aiVector3D& pos)
  87. {
  88. // See note in ReadColor()
  89. stream >> (float&)pos.x >> (float&)pos.y >> (float&)pos.z;
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. // Imports the given file into the given scene structure.
  93. void MS3DImporter::InternReadFile( const std::string& pFile,
  94. aiScene* pScene, IOSystem* pIOHandler)
  95. {
  96. StreamReaderLE stream(pIOHandler->Open(pFile,"rb"));
  97. // CanRead() should have done this already
  98. char head[10];
  99. int32_t version;
  100. // 1 ------------ read into temporary data structures mirroring the original file
  101. stream.CopyAndAdvance(head,10);
  102. stream >> version;
  103. if (strncmp(head,"MS3D000000",10)) {
  104. throw new ImportErrorException("Not a MS3D file, magic string MS3D000000 not found: "+pFile);
  105. }
  106. if (version != 4) {
  107. throw new ImportErrorException("MS3D: Unsupported file format version, 4 was expected");
  108. }
  109. uint16_t verts;
  110. stream >> verts;
  111. std::vector<TempVertex> vertices(verts);
  112. for (unsigned int i = 0; i < verts; ++i) {
  113. TempVertex& v = vertices[i];
  114. stream.IncPtr(1);
  115. ReadVector(stream,v.pos);
  116. v.bone_id = static_cast<unsigned int>(stream.GetI1()); // signed in original spec !! intentional bit hack.
  117. v.ref_cnt = static_cast<unsigned int>(stream.GetI1());
  118. }
  119. uint16_t tris;
  120. stream >> tris;
  121. std::vector<TempTriangle> triangles(tris);
  122. for (unsigned int i = 0;i < tris; ++i) {
  123. TempTriangle& t = triangles[i];
  124. stream.IncPtr(2);
  125. for (unsigned int i = 0; i < 3; ++i) {
  126. t.indices[i] = static_cast<unsigned int>(stream.GetI2());
  127. }
  128. for (unsigned int i = 0; i < 3; ++i) {
  129. ReadVector(stream,t.normals[i]);
  130. }
  131. for (unsigned int i = 0; i < 3; ++i) {
  132. stream >> (float&)(t.uv[i].x); // see note in ReadColor()
  133. }
  134. for (unsigned int i = 0; i < 3; ++i) {
  135. stream >> (float&)(t.uv[i].y);
  136. }
  137. t.sg = static_cast<unsigned int>(stream.GetI1());
  138. t.group = static_cast<unsigned int>(stream.GetI1());
  139. }
  140. uint16_t grp;
  141. stream >> grp;
  142. bool need_default = false;
  143. std::vector<TempGroup> groups(grp);
  144. for (unsigned int i = 0;i < grp; ++i) {
  145. TempGroup& t = groups[i];
  146. stream.IncPtr(1);
  147. stream.CopyAndAdvance(t.name,32);
  148. t.name[32] = '\0';
  149. uint16_t num;
  150. stream >> num;
  151. t.triangles.resize(num);
  152. for (unsigned int i = 0; i < num; ++i) {
  153. t.triangles[i] = static_cast<unsigned int>(stream.GetI2());
  154. }
  155. t.mat = static_cast<unsigned int>(stream.GetI1());
  156. if (t.mat == 0xff) {
  157. need_default = true;
  158. }
  159. }
  160. uint16_t mat;
  161. stream >> mat;
  162. std::vector<TempMaterial> materials(mat);
  163. for (unsigned int i = 0;i < mat; ++i) {
  164. TempMaterial& t = materials[i];
  165. stream.CopyAndAdvance(t.name,32);
  166. t.name[32] = '\0';
  167. ReadColor(stream,t.ambient);
  168. ReadColor(stream,t.diffuse);
  169. ReadColor(stream,t.specular);
  170. ReadColor(stream,t.emissive);
  171. stream >> t.shininess >> t.transparency;
  172. stream.IncPtr(1);
  173. stream.CopyAndAdvance(t.texture,128);
  174. t.texture[128] = '\0';
  175. stream.CopyAndAdvance(t.alphamap,128);
  176. t.alphamap[128] = '\0';
  177. }
  178. // 2 ------------ convert to proper aiXX data structures
  179. if (need_default && materials.size()) {
  180. DefaultLogger::get()->warn("MS3D: Found group with no material assigned, spawning default material");
  181. // if one of the groups has no material assigned, but there are other
  182. // groups with materials, a default material needs to be added (
  183. // scenepreprocessor adds a default material only if nummat==0).
  184. materials.push_back(TempMaterial());
  185. TempMaterial& m = materials.back();
  186. strcpy(m.name,"<MS3D_DefaultMat>");
  187. m.diffuse = aiColor4D(0.6f,0.6f,0.6f,1.0);
  188. m.transparency = 1.f;
  189. m.shininess = 0.f;
  190. for (unsigned int i = 0; i < groups.size(); ++i) {
  191. TempGroup& g = groups[i];
  192. if (g.mat == 0xff) {
  193. g.mat = materials.size()-1;
  194. }
  195. }
  196. }
  197. // convert materials to our generic key-value dict-alike
  198. if (materials.size()) {
  199. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials=static_cast<unsigned int>(materials.size())];
  200. for (unsigned int i = 0; i < pScene->mNumMaterials; ++i) {
  201. MaterialHelper* mo = new MaterialHelper();
  202. pScene->mMaterials[i] = mo;
  203. const TempMaterial& mi = materials[i];
  204. aiString tmp;
  205. if (0[mi.alphamap]) {
  206. tmp = aiString(mi.alphamap);
  207. mo->AddProperty(&tmp,AI_MATKEY_TEXTURE_OPACITY(0));
  208. }
  209. if (0[mi.texture]) {
  210. tmp = aiString(mi.texture);
  211. mo->AddProperty(&tmp,AI_MATKEY_TEXTURE_DIFFUSE(0));
  212. }
  213. if (0[mi.name]) {
  214. tmp = aiString(mi.name);
  215. mo->AddProperty(&tmp,AI_MATKEY_NAME);
  216. }
  217. mo->AddProperty(&mi.ambient,1,AI_MATKEY_COLOR_AMBIENT);
  218. mo->AddProperty(&mi.diffuse,1,AI_MATKEY_COLOR_DIFFUSE);
  219. mo->AddProperty(&mi.specular,1,AI_MATKEY_COLOR_SPECULAR);
  220. mo->AddProperty(&mi.emissive,1,AI_MATKEY_COLOR_EMISSIVE);
  221. mo->AddProperty(&mi.shininess,1,AI_MATKEY_SHININESS);
  222. mo->AddProperty(&mi.transparency,1,AI_MATKEY_OPACITY);
  223. const int sm = mi.shininess>0.f?aiShadingMode_Phong:aiShadingMode_Gouraud;
  224. mo->AddProperty(&sm,1,AI_MATKEY_SHADING_MODEL);
  225. }
  226. }
  227. // convert groups to meshes
  228. if (groups.empty()) {
  229. throw new ImportErrorException("MS3D: Didn't get any group records, file is malformed");
  230. }
  231. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes=static_cast<unsigned int>(groups.size())];
  232. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  233. aiMesh* m = pScene->mMeshes[i] = new aiMesh();
  234. const TempGroup& g = groups[i];
  235. if (pScene->mNumMaterials && g.mat > pScene->mNumMaterials) {
  236. throw new ImportErrorException("MS3D: Encountered invalid material index, file is malformed");
  237. } // no error if no materials at all - scenepreprocessor adds one then
  238. m->mMaterialIndex = g.mat;
  239. m->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  240. m->mFaces = new aiFace[m->mNumFaces = g.triangles.size()];
  241. m->mNumVertices = m->mNumFaces*3;
  242. // storage for vertices - verbose format, as requested by the postprocessing pipeline
  243. m->mVertices = new aiVector3D[m->mNumVertices];
  244. m->mNormals = new aiVector3D[m->mNumVertices];
  245. m->mTextureCoords[0] = new aiVector3D[m->mNumVertices];
  246. m->mNumUVComponents[0] = 2;
  247. for (unsigned int i = 0,n = 0; i < m->mNumFaces; ++i) {
  248. aiFace& f = m->mFaces[i];
  249. if (g.triangles[i]>triangles.size()) {
  250. throw new ImportErrorException("MS3D: Encountered invalid triangle index, file is malformed");
  251. }
  252. TempTriangle& t = triangles[g.triangles[i]];
  253. f.mIndices = new unsigned int[f.mNumIndices=3];
  254. for (unsigned int i = 0; i < 3; ++i,++n) {
  255. if (t.indices[i]>vertices.size()) {
  256. throw new ImportErrorException("MS3D: Encountered invalid vertex index, file is malformed");
  257. }
  258. // collect vertex components
  259. m->mVertices[n] = vertices[t.indices[i]].pos;
  260. m->mNormals[n] = t.normals[i];
  261. m->mTextureCoords[0][n] = aiVector3D(t.uv[i].x,t.uv[i].y,0.0);
  262. f.mIndices[i] = n;
  263. }
  264. }
  265. }
  266. // ... add dummy nodes under a single root, each holding a reference to one
  267. // mesh. If we didn't do this, we'd loose the group name.
  268. aiNode* rt = pScene->mRootNode = new aiNode("<MS3DRoot>");
  269. rt->mChildren = new aiNode*[rt->mNumChildren=pScene->mNumMeshes];
  270. for (unsigned int i = 0; i < rt->mNumChildren; ++i) {
  271. aiNode* nd = rt->mChildren[i] = new aiNode();
  272. const TempGroup& g = groups[i];
  273. nd->mName = aiString(g.name);
  274. nd->mParent = rt;
  275. nd->mMeshes = new unsigned int[nd->mNumMeshes = 1];
  276. nd->mMeshes[0] = i;
  277. }
  278. }
  279. #endif