3DSConverter.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 "3DSLoader.h"
  36. #include "MaterialSystem.h"
  37. #include "../include/DefaultLogger.h"
  38. #include "../include/IOStream.h"
  39. #include "../include/IOSystem.h"
  40. #include "../include/aiMesh.h"
  41. #include "../include/aiScene.h"
  42. #include "../include/aiAssert.h"
  43. #include <boost/scoped_ptr.hpp>
  44. using namespace Assimp;
  45. // ------------------------------------------------------------------------------------------------
  46. void Dot3DSImporter::ReplaceDefaultMaterial()
  47. {
  48. // try to find an existing material that matches the
  49. // typical default material setting:
  50. // - no textures
  51. // - diffuse color (in grey!)
  52. // NOTE: This is here to workaround the fact that some
  53. // exporters are writing a default material, too.
  54. unsigned int iIndex = 0xcdcdcdcd;
  55. for (unsigned int i = 0; i < this->mScene->mMaterials.size();++i)
  56. {
  57. if (std::string::npos == this->mScene->mMaterials[i].mName.find("default") &&
  58. std::string::npos == this->mScene->mMaterials[i].mName.find("DEFAULT"))continue;
  59. if (this->mScene->mMaterials[i].mDiffuse.r !=
  60. this->mScene->mMaterials[i].mDiffuse.g ||
  61. this->mScene->mMaterials[i].mDiffuse.r !=
  62. this->mScene->mMaterials[i].mDiffuse.b)continue;
  63. if (this->mScene->mMaterials[i].sTexDiffuse.mMapName.length() != 0 ||
  64. this->mScene->mMaterials[i].sTexBump.mMapName.length()!= 0 ||
  65. this->mScene->mMaterials[i].sTexOpacity.mMapName.length() != 0 ||
  66. this->mScene->mMaterials[i].sTexEmissive.mMapName.length() != 0 ||
  67. this->mScene->mMaterials[i].sTexSpecular.mMapName.length() != 0 ||
  68. this->mScene->mMaterials[i].sTexShininess.mMapName.length() != 0 )continue;
  69. iIndex = i;
  70. }
  71. if (0xcdcdcdcd == iIndex)iIndex = this->mScene->mMaterials.size();
  72. // now iterate through all meshes and through all faces and
  73. // find all faces that are using the default material
  74. unsigned int iCnt = 0;
  75. for (std::vector<Dot3DS::Mesh>::iterator
  76. i = this->mScene->mMeshes.begin();
  77. i != this->mScene->mMeshes.end();++i)
  78. {
  79. for (std::vector<unsigned int>::iterator
  80. a = (*i).mFaceMaterials.begin();
  81. a != (*i).mFaceMaterials.end();++a)
  82. {
  83. // NOTE: The additional check seems to be necessary,
  84. // some exporters seem to generate invalid data here
  85. if (0xcdcdcdcd == (*a))
  86. {
  87. (*a) = iIndex;
  88. ++iCnt;
  89. }
  90. else if ( (*a) >= this->mScene->mMaterials.size())
  91. {
  92. (*a) = iIndex;
  93. ++iCnt;
  94. DefaultLogger::get()->warn("Material index overflow in 3DS file. Assigning "
  95. "default material ...");
  96. }
  97. }
  98. }
  99. if (0 != iCnt && iIndex == this->mScene->mMaterials.size())
  100. {
  101. // we need to create our own default material
  102. Dot3DS::Material sMat;
  103. sMat.mDiffuse = aiColor3D(0.3f,0.3f,0.3f);
  104. sMat.mName = "%%%DEFAULT";
  105. this->mScene->mMaterials.push_back(sMat);
  106. }
  107. return;
  108. }
  109. // ------------------------------------------------------------------------------------------------
  110. void Dot3DSImporter::CheckIndices(Dot3DS::Mesh* sMesh)
  111. {
  112. for (std::vector< Dot3DS::Face >::iterator
  113. i = sMesh->mFaces.begin();
  114. i != sMesh->mFaces.end();++i)
  115. {
  116. // check whether all indices are in range
  117. if ((*i).mIndices[0] >= sMesh->mPositions.size())
  118. {
  119. DefaultLogger::get()->warn("Face index overflow in 3DS file (#1)");
  120. (*i).mIndices[0] = sMesh->mPositions.size()-1;
  121. }
  122. if ((*i).mIndices[1] >= sMesh->mPositions.size())
  123. {
  124. DefaultLogger::get()->warn("Face index overflow in 3DS file (#2)");
  125. (*i).mIndices[1] = sMesh->mPositions.size()-1;
  126. }
  127. if ((*i).mIndices[2] >= sMesh->mPositions.size())
  128. {
  129. DefaultLogger::get()->warn("Face index overflow in 3DS file (#3)");
  130. (*i).mIndices[2] = sMesh->mPositions.size()-1;
  131. }
  132. }
  133. return;
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. void Dot3DSImporter::MakeUnique(Dot3DS::Mesh* sMesh)
  137. {
  138. std::vector<aiVector3D> vNew;
  139. vNew.resize(sMesh->mFaces.size() * 3);
  140. std::vector<aiVector2D> vNew2;
  141. // TODO: Remove this step. By maintaining a small LUT it
  142. // would be possible to do this directly in the parsing step
  143. unsigned int iBase = 0;
  144. if (0 != sMesh->mTexCoords.size())
  145. {
  146. vNew2.resize(sMesh->mFaces.size() * 3);
  147. for (unsigned int i = 0; i < sMesh->mFaces.size();++i)
  148. {
  149. uint32_t iTemp1,iTemp2;
  150. // position and texture coordinates
  151. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[2]];
  152. vNew2[iBase] = sMesh->mTexCoords[sMesh->mFaces[i].mIndices[2]];
  153. iTemp1 = iBase++;
  154. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[1]];
  155. vNew2[iBase] = sMesh->mTexCoords[sMesh->mFaces[i].mIndices[1]];
  156. iTemp2 = iBase++;
  157. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[0]];
  158. vNew2[iBase] = sMesh->mTexCoords[sMesh->mFaces[i].mIndices[0]];
  159. sMesh->mFaces[i].mIndices[2] = iBase++;
  160. sMesh->mFaces[i].mIndices[0] = iTemp1;
  161. sMesh->mFaces[i].mIndices[1] = iTemp2;
  162. // handle the face order ...
  163. /*if (iTemp1 > iTemp2)
  164. {
  165. sMesh->mFaces[i].bFlipped = true;
  166. }*/
  167. }
  168. }
  169. else
  170. {
  171. for (unsigned int i = 0; i < sMesh->mFaces.size();++i)
  172. {
  173. uint32_t iTemp1,iTemp2;
  174. // position only
  175. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[2]];
  176. iTemp1 = iBase++;
  177. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[1]];
  178. iTemp2 = iBase++;
  179. vNew[iBase] = sMesh->mPositions[sMesh->mFaces[i].mIndices[0]];
  180. sMesh->mFaces[i].mIndices[2] = iBase++;
  181. sMesh->mFaces[i].mIndices[0] = iTemp1;
  182. sMesh->mFaces[i].mIndices[1] = iTemp2;
  183. // handle the face order ...
  184. /*if (iTemp1 > iTemp2)
  185. {
  186. sMesh->mFaces[i].bFlipped = true;
  187. }*/
  188. }
  189. }
  190. sMesh->mPositions = vNew;
  191. sMesh->mTexCoords = vNew2;
  192. return;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. void Dot3DSImporter::ConvertMaterial(Dot3DS::Material& oldMat,
  196. MaterialHelper& mat)
  197. {
  198. // NOTE: Pass the background image to the viewer by bypassing the
  199. // material system. This is an evil hack, never do it again!
  200. if (0 != this->mBackgroundImage.length() && this->bHasBG)
  201. {
  202. aiString tex;
  203. tex.Set( this->mBackgroundImage);
  204. mat.AddProperty( &tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
  205. // be sure this is only done for the first material
  206. this->mBackgroundImage = std::string("");
  207. }
  208. // At first add the base ambient color of the
  209. // scene to the material
  210. oldMat.mAmbient.r += this->mClrAmbient.r;
  211. oldMat.mAmbient.g += this->mClrAmbient.g;
  212. oldMat.mAmbient.b += this->mClrAmbient.b;
  213. aiString name;
  214. name.Set( oldMat.mName);
  215. mat.AddProperty( &name, AI_MATKEY_NAME);
  216. // material colors
  217. mat.AddProperty( &oldMat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  218. mat.AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  219. mat.AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  220. mat.AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  221. // phong shininess and shininess strength
  222. if (Dot3DS::Dot3DSFile::Phong == oldMat.mShading ||
  223. Dot3DS::Dot3DSFile::Metal == oldMat.mShading)
  224. {
  225. mat.AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  226. mat.AddProperty( &oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  227. }
  228. // opacity
  229. mat.AddProperty<float>( &oldMat.mTransparency,1,AI_MATKEY_OPACITY);
  230. // bump height scaling
  231. mat.AddProperty<float>( &oldMat.mBumpHeight,1,AI_MATKEY_BUMPSCALING);
  232. // shading mode
  233. aiShadingMode eShading = aiShadingMode_NoShading;
  234. switch (oldMat.mShading)
  235. {
  236. case Dot3DS::Dot3DSFile::Flat:
  237. eShading = aiShadingMode_Flat; break;
  238. // I don't know what "Wire" shading should be,
  239. // assume it is simple lambertian diffuse (L dot N) shading
  240. case Dot3DS::Dot3DSFile::Wire:
  241. case Dot3DS::Dot3DSFile::Gouraud:
  242. eShading = aiShadingMode_Gouraud; break;
  243. // assume cook-torrance shading for metals.
  244. // NOTE: I assume the real shader inside 3ds max is an anisotropic
  245. // Phong-Blinn shader, but this is a good approximation too
  246. case Dot3DS::Dot3DSFile::Phong :
  247. eShading = aiShadingMode_Phong; break;
  248. case Dot3DS::Dot3DSFile::Metal :
  249. eShading = aiShadingMode_CookTorrance; break;
  250. }
  251. mat.AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  252. if (Dot3DS::Dot3DSFile::Wire == oldMat.mShading)
  253. {
  254. // set the wireframe flag
  255. unsigned int iWire = 1;
  256. mat.AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  257. }
  258. // texture, if there is one
  259. if( oldMat.sTexDiffuse.mMapName.length() > 0)
  260. {
  261. aiString tex;
  262. tex.Set( oldMat.sTexDiffuse.mMapName);
  263. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  264. if (is_not_qnan(oldMat.sTexDiffuse.mTextureBlend))
  265. mat.AddProperty<float>( &oldMat.sTexDiffuse.mTextureBlend, 1, AI_MATKEY_TEXBLEND_DIFFUSE(0));
  266. }
  267. if( oldMat.sTexSpecular.mMapName.length() > 0)
  268. {
  269. aiString tex;
  270. tex.Set( oldMat.sTexSpecular.mMapName);
  271. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  272. if (is_not_qnan(oldMat.sTexSpecular.mTextureBlend))
  273. mat.AddProperty<float>( &oldMat.sTexSpecular.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SPECULAR(0));
  274. }
  275. if( oldMat.sTexOpacity.mMapName.length() > 0)
  276. {
  277. aiString tex;
  278. tex.Set( oldMat.sTexOpacity.mMapName);
  279. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  280. if (is_not_qnan(oldMat.sTexOpacity.mTextureBlend))
  281. mat.AddProperty<float>( &oldMat.sTexOpacity.mTextureBlend, 1,AI_MATKEY_TEXBLEND_OPACITY(0));
  282. }
  283. if( oldMat.sTexEmissive.mMapName.length() > 0)
  284. {
  285. aiString tex;
  286. tex.Set( oldMat.sTexEmissive.mMapName);
  287. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  288. if (is_not_qnan(oldMat.sTexEmissive.mTextureBlend))
  289. mat.AddProperty<float>( &oldMat.sTexEmissive.mTextureBlend, 1, AI_MATKEY_TEXBLEND_EMISSIVE(0));
  290. }
  291. if( oldMat.sTexBump.mMapName.length() > 0)
  292. {
  293. aiString tex;
  294. tex.Set( oldMat.sTexBump.mMapName);
  295. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  296. if (is_not_qnan(oldMat.sTexBump.mTextureBlend))
  297. mat.AddProperty<float>( &oldMat.sTexBump.mTextureBlend, 1, AI_MATKEY_TEXBLEND_HEIGHT(0));
  298. }
  299. if( oldMat.sTexShininess.mMapName.length() > 0)
  300. {
  301. aiString tex;
  302. tex.Set( oldMat.sTexShininess.mMapName);
  303. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  304. if (is_not_qnan(oldMat.sTexShininess.mTextureBlend))
  305. mat.AddProperty<float>( &oldMat.sTexShininess.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SHININESS(0));
  306. }
  307. // store the name of the material itself, too
  308. if( oldMat.mName.length() > 0)
  309. {
  310. aiString tex;
  311. tex.Set( oldMat.mName);
  312. mat.AddProperty( &tex, AI_MATKEY_NAME);
  313. }
  314. return;
  315. }
  316. // ------------------------------------------------------------------------------------------------
  317. void SetupMatUVSrc (aiMaterial* pcMat, const Dot3DS::Material* pcMatIn)
  318. {
  319. MaterialHelper* pcHelper = (MaterialHelper*)pcMat;
  320. pcHelper->AddProperty<int>(&pcMatIn->sTexDiffuse.iUVSrc,1,AI_MATKEY_UVWSRC_DIFFUSE(0));
  321. pcHelper->AddProperty<int>(&pcMatIn->sTexSpecular.iUVSrc,1,AI_MATKEY_UVWSRC_SPECULAR(0));
  322. pcHelper->AddProperty<int>(&pcMatIn->sTexEmissive.iUVSrc,1,AI_MATKEY_UVWSRC_EMISSIVE(0));
  323. pcHelper->AddProperty<int>(&pcMatIn->sTexBump.iUVSrc,1,AI_MATKEY_UVWSRC_HEIGHT(0));
  324. pcHelper->AddProperty<int>(&pcMatIn->sTexShininess.iUVSrc,1,AI_MATKEY_UVWSRC_SHININESS(0));
  325. pcHelper->AddProperty<int>(&pcMatIn->sTexOpacity.iUVSrc,1,AI_MATKEY_UVWSRC_OPACITY(0));
  326. }
  327. // ------------------------------------------------------------------------------------------------
  328. void Dot3DSImporter::ConvertMeshes(aiScene* pcOut)
  329. {
  330. std::vector<aiMesh*> avOutMeshes;
  331. avOutMeshes.reserve(this->mScene->mMeshes.size() * 2);
  332. unsigned int iFaceCnt = 0;
  333. // we need to split all meshes by their materials
  334. for (std::vector<Dot3DS::Mesh>::iterator
  335. i = this->mScene->mMeshes.begin();
  336. i != this->mScene->mMeshes.end();++i)
  337. {
  338. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  339. this->mScene->mMaterials.size()];
  340. unsigned int iNum = 0;
  341. for (std::vector<unsigned int>::const_iterator
  342. a = (*i).mFaceMaterials.begin();
  343. a != (*i).mFaceMaterials.end();++a,++iNum)
  344. {
  345. // check range
  346. if ((*a) >= this->mScene->mMaterials.size())
  347. {
  348. // use the last material instead
  349. aiSplit[this->mScene->mMaterials.size()-1].push_back(iNum);
  350. }
  351. else aiSplit[*a].push_back(iNum);
  352. }
  353. // now generate submeshes
  354. bool bFirst = true;
  355. for (unsigned int p = 0; p < this->mScene->mMaterials.size();++p)
  356. {
  357. if (aiSplit[p].size() != 0)
  358. {
  359. aiMesh* p_pcOut = new aiMesh();
  360. // be sure to setup the correct material index
  361. p_pcOut->mMaterialIndex = p;
  362. // use the color data as temporary storage
  363. p_pcOut->mColors[0] = (aiColor4D*)new std::string((*i).mName);
  364. avOutMeshes.push_back(p_pcOut);
  365. if (bFirst)
  366. {
  367. p_pcOut->mColors[1] = (aiColor4D*)new aiMatrix4x4();
  368. *((aiMatrix4x4*)p_pcOut->mColors[1]) = (*i).mMat;
  369. bFirst = false;
  370. }
  371. // convert vertices
  372. p_pcOut->mNumVertices = aiSplit[p].size()*3;
  373. p_pcOut->mNumFaces = aiSplit[p].size();
  374. // allocate enough storage for faces
  375. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  376. iFaceCnt += p_pcOut->mNumFaces;
  377. if (p_pcOut->mNumVertices != 0)
  378. {
  379. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  380. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  381. unsigned int iBase = 0;
  382. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  383. {
  384. unsigned int iIndex = aiSplit[p][q];
  385. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  386. p_pcOut->mFaces[q].mNumIndices = 3;
  387. p_pcOut->mFaces[q].mIndices[2] = iBase;
  388. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[0]];
  389. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[0]];
  390. p_pcOut->mFaces[q].mIndices[1] = iBase;
  391. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[1]];
  392. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[1]];
  393. p_pcOut->mFaces[q].mIndices[0] = iBase;
  394. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[2]];
  395. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[2]];
  396. }
  397. }
  398. // convert texture coordinates
  399. if ((*i).mTexCoords.size() != 0)
  400. {
  401. p_pcOut->mTextureCoords[0] = new aiVector3D[p_pcOut->mNumVertices];
  402. unsigned int iBase = 0;
  403. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  404. {
  405. unsigned int iIndex2 = aiSplit[p][q];
  406. unsigned int iIndex = (*i).mFaces[iIndex2].mIndices[0];
  407. aiVector2D& pc = (*i).mTexCoords[iIndex];
  408. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  409. iIndex = (*i).mFaces[iIndex2].mIndices[1];
  410. pc = (*i).mTexCoords[iIndex];
  411. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  412. iIndex = (*i).mFaces[iIndex2].mIndices[2];
  413. pc = (*i).mTexCoords[iIndex];
  414. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc.x,pc.y,0.0f);
  415. }
  416. // apply texture coordinate scalings
  417. this->BakeScaleNOffset ( p_pcOut, &this->mScene->mMaterials[
  418. p_pcOut->mMaterialIndex] );
  419. // setup bitflags to indicate which texture coordinate
  420. // channels are used
  421. p_pcOut->mNumUVComponents[0] = 2;
  422. if (p_pcOut->HasTextureCoords(1))
  423. p_pcOut->mNumUVComponents[1] = 2;
  424. if (p_pcOut->HasTextureCoords(2))
  425. p_pcOut->mNumUVComponents[2] = 2;
  426. if (p_pcOut->HasTextureCoords(3))
  427. p_pcOut->mNumUVComponents[3] = 2;
  428. }
  429. }
  430. }
  431. delete[] aiSplit;
  432. }
  433. pcOut->mNumMeshes = avOutMeshes.size();
  434. pcOut->mMeshes = new aiMesh*[pcOut->mNumMeshes]();
  435. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  436. {
  437. pcOut->mMeshes[a] = avOutMeshes[a];
  438. }
  439. if (0 == iFaceCnt)
  440. {
  441. throw new ImportErrorException("No faces loaded. The mesh is empty");
  442. }
  443. // for each material in the scene we need to setup the UV source
  444. // set for each texture
  445. for (unsigned int a = 0; a < pcOut->mNumMaterials;++a)
  446. {
  447. SetupMatUVSrc( pcOut->mMaterials[a], &this->mScene->mMaterials[a] );
  448. }
  449. return;
  450. }
  451. // ------------------------------------------------------------------------------------------------
  452. void Dot3DSImporter::AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,Dot3DS::Node* pcIn)
  453. {
  454. // find the corresponding mesh indices
  455. std::vector<unsigned int> iArray;
  456. if (pcIn->mName != "$$$DUMMY")
  457. {
  458. for (unsigned int a = 0; a < pcSOut->mNumMeshes;++a)
  459. {
  460. if (0 == ASSIMP_stricmp(pcIn->mName.c_str(),
  461. ((std::string*)pcSOut->mMeshes[a]->mColors[0])->c_str()))
  462. {
  463. iArray.push_back(a);
  464. }
  465. }
  466. }
  467. pcOut->mName.Set(pcIn->mName);
  468. pcOut->mNumMeshes = iArray.size();
  469. pcOut->mMeshes = new unsigned int[iArray.size()];
  470. for (unsigned int i = 0;i < iArray.size();++i)
  471. {
  472. const unsigned int iIndex = iArray[i];
  473. if (NULL != pcSOut->mMeshes[iIndex]->mColors[1])
  474. {
  475. pcOut->mTransformation = *((aiMatrix4x4*)
  476. (pcSOut->mMeshes[iIndex]->mColors[1]));
  477. delete (aiMatrix4x4*)pcSOut->mMeshes[iIndex]->mColors[1];
  478. pcSOut->mMeshes[iIndex]->mColors[1] = NULL;
  479. }
  480. pcOut->mMeshes[i] = iIndex;
  481. }
  482. // (code for keyframe animation. however, this is currently not supported by Assimp)
  483. #if 0
  484. // build the scaling matrix. Toggle y and z axis
  485. aiMatrix4x4 mS;
  486. mS.a1 = pcIn->vScaling.x;
  487. mS.b2 = pcIn->vScaling.z;
  488. mS.c3 = pcIn->vScaling.y;
  489. // build the translation matrix. Toggle y and z axis
  490. aiMatrix4x4 mT;
  491. mT.a4 = pcIn->vPosition.x;
  492. mT.b4 = pcIn->vPosition.z;
  493. mT.c4 = pcIn->vPosition.y;
  494. // build the pivot matrix. Toggle y and z axis
  495. aiMatrix4x4 mP;
  496. mP.a4 = -pcIn->vPivot.x;
  497. mP.b4 = -pcIn->vPivot.z;
  498. mP.c4 = -pcIn->vPivot.y;
  499. #endif
  500. // build a matrix to flip the z coordinate of the vertices
  501. aiMatrix4x4 mF;
  502. mF.c3 = -1.0f;
  503. // build the final matrix
  504. // NOTE: This should be the identity. Theoretically. In reality
  505. // there are many models with very funny local matrices and
  506. // very different keyframe values ... this is the only reason
  507. // why we extract the data from the first keyframe.
  508. pcOut->mTransformation = mF; /* mF * mT * pcIn->mRotation * mS * mP *
  509. pcOut->mTransformation.Inverse(); */
  510. // (code for keyframe animation. however, this is currently not supported by Assimp)
  511. #if 0
  512. if (pcOut->mTransformation != mF)
  513. {
  514. DefaultLogger::get()->warn("The local transformation matrix of the "
  515. "3ds file does not match the first keyframe. Using the "
  516. "information from the keyframe.");
  517. }
  518. #endif
  519. pcOut->mNumChildren = pcIn->mChildren.size();
  520. pcOut->mChildren = new aiNode*[pcIn->mChildren.size()];
  521. for (unsigned int i = 0; i < pcIn->mChildren.size();++i)
  522. {
  523. pcOut->mChildren[i] = new aiNode();
  524. pcOut->mChildren[i]->mParent = pcOut;
  525. AddNodeToGraph(pcSOut,pcOut->mChildren[i],
  526. pcIn->mChildren[i]);
  527. }
  528. return;
  529. }
  530. // ------------------------------------------------------------------------------------------------
  531. inline bool HasUVTransform(const Dot3DS::Texture& rcIn)
  532. {
  533. return (0.0f != rcIn.mOffsetU ||
  534. 0.0f != rcIn.mOffsetV ||
  535. 1.0f != rcIn.mScaleU ||
  536. 1.0f != rcIn.mScaleV ||
  537. 0.0f != rcIn.mRotation);
  538. }
  539. // ------------------------------------------------------------------------------------------------
  540. void Dot3DSImporter::ApplyScaleNOffset()
  541. {
  542. unsigned int iNum = 0;
  543. for (std::vector<Dot3DS::Material>::iterator
  544. i = this->mScene->mMaterials.begin();
  545. i != this->mScene->mMaterials.end();++i,++iNum)
  546. {
  547. unsigned int iCnt = 0;
  548. Dot3DS::Texture* pcTexture = NULL;
  549. if (HasUVTransform((*i).sTexDiffuse))
  550. {
  551. (*i).sTexDiffuse.bPrivate = true;
  552. pcTexture = &(*i).sTexDiffuse;
  553. ++iCnt;
  554. }
  555. if (HasUVTransform((*i).sTexSpecular))
  556. {
  557. (*i).sTexSpecular.bPrivate = true;
  558. pcTexture = &(*i).sTexSpecular;
  559. ++iCnt;
  560. }
  561. if (HasUVTransform((*i).sTexOpacity))
  562. {
  563. (*i).sTexOpacity.bPrivate = true;
  564. pcTexture = &(*i).sTexOpacity;
  565. ++iCnt;
  566. }
  567. if (HasUVTransform((*i).sTexEmissive))
  568. {
  569. (*i).sTexEmissive.bPrivate = true;
  570. pcTexture = &(*i).sTexEmissive;
  571. ++iCnt;
  572. }
  573. if (HasUVTransform((*i).sTexBump))
  574. {
  575. (*i).sTexBump.bPrivate = true;
  576. pcTexture = &(*i).sTexBump;
  577. ++iCnt;
  578. }
  579. if (HasUVTransform((*i).sTexShininess))
  580. {
  581. (*i).sTexShininess.bPrivate = true;
  582. pcTexture = &(*i).sTexShininess;
  583. ++iCnt;
  584. }
  585. if (0 != iCnt)
  586. {
  587. // if only one texture needs scaling/offset operations
  588. // we can apply them directly to the first texture
  589. // coordinate sets of all meshes referencing *this* material
  590. // However, we can't do it now. We need to wait until
  591. // everything is sorted by materials.
  592. if (1 == iCnt)
  593. {
  594. (*i).iBakeUVTransform = 1;
  595. (*i).pcSingleTexture = pcTexture;
  596. }
  597. // we will need to generate a separate new texture channel
  598. // for each texture.
  599. // However, we can't do it now. We need to wait until
  600. // everything is sorted by materials.
  601. else (*i).iBakeUVTransform = 2;
  602. }
  603. }
  604. }
  605. // ------------------------------------------------------------------------------------------------
  606. struct STransformVecInfo
  607. {
  608. float fScaleU;
  609. float fScaleV;
  610. float fOffsetU;
  611. float fOffsetV;
  612. float fRotation;
  613. std::vector<Dot3DS::Texture*> pcTextures;
  614. };
  615. // ------------------------------------------------------------------------------------------------
  616. void AddToList(std::vector<STransformVecInfo>& rasVec,Dot3DS::Texture* pcTex)
  617. {
  618. if (0 == pcTex->mMapName.length())return;
  619. for (std::vector<STransformVecInfo>::iterator
  620. i = rasVec.begin();
  621. i != rasVec.end();++i)
  622. {
  623. if ((*i).fOffsetU == pcTex->mOffsetU &&
  624. (*i).fOffsetV == pcTex->mOffsetV &&
  625. (*i).fScaleU == pcTex->mScaleU &&
  626. (*i).fScaleV == pcTex->mScaleV &&
  627. (*i).fRotation == pcTex->mRotation)
  628. {
  629. (*i).pcTextures.push_back(pcTex);
  630. return;
  631. }
  632. }
  633. STransformVecInfo sInfo;
  634. sInfo.fScaleU = pcTex->mScaleU;
  635. sInfo.fScaleV = pcTex->mScaleV;
  636. sInfo.fOffsetU = pcTex->mOffsetU;
  637. sInfo.fOffsetV = pcTex->mOffsetV;
  638. sInfo.fRotation = pcTex->mRotation;
  639. sInfo.pcTextures.push_back(pcTex);
  640. rasVec.push_back(sInfo);
  641. }
  642. // ------------------------------------------------------------------------------------------------
  643. void Dot3DSImporter::BakeScaleNOffset(
  644. aiMesh* pcMesh, Dot3DS::Material* pcSrc)
  645. {
  646. if (!pcMesh->mTextureCoords[0])return;
  647. if (1 == pcSrc->iBakeUVTransform)
  648. {
  649. if (0.0f == pcSrc->pcSingleTexture->mRotation)
  650. {
  651. for (unsigned int i = 0; i < pcMesh->mNumVertices;++i)
  652. {
  653. pcMesh->mTextureCoords[0][i].x /= pcSrc->pcSingleTexture->mScaleU;
  654. pcMesh->mTextureCoords[0][i].y /= pcSrc->pcSingleTexture->mScaleV;
  655. pcMesh->mTextureCoords[0][i].x += pcSrc->pcSingleTexture->mOffsetU;
  656. pcMesh->mTextureCoords[0][i].y += pcSrc->pcSingleTexture->mOffsetV;
  657. }
  658. }
  659. else
  660. {
  661. const float fSin = sinf(pcSrc->pcSingleTexture->mRotation);
  662. const float fCos = cosf(pcSrc->pcSingleTexture->mRotation);
  663. for (unsigned int i = 0; i < pcMesh->mNumVertices;++i)
  664. {
  665. pcMesh->mTextureCoords[0][i].x /= pcSrc->pcSingleTexture->mScaleU;
  666. pcMesh->mTextureCoords[0][i].y /= pcSrc->pcSingleTexture->mScaleV;
  667. pcMesh->mTextureCoords[0][i].x *= fCos;
  668. pcMesh->mTextureCoords[0][i].y *= fSin;
  669. pcMesh->mTextureCoords[0][i].x += pcSrc->pcSingleTexture->mOffsetU;
  670. pcMesh->mTextureCoords[0][i].y += pcSrc->pcSingleTexture->mOffsetV;
  671. }
  672. }
  673. }
  674. else if (2 == pcSrc->iBakeUVTransform)
  675. {
  676. // now we need to find all textures in the material
  677. // which require scaling/offset operations
  678. std::vector<STransformVecInfo> sOps;
  679. AddToList(sOps,&pcSrc->sTexDiffuse);
  680. AddToList(sOps,&pcSrc->sTexSpecular);
  681. AddToList(sOps,&pcSrc->sTexEmissive);
  682. AddToList(sOps,&pcSrc->sTexOpacity);
  683. AddToList(sOps,&pcSrc->sTexBump);
  684. AddToList(sOps,&pcSrc->sTexShininess);
  685. const aiVector3D* _pvBase;
  686. if (0.0f == sOps[0].fOffsetU && 0.0f == sOps[0].fOffsetV &&
  687. 1.0f == sOps[0].fScaleU && 1.0f == sOps[0].fScaleV &&
  688. 0.0f == sOps[0].fRotation)
  689. {
  690. // we'll have an unmodified set, so we can use *this* one
  691. _pvBase = pcMesh->mTextureCoords[0];
  692. }
  693. else
  694. {
  695. _pvBase = new aiVector3D[pcMesh->mNumVertices];
  696. memcpy(const_cast<aiVector3D*>(_pvBase),pcMesh->mTextureCoords[0],
  697. pcMesh->mNumVertices * sizeof(aiVector3D));
  698. }
  699. unsigned int iCnt = 0;
  700. for (std::vector<STransformVecInfo>::iterator
  701. i = sOps.begin();
  702. i != sOps.end();++i,++iCnt)
  703. {
  704. if (!pcMesh->mTextureCoords[iCnt])
  705. {
  706. pcMesh->mTextureCoords[iCnt] = new aiVector3D[pcMesh->mNumVertices];
  707. }
  708. // more than 4 UV texture channels are not available
  709. if (iCnt > 3)
  710. {
  711. for (std::vector<Dot3DS::Texture*>::iterator
  712. a = (*i).pcTextures.begin();
  713. a != (*i).pcTextures.end();++a)
  714. {
  715. (*a)->iUVSrc = 0;
  716. }
  717. DefaultLogger::get()->error("There are too many "
  718. "combinations of different UV scaling/offset/rotation operations "
  719. "to generate an UV channel for each (maximum is 4). Using the "
  720. "first UV channel ...");
  721. continue;
  722. }
  723. const aiVector3D* pvBase = _pvBase;
  724. if (0.0f == (*i).fRotation)
  725. {
  726. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  727. {
  728. pcMesh->mTextureCoords[iCnt][n].x = pvBase->x / (*i).fScaleU;
  729. pcMesh->mTextureCoords[iCnt][n].y = pvBase->y / (*i).fScaleV;
  730. pcMesh->mTextureCoords[iCnt][n].x += (*i).fOffsetU;
  731. pcMesh->mTextureCoords[iCnt][n].y += (*i).fOffsetV;
  732. pvBase++;
  733. }
  734. }
  735. else
  736. {
  737. const float fSin = sinf((*i).fRotation);
  738. const float fCos = cosf((*i).fRotation);
  739. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  740. {
  741. pcMesh->mTextureCoords[iCnt][n].x = pvBase->x / (*i).fScaleU;
  742. pcMesh->mTextureCoords[iCnt][n].y = pvBase->y / (*i).fScaleV;
  743. pcMesh->mTextureCoords[iCnt][n].x *= fCos;
  744. pcMesh->mTextureCoords[iCnt][n].y *= fSin;
  745. pcMesh->mTextureCoords[iCnt][n].x += (*i).fOffsetU;
  746. pcMesh->mTextureCoords[iCnt][n].y += (*i).fOffsetV;
  747. pvBase++;
  748. }
  749. }
  750. // setup UV source
  751. for (std::vector<Dot3DS::Texture*>::iterator
  752. a = (*i).pcTextures.begin();
  753. a != (*i).pcTextures.end();++a)
  754. {
  755. (*a)->iUVSrc = iCnt;
  756. }
  757. }
  758. // release temporary storage
  759. if (_pvBase != pcMesh->mTextureCoords[0])
  760. delete[] _pvBase;
  761. }
  762. }
  763. // ------------------------------------------------------------------------------------------------
  764. void Dot3DSImporter::GenerateNodeGraph(aiScene* pcOut)
  765. {
  766. pcOut->mRootNode = new aiNode();
  767. if (0 == this->mRootNode->mChildren.size())
  768. {
  769. // seems the file has not even a hierarchy.
  770. // generate a flat hiearachy which looks like this:
  771. //
  772. // ROOT_NODE
  773. // |
  774. // ----------------------------------------
  775. // | | | |
  776. // MESH_0 MESH_1 MESH_2 ... MESH_N
  777. //
  778. unsigned int iCnt = 0;
  779. DefaultLogger::get()->warn("No hierarchy information has been "
  780. "found in the file. A flat hierarchy tree is built ...");
  781. pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes;
  782. pcOut->mRootNode->mChildren = new aiNode* [ pcOut->mNumMeshes ];
  783. for (unsigned int i = 0; i < pcOut->mNumMeshes;++i)
  784. {
  785. aiNode* pcNode = new aiNode();
  786. pcNode->mParent = pcOut->mRootNode;
  787. pcNode->mNumChildren = 0;
  788. pcNode->mChildren = 0;
  789. pcNode->mMeshes = new unsigned int[1];
  790. pcNode->mMeshes[0] = i;
  791. pcNode->mNumMeshes = 1;
  792. std::string s;
  793. std::stringstream ss(s);
  794. ss << "UNNAMED[" << i << + "]";
  795. pcNode->mName.Set(s);
  796. // add the new child to the parent node
  797. pcOut->mRootNode->mChildren[i] = pcNode;
  798. }
  799. }
  800. else this->AddNodeToGraph(pcOut, pcOut->mRootNode, this->mRootNode);
  801. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  802. {
  803. delete (std::string*)pcOut->mMeshes[a]->mColors[0];
  804. pcOut->mMeshes[a]->mColors[0] = NULL;
  805. // may be NULL
  806. delete (aiMatrix4x4*)pcOut->mMeshes[a]->mColors[1];
  807. pcOut->mMeshes[a]->mColors[1] = NULL;
  808. }
  809. }
  810. // ------------------------------------------------------------------------------------------------
  811. void Dot3DSImporter::ConvertScene(aiScene* pcOut)
  812. {
  813. pcOut->mNumMaterials = this->mScene->mMaterials.size();
  814. pcOut->mMaterials = new aiMaterial*[pcOut->mNumMaterials];
  815. for (unsigned int i = 0; i < pcOut->mNumMaterials;++i)
  816. {
  817. MaterialHelper* pcNew = new MaterialHelper();
  818. this->ConvertMaterial(this->mScene->mMaterials[i],*pcNew);
  819. pcOut->mMaterials[i] = pcNew;
  820. }
  821. this->ConvertMeshes(pcOut);
  822. return;
  823. }
  824. #if 0
  825. // ------------------------------------------------------------------------------------------------
  826. void Dot3DSImporter::GenTexCoord (Dot3DS::Texture* pcTexture,
  827. const std::vector<aiVector2D>& p_vIn,
  828. std::vector<aiVector2D>& p_vOut)
  829. {
  830. p_vOut.resize(p_vIn.size());
  831. std::vector<aiVector2D>::const_iterator i = p_vIn.begin();
  832. std::vector<aiVector2D>::iterator a = p_vOut.begin();
  833. for(;i != p_vOut.end();++i,++a)
  834. {
  835. // TODO: Find out in which order 3ds max is performing
  836. // scaling and translation. However it seems reasonable to
  837. // scale first.
  838. //
  839. // TODO: http://www.jalix.org/ressources/graphics/3DS/_specifications/3ds-0.1.htm
  840. // says it is not u and v scale but 1/u and 1/v scale. Other sources
  841. // tell different things. Believe this one, the author seems to be funny
  842. // or drunken or both ;-)
  843. (*a) = (*i);
  844. (*a).x /= pcTexture->mScaleU;
  845. (*a).y /= pcTexture->mScaleV;
  846. (*a).x += pcTexture->mOffsetU;
  847. (*a).y += pcTexture->mOffsetV;
  848. }
  849. return;
  850. }
  851. #endif