3DSConverter.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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 "TargetAnimation.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. // Allocate output storage
  142. std::vector<aiVector3D> vNew (sMesh.mFaces.size() * 3);
  143. std::vector<aiVector3D> vNew2;
  144. if (sMesh.mTexCoords.size())
  145. vNew2.resize(sMesh.mFaces.size() * 3);
  146. for (unsigned int i = 0, base = 0; i < sMesh.mFaces.size();++i)
  147. {
  148. D3DS::Face& face = sMesh.mFaces[i];
  149. // Positions
  150. for (unsigned int a = 0; a < 3;++a,++base)
  151. {
  152. vNew[base] = sMesh.mPositions[face.mIndices[a]];
  153. if (sMesh.mTexCoords.size())
  154. vNew2[base] = sMesh.mTexCoords[face.mIndices[a]];
  155. face.mIndices[a] = base;
  156. }
  157. }
  158. sMesh.mPositions = vNew;
  159. sMesh.mTexCoords = vNew2;
  160. return;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. void CopyTexture(MaterialHelper& mat, D3DS::Texture& texture, aiTextureType type)
  164. {
  165. // Setup the texture name
  166. aiString tex;
  167. tex.Set( texture.mMapName);
  168. mat.AddProperty( &tex, AI_MATKEY_TEXTURE(type,0));
  169. // Setup the texture blend factor
  170. if (is_not_qnan(texture.mTextureBlend))
  171. mat.AddProperty<float>( &texture.mTextureBlend, 1, AI_MATKEY_TEXBLEND(type,0));
  172. // Setup the texture mapping mode
  173. mat.AddProperty<int>((int*)&texture.mMapMode,1,AI_MATKEY_MAPPINGMODE_U(type,0));
  174. mat.AddProperty<int>((int*)&texture.mMapMode,1,AI_MATKEY_MAPPINGMODE_V(type,0));
  175. // Mirroring - double the scaling values
  176. if (texture.mMapMode == aiTextureMapMode_Mirror)
  177. {
  178. texture.mScaleU *= 2.f;
  179. texture.mScaleV *= 2.f;
  180. texture.mOffsetU /= 2.f;
  181. texture.mOffsetV /= 2.f;
  182. }
  183. // Setup texture UV transformations
  184. mat.AddProperty<float>(&texture.mOffsetU,5,AI_MATKEY_UVTRANSFORM(type,0));
  185. }
  186. // ------------------------------------------------------------------------------------------------
  187. // Convert a 3DS material to an aiMaterial
  188. void Discreet3DSImporter::ConvertMaterial(D3DS::Material& oldMat,
  189. MaterialHelper& mat)
  190. {
  191. // NOTE: Pass the background image to the viewer by bypassing the
  192. // material system. This is an evil hack, never do it again!
  193. if (0 != mBackgroundImage.length() && bHasBG)
  194. {
  195. aiString tex;
  196. tex.Set( mBackgroundImage);
  197. mat.AddProperty( &tex, AI_MATKEY_GLOBAL_BACKGROUND_IMAGE);
  198. // Be sure this is only done for the first material
  199. mBackgroundImage = std::string("");
  200. }
  201. // At first add the base ambient color of the scene to the material
  202. oldMat.mAmbient.r += mClrAmbient.r;
  203. oldMat.mAmbient.g += mClrAmbient.g;
  204. oldMat.mAmbient.b += mClrAmbient.b;
  205. aiString name;
  206. name.Set( oldMat.mName);
  207. mat.AddProperty( &name, AI_MATKEY_NAME);
  208. // Material colors
  209. mat.AddProperty( &oldMat.mAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  210. mat.AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  211. mat.AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  212. mat.AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
  213. // Phong shininess and shininess strength
  214. if (D3DS::Discreet3DS::Phong == oldMat.mShading ||
  215. D3DS::Discreet3DS::Metal == oldMat.mShading)
  216. {
  217. if (!oldMat.mSpecularExponent || !oldMat.mShininessStrength)
  218. {
  219. oldMat.mShading = D3DS::Discreet3DS::Gouraud;
  220. }
  221. else
  222. {
  223. mat.AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  224. mat.AddProperty( &oldMat.mShininessStrength, 1, AI_MATKEY_SHININESS_STRENGTH);
  225. }
  226. }
  227. // Opacity
  228. mat.AddProperty<float>( &oldMat.mTransparency,1,AI_MATKEY_OPACITY);
  229. // Bump height scaling
  230. mat.AddProperty<float>( &oldMat.mBumpHeight,1,AI_MATKEY_BUMPSCALING);
  231. // Two sided rendering?
  232. if (oldMat.mTwoSided)
  233. {
  234. int i = 1;
  235. mat.AddProperty<int>(&i,1,AI_MATKEY_TWOSIDED);
  236. }
  237. // Shading mode
  238. aiShadingMode eShading = aiShadingMode_NoShading;
  239. switch (oldMat.mShading)
  240. {
  241. case D3DS::Discreet3DS::Flat:
  242. eShading = aiShadingMode_Flat; break;
  243. // I don't know what "Wire" shading should be,
  244. // assume it is simple lambertian diffuse shading
  245. case D3DS::Discreet3DS::Wire:
  246. {
  247. // Set the wireframe flag
  248. unsigned int iWire = 1;
  249. mat.AddProperty<int>( (int*)&iWire,1,AI_MATKEY_ENABLE_WIREFRAME);
  250. }
  251. case D3DS::Discreet3DS::Gouraud:
  252. eShading = aiShadingMode_Gouraud; break;
  253. // assume cook-torrance shading for metals.
  254. case D3DS::Discreet3DS::Phong :
  255. eShading = aiShadingMode_Phong; break;
  256. case D3DS::Discreet3DS::Metal :
  257. eShading = aiShadingMode_CookTorrance; break;
  258. // FIX to workaround a warning with GCC 4 who complained
  259. // about a missing case Blinn: here - Blinn isn't a valid
  260. // value in the 3DS Loader, it is just needed for ASE
  261. case D3DS::Discreet3DS::Blinn :
  262. eShading = aiShadingMode_Blinn; break;
  263. }
  264. mat.AddProperty<int>( (int*)&eShading,1,AI_MATKEY_SHADING_MODEL);
  265. // DIFFUSE texture
  266. if( oldMat.sTexDiffuse.mMapName.length() > 0)
  267. CopyTexture(mat,oldMat.sTexDiffuse, aiTextureType_DIFFUSE);
  268. // SPECULAR texture
  269. if( oldMat.sTexSpecular.mMapName.length() > 0)
  270. CopyTexture(mat,oldMat.sTexSpecular, aiTextureType_SPECULAR);
  271. // OPACITY texture
  272. if( oldMat.sTexOpacity.mMapName.length() > 0)
  273. CopyTexture(mat,oldMat.sTexOpacity, aiTextureType_OPACITY);
  274. // EMISSIVE texture
  275. if( oldMat.sTexEmissive.mMapName.length() > 0)
  276. CopyTexture(mat,oldMat.sTexEmissive, aiTextureType_EMISSIVE);
  277. // BUMP texture
  278. if( oldMat.sTexBump.mMapName.length() > 0)
  279. CopyTexture(mat,oldMat.sTexBump, aiTextureType_HEIGHT);
  280. // SHININESS texture
  281. if( oldMat.sTexShininess.mMapName.length() > 0)
  282. CopyTexture(mat,oldMat.sTexShininess, aiTextureType_SHININESS);
  283. // Store the name of the material itself, too
  284. if( oldMat.mName.length())
  285. {
  286. aiString tex;
  287. tex.Set( oldMat.mName);
  288. mat.AddProperty( &tex, AI_MATKEY_NAME);
  289. }
  290. return;
  291. }
  292. // ------------------------------------------------------------------------------------------------
  293. // Split meshes by their materials and generate output aiMesh'es
  294. void Discreet3DSImporter::ConvertMeshes(aiScene* pcOut)
  295. {
  296. std::vector<aiMesh*> avOutMeshes;
  297. avOutMeshes.reserve(mScene->mMeshes.size() * 2);
  298. unsigned int iFaceCnt = 0;
  299. // we need to split all meshes by their materials
  300. for (std::vector<D3DS::Mesh>::iterator i = mScene->mMeshes.begin();
  301. i != mScene->mMeshes.end();++i)
  302. {
  303. std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[
  304. mScene->mMaterials.size()];
  305. unsigned int iNum = 0;
  306. for (std::vector<unsigned int>::const_iterator a = (*i).mFaceMaterials.begin();
  307. a != (*i).mFaceMaterials.end();++a,++iNum)
  308. {
  309. aiSplit[*a].push_back(iNum);
  310. }
  311. // now generate submeshes
  312. for (unsigned int p = 0; p < mScene->mMaterials.size();++p)
  313. {
  314. if (aiSplit[p].size())
  315. {
  316. aiMesh* meshOut = new aiMesh();
  317. meshOut->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  318. // be sure to setup the correct material index
  319. meshOut->mMaterialIndex = p;
  320. // use the color data as temporary storage
  321. meshOut->mColors[0] = (aiColor4D*)(&*i);
  322. avOutMeshes.push_back(meshOut);
  323. // convert vertices
  324. meshOut->mNumFaces = (unsigned int)aiSplit[p].size();
  325. meshOut->mNumVertices = meshOut->mNumFaces*3;
  326. // allocate enough storage for faces
  327. meshOut->mFaces = new aiFace[meshOut->mNumFaces];
  328. iFaceCnt += meshOut->mNumFaces;
  329. meshOut->mVertices = new aiVector3D[meshOut->mNumVertices];
  330. meshOut->mNormals = new aiVector3D[meshOut->mNumVertices];
  331. if ((*i).mTexCoords.size())
  332. {
  333. meshOut->mTextureCoords[0] = new aiVector3D[meshOut->mNumVertices];
  334. }
  335. for (unsigned int q = 0, base = 0; q < aiSplit[p].size();++q)
  336. {
  337. register unsigned int index = aiSplit[p][q];
  338. aiFace& face = meshOut->mFaces[q];
  339. face.mIndices = new unsigned int[3];
  340. face.mNumIndices = 3;
  341. for (unsigned int a = 0; a < 3;++a,++base)
  342. {
  343. unsigned int idx = (*i).mFaces[index].mIndices[a];
  344. meshOut->mVertices[base] = (*i).mPositions[idx];
  345. meshOut->mNormals [base] = (*i).mNormals[idx];
  346. if ((*i).mTexCoords.size())
  347. meshOut->mTextureCoords[0][base] = (*i).mTexCoords[idx];
  348. face.mIndices[a] = base;
  349. }
  350. }
  351. }
  352. }
  353. delete[] aiSplit;
  354. }
  355. // Copy them to the output array
  356. pcOut->mNumMeshes = (unsigned int)avOutMeshes.size();
  357. pcOut->mMeshes = new aiMesh*[pcOut->mNumMeshes]();
  358. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  359. pcOut->mMeshes[a] = avOutMeshes[a];
  360. // We should have at least one face here
  361. if (!iFaceCnt)
  362. throw new ImportErrorException("No faces loaded. The mesh is empty");
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. // Add a node to the scenegraph and setup its final transformation
  366. void Discreet3DSImporter::AddNodeToGraph(aiScene* pcSOut,aiNode* pcOut,
  367. D3DS::Node* pcIn, aiMatrix4x4& absTrafo)
  368. {
  369. std::vector<unsigned int> iArray;
  370. iArray.reserve(3);
  371. aiMatrix4x4 abs;
  372. if (pcIn->mName == "$$$DUMMY")
  373. {
  374. // FIX: Append the "real" name of the dummy to the string
  375. pcIn->mName = "Dummy." + pcIn->mDummyName;
  376. }
  377. else // if (pcIn->mName != "$$$DUMMY")
  378. {
  379. // Find all meshes with the same name as the node
  380. for (unsigned int a = 0; a < pcSOut->mNumMeshes;++a)
  381. {
  382. const D3DS::Mesh* pcMesh = (const D3DS::Mesh*)pcSOut->mMeshes[a]->mColors[0];
  383. ai_assert(NULL != pcMesh);
  384. if (pcIn->mName == pcMesh->mName)
  385. iArray.push_back(a);
  386. }
  387. if (!iArray.empty())
  388. {
  389. // The matrix should be identical for all meshes with the
  390. // same name. It HAS to be identical for all meshes .....
  391. aiMatrix4x4 mInv = ((D3DS::Mesh*)pcSOut->mMeshes[iArray[0]]->mColors[0])->mMat;
  392. mInv.Inverse();
  393. const aiVector3D& pivot = pcIn->vPivot;
  394. pcOut->mNumMeshes = (unsigned int)iArray.size();
  395. pcOut->mMeshes = new unsigned int[iArray.size()];
  396. for (unsigned int i = 0;i < iArray.size();++i)
  397. {
  398. const unsigned int iIndex = iArray[i];
  399. aiMesh* const mesh = pcSOut->mMeshes[iIndex];
  400. // Pivot point adjustment
  401. // See: http://www.zfx.info/DisplayThread.php?MID=235690#235690
  402. const aiVector3D* const pvEnd = mesh->mVertices+mesh->mNumVertices;
  403. aiVector3D* pvCurrent = mesh->mVertices;
  404. if(pivot.x || pivot.y || pivot.z)
  405. {
  406. for (;pvCurrent != pvEnd;++pvCurrent)
  407. {
  408. *pvCurrent = mInv * (*pvCurrent);
  409. *pvCurrent -= pivot;
  410. }
  411. }
  412. else
  413. {
  414. for (;pvCurrent != pvEnd;++pvCurrent)
  415. *pvCurrent = mInv * (*pvCurrent);
  416. }
  417. // Setup the mesh index
  418. pcOut->mMeshes[i] = iIndex;
  419. }
  420. }
  421. }
  422. // Now build the transformation matrix of the node
  423. // ROTATION
  424. if (pcIn->aRotationKeys.size())
  425. {
  426. pcOut->mTransformation = aiMatrix4x4( pcIn->aRotationKeys[0].mValue.GetMatrix() );
  427. }
  428. else if (pcIn->aCameraRollKeys.size())
  429. {
  430. aiMatrix4x4::RotationZ(AI_DEG_TO_RAD(- pcIn->aCameraRollKeys[0].mValue),
  431. pcOut->mTransformation);
  432. }
  433. // SCALING
  434. aiMatrix4x4& m = pcOut->mTransformation;
  435. if (pcIn->aScalingKeys.size())
  436. {
  437. const aiVector3D& v = pcIn->aScalingKeys[0].mValue;
  438. m.a1 *= v.x; m.b1 *= v.x; m.c1 *= v.x;
  439. m.a2 *= v.y; m.b2 *= v.y; m.c2 *= v.y;
  440. m.a3 *= v.z; m.b3 *= v.z; m.c3 *= v.z;
  441. }
  442. // TRANSLATION
  443. if (pcIn->aPositionKeys.size())
  444. {
  445. const aiVector3D& v = pcIn->aPositionKeys[0].mValue;
  446. m.a4 += v.x;
  447. m.b4 += v.y;
  448. m.c4 += v.z;
  449. }
  450. // Generate animation channels for the node
  451. if (pcIn->aPositionKeys.size() > 1 || pcIn->aRotationKeys.size() > 1 ||
  452. pcIn->aScalingKeys.size() > 1 || pcIn->aCameraRollKeys.size() > 1 ||
  453. pcIn->aTargetPositionKeys.size() > 1)
  454. {
  455. aiAnimation* anim = pcSOut->mAnimations[0];
  456. ai_assert(NULL != anim);
  457. if (pcIn->aCameraRollKeys.size() > 1)
  458. {
  459. DefaultLogger::get()->debug("3DS: Converting camera roll track ...");
  460. // Camera roll keys - in fact they're just rotations
  461. // around the camera's z axis. The angles are given
  462. // in degrees (and they're clockwise).
  463. pcIn->aRotationKeys.resize(pcIn->aCameraRollKeys.size());
  464. for (unsigned int i = 0; i < pcIn->aCameraRollKeys.size();++i)
  465. {
  466. aiQuatKey& q = pcIn->aRotationKeys[i];
  467. aiFloatKey& f = pcIn->aCameraRollKeys[i];
  468. q.mTime = f.mTime;
  469. q.mValue = aiQuaternion(0.f,0.f,AI_DEG_TO_RAD(- f.mValue));
  470. }
  471. }
  472. //if (pcIn->aTargetPositionKeys.size() > 1)
  473. //{
  474. // DefaultLogger::get()->debug("3DS: Converting target track ...");
  475. // // Camera or spot light - need to convert the separate
  476. // // target position channel to our representation
  477. // TargetAnimationHelper helper;
  478. // if (pcIn->aPositionKeys.empty())
  479. // {
  480. // // We can just pass zero here ...
  481. // helper.SetFixedMainAnimationChannel(aiVector3D());
  482. // }
  483. // else helper.SetMainAnimationChannel(&pcIn->aPositionKeys);
  484. // helper.SetTargetAnimationChannel(&pcIn->aTargetPositionKeys);
  485. // // Do the conversion
  486. // std::vector<aiVectorKey> distanceTrack;
  487. // helper.Process(&distanceTrack);
  488. // // Now add a new node as child, name it <ourName>.Target
  489. // // and assign the distance track to it. This is that the
  490. // // information where the target is and how it moves is
  491. // // not lost
  492. // D3DS::Node* nd = new D3DS::Node();
  493. // pcIn->push_back(nd);
  494. // nd->mName = pcIn->mName + ".Target";
  495. // aiNodeAnim* nda = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
  496. // nda->mNodeName.Set(nd->mName);
  497. // nda->mNumPositionKeys = (unsigned int)distanceTrack.size();
  498. // nda->mPositionKeys = new aiVectorKey[nda->mNumPositionKeys];
  499. // ::memcpy(nda->mPositionKeys,&distanceTrack[0],
  500. // sizeof(aiVectorKey)*nda->mNumPositionKeys);
  501. //}
  502. // Allocate a new nda, increment the nda index
  503. aiNodeAnim* nda = anim->mChannels[anim->mNumChannels++] = new aiNodeAnim();
  504. nda->mNodeName.Set(pcIn->mName);
  505. // POSITION keys
  506. if (pcIn->aPositionKeys.size() > 0)
  507. {
  508. nda->mNumPositionKeys = (unsigned int)pcIn->aPositionKeys.size();
  509. nda->mPositionKeys = new aiVectorKey[nda->mNumPositionKeys];
  510. ::memcpy(nda->mPositionKeys,&pcIn->aPositionKeys[0],
  511. sizeof(aiVectorKey)*nda->mNumPositionKeys);
  512. }
  513. // ROTATION keys
  514. if (pcIn->aRotationKeys.size() > 0)
  515. {
  516. nda->mNumRotationKeys = (unsigned int)pcIn->aRotationKeys.size();
  517. nda->mRotationKeys = new aiQuatKey[nda->mNumRotationKeys];
  518. // Rotations are quaternion offsets
  519. aiQuaternion abs;
  520. for (unsigned int n = 0; n < nda->mNumRotationKeys;++n)
  521. {
  522. const aiQuatKey& q = pcIn->aRotationKeys[n];
  523. abs = (n ? abs * q.mValue : q.mValue);
  524. nda->mRotationKeys[n].mTime = q.mTime;
  525. nda->mRotationKeys[n].mValue = abs.Normalize();
  526. }
  527. }
  528. // SCALING keys
  529. if (pcIn->aScalingKeys.size() > 0)
  530. {
  531. nda->mNumScalingKeys = (unsigned int)pcIn->aScalingKeys.size();
  532. nda->mScalingKeys = new aiVectorKey[nda->mNumScalingKeys];
  533. ::memcpy(nda->mScalingKeys,&pcIn->aScalingKeys[0],
  534. sizeof(aiVectorKey)*nda->mNumScalingKeys);
  535. }
  536. }
  537. // Setup the name of the node
  538. pcOut->mName.Set(pcIn->mName);
  539. // Allocate storage for children
  540. pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size();
  541. pcOut->mChildren = new aiNode*[pcIn->mChildren.size()];
  542. // Recursively process all children
  543. const unsigned int size = pcIn->mChildren.size();
  544. for (unsigned int i = 0; i < size;++i)
  545. {
  546. pcOut->mChildren[i] = new aiNode();
  547. pcOut->mChildren[i]->mParent = pcOut;
  548. AddNodeToGraph(pcSOut,pcOut->mChildren[i],pcIn->mChildren[i],abs);
  549. }
  550. return;
  551. }
  552. // ------------------------------------------------------------------------------------------------
  553. // Find out how many node animation channels we'll have finally
  554. void CountTracks(D3DS::Node* node, unsigned int& cnt)
  555. {
  556. // We will never generate more than one channel for a node, so
  557. // this is rather easy here.
  558. if (node->aPositionKeys.size() > 1 || node->aRotationKeys.size() > 1 ||
  559. node->aScalingKeys.size() > 1 || node->aCameraRollKeys.size() > 1 ||
  560. node->aTargetPositionKeys.size() > 1)
  561. {
  562. ++cnt;
  563. if (node->aTargetPositionKeys.size() > 1)++cnt;
  564. }
  565. // Recursively process all children
  566. for (unsigned int i = 0; i < node->mChildren.size();++i)
  567. CountTracks(node->mChildren[i],cnt);
  568. }
  569. // ------------------------------------------------------------------------------------------------
  570. // Generate the output node graph
  571. void Discreet3DSImporter::GenerateNodeGraph(aiScene* pcOut)
  572. {
  573. pcOut->mRootNode = new aiNode();
  574. if (0 == mRootNode->mChildren.size())
  575. {
  576. // seems the file has not even a hierarchy.
  577. // generate a flat hiearachy which looks like this:
  578. //
  579. // ROOT_NODE
  580. // |
  581. // ----------------------------------------
  582. // | | | | |
  583. // MESH_0 MESH_1 MESH_2 ... MESH_N CAMERA_0 ....
  584. //
  585. DefaultLogger::get()->warn("No hierarchy information has been found in the file. ");
  586. pcOut->mRootNode->mNumChildren = pcOut->mNumMeshes +
  587. mScene->mCameras.size() + mScene->mLights.size();
  588. pcOut->mRootNode->mChildren = new aiNode* [ pcOut->mRootNode->mNumChildren ];
  589. pcOut->mRootNode->mName.Set("<3DSDummyRoot>");
  590. // Build dummy nodes for all meshes
  591. unsigned int a = 0;
  592. for (unsigned int i = 0; i < pcOut->mNumMeshes;++i,++a)
  593. {
  594. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  595. pcNode->mParent = pcOut->mRootNode;
  596. pcNode->mMeshes = new unsigned int[1];
  597. pcNode->mMeshes[0] = i;
  598. pcNode->mNumMeshes = 1;
  599. // Build a name for the node
  600. pcNode->mName.length = sprintf(pcNode->mName.data,"3DSMesh_%i",i);
  601. }
  602. // Build dummy nodes for all cameras
  603. for (unsigned int i = 0; i < (unsigned int )mScene->mCameras.size();++i,++a)
  604. {
  605. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  606. pcNode->mParent = pcOut->mRootNode;
  607. // Build a name for the node
  608. pcNode->mName = mScene->mCameras[i]->mName;
  609. }
  610. // Build dummy nodes for all lights
  611. for (unsigned int i = 0; i < (unsigned int )mScene->mLights.size();++i,++a)
  612. {
  613. aiNode* pcNode = pcOut->mRootNode->mChildren[a] = new aiNode();
  614. pcNode->mParent = pcOut->mRootNode;
  615. // Build a name for the node
  616. pcNode->mName = mScene->mLights[i]->mName;
  617. }
  618. }
  619. else
  620. {
  621. // First of all: find out how many scaling, rotation and translation
  622. // animation tracks we'll have afterwards
  623. unsigned int numChannel = 0;
  624. CountTracks(mRootNode,numChannel);
  625. if (numChannel)
  626. {
  627. // Allocate a primary animation channel
  628. pcOut->mNumAnimations = 1;
  629. pcOut->mAnimations = new aiAnimation*[1];
  630. aiAnimation* anim = pcOut->mAnimations[0] = new aiAnimation();
  631. anim->mName.Set("3DSMasterAnim");
  632. // Allocate enough storage for all node animation channels,
  633. // but don't set the mNumChannels member - we'll use it to
  634. // index into the array
  635. anim->mChannels = new aiNodeAnim*[numChannel];
  636. }
  637. aiMatrix4x4 m;
  638. AddNodeToGraph(pcOut, pcOut->mRootNode, mRootNode,m);
  639. // If the root node is unnamed name it "<3DSRoot>"
  640. if (::strstr( pcOut->mRootNode->mName.data, "UNNAMED" )
  641. || pcOut->mRootNode->mName.data[0] == '$'
  642. && pcOut->mRootNode->mName.data[1] == '$')
  643. {
  644. pcOut->mRootNode->mName.Set("<3DSRoot>");
  645. }
  646. }
  647. // We used the first vertex color set to store some
  648. // temporary values, so we need to cleanup here
  649. for (unsigned int a = 0; a < pcOut->mNumMeshes;++a)
  650. pcOut->mMeshes[a]->mColors[0] = NULL;
  651. // if the root node has only one child ... set the child as root node
  652. if (1 == pcOut->mRootNode->mNumChildren)
  653. {
  654. aiNode* pcOld = pcOut->mRootNode;
  655. pcOut->mRootNode = pcOut->mRootNode->mChildren[0];
  656. pcOut->mRootNode->mParent = NULL;
  657. pcOld->mChildren[0] = NULL;
  658. delete pcOld;
  659. }
  660. }
  661. // ------------------------------------------------------------------------------------------------
  662. // Convert all meshes in the scene and generate the final output scene.
  663. void Discreet3DSImporter::ConvertScene(aiScene* pcOut)
  664. {
  665. // Allocate enough storage for all output materials
  666. pcOut->mNumMaterials = (unsigned int)mScene->mMaterials.size();
  667. pcOut->mMaterials = new aiMaterial*[pcOut->mNumMaterials];
  668. // ... and convert the 3DS materials to aiMaterial's
  669. for (unsigned int i = 0; i < pcOut->mNumMaterials;++i)
  670. {
  671. MaterialHelper* pcNew = new MaterialHelper();
  672. ConvertMaterial(mScene->mMaterials[i],*pcNew);
  673. pcOut->mMaterials[i] = pcNew;
  674. }
  675. // Generate the output mesh list
  676. ConvertMeshes(pcOut);
  677. // Now copy all light sources to the output scene
  678. pcOut->mNumLights = (unsigned int)mScene->mLights.size();
  679. if (pcOut->mNumLights)
  680. {
  681. pcOut->mLights = new aiLight*[pcOut->mNumLights];
  682. ::memcpy(pcOut->mLights,&mScene->mLights[0],sizeof(void*)*pcOut->mNumLights);
  683. }
  684. // Now copy all cameras to the output scene
  685. pcOut->mNumCameras = (unsigned int)mScene->mCameras.size();
  686. if (pcOut->mNumCameras)
  687. {
  688. pcOut->mCameras = new aiCamera*[pcOut->mNumCameras];
  689. ::memcpy(pcOut->mCameras,&mScene->mCameras[0],sizeof(void*)*pcOut->mNumCameras);
  690. }
  691. return;
  692. }