3DSConverter.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 "TextureTransform.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // Setup final material indices, generae a default material if necessary
  42. void Discreet3DSImporter::ReplaceDefaultMaterial()
  43. {
  44. // *******************************************************************
  45. // try to find an existing material that matches the
  46. // typical default material setting:
  47. // - no textures
  48. // - diffuse color (in grey!)
  49. // NOTE: This is here to workaround the fact that some
  50. // exporters are writing a default material, too.
  51. // *******************************************************************
  52. unsigned int idx = 0xcdcdcdcd;
  53. for (unsigned int i = 0; i < mScene->mMaterials.size();++i)
  54. {
  55. std::string s = mScene->mMaterials[i].mName;
  56. for (std::string::iterator it = s.begin(); it != s.end(); ++it)
  57. *it = ::tolower(*it);
  58. if (std::string::npos == s.find("default"))continue;
  59. if (mScene->mMaterials[i].mDiffuse.r !=
  60. mScene->mMaterials[i].mDiffuse.g ||
  61. mScene->mMaterials[i].mDiffuse.r !=
  62. mScene->mMaterials[i].mDiffuse.b)continue;
  63. if (mScene->mMaterials[i].sTexDiffuse.mMapName.length() != 0 ||
  64. mScene->mMaterials[i].sTexBump.mMapName.length() != 0 ||
  65. mScene->mMaterials[i].sTexOpacity.mMapName.length() != 0 ||
  66. mScene->mMaterials[i].sTexEmissive.mMapName.length() != 0 ||
  67. mScene->mMaterials[i].sTexSpecular.mMapName.length() != 0 ||
  68. mScene->mMaterials[i].sTexShininess.mMapName.length() != 0 )
  69. {
  70. continue;
  71. }
  72. idx = i;
  73. }
  74. if (0xcdcdcdcd == idx)idx = (unsigned int)mScene->mMaterials.size();
  75. // now iterate through all meshes and through all faces and
  76. // find all faces that are using the default material
  77. unsigned int cnt = 0;
  78. for (std::vector<D3DS::Mesh>::iterator
  79. i = mScene->mMeshes.begin();
  80. i != mScene->mMeshes.end();++i)
  81. {
  82. for (std::vector<unsigned int>::iterator
  83. a = (*i).mFaceMaterials.begin();
  84. a != (*i).mFaceMaterials.end();++a)
  85. {
  86. // NOTE: The additional check seems to be necessary,
  87. // some exporters seem to generate invalid data here
  88. if (0xcdcdcdcd == (*a))
  89. {
  90. (*a) = idx;
  91. ++cnt;
  92. }
  93. else if ( (*a) >= mScene->mMaterials.size())
  94. {
  95. (*a) = idx;
  96. DefaultLogger::get()->warn("Material index overflow in 3DS file. Using default material");
  97. ++cnt;
  98. }
  99. }
  100. }
  101. if (cnt && idx == mScene->mMaterials.size())
  102. {
  103. // We need to create our own default material
  104. D3DS::Material sMat;
  105. sMat.mDiffuse = aiColor3D(0.3f,0.3f,0.3f);
  106. sMat.mName = "%%%DEFAULT";
  107. mScene->mMaterials.push_back(sMat);
  108. DefaultLogger::get()->info("3DS: Generating default material");
  109. }
  110. return;
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // Check whether all indices are valid. Otherwise we'd crash before the validation step was reached
  114. void Discreet3DSImporter::CheckIndices(D3DS::Mesh& sMesh)
  115. {
  116. for (std::vector< D3DS::Face >::iterator
  117. i = sMesh.mFaces.begin();
  118. i != sMesh.mFaces.end();++i)
  119. {
  120. // check whether all indices are in range
  121. for (unsigned int a = 0; a < 3;++a)
  122. {
  123. if ((*i).mIndices[a] >= sMesh.mPositions.size())
  124. {
  125. DefaultLogger::get()->warn("3DS: Vertex index overflow)");
  126. (*i).mIndices[a] = (uint32_t)sMesh.mPositions.size()-1;
  127. }
  128. if ( !sMesh.mTexCoords.empty() && (*i).mIndices[a] >= sMesh.mTexCoords.size())
  129. {
  130. DefaultLogger::get()->warn("3DS: Texture coordinate index overflow)");
  131. (*i).mIndices[a] = (uint32_t)sMesh.mTexCoords.size()-1;
  132. }
  133. }
  134. }
  135. return;
  136. }
  137. // ------------------------------------------------------------------------------------------------
  138. // Generate out unique verbose format representation
  139. void Discreet3DSImporter::MakeUnique(D3DS::Mesh& sMesh)
  140. {
  141. unsigned int iBase = 0;
  142. // Allocate output storage
  143. std::vector<aiVector3D> vNew (sMesh.mFaces.size() * 3);
  144. std::vector<aiVector2D> vNew2;
  145. if (sMesh.mTexCoords.size())vNew2.resize(sMesh.mFaces.size() * 3);
  146. for (unsigned int i = 0; i < sMesh.mFaces.size();++i)
  147. {
  148. uint32_t iTemp1,iTemp2;
  149. // positions
  150. vNew[iBase] = sMesh.mPositions[sMesh.mFaces[i].mIndices[2]];
  151. iTemp1 = iBase++;
  152. vNew[iBase] = sMesh.mPositions[sMesh.mFaces[i].mIndices[1]];
  153. iTemp2 = iBase++;
  154. vNew[iBase] = sMesh.mPositions[sMesh.mFaces[i].mIndices[0]];
  155. // texture coordinates
  156. if (sMesh.mTexCoords.size())
  157. {
  158. vNew2[iTemp1] = sMesh.mTexCoords[sMesh.mFaces[i].mIndices[2]];
  159. vNew2[iTemp2] = sMesh.mTexCoords[sMesh.mFaces[i].mIndices[1]];
  160. vNew2[iBase] = sMesh.mTexCoords[sMesh.mFaces[i].mIndices[0]];
  161. }
  162. sMesh.mFaces[i].mIndices[2] = iBase++;
  163. sMesh.mFaces[i].mIndices[0] = iTemp1;
  164. sMesh.mFaces[i].mIndices[1] = iTemp2;
  165. }
  166. sMesh.mPositions = vNew;
  167. sMesh.mTexCoords = vNew2;
  168. return;
  169. }
  170. // ------------------------------------------------------------------------------------------------
  171. // Convert a 3DS material to an aiMaterial
  172. void Discreet3DSImporter::ConvertMaterial(D3DS::Material& oldMat,
  173. MaterialHelper& mat)
  174. {
  175. // NOTE: Pass the background image to the viewer by bypassing the
  176. // material system. This is an evil hack, never do it again!
  177. if (0 != mBackgroundImage.length() && bHasBG)
  178. {
  179. aiString tex;
  180. tex.Set( mBackgroundImage);
  181. mat.AddProperty( &tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
  182. // be sure this is only done for the first material
  183. mBackgroundImage = std::string("");
  184. }
  185. // At first add the base ambient color of the
  186. // scene to the material
  187. oldMat.mAmbient.r += mClrAmbient.r;
  188. oldMat.mAmbient.g += mClrAmbient.g;
  189. oldMat.mAmbient.b += mClrAmbient.b;
  190. aiString name;
  191. name.Set( oldMat.mName);
  192. mat.AddProperty( &name, AI_MATKEY_NAME);
  193. // material colors
  194. mat.AddProperty( &oldMat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  195. mat.AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  196. mat.AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  197. mat.AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  198. // phong shininess and shininess strength
  199. if (D3DS::Discreet3DS::Phong == oldMat.mShading ||
  200. D3DS::Discreet3DS::Metal == oldMat.mShading)
  201. {
  202. if (!oldMat.mSpecularExponent || !oldMat.mShininessStrength)
  203. {
  204. oldMat.mShading = D3DS::Discreet3DS::Gouraud;
  205. }
  206. else
  207. {
  208. mat.AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  209. mat.AddProperty( &oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  210. }
  211. }
  212. // opacity
  213. mat.AddProperty<float>( &oldMat.mTransparency,1,AI_MATKEY_OPACITY);
  214. // bump height scaling
  215. mat.AddProperty<float>( &oldMat.mBumpHeight,1,AI_MATKEY_BUMPSCALING);
  216. // two sided rendering?
  217. if (oldMat.mTwoSided)
  218. {
  219. int i = 1;
  220. mat.AddProperty<int>(&i,1,AI_MATKEY_TWOSIDED);
  221. }
  222. // shading mode
  223. aiShadingMode eShading = aiShadingMode_NoShading;
  224. switch (oldMat.mShading)
  225. {
  226. case D3DS::Discreet3DS::Flat:
  227. eShading = aiShadingMode_Flat; break;
  228. // I don't know what "Wire" shading should be,
  229. // assume it is simple lambertian diffuse (L dot N) shading
  230. case D3DS::Discreet3DS::Wire:
  231. case D3DS::Discreet3DS::Gouraud:
  232. eShading = aiShadingMode_Gouraud; break;
  233. // assume cook-torrance shading for metals.
  234. case D3DS::Discreet3DS::Phong :
  235. eShading = aiShadingMode_Phong; break;
  236. case D3DS::Discreet3DS::Metal :
  237. eShading = aiShadingMode_CookTorrance; break;
  238. // FIX to workaround a warning with GCC 4 who complained
  239. // about a missing case Blinn: here - Blinn isn't a valid
  240. // value in the 3DS Loader, it is just needed for ASE
  241. case D3DS::Discreet3DS::Blinn :
  242. eShading = aiShadingMode_Blinn; break;
  243. }
  244. mat.AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  245. if (D3DS::Discreet3DS::Wire == oldMat.mShading)
  246. {
  247. // set the wireframe flag
  248. unsigned int iWire = 1;
  249. mat.AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  250. }
  251. // texture, if there is one
  252. // DIFFUSE texture
  253. if( oldMat.sTexDiffuse.mMapName.length() > 0)
  254. {
  255. aiString tex;
  256. tex.Set( oldMat.sTexDiffuse.mMapName);
  257. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  258. if (is_not_qnan(oldMat.sTexDiffuse.mTextureBlend))
  259. mat.AddProperty<float>( &oldMat.sTexDiffuse.mTextureBlend, 1, AI_MATKEY_TEXBLEND_DIFFUSE(0));
  260. if (aiTextureMapMode_Clamp != oldMat.sTexDiffuse.mMapMode)
  261. {
  262. int i = (int)oldMat.sTexSpecular.mMapMode;
  263. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  264. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  265. }
  266. }
  267. // SPECULAR texture
  268. if( oldMat.sTexSpecular.mMapName.length() > 0)
  269. {
  270. aiString tex;
  271. tex.Set( oldMat.sTexSpecular.mMapName);
  272. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(0));
  273. if (is_not_qnan(oldMat.sTexSpecular.mTextureBlend))
  274. mat.AddProperty<float>( &oldMat.sTexSpecular.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SPECULAR(0));
  275. if (aiTextureMapMode_Clamp != oldMat.sTexSpecular.mMapMode)
  276. {
  277. int i = (int)oldMat.sTexSpecular.mMapMode;
  278. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SPECULAR(0));
  279. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SPECULAR(0));
  280. }
  281. }
  282. // OPACITY texture
  283. if( oldMat.sTexOpacity.mMapName.length() > 0)
  284. {
  285. aiString tex;
  286. tex.Set( oldMat.sTexOpacity.mMapName);
  287. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_OPACITY(0));
  288. if (is_not_qnan(oldMat.sTexOpacity.mTextureBlend))
  289. mat.AddProperty<float>( &oldMat.sTexOpacity.mTextureBlend, 1,AI_MATKEY_TEXBLEND_OPACITY(0));
  290. if (aiTextureMapMode_Clamp != oldMat.sTexOpacity.mMapMode)
  291. {
  292. int i = (int)oldMat.sTexOpacity.mMapMode;
  293. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_OPACITY(0));
  294. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_OPACITY(0));
  295. }
  296. }
  297. // EMISSIVE texture
  298. if( oldMat.sTexEmissive.mMapName.length() > 0)
  299. {
  300. aiString tex;
  301. tex.Set( oldMat.sTexEmissive.mMapName);
  302. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(0));
  303. if (is_not_qnan(oldMat.sTexEmissive.mTextureBlend))
  304. mat.AddProperty<float>( &oldMat.sTexEmissive.mTextureBlend, 1, AI_MATKEY_TEXBLEND_EMISSIVE(0));
  305. if (aiTextureMapMode_Clamp != oldMat.sTexEmissive.mMapMode)
  306. {
  307. int i = (int)oldMat.sTexEmissive.mMapMode;
  308. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_EMISSIVE(0));
  309. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_EMISSIVE(0));
  310. }
  311. }
  312. // BUMP texturee
  313. if( oldMat.sTexBump.mMapName.length() > 0)
  314. {
  315. aiString tex;
  316. tex.Set( oldMat.sTexBump.mMapName);
  317. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(0));
  318. if (is_not_qnan(oldMat.sTexBump.mTextureBlend))
  319. mat.AddProperty<float>( &oldMat.sTexBump.mTextureBlend, 1, AI_MATKEY_TEXBLEND_HEIGHT(0));
  320. if (aiTextureMapMode_Clamp != oldMat.sTexBump.mMapMode)
  321. {
  322. int i = (int)oldMat.sTexBump.mMapMode;
  323. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_HEIGHT(0));
  324. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_HEIGHT(0));
  325. }
  326. }
  327. // SHININESS texture
  328. if( oldMat.sTexShininess.mMapName.length() > 0)
  329. {
  330. aiString tex;
  331. tex.Set( oldMat.sTexShininess.mMapName);
  332. mat.AddProperty( &tex, AI_MATKEY_TEXTURE_SHININESS(0));
  333. if (is_not_qnan(oldMat.sTexShininess.mTextureBlend))
  334. mat.AddProperty<float>( &oldMat.sTexShininess.mTextureBlend, 1, AI_MATKEY_TEXBLEND_SHININESS(0));
  335. if (aiTextureMapMode_Clamp != oldMat.sTexShininess.mMapMode)
  336. {
  337. int i = (int)oldMat.sTexShininess.mMapMode;
  338. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_U_SHININESS(0));
  339. mat.AddProperty<int>(&i,1,AI_MATKEY_MAPPINGMODE_V_SHININESS(0));
  340. }
  341. }
  342. // Store the name of the material itself, too
  343. if( oldMat.mName.length())
  344. {
  345. aiString tex;
  346. tex.Set( oldMat.mName);
  347. mat.AddProperty( &tex, AI_MATKEY_NAME);
  348. }
  349. return;
  350. }
  351. // ------------------------------------------------------------------------------------------------
  352. // Split meshes by their materials and generate output aiMesh'es
  353. void Discreet3DSImporter::ConvertMeshes(aiScene* pcOut)
  354. {
  355. std::vector<aiMesh*> avOutMeshes;
  356. avOutMeshes.reserve(mScene->mMeshes.size() * 2);
  357. unsigned int iFaceCnt = 0;
  358. // we need to split all meshes by their materials
  359. for (std::vector<D3DS::Mesh>::iterator i = mScene->mMeshes.begin();
  360. i != mScene->mMeshes.end();++i)
  361. {
  362. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  363. mScene->mMaterials.size()];
  364. unsigned int iNum = 0;
  365. for (std::vector<unsigned int>::const_iterator a = (*i).mFaceMaterials.begin();
  366. a != (*i).mFaceMaterials.end();++a,++iNum)
  367. {
  368. aiSplit[*a].push_back(iNum);
  369. }
  370. // now generate submeshes
  371. for (unsigned int p = 0; p < mScene->mMaterials.size();++p)
  372. {
  373. if (aiSplit[p].size() != 0)
  374. {
  375. aiMesh* p_pcOut = new aiMesh();
  376. p_pcOut->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  377. // be sure to setup the correct material index
  378. p_pcOut->mMaterialIndex = p;
  379. // use the color data as temporary storage
  380. p_pcOut->mColors[0] = (aiColor4D*)(&*i);
  381. avOutMeshes.push_back(p_pcOut);
  382. // convert vertices
  383. p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
  384. p_pcOut->mNumVertices = p_pcOut->mNumFaces*3;
  385. // allocate enough storage for faces
  386. p_pcOut->mFaces = new aiFace[p_pcOut->mNumFaces];
  387. iFaceCnt += p_pcOut->mNumFaces;
  388. if (p_pcOut->mNumVertices)
  389. {
  390. p_pcOut->mVertices = new aiVector3D[p_pcOut->mNumVertices];
  391. p_pcOut->mNormals = new aiVector3D[p_pcOut->mNumVertices];
  392. unsigned int iBase = 0;
  393. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  394. {
  395. unsigned int iIndex = aiSplit[p][q];
  396. p_pcOut->mFaces[q].mIndices = new unsigned int[3];
  397. p_pcOut->mFaces[q].mNumIndices = 3;
  398. p_pcOut->mFaces[q].mIndices[2] = iBase;
  399. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[0]];
  400. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[0]];
  401. p_pcOut->mFaces[q].mIndices[1] = iBase;
  402. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[1]];
  403. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[1]];
  404. p_pcOut->mFaces[q].mIndices[0] = iBase;
  405. p_pcOut->mVertices[iBase] = (*i).mPositions[(*i).mFaces[iIndex].mIndices[2]];
  406. p_pcOut->mNormals[iBase++] = (*i).mNormals[(*i).mFaces[iIndex].mIndices[2]];
  407. }
  408. }
  409. // convert texture coordinates
  410. if ((*i).mTexCoords.size())
  411. {
  412. p_pcOut->mTextureCoords[0] = new aiVector3D[p_pcOut->mNumVertices];
  413. unsigned int iBase = 0;
  414. for (unsigned int q = 0; q < aiSplit[p].size();++q)
  415. {
  416. unsigned int iIndex2 = aiSplit[p][q];
  417. aiVector2D* pc = &(*i).mTexCoords[(*i).mFaces[iIndex2].mIndices[0]];
  418. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc->x,pc->y,0.0f);
  419. pc = &(*i).mTexCoords[(*i).mFaces[iIndex2].mIndices[1]];
  420. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc->x,pc->y,0.0f);
  421. pc = &(*i).mTexCoords[(*i).mFaces[iIndex2].mIndices[2]];
  422. p_pcOut->mTextureCoords[0][iBase++] = aiVector3D(pc->x,pc->y,0.0f);
  423. }
  424. // apply texture coordinate scalings
  425. TextureTransform::BakeScaleNOffset ( p_pcOut, &mScene->mMaterials[
  426. p_pcOut->mMaterialIndex] );
  427. }
  428. }
  429. }
  430. delete[] aiSplit;
  431. }
  432. // Copy them to the output array
  433. pcOut->mNumMeshes = (unsigned int)avOutMeshes.size();
  434. pcOut->mMeshes = new aiMesh*[pcOut->mNumMeshes]();
  435. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  436. pcOut->mMeshes[a] = avOutMeshes[a];
  437. // We should have at least one face here
  438. if (!iFaceCnt)
  439. throw new ImportErrorException("No faces loaded. The mesh is empty");
  440. // for each material in the scene we need to setup the UV source
  441. // set for each texture
  442. for (unsigned int a = 0; a < pcOut->mNumMaterials;++a)
  443. {
  444. TextureTransform::SetupMatUVSrc( pcOut->mMaterials[a], &mScene->mMaterials[a] );
  445. }
  446. return;
  447. }
  448. // ------------------------------------------------------------------------------------------------
  449. // Add a node to the scenegraph and setup its final transformation
  450. void Discreet3DSImporter::AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,D3DS::Node* pcIn,
  451. aiMatrix4x4& absTrafo)
  452. {
  453. std::vector<unsigned int> iArray;
  454. iArray.reserve(3);
  455. aiMatrix4x4 abs;
  456. if (pcIn->mName == "$$$DUMMY")
  457. {
  458. // append the "real" name of the dummy to the string
  459. pcIn->mName.append(pcIn->mDummyName);
  460. }
  461. else // if (pcIn->mName != "$$$DUMMY")
  462. {
  463. for (unsigned int a = 0; a < pcSOut->mNumMeshes;++a)
  464. {
  465. const D3DS::Mesh* pcMesh = (const D3DS::Mesh*)pcSOut->mMeshes[a]->mColors[0];
  466. ai_assert(NULL != pcMesh);
  467. // do case independent comparisons here, just for safety
  468. if (!ASSIMP_stricmp(pcIn->mName,pcMesh->mName))
  469. iArray.push_back(a);
  470. }
  471. if (!iArray.empty())
  472. {
  473. // The matrix should be identical for all meshes.
  474. // It HAS to be identical for all meshes ........
  475. aiMatrix4x4& mTrafo = ((D3DS::Mesh*)pcSOut->mMeshes[iArray[0]]->mColors[0])->mMat;
  476. aiMatrix4x4 mInv = mTrafo;
  477. if (!configSkipPivot)
  478. mInv.Inverse();
  479. /* abs = mTrafo;
  480. pcOut->mTransformation = absTrafo;
  481. pcOut->mTransformation = pcOut->mTransformation.Inverse() * mTrafo;
  482. const aiVector3D& pivot = pcIn->vPivot;
  483. aiMatrix4x4 trans;
  484. */
  485. const aiVector3D& pivot = pcIn->vPivot;
  486. pcOut->mNumMeshes = (unsigned int)iArray.size();
  487. pcOut->mMeshes = new unsigned int[iArray.size()];
  488. for (unsigned int i = 0;i < iArray.size();++i)
  489. {
  490. const unsigned int iIndex = iArray[i];
  491. aiMesh* const mesh = pcSOut->mMeshes[iIndex];
  492. // http://www.zfx.info/DisplayThread.php?MID=235690#235690
  493. const aiVector3D* const pvEnd = mesh->mVertices+mesh->mNumVertices;
  494. aiVector3D* pvCurrent = mesh->mVertices;
  495. if(pivot.x || pivot.y || pivot.z && !configSkipPivot)
  496. {
  497. while (pvCurrent != pvEnd)
  498. {
  499. *pvCurrent = mInv * (*pvCurrent);
  500. pvCurrent->x -= pivot.x;
  501. pvCurrent->y -= pivot.y;
  502. pvCurrent->z -= pivot.z;
  503. *pvCurrent = mTrafo * (*pvCurrent);
  504. ++pvCurrent;
  505. }
  506. }
  507. #if 0
  508. else
  509. {
  510. while (pvCurrent != pvEnd)
  511. {
  512. *pvCurrent = mInv * (*pvCurrent);
  513. ++pvCurrent;
  514. }
  515. }
  516. #endif
  517. // Setup the mesh index
  518. pcOut->mMeshes[i] = iIndex;
  519. }
  520. }
  521. }
  522. // Generate animation channels for the node
  523. if (pcIn->aPositionKeys.size() > 0 || pcIn->aRotationKeys.size() > 0 ||
  524. pcIn->aScalingKeys.size() > 0 || pcIn->aCameraRollKeys.size() > 0 ||
  525. pcIn->aTargetPositionKeys.size() > 0)
  526. {
  527. aiAnimation* anim = pcSOut->mAnimations[0];
  528. ai_assert(NULL != anim);
  529. // Allocate a new channel, increment the channel index
  530. aiNodeAnim* channel = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
  531. // POSITION keys
  532. if (pcIn->aPositionKeys.size() > 0)
  533. {
  534. // Sort all keys with ascending time values
  535. std::sort(pcIn->aPositionKeys.begin(),pcIn->aPositionKeys.end());
  536. channel->mNumPositionKeys = (unsigned int)pcIn->aPositionKeys.size();
  537. channel->mPositionKeys = new aiVectorKey[channel->mNumPositionKeys];
  538. ::memcpy(channel->mPositionKeys,&pcIn->aPositionKeys[0],
  539. sizeof(aiVectorKey)*channel->mNumPositionKeys);
  540. // Get the maximum key
  541. anim->mDuration = std::max(anim->mDuration,channel->
  542. mPositionKeys[channel->mNumPositionKeys-1].mTime);
  543. }
  544. // ROTATION keys
  545. if (pcIn->aRotationKeys.size() > 0)
  546. {
  547. // Sort all keys with ascending time values
  548. std::sort(pcIn->aRotationKeys.begin(),pcIn->aRotationKeys.end());
  549. channel->mNumRotationKeys = (unsigned int)pcIn->aRotationKeys.size();
  550. channel->mRotationKeys = new aiQuatKey[channel->mNumRotationKeys];
  551. ::memcpy(channel->mRotationKeys,&pcIn->aRotationKeys[0],
  552. sizeof(aiQuatKey)*channel->mNumRotationKeys);
  553. // Get the maximum key
  554. anim->mDuration = std::max(anim->mDuration,channel->
  555. mRotationKeys[channel->mNumRotationKeys-1].mTime);
  556. }
  557. // SCALING keys
  558. if (pcIn->aScalingKeys.size() > 0)
  559. {
  560. // Sort all keys with ascending time values
  561. std::sort(pcIn->aScalingKeys.begin(),pcIn->aScalingKeys.end());
  562. channel->mNumScalingKeys = (unsigned int)pcIn->aScalingKeys.size();
  563. channel->mScalingKeys = new aiVectorKey[channel->mNumScalingKeys];
  564. ::memcpy(channel->mScalingKeys,&pcIn->aScalingKeys[0],
  565. sizeof(aiVectorKey)*channel->mNumScalingKeys);
  566. // Get the maximum key
  567. anim->mDuration = std::max(anim->mDuration,channel->
  568. mScalingKeys[channel->mNumScalingKeys-1].mTime);
  569. }
  570. }
  571. // Setup the name of the node
  572. pcOut->mName.Set(pcIn->mName);
  573. // Allocate storage for children
  574. pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size();
  575. pcOut->mChildren = new aiNode*[pcIn->mChildren.size()];
  576. // Recursively process all children
  577. for (unsigned int i = 0; i < pcIn->mChildren.size();++i)
  578. {
  579. pcOut->mChildren[i] = new aiNode();
  580. pcOut->mChildren[i]->mParent = pcOut;
  581. AddNodeToGraph(pcSOut,pcOut->mChildren[i],pcIn->mChildren[i],abs);
  582. }
  583. return;
  584. }
  585. // ------------------------------------------------------------------------------------------------
  586. // Find out how many node animation channels we'll have finally
  587. void CountTracks(D3DS::Node* node, unsigned int& cnt)
  588. {
  589. // We will never generate more than one channel for a node, so
  590. // this is rather easy here.
  591. if (node->aPositionKeys.size() > 0 || node->aRotationKeys.size() > 0 ||
  592. node->aScalingKeys.size() > 0 || node->aCameraRollKeys.size() > 0 ||
  593. node->aTargetPositionKeys.size() > 0)
  594. {
  595. ++cnt;
  596. }
  597. // Recursively process all children
  598. for (unsigned int i = 0; i < node->mChildren.size();++i)
  599. CountTracks(node->mChildren[i],cnt);
  600. }
  601. // ------------------------------------------------------------------------------------------------
  602. // Generate the output node graph
  603. void Discreet3DSImporter::GenerateNodeGraph(aiScene* pcOut)
  604. {
  605. pcOut->mRootNode = new aiNode();
  606. if (0 == mRootNode->mChildren.size())
  607. {
  608. // seems the file has not even a hierarchy.
  609. // generate a flat hiearachy which looks like this:
  610. //
  611. // ROOT_NODE
  612. // |
  613. // ----------------------------------------
  614. // | | | | |
  615. // MESH_0 MESH_1 MESH_2 ... MESH_N CAMERA_0 ....
  616. //
  617. DefaultLogger::get()->warn("No hierarchy information has been found in the file. ");
  618. pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes +
  619. mScene->mCameras.size() + mScene->mLights.size();
  620. pcOut->mRootNode->mChildren = new aiNode* [ pcOut->mRootNode->mNumChildren ];
  621. pcOut->mRootNode->mName.Set("<3DSDummyRoot>");
  622. // Build dummy nodes for all meshes
  623. unsigned int a = 0;
  624. for (unsigned int i = 0; i < pcOut->mNumMeshes;++i,++a)
  625. {
  626. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  627. pcNode->mParent = pcOut->mRootNode;
  628. pcNode->mMeshes = new unsigned int[1];
  629. pcNode->mMeshes[0] = i;
  630. pcNode->mNumMeshes = 1;
  631. // Build a name for the node
  632. pcNode->mName.length = sprintf(pcNode->mName.data,"3DSMesh_%i",i);
  633. }
  634. // Build dummy nodes for all cameras
  635. for (unsigned int i = 0; i < (unsigned int )mScene->mCameras.size();++i,++a)
  636. {
  637. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  638. pcNode->mParent = pcOut->mRootNode;
  639. // Build a name for the node
  640. pcNode->mName = mScene->mCameras[i]->mName;
  641. }
  642. // Build dummy nodes for all lights
  643. for (unsigned int i = 0; i < (unsigned int )mScene->mLights.size();++i,++a)
  644. {
  645. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  646. pcNode->mParent = pcOut->mRootNode;
  647. // Build a name for the node
  648. pcNode->mName = mScene->mLights[i]->mName;
  649. }
  650. }
  651. else
  652. {
  653. // First of all: find out how many scaling, rotation and translation
  654. // animation tracks we'll have afterwards
  655. unsigned int numChannel = 0;
  656. CountTracks(mRootNode,numChannel);
  657. if (numChannel)
  658. {
  659. // Allocate a primary animation channel
  660. pcOut->mNumAnimations = 1;
  661. pcOut->mAnimations = new aiAnimation*[1];
  662. aiAnimation* anim = pcOut->mAnimations[0] = new aiAnimation();
  663. anim->mName.Set("3DSMasterAnim");
  664. // Allocate enough storage for all node animation channels,
  665. // but don't set the mNumChannels member - we'll use it to
  666. // index into the array
  667. anim->mChannels = new aiNodeAnim*[numChannel];
  668. }
  669. aiMatrix4x4 m;
  670. AddNodeToGraph(pcOut, pcOut->mRootNode, mRootNode,m);
  671. }
  672. // We used the first vertex color set to store some
  673. // temporary values, so we need to cleanup here
  674. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  675. pcOut->mMeshes[a]->mColors[0] = NULL;
  676. // if the root node has only one child ... set the child as root node
  677. if (1 == pcOut->mRootNode->mNumChildren)
  678. {
  679. aiNode* pcOld = pcOut->mRootNode;
  680. pcOut->mRootNode = pcOut->mRootNode->mChildren[0];
  681. pcOut->mRootNode->mParent = NULL;
  682. pcOld->mChildren[0] = NULL;
  683. delete pcOld;
  684. }
  685. // if the root node is a default node setup a name for it
  686. if (pcOut->mRootNode->mName.data[0] == '$' && pcOut->mRootNode->mName.data[1] == '$')
  687. pcOut->mRootNode->mName.Set("<root>");
  688. #if 0
  689. // modify the transformation of the root node to change
  690. // the coordinate system of the whole scene from Max' to OpenGL
  691. pcOut->mRootNode->mTransformation.a3 *= -1.f;
  692. pcOut->mRootNode->mTransformation.b3 *= -1.f;
  693. pcOut->mRootNode->mTransformation.c3 *= -1.f;
  694. #endif
  695. }
  696. // ------------------------------------------------------------------------------------------------
  697. // Convert all meshes in the scene and generate the final output scene.
  698. void Discreet3DSImporter::ConvertScene(aiScene* pcOut)
  699. {
  700. // Allocate enough storage for all output materials
  701. pcOut->mNumMaterials = (unsigned int)mScene->mMaterials.size();
  702. pcOut->mMaterials = new aiMaterial*[pcOut->mNumMaterials];
  703. // ... and convert the 3DS materials to aiMaterial's
  704. for (unsigned int i = 0; i < pcOut->mNumMaterials;++i)
  705. {
  706. MaterialHelper* pcNew = new MaterialHelper();
  707. ConvertMaterial(mScene->mMaterials[i],*pcNew);
  708. pcOut->mMaterials[i] = pcNew;
  709. }
  710. // Generate the output mesh list
  711. ConvertMeshes(pcOut);
  712. // Now copy all light sources to the output scene
  713. pcOut->mNumLights = (unsigned int)mScene->mLights.size();
  714. if (pcOut->mNumLights)
  715. {
  716. pcOut->mLights = new aiLight*[pcOut->mNumLights];
  717. ::memcpy(pcOut->mLights,&mScene->mLights[0],sizeof(void*)*pcOut->mNumLights);
  718. }
  719. // Now copy all cameras to the output scene
  720. pcOut->mNumCameras = (unsigned int)mScene->mCameras.size();
  721. if (pcOut->mNumCameras)
  722. {
  723. pcOut->mCameras = new aiCamera*[pcOut->mNumCameras];
  724. ::memcpy(pcOut->mCameras,&mScene->mCameras[0],sizeof(void*)*pcOut->mNumCameras);
  725. }
  726. return;
  727. }