3DSConverter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development Team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the ASSIMP team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the ASSIMP Development Team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the 3ds importer class */
  35. #include "AssimpPCH.h"
  36. // internal headers
  37. #include "3DSLoader.h"
  38. #include "MaterialSystem.h"
  39. #include "TextureTransform.h"
  40. #include "StringComparison.h"
  41. #include "qnan.h"
  42. using namespace Assimp;
  43. // ------------------------------------------------------------------------------------------------
  44. void Dot3DSImporter::ReplaceDefaultMaterial()
  45. {
  46. // try to find an existing material that matches the
  47. // typical default material setting:
  48. // - no textures
  49. // - diffuse color (in grey!)
  50. // NOTE: This is here to workaround the fact that some
  51. // exporters are writing a default material, too.
  52. unsigned int iIndex = 0xcdcdcdcd;
  53. for (unsigned int i = 0; i < this->mScene->mMaterials.size();++i)
  54. {
  55. if (std::string::npos == this->mScene->mMaterials[i].mName.find("default") &&
  56. std::string::npos == this->mScene->mMaterials[i].mName.find("DEFAULT"))continue;
  57. if (this->mScene->mMaterials[i].mDiffuse.r !=
  58. this->mScene->mMaterials[i].mDiffuse.g ||
  59. this->mScene->mMaterials[i].mDiffuse.r !=
  60. this->mScene->mMaterials[i].mDiffuse.b)continue;
  61. if (this->mScene->mMaterials[i].sTexDiffuse.mMapName.length() != 0 ||
  62. this->mScene->mMaterials[i].sTexBump.mMapName.length()!= 0 ||
  63. this->mScene->mMaterials[i].sTexOpacity.mMapName.length() != 0 ||
  64. this->mScene->mMaterials[i].sTexEmissive.mMapName.length() != 0 ||
  65. this->mScene->mMaterials[i].sTexSpecular.mMapName.length() != 0 ||
  66. this->mScene->mMaterials[i].sTexShininess.mMapName.length() != 0 )continue;
  67. iIndex = i;
  68. }
  69. if (0xcdcdcdcd == iIndex)iIndex = (unsigned int)this->mScene->mMaterials.size();
  70. // now iterate through all meshes and through all faces and
  71. // find all faces that are using the default material
  72. unsigned int iCnt = 0;
  73. for (std::vector<Dot3DS::Mesh>::iterator
  74. i = this->mScene->mMeshes.begin();
  75. i != this->mScene->mMeshes.end();++i)
  76. {
  77. for (std::vector<unsigned int>::iterator
  78. a = (*i).mFaceMaterials.begin();
  79. a != (*i).mFaceMaterials.end();++a)
  80. {
  81. // NOTE: The additional check seems to be necessary,
  82. // some exporters seem to generate invalid data here
  83. if (0xcdcdcdcd == (*a))
  84. {
  85. (*a) = iIndex;
  86. ++iCnt;
  87. }
  88. else if ( (*a) >= this->mScene->mMaterials.size())
  89. {
  90. (*a) = iIndex;
  91. ++iCnt;
  92. DefaultLogger::get()->warn("Material index overflow in 3DS file. Using default material");
  93. }
  94. }
  95. }
  96. if (iCnt && iIndex == this->mScene->mMaterials.size())
  97. {
  98. // we need to create our own default material
  99. Dot3DS::Material sMat;
  100. sMat.mDiffuse = aiColor3D(0.3f,0.3f,0.3f);
  101. sMat.mName = "%%%DEFAULT";
  102. this->mScene->mMaterials.push_back(sMat);
  103. }
  104. return;
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. void Dot3DSImporter::CheckIndices(Dot3DS::Mesh& sMesh)
  108. {
  109. for (std::vector< Dot3DS::Face >::iterator
  110. i = sMesh.mFaces.begin();
  111. i != sMesh.mFaces.end();++i)
  112. {
  113. // check whether all indices are in range
  114. for (unsigned int a = 0; a < 3;++a)
  115. {
  116. if ((*i).mIndices[a] >= sMesh.mPositions.size())
  117. {
  118. DefaultLogger::get()->warn("3DS: Face index overflow)");
  119. (*i).mIndices[a] = (uint32_t)sMesh.mPositions.size()-1;
  120. }
  121. }
  122. }
  123. return;
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. void Dot3DSImporter::MakeUnique(Dot3DS::Mesh& sMesh)
  127. {
  128. unsigned int iBase = 0;
  129. std::vector<aiVector3D> vNew;
  130. std::vector<aiVector2D> vNew2;
  131. vNew.resize(sMesh.mFaces.size() * 3);
  132. if (sMesh.mTexCoords.size())vNew2.resize(sMesh.mFaces.size() * 3);
  133. for (unsigned int i = 0; i < sMesh.mFaces.size();++i)
  134. {
  135. uint32_t iTemp1,iTemp2;
  136. // positions
  137. vNew[iBase] = sMesh.mPositions[sMesh.mFaces[i].mIndices[2]];
  138. iTemp1 = iBase++;
  139. vNew[iBase] = sMesh.mPositions[sMesh.mFaces[i].mIndices[1]];
  140. iTemp2 = iBase++;
  141. vNew[iBase] = sMesh.mPositions[sMesh.mFaces[i].mIndices[0]];
  142. // texture coordinates
  143. if (sMesh.mTexCoords.size())
  144. {
  145. vNew2[iTemp1] = sMesh.mTexCoords[sMesh.mFaces[i].mIndices[2]];
  146. vNew2[iTemp2] = sMesh.mTexCoords[sMesh.mFaces[i].mIndices[1]];
  147. vNew2[iBase] = sMesh.mTexCoords[sMesh.mFaces[i].mIndices[0]];
  148. }
  149. sMesh.mFaces[i].mIndices[2] = iBase++;
  150. sMesh.mFaces[i].mIndices[0] = iTemp1;
  151. sMesh.mFaces[i].mIndices[1] = iTemp2;
  152. }
  153. sMesh.mPositions = vNew;
  154. sMesh.mTexCoords = vNew2;
  155. return;
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. void Dot3DSImporter::ConvertMaterial(Dot3DS::Material& oldMat,
  159. MaterialHelper& mat)
  160. {
  161. // NOTE: Pass the background image to the viewer by bypassing the
  162. // material system. This is an evil hack, never do it again!
  163. if (0 != this->mBackgroundImage.length() && this->bHasBG)
  164. {
  165. aiString tex;
  166. tex.Set( this->mBackgroundImage);
  167. mat.AddProperty( &tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
  168. // be sure this is only done for the first material
  169. this->mBackgroundImage = std::string("");
  170. }
  171. // At first add the base ambient color of the
  172. // scene to the material
  173. oldMat.mAmbient.r += this->mClrAmbient.r;
  174. oldMat.mAmbient.g += this->mClrAmbient.g;
  175. oldMat.mAmbient.b += this->mClrAmbient.b;
  176. aiString name;
  177. name.Set( oldMat.mName);
  178. mat.AddProperty( &name, AI_MATKEY_NAME);
  179. // material colors
  180. mat.AddProperty( &oldMat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  181. mat.AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  182. mat.AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  183. mat.AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  184. // phong shininess and shininess strength
  185. if (Dot3DS::Dot3DSFile::Phong == oldMat.mShading ||
  186. Dot3DS::Dot3DSFile::Metal == oldMat.mShading)
  187. {
  188. if (!oldMat.mSpecularExponent || !oldMat.mShininessStrength)
  189. {
  190. oldMat.mShading = Dot3DS::Dot3DSFile::Gouraud;
  191. }
  192. else
  193. {
  194. mat.AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  195. mat.AddProperty( &oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  196. }
  197. }
  198. // opacity
  199. mat.AddProperty<float>( &oldMat.mTransparency,1,AI_MATKEY_OPACITY);
  200. // bump height scaling
  201. mat.AddProperty<float>( &oldMat.mBumpHeight,1,AI_MATKEY_BUMPSCALING);
  202. // two sided rendering?
  203. if (oldMat.mTwoSided)
  204. {
  205. int i = 1;
  206. mat.AddProperty<int>(&i,1,AI_MATKEY_TWOSIDED);
  207. }
  208. // shading mode
  209. aiShadingMode eShading = aiShadingMode_NoShading;
  210. switch (oldMat.mShading)
  211. {
  212. case Dot3DS::Dot3DSFile::Flat:
  213. eShading = aiShadingMode_Flat; break;
  214. // I don't know what "Wire" shading should be,
  215. // assume it is simple lambertian diffuse (L dot N) shading
  216. case Dot3DS::Dot3DSFile::Wire:
  217. case Dot3DS::Dot3DSFile::Gouraud:
  218. eShading = aiShadingMode_Gouraud; break;
  219. // assume cook-torrance shading for metals.
  220. case Dot3DS::Dot3DSFile::Phong :
  221. eShading = aiShadingMode_Phong; break;
  222. case Dot3DS::Dot3DSFile::Metal :
  223. eShading = aiShadingMode_CookTorrance; break;
  224. // FIX to workaround a warning with GCC 4 who complained
  225. // about a missing case Blinn: here - Blinn isn't a valid
  226. // value in the 3DS Loader, it is just needed for ASE
  227. case Dot3DS::Dot3DSFile::Blinn :
  228. eShading = aiShadingMode_Blinn; break;
  229. }
  230. mat.AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  231. if (Dot3DS::Dot3DSFile::Wire == oldMat.mShading)
  232. {
  233. // set the wireframe flag
  234. unsigned int iWire = 1;
  235. mat.AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  236. }
  237. // texture, if there is one
  238. if( oldMat.sTexDiffuse.mMapName.length() > 0)
  239. {
  240. aiString tex;
  241. tex.Set( oldMat.sTexDiffuse.mMapName);
  242. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  243. if (is_not_qnan(oldMat.sTexDiffuse.mTextureBlend))
  244. mat.AddProperty<float>( &oldMat.sTexDiffuse.mTextureBlend, 1, AI_MATKEY_TEXBLEND_DIFFUSE(0));
  245. if (aiTextureMapMode_Clamp != oldMat.sTexDiffuse.mMapMode)
  246. {
  247. int i = (int)oldMat.sTexSpecular.mMapMode;
  248. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  249. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  250. }
  251. }
  252. if( oldMat.sTexSpecular.mMapName.length() > 0)
  253. {
  254. aiString tex;
  255. tex.Set( oldMat.sTexSpecular.mMapName);
  256. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  257. if (is_not_qnan(oldMat.sTexSpecular.mTextureBlend))
  258. mat.AddProperty<float>( &oldMat.sTexSpecular.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SPECULAR(0));
  259. if (aiTextureMapMode_Clamp != oldMat.sTexSpecular.mMapMode)
  260. {
  261. int i = (int)oldMat.sTexSpecular.mMapMode;
  262. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SPECULAR(0));
  263. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SPECULAR(0));
  264. }
  265. }
  266. if( oldMat.sTexOpacity.mMapName.length() > 0)
  267. {
  268. aiString tex;
  269. tex.Set( oldMat.sTexOpacity.mMapName);
  270. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  271. if (is_not_qnan(oldMat.sTexOpacity.mTextureBlend))
  272. mat.AddProperty<float>( &oldMat.sTexOpacity.mTextureBlend, 1,AI_MATKEY_TEXBLEND_OPACITY(0));
  273. if (aiTextureMapMode_Clamp != oldMat.sTexOpacity.mMapMode)
  274. {
  275. int i = (int)oldMat.sTexOpacity.mMapMode;
  276. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_OPACITY(0));
  277. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_OPACITY(0));
  278. }
  279. }
  280. if( oldMat.sTexEmissive.mMapName.length() > 0)
  281. {
  282. aiString tex;
  283. tex.Set( oldMat.sTexEmissive.mMapName);
  284. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  285. if (is_not_qnan(oldMat.sTexEmissive.mTextureBlend))
  286. mat.AddProperty<float>( &oldMat.sTexEmissive.mTextureBlend, 1, AI_MATKEY_TEXBLEND_EMISSIVE(0));
  287. if (aiTextureMapMode_Clamp != oldMat.sTexEmissive.mMapMode)
  288. {
  289. int i = (int)oldMat.sTexEmissive.mMapMode;
  290. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_EMISSIVE(0));
  291. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_EMISSIVE(0));
  292. }
  293. }
  294. if( oldMat.sTexBump.mMapName.length() > 0)
  295. {
  296. aiString tex;
  297. tex.Set( oldMat.sTexBump.mMapName);
  298. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  299. if (is_not_qnan(oldMat.sTexBump.mTextureBlend))
  300. mat.AddProperty<float>( &oldMat.sTexBump.mTextureBlend, 1, AI_MATKEY_TEXBLEND_HEIGHT(0));
  301. if (aiTextureMapMode_Clamp != oldMat.sTexBump.mMapMode)
  302. {
  303. int i = (int)oldMat.sTexBump.mMapMode;
  304. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_HEIGHT(0));
  305. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_HEIGHT(0));
  306. }
  307. }
  308. if( oldMat.sTexShininess.mMapName.length() > 0)
  309. {
  310. aiString tex;
  311. tex.Set( oldMat.sTexShininess.mMapName);
  312. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  313. if (is_not_qnan(oldMat.sTexShininess.mTextureBlend))
  314. mat.AddProperty<float>( &oldMat.sTexShininess.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SHININESS(0));
  315. if (aiTextureMapMode_Clamp != oldMat.sTexShininess.mMapMode)
  316. {
  317. int i = (int)oldMat.sTexShininess.mMapMode;
  318. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SHININESS(0));
  319. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SHININESS(0));
  320. }
  321. }
  322. // store the name of the material itself, too
  323. if( oldMat.mName.length())
  324. {
  325. aiString tex;
  326. tex.Set( oldMat.mName);
  327. mat.AddProperty( &tex, AI_MATKEY_NAME);
  328. }
  329. return;
  330. }
  331. // ------------------------------------------------------------------------------------------------
  332. void Dot3DSImporter::ConvertMeshes(aiScene* pcOut)
  333. {
  334. std::vector<aiMesh*> avOutMeshes;
  335. avOutMeshes.reserve(this->mScene->mMeshes.size() * 2);
  336. unsigned int iFaceCnt = 0;
  337. // we need to split all meshes by their materials
  338. for (std::vector<Dot3DS::Mesh>::iterator
  339. i = this->mScene->mMeshes.begin();
  340. i != this->mScene->mMeshes.end();++i)
  341. {
  342. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  343. this->mScene->mMaterials.size()];
  344. unsigned int iNum = 0;
  345. for (std::vector<unsigned int>::const_iterator
  346. a = (*i).mFaceMaterials.begin();
  347. a != (*i).mFaceMaterials.end();++a,++iNum)
  348. {
  349. aiSplit[*a].push_back(iNum);
  350. }
  351. // now generate submeshes
  352. for (unsigned int p = 0; p < this->mScene->mMaterials.size();++p)
  353. {
  354. if (aiSplit[p].size() != 0)
  355. {
  356. aiMesh* p_pcOut = new aiMesh();
  357. p_pcOut->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  358. // be sure to setup the correct material index
  359. p_pcOut->mMaterialIndex = p;
  360. // use the color data as temporary storage
  361. p_pcOut->mColors[0] = (aiColor4D*)(&*i);
  362. avOutMeshes.push_back(p_pcOut);
  363. // convert vertices
  364. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  365. p_pcOut->mNumVertices = p_pcOut->mNumFaces*3;
  366. // allocate enough storage for faces
  367. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  368. iFaceCnt += p_pcOut->mNumFaces;
  369. if (p_pcOut->mNumVertices)
  370. {
  371. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  372. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  373. unsigned int iBase = 0;
  374. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  375. {
  376. unsigned int iIndex = aiSplit[p][q];
  377. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  378. p_pcOut->mFaces[q].mNumIndices = 3;
  379. p_pcOut->mFaces[q].mIndices[2] = iBase;
  380. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[0]];
  381. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[0]];
  382. p_pcOut->mFaces[q].mIndices[1] = iBase;
  383. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[1]];
  384. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[1]];
  385. p_pcOut->mFaces[q].mIndices[0] = iBase;
  386. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[2]];
  387. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[2]];
  388. }
  389. }
  390. // convert texture coordinates
  391. if ((*i).mTexCoords.size())
  392. {
  393. p_pcOut->mTextureCoords[0] = new aiVector3D[p_pcOut->mNumVertices];
  394. unsigned int iBase = 0;
  395. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  396. {
  397. unsigned int iIndex2 = aiSplit[p][q];
  398. unsigned int iIndex = (*i).mFaces[iIndex2].mIndices[0];
  399. aiVector2D& pc = (*i).mTexCoords[iIndex];
  400. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  401. iIndex = (*i).mFaces[iIndex2].mIndices[1];
  402. pc = (*i).mTexCoords[iIndex];
  403. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  404. iIndex = (*i).mFaces[iIndex2].mIndices[2];
  405. pc = (*i).mTexCoords[iIndex];
  406. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  407. }
  408. // apply texture coordinate scalings
  409. TextureTransform::BakeScaleNOffset ( p_pcOut, &this->mScene->mMaterials[
  410. p_pcOut->mMaterialIndex] );
  411. }
  412. }
  413. }
  414. delete[] aiSplit;
  415. }
  416. pcOut->mNumMeshes = (unsigned int)avOutMeshes.size();
  417. pcOut->mMeshes = new aiMesh*[pcOut->mNumMeshes]();
  418. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  419. pcOut->mMeshes[a] = avOutMeshes[a];
  420. if (!iFaceCnt)throw new ImportErrorException("No faces loaded. The mesh is empty");
  421. // for each material in the scene we need to setup the UV source
  422. // set for each texture
  423. for (unsigned int a = 0; a < pcOut->mNumMaterials;++a)
  424. TextureTransform::SetupMatUVSrc( pcOut->mMaterials[a], &this->mScene->mMaterials[a] );
  425. return;
  426. }
  427. // ------------------------------------------------------------------------------------------------
  428. void Dot3DSImporter::AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,Dot3DS::Node* pcIn)
  429. {
  430. std::vector<unsigned int> iArray;
  431. iArray.reserve(3);
  432. if (pcIn->mName != "$$$DUMMY")
  433. {
  434. for (unsigned int a = 0; a < pcSOut->mNumMeshes;++a)
  435. {
  436. const Dot3DS::Mesh* pcMesh = (const Dot3DS::Mesh*)pcSOut->mMeshes[a]->mColors[0];
  437. ai_assert(NULL != pcMesh);
  438. // do case independent comparisons here, just for safety
  439. if (!ASSIMP_stricmp(pcIn->mName,pcMesh->mName))
  440. iArray.push_back(a);
  441. }
  442. if (!iArray.empty())
  443. {
  444. aiMatrix4x4& mTrafo = ((Dot3DS::Mesh*)pcSOut->mMeshes[iArray[0]]->mColors[0])->mMat;
  445. aiMatrix4x4 mInv = mTrafo;
  446. if (!this->configSkipPivot)
  447. mInv.Inverse();
  448. pcOut->mName.Set(pcIn->mName);
  449. pcOut->mNumMeshes = (unsigned int)iArray.size();
  450. pcOut->mMeshes = new unsigned int[iArray.size()];
  451. for (unsigned int i = 0;i < iArray.size();++i)
  452. {
  453. const unsigned int iIndex = iArray[i];
  454. aiMesh* const mesh = pcSOut->mMeshes[iIndex];
  455. // http://www.zfx.info/DisplayThread.php?MID=235690#235690
  456. const aiVector3D& pivot = pcIn->vPivot;
  457. const aiVector3D* const pvEnd = mesh->mVertices+mesh->mNumVertices;
  458. aiVector3D* pvCurrent = mesh->mVertices;
  459. if(pivot.x || pivot.y || pivot.z && !this->configSkipPivot)
  460. {
  461. while (pvCurrent != pvEnd)
  462. {
  463. *pvCurrent = mInv * (*pvCurrent);
  464. pvCurrent->x -= pivot.x;
  465. pvCurrent->y -= pivot.y;
  466. pvCurrent->z -= pivot.z;
  467. *pvCurrent = mTrafo * (*pvCurrent);
  468. //std::swap( pvCurrent->y, pvCurrent->z );
  469. ++pvCurrent;
  470. }
  471. }
  472. #if 0
  473. else
  474. {
  475. while (pvCurrent != pvEnd)
  476. {
  477. std::swap( pvCurrent->y, pvCurrent->z );
  478. ++pvCurrent;
  479. }
  480. }
  481. #endif
  482. pcOut->mMeshes[i] = iIndex;
  483. }
  484. }
  485. }
  486. pcOut->mTransformation = aiMatrix4x4();
  487. pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size();
  488. pcOut->mChildren = new aiNode*[pcIn->mChildren.size()];
  489. for (unsigned int i = 0; i < pcIn->mChildren.size();++i)
  490. {
  491. pcOut->mChildren[i] = new aiNode();
  492. pcOut->mChildren[i]->mParent = pcOut;
  493. AddNodeToGraph(pcSOut,pcOut->mChildren[i],
  494. pcIn->mChildren[i]);
  495. }
  496. return;
  497. }
  498. // ------------------------------------------------------------------------------------------------
  499. void Dot3DSImporter::GenerateNodeGraph(aiScene* pcOut)
  500. {
  501. pcOut->mRootNode = new aiNode();
  502. if (0 == this->mRootNode->mChildren.size())
  503. {
  504. // seems the file has not even a hierarchy.
  505. // generate a flat hiearachy which looks like this:
  506. //
  507. // ROOT_NODE
  508. // |
  509. // ----------------------------------------
  510. // | | | |
  511. // MESH_0 MESH_1 MESH_2 ... MESH_N
  512. //
  513. DefaultLogger::get()->warn("No hierarchy information has been found in the file. ");
  514. pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes;
  515. pcOut->mRootNode->mChildren = new aiNode* [ pcOut->mNumMeshes ];
  516. for (unsigned int i = 0; i < pcOut->mNumMeshes;++i)
  517. {
  518. aiNode* pcNode = new aiNode();
  519. pcNode->mParent = pcOut->mRootNode;
  520. pcNode->mNumChildren = 0;
  521. pcNode->mChildren = 0;
  522. pcNode->mMeshes = new unsigned int[1];
  523. pcNode->mMeshes[0] = i;
  524. pcNode->mNumMeshes = 1;
  525. char szBuffer[128];
  526. int iLen;
  527. iLen = sprintf(szBuffer,"UNNAMED_%i",i);
  528. ai_assert(0 < iLen);
  529. ::memcpy(pcNode->mName.data,szBuffer,iLen);
  530. pcNode->mName.data[iLen] = '\0';
  531. pcNode->mName.length = iLen;
  532. // add the new child to the parent node
  533. pcOut->mRootNode->mChildren[i] = pcNode;
  534. }
  535. }
  536. else this->AddNodeToGraph(pcOut, pcOut->mRootNode, this->mRootNode);
  537. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  538. pcOut->mMeshes[a]->mColors[0] = NULL;
  539. // if the root node has only one child ... set the child as root node
  540. if (1 == pcOut->mRootNode->mNumChildren)
  541. {
  542. aiNode* pcOld = pcOut->mRootNode;
  543. pcOut->mRootNode = pcOut->mRootNode->mChildren[0];
  544. pcOut->mRootNode->mParent = NULL;
  545. pcOld->mChildren[0] = NULL;
  546. delete pcOld;
  547. }
  548. // if the root node is a default node setup a name for it
  549. if (pcOut->mRootNode->mName.data[0] == '$' && pcOut->mRootNode->mName.data[1] == '$')
  550. pcOut->mRootNode->mName.Set("<root>");
  551. }
  552. // ------------------------------------------------------------------------------------------------
  553. void Dot3DSImporter::ConvertScene(aiScene* pcOut)
  554. {
  555. pcOut->mNumMaterials = (unsigned int)this->mScene->mMaterials.size();
  556. pcOut->mMaterials = new aiMaterial*[pcOut->mNumMaterials];
  557. for (unsigned int i = 0; i < pcOut->mNumMaterials;++i)
  558. {
  559. MaterialHelper* pcNew = new MaterialHelper();
  560. this->ConvertMaterial(this->mScene->mMaterials[i],*pcNew);
  561. pcOut->mMaterials[i] = pcNew;
  562. }
  563. this->ConvertMeshes(pcOut);
  564. return;
  565. }