Q3DLoader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, 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 Q3DLoader.cpp
  35. * @brief Implementation of the Q3D importer class
  36. */
  37. #ifndef ASSIMP_BUILD_NO_Q3D_IMPORTER
  38. // internal headers
  39. #include "Q3DLoader.h"
  40. #include <assimp/StreamReader.h>
  41. #include <assimp/fast_atof.h>
  42. #include <assimp/importerdesc.h>
  43. #include <assimp/scene.h>
  44. #include <assimp/DefaultLogger.hpp>
  45. #include <assimp/IOSystem.hpp>
  46. using namespace Assimp;
  47. static const aiImporterDesc desc = {
  48. "Quick3D Importer",
  49. "",
  50. "",
  51. "http://www.quick3d.com/",
  52. aiImporterFlags_SupportBinaryFlavour,
  53. 0,
  54. 0,
  55. 0,
  56. 0,
  57. "q3o q3s"
  58. };
  59. // ------------------------------------------------------------------------------------------------
  60. // Constructor to be privately used by Importer
  61. Q3DImporter::Q3DImporter() {
  62. // empty
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. // Destructor, private as well
  66. Q3DImporter::~Q3DImporter() {
  67. // empty
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. // Returns whether the class can handle the format of the given file.
  71. bool Q3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
  72. const std::string extension = GetExtension(pFile);
  73. if (extension == "q3s" || extension == "q3o")
  74. return true;
  75. else if (!extension.length() || checkSig) {
  76. if (!pIOHandler)
  77. return true;
  78. const char *tokens[] = { "quick3Do", "quick3Ds" };
  79. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 2);
  80. }
  81. return false;
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. const aiImporterDesc *Q3DImporter::GetInfo() const {
  85. return &desc;
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // Imports the given file into the given scene structure.
  89. void Q3DImporter::InternReadFile(const std::string &pFile,
  90. aiScene *pScene, IOSystem *pIOHandler) {
  91. StreamReaderLE stream(pIOHandler->Open(pFile, "rb"));
  92. // The header is 22 bytes large
  93. if (stream.GetRemainingSize() < 22)
  94. throw DeadlyImportError("File is either empty or corrupt: ", pFile);
  95. // Check the file's signature
  96. if (ASSIMP_strincmp((const char *)stream.GetPtr(), "quick3Do", 8) &&
  97. ASSIMP_strincmp((const char *)stream.GetPtr(), "quick3Ds", 8)) {
  98. throw DeadlyImportError("Not a Quick3D file. Signature string is: ", std::string((const char *)stream.GetPtr(), 8));
  99. }
  100. // Print the file format version
  101. ASSIMP_LOG_INFO_F("Quick3D File format version: ",
  102. std::string(&((const char *)stream.GetPtr())[8], 2));
  103. // ... an store it
  104. char major = ((const char *)stream.GetPtr())[8];
  105. char minor = ((const char *)stream.GetPtr())[9];
  106. stream.IncPtr(10);
  107. unsigned int numMeshes = (unsigned int)stream.GetI4();
  108. unsigned int numMats = (unsigned int)stream.GetI4();
  109. unsigned int numTextures = (unsigned int)stream.GetI4();
  110. std::vector<Material> materials;
  111. materials.reserve(numMats);
  112. std::vector<Mesh> meshes;
  113. meshes.reserve(numMeshes);
  114. // Allocate the scene root node
  115. pScene->mRootNode = new aiNode();
  116. aiColor3D fgColor(0.6f, 0.6f, 0.6f);
  117. // Now read all file chunks
  118. while (true) {
  119. if (stream.GetRemainingSize() < 1) break;
  120. char c = stream.GetI1();
  121. switch (c) {
  122. // Meshes chunk
  123. case 'm': {
  124. for (unsigned int quak = 0; quak < numMeshes; ++quak) {
  125. meshes.push_back(Mesh());
  126. Mesh &mesh = meshes.back();
  127. // read all vertices
  128. unsigned int numVerts = (unsigned int)stream.GetI4();
  129. if (!numVerts)
  130. throw DeadlyImportError("Quick3D: Found mesh with zero vertices");
  131. std::vector<aiVector3D> &verts = mesh.verts;
  132. verts.resize(numVerts);
  133. for (unsigned int i = 0; i < numVerts; ++i) {
  134. verts[i].x = stream.GetF4();
  135. verts[i].y = stream.GetF4();
  136. verts[i].z = stream.GetF4();
  137. }
  138. // read all faces
  139. numVerts = (unsigned int)stream.GetI4();
  140. if (!numVerts)
  141. throw DeadlyImportError("Quick3D: Found mesh with zero faces");
  142. std::vector<Face> &faces = mesh.faces;
  143. faces.reserve(numVerts);
  144. // number of indices
  145. for (unsigned int i = 0; i < numVerts; ++i) {
  146. faces.push_back(Face(stream.GetI2()));
  147. if (faces.back().indices.empty())
  148. throw DeadlyImportError("Quick3D: Found face with zero indices");
  149. }
  150. // indices
  151. for (unsigned int i = 0; i < numVerts; ++i) {
  152. Face &vec = faces[i];
  153. for (unsigned int a = 0; a < (unsigned int)vec.indices.size(); ++a)
  154. vec.indices[a] = stream.GetI4();
  155. }
  156. // material indices
  157. for (unsigned int i = 0; i < numVerts; ++i) {
  158. faces[i].mat = (unsigned int)stream.GetI4();
  159. }
  160. // read all normals
  161. numVerts = (unsigned int)stream.GetI4();
  162. std::vector<aiVector3D> &normals = mesh.normals;
  163. normals.resize(numVerts);
  164. for (unsigned int i = 0; i < numVerts; ++i) {
  165. normals[i].x = stream.GetF4();
  166. normals[i].y = stream.GetF4();
  167. normals[i].z = stream.GetF4();
  168. }
  169. numVerts = (unsigned int)stream.GetI4();
  170. if (numTextures && numVerts) {
  171. // read all texture coordinates
  172. std::vector<aiVector3D> &uv = mesh.uv;
  173. uv.resize(numVerts);
  174. for (unsigned int i = 0; i < numVerts; ++i) {
  175. uv[i].x = stream.GetF4();
  176. uv[i].y = stream.GetF4();
  177. }
  178. // UV indices
  179. for (unsigned int i = 0; i < (unsigned int)faces.size(); ++i) {
  180. Face &vec = faces[i];
  181. for (unsigned int a = 0; a < (unsigned int)vec.indices.size(); ++a) {
  182. vec.uvindices[a] = stream.GetI4();
  183. if (!i && !a)
  184. mesh.prevUVIdx = vec.uvindices[a];
  185. else if (vec.uvindices[a] != mesh.prevUVIdx)
  186. mesh.prevUVIdx = UINT_MAX;
  187. }
  188. }
  189. }
  190. // we don't need the rest, but we need to get to the next chunk
  191. stream.IncPtr(36);
  192. if (minor > '0' && major == '3')
  193. stream.IncPtr(mesh.faces.size());
  194. }
  195. // stream.IncPtr(4); // unknown value here
  196. } break;
  197. // materials chunk
  198. case 'c':
  199. for (unsigned int i = 0; i < numMats; ++i) {
  200. materials.push_back(Material());
  201. Material &mat = materials.back();
  202. // read the material name
  203. c = stream.GetI1();
  204. while (c) {
  205. mat.name.data[mat.name.length++] = c;
  206. c = stream.GetI1();
  207. }
  208. // add the terminal character
  209. mat.name.data[mat.name.length] = '\0';
  210. // read the ambient color
  211. mat.ambient.r = stream.GetF4();
  212. mat.ambient.g = stream.GetF4();
  213. mat.ambient.b = stream.GetF4();
  214. // read the diffuse color
  215. mat.diffuse.r = stream.GetF4();
  216. mat.diffuse.g = stream.GetF4();
  217. mat.diffuse.b = stream.GetF4();
  218. // read the ambient color
  219. mat.specular.r = stream.GetF4();
  220. mat.specular.g = stream.GetF4();
  221. mat.specular.b = stream.GetF4();
  222. // read the transparency
  223. mat.transparency = stream.GetF4();
  224. // unknown value here
  225. // stream.IncPtr(4);
  226. // FIX: it could be the texture index ...
  227. mat.texIdx = (unsigned int)stream.GetI4();
  228. }
  229. break;
  230. // texture chunk
  231. case 't':
  232. pScene->mNumTextures = numTextures;
  233. if (!numTextures) {
  234. break;
  235. }
  236. pScene->mTextures = new aiTexture *[pScene->mNumTextures];
  237. // to make sure we won't crash if we leave through an exception
  238. ::memset(pScene->mTextures, 0, sizeof(void *) * pScene->mNumTextures);
  239. for (unsigned int i = 0; i < pScene->mNumTextures; ++i) {
  240. aiTexture *tex = pScene->mTextures[i] = new aiTexture;
  241. // skip the texture name
  242. while (stream.GetI1())
  243. ;
  244. // read texture width and height
  245. tex->mWidth = (unsigned int)stream.GetI4();
  246. tex->mHeight = (unsigned int)stream.GetI4();
  247. if (!tex->mWidth || !tex->mHeight) {
  248. throw DeadlyImportError("Quick3D: Invalid texture. Width or height is zero");
  249. }
  250. unsigned int mul = tex->mWidth * tex->mHeight;
  251. aiTexel *begin = tex->pcData = new aiTexel[mul];
  252. aiTexel *const end = &begin[mul - 1] + 1;
  253. for (; begin != end; ++begin) {
  254. begin->r = stream.GetI1();
  255. begin->g = stream.GetI1();
  256. begin->b = stream.GetI1();
  257. begin->a = 0xff;
  258. }
  259. }
  260. break;
  261. // scene chunk
  262. case 's': {
  263. // skip position and rotation
  264. stream.IncPtr(12);
  265. for (unsigned int i = 0; i < 4; ++i)
  266. for (unsigned int a = 0; a < 4; ++a)
  267. pScene->mRootNode->mTransformation[i][a] = stream.GetF4();
  268. stream.IncPtr(16);
  269. // now setup a single camera
  270. pScene->mNumCameras = 1;
  271. pScene->mCameras = new aiCamera *[1];
  272. aiCamera *cam = pScene->mCameras[0] = new aiCamera();
  273. cam->mPosition.x = stream.GetF4();
  274. cam->mPosition.y = stream.GetF4();
  275. cam->mPosition.z = stream.GetF4();
  276. cam->mName.Set("Q3DCamera");
  277. // skip eye rotation for the moment
  278. stream.IncPtr(12);
  279. // read the default material color
  280. fgColor.r = stream.GetF4();
  281. fgColor.g = stream.GetF4();
  282. fgColor.b = stream.GetF4();
  283. // skip some unimportant properties
  284. stream.IncPtr(29);
  285. // setup a single point light with no attenuation
  286. pScene->mNumLights = 1;
  287. pScene->mLights = new aiLight *[1];
  288. aiLight *light = pScene->mLights[0] = new aiLight();
  289. light->mName.Set("Q3DLight");
  290. light->mType = aiLightSource_POINT;
  291. light->mAttenuationConstant = 1;
  292. light->mAttenuationLinear = 0;
  293. light->mAttenuationQuadratic = 0;
  294. light->mColorDiffuse.r = stream.GetF4();
  295. light->mColorDiffuse.g = stream.GetF4();
  296. light->mColorDiffuse.b = stream.GetF4();
  297. light->mColorSpecular = light->mColorDiffuse;
  298. // We don't need the rest, but we need to know where this chunk ends.
  299. unsigned int temp = (unsigned int)(stream.GetI4() * stream.GetI4());
  300. // skip the background file name
  301. while (stream.GetI1())
  302. ;
  303. // skip background texture data + the remaining fields
  304. stream.IncPtr(temp * 3 + 20); // 4 bytes of unknown data here
  305. // TODO
  306. goto outer;
  307. } break;
  308. default:
  309. throw DeadlyImportError("Quick3D: Unknown chunk");
  310. break;
  311. };
  312. }
  313. outer:
  314. // If we have no mesh loaded - break here
  315. if (meshes.empty())
  316. throw DeadlyImportError("Quick3D: No meshes loaded");
  317. // If we have no materials loaded - generate a default mat
  318. if (materials.empty()) {
  319. ASSIMP_LOG_INFO("Quick3D: No material found, generating one");
  320. materials.push_back(Material());
  321. materials.back().diffuse = fgColor;
  322. }
  323. // find out which materials we'll need
  324. typedef std::pair<unsigned int, unsigned int> FaceIdx;
  325. typedef std::vector<FaceIdx> FaceIdxArray;
  326. FaceIdxArray *fidx = new FaceIdxArray[materials.size()];
  327. unsigned int p = 0;
  328. for (std::vector<Mesh>::iterator it = meshes.begin(), end = meshes.end();
  329. it != end; ++it, ++p) {
  330. unsigned int q = 0;
  331. for (std::vector<Face>::iterator fit = (*it).faces.begin(), fend = (*it).faces.end();
  332. fit != fend; ++fit, ++q) {
  333. if ((*fit).mat >= materials.size()) {
  334. ASSIMP_LOG_WARN("Quick3D: Material index overflow");
  335. (*fit).mat = 0;
  336. }
  337. if (fidx[(*fit).mat].empty()) ++pScene->mNumMeshes;
  338. fidx[(*fit).mat].push_back(FaceIdx(p, q));
  339. }
  340. }
  341. pScene->mNumMaterials = pScene->mNumMeshes;
  342. pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
  343. pScene->mMeshes = new aiMesh *[pScene->mNumMaterials];
  344. for (unsigned int i = 0, real = 0; i < (unsigned int)materials.size(); ++i) {
  345. if (fidx[i].empty()) continue;
  346. // Allocate a mesh and a material
  347. aiMesh *mesh = pScene->mMeshes[real] = new aiMesh();
  348. aiMaterial *mat = new aiMaterial();
  349. pScene->mMaterials[real] = mat;
  350. mesh->mMaterialIndex = real;
  351. // Build the output material
  352. Material &srcMat = materials[i];
  353. mat->AddProperty(&srcMat.diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  354. mat->AddProperty(&srcMat.specular, 1, AI_MATKEY_COLOR_SPECULAR);
  355. mat->AddProperty(&srcMat.ambient, 1, AI_MATKEY_COLOR_AMBIENT);
  356. // NOTE: Ignore transparency for the moment - it seems
  357. // unclear how to interpret the data
  358. #if 0
  359. if (!(minor > '0' && major == '3'))
  360. srcMat.transparency = 1.0f - srcMat.transparency;
  361. mat->AddProperty(&srcMat.transparency, 1, AI_MATKEY_OPACITY);
  362. #endif
  363. // add shininess - Quick3D seems to use it ins its viewer
  364. srcMat.transparency = 16.f;
  365. mat->AddProperty(&srcMat.transparency, 1, AI_MATKEY_SHININESS);
  366. int m = (int)aiShadingMode_Phong;
  367. mat->AddProperty(&m, 1, AI_MATKEY_SHADING_MODEL);
  368. if (srcMat.name.length)
  369. mat->AddProperty(&srcMat.name, AI_MATKEY_NAME);
  370. // Add a texture
  371. if (srcMat.texIdx < pScene->mNumTextures || real < pScene->mNumTextures) {
  372. srcMat.name.data[0] = '*';
  373. srcMat.name.length = ASSIMP_itoa10(&srcMat.name.data[1], 1000,
  374. (srcMat.texIdx < pScene->mNumTextures ? srcMat.texIdx : real));
  375. mat->AddProperty(&srcMat.name, AI_MATKEY_TEXTURE_DIFFUSE(0));
  376. }
  377. mesh->mNumFaces = (unsigned int)fidx[i].size();
  378. aiFace *faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  379. // Now build the output mesh. First find out how many
  380. // vertices we'll need
  381. for (FaceIdxArray::const_iterator it = fidx[i].begin(), end = fidx[i].end();
  382. it != end; ++it) {
  383. mesh->mNumVertices += (unsigned int)meshes[(*it).first].faces[(*it).second].indices.size();
  384. }
  385. aiVector3D *verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  386. aiVector3D *norms = mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  387. aiVector3D *uv = nullptr;
  388. if (real < pScene->mNumTextures) {
  389. uv = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  390. mesh->mNumUVComponents[0] = 2;
  391. }
  392. // Build the final array
  393. unsigned int cnt = 0;
  394. for (FaceIdxArray::const_iterator it = fidx[i].begin(), end = fidx[i].end();
  395. it != end; ++it, ++faces) {
  396. Mesh &curMesh = meshes[(*it).first];
  397. Face &face = curMesh.faces[(*it).second];
  398. faces->mNumIndices = (unsigned int)face.indices.size();
  399. faces->mIndices = new unsigned int[faces->mNumIndices];
  400. aiVector3D faceNormal;
  401. bool fnOK = false;
  402. for (unsigned int n = 0; n < faces->mNumIndices; ++n, ++cnt, ++norms, ++verts) {
  403. if (face.indices[n] >= curMesh.verts.size()) {
  404. ASSIMP_LOG_WARN("Quick3D: Vertex index overflow");
  405. face.indices[n] = 0;
  406. }
  407. // copy vertices
  408. *verts = curMesh.verts[face.indices[n]];
  409. if (face.indices[n] >= curMesh.normals.size() && faces->mNumIndices >= 3) {
  410. // we have no normal here - assign the face normal
  411. if (!fnOK) {
  412. const aiVector3D &pV1 = curMesh.verts[face.indices[0]];
  413. const aiVector3D &pV2 = curMesh.verts[face.indices[1]];
  414. const aiVector3D &pV3 = curMesh.verts[face.indices.size() - 1];
  415. faceNormal = (pV2 - pV1) ^ (pV3 - pV1).Normalize();
  416. fnOK = true;
  417. }
  418. *norms = faceNormal;
  419. } else {
  420. *norms = curMesh.normals[face.indices[n]];
  421. }
  422. // copy texture coordinates
  423. if (uv && curMesh.uv.size()) {
  424. if (curMesh.prevUVIdx != 0xffffffff && curMesh.uv.size() >= curMesh.verts.size()) // workaround
  425. {
  426. *uv = curMesh.uv[face.indices[n]];
  427. } else {
  428. if (face.uvindices[n] >= curMesh.uv.size()) {
  429. ASSIMP_LOG_WARN("Quick3D: Texture coordinate index overflow");
  430. face.uvindices[n] = 0;
  431. }
  432. *uv = curMesh.uv[face.uvindices[n]];
  433. }
  434. uv->y = 1.f - uv->y;
  435. ++uv;
  436. }
  437. // setup the new vertex index
  438. faces->mIndices[n] = cnt;
  439. }
  440. }
  441. ++real;
  442. }
  443. // Delete our nice helper array
  444. delete[] fidx;
  445. // Now we need to attach the meshes to the root node of the scene
  446. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  447. pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
  448. for (unsigned int i = 0; i < pScene->mNumMeshes; ++i)
  449. pScene->mRootNode->mMeshes[i] = i;
  450. /*pScene->mRootNode->mTransformation *= aiMatrix4x4(
  451. 1.f, 0.f, 0.f, 0.f,
  452. 0.f, -1.f,0.f, 0.f,
  453. 0.f, 0.f, 1.f, 0.f,
  454. 0.f, 0.f, 0.f, 1.f);*/
  455. // Add cameras and light sources to the scene root node
  456. pScene->mRootNode->mNumChildren = pScene->mNumLights + pScene->mNumCameras;
  457. if (pScene->mRootNode->mNumChildren) {
  458. pScene->mRootNode->mChildren = new aiNode *[pScene->mRootNode->mNumChildren];
  459. // the light source
  460. aiNode *nd = pScene->mRootNode->mChildren[0] = new aiNode();
  461. nd->mParent = pScene->mRootNode;
  462. nd->mName.Set("Q3DLight");
  463. nd->mTransformation = pScene->mRootNode->mTransformation;
  464. nd->mTransformation.Inverse();
  465. // camera
  466. nd = pScene->mRootNode->mChildren[1] = new aiNode();
  467. nd->mParent = pScene->mRootNode;
  468. nd->mName.Set("Q3DCamera");
  469. nd->mTransformation = pScene->mRootNode->mChildren[0]->mTransformation;
  470. }
  471. }
  472. #endif // !! ASSIMP_BUILD_NO_Q3D_IMPORTER