Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

612 lignes
18 KiB

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