PretransformVertices.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "PretransformVertices" post processing step
  35. */
  36. #include "PretransformVertices.h"
  37. #include "../include/DefaultLogger.h"
  38. #include "../include/aiPostProcess.h"
  39. #include "../include/aiMesh.h"
  40. #include "../include/aiScene.h"
  41. #include "../include/aiAssert.h"
  42. #include <list>
  43. using namespace Assimp;
  44. // Constructor to be privately used by Importer
  45. PretransformVertices::PretransformVertices()
  46. {
  47. }
  48. // Destructor, private as well
  49. PretransformVertices::~PretransformVertices()
  50. {
  51. // nothing to do here
  52. }
  53. // ------------------------------------------------------------------------------------------------
  54. // Returns whether the processing step is present in the given flag field.
  55. bool PretransformVertices::IsActive( unsigned int pFlags) const
  56. {
  57. return (pFlags & aiProcess_PreTransformVertices) != 0;
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // Count the number of nodes
  61. unsigned int CountNodes( aiNode* pcNode )
  62. {
  63. unsigned int iRet = 1;
  64. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  65. {
  66. iRet += CountNodes(pcNode->mChildren[i]);
  67. }
  68. return iRet;
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. // Get a bitwise combination identifying the vertex format of a mesh
  72. unsigned int GetMeshVFormat(aiMesh* pcMesh)
  73. {
  74. if (0xdeadbeef == pcMesh->mNumUVComponents[0])
  75. return pcMesh->mNumUVComponents[1];
  76. unsigned int iRet = 0;
  77. // normals
  78. if (pcMesh->HasNormals())iRet |= 0x1;
  79. // tangents and bitangents
  80. if (pcMesh->HasTangentsAndBitangents())iRet |= 0x2;
  81. // texture coordinates
  82. unsigned int p = 0;
  83. ai_assert(4 >= AI_MAX_NUMBER_OF_TEXTURECOORDS);
  84. while (pcMesh->HasTextureCoords(p))
  85. {
  86. iRet |= (0x100 << p++);
  87. if (3 == pcMesh->mNumUVComponents[p])
  88. iRet |= (0x1000 << p++);
  89. }
  90. // vertex colors
  91. p = 0;
  92. while (pcMesh->HasVertexColors(p))iRet |= (0x10000 << p++);
  93. // store the value for later use
  94. pcMesh->mNumUVComponents[0] = 0xdeadbeef;
  95. pcMesh->mNumUVComponents[1] = iRet;
  96. return iRet;
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. // Count the number of vertices in the whole scene and a given
  100. // material index
  101. void CountVerticesAndFaces( aiScene* pcScene, aiNode* pcNode, unsigned int iMat,
  102. unsigned int iVFormat, unsigned int* piFaces, unsigned int* piVertices)
  103. {
  104. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i)
  105. {
  106. aiMesh* pcMesh = pcScene->mMeshes[ pcNode->mMeshes[i] ];
  107. if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh))
  108. {
  109. *piVertices += pcMesh->mNumVertices;
  110. *piFaces += pcMesh->mNumFaces;
  111. }
  112. }
  113. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  114. {
  115. CountVerticesAndFaces(pcScene,pcNode->mChildren[i],iMat,
  116. iVFormat,piFaces,piVertices);
  117. }
  118. return;
  119. }
  120. #define AI_PTVS_VERTEX 0x0
  121. #define AI_PTVS_FACE 0x1
  122. // ------------------------------------------------------------------------------------------------
  123. // Collect vertex/face data
  124. void CollectData( aiScene* pcScene, aiNode* pcNode, unsigned int iMat,
  125. unsigned int iVFormat, aiMesh* pcMeshOut,
  126. unsigned int aiCurrent[2])
  127. {
  128. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i)
  129. {
  130. aiMesh* pcMesh = pcScene->mMeshes[ pcNode->mMeshes[i] ];
  131. if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh))
  132. {
  133. // copy positions, transform them to worldspace
  134. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  135. {
  136. pcMeshOut->mVertices[aiCurrent[AI_PTVS_VERTEX]+n] =
  137. pcNode->mTransformation * pcMesh->mVertices[n];
  138. }
  139. if (iVFormat & 0x1)
  140. {
  141. aiMatrix4x4 mWorldIT = pcNode->mTransformation;
  142. mWorldIT.Inverse().Transpose();
  143. // copy normals, transform them to worldspace
  144. for (unsigned int n = 0; n < pcMesh->mNumVertices;++n)
  145. {
  146. pcMeshOut->mNormals[aiCurrent[AI_PTVS_VERTEX]+n] =
  147. mWorldIT * pcMesh->mNormals[n];
  148. }
  149. }
  150. if (iVFormat & 0x2)
  151. {
  152. // copy tangents
  153. memcpy(pcMeshOut->mTangents + aiCurrent[AI_PTVS_VERTEX],
  154. pcMesh->mTangents,
  155. pcMesh->mNumVertices * sizeof(aiVector3D));
  156. // copy bitangents
  157. memcpy(pcMeshOut->mBitangents + aiCurrent[AI_PTVS_VERTEX],
  158. pcMesh->mBitangents,
  159. pcMesh->mNumVertices * sizeof(aiVector3D));
  160. }
  161. unsigned int p = 0;
  162. while (iVFormat & (0x100 << p))
  163. {
  164. // copy texture coordinates
  165. memcpy(pcMeshOut->mTextureCoords[p] + aiCurrent[AI_PTVS_VERTEX],
  166. pcMesh->mTextureCoords[p],
  167. pcMesh->mNumVertices * sizeof(aiVector3D));
  168. ++p;
  169. }
  170. p = 0;
  171. while (iVFormat & (0x10000 << p))
  172. {
  173. // copy vertex colors
  174. memcpy(pcMeshOut->mColors[p] + aiCurrent[AI_PTVS_VERTEX],
  175. pcMesh->mColors[p],
  176. pcMesh->mNumVertices * sizeof(aiColor4D));
  177. ++p;
  178. }
  179. // now we need to copy all faces
  180. // since we will delete the source mesh afterwards,
  181. // we don't need to reallocate the array of indices
  182. for (unsigned int planck = 0;planck<pcMesh->mNumFaces;++planck)
  183. {
  184. pcMeshOut->mFaces[aiCurrent[AI_PTVS_FACE]+planck].mNumIndices =
  185. pcMesh->mFaces[planck].mNumIndices;
  186. unsigned int* pi = pcMeshOut->mFaces[aiCurrent[AI_PTVS_FACE]+planck].
  187. mIndices = pcMesh->mFaces[planck].mIndices;
  188. // offset all vrtex indices
  189. for (unsigned int hahn = 0; hahn < pcMesh->mFaces[planck].mNumIndices;++hahn)
  190. {
  191. pi[hahn] += aiCurrent[AI_PTVS_VERTEX];
  192. }
  193. // just make sure the array won't be deleted by the
  194. // aiFace destructor ...
  195. pcMesh->mFaces[planck].mIndices = NULL;
  196. }
  197. aiCurrent[AI_PTVS_VERTEX] += pcMesh->mNumVertices;
  198. aiCurrent[AI_PTVS_FACE] += pcMesh->mNumFaces;
  199. }
  200. }
  201. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  202. {
  203. CollectData(pcScene,pcNode->mChildren[i],iMat,
  204. iVFormat,pcMeshOut,aiCurrent);
  205. }
  206. return;
  207. }
  208. // ------------------------------------------------------------------------------------------------
  209. // Get a list of all vertex formats that occur for a given material index
  210. // The output list contains duplicate elements
  211. void GetVFormatList( aiScene* pcScene, aiNode* pcNode, unsigned int iMat,
  212. std::list<unsigned int>& aiOut)
  213. {
  214. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i)
  215. {
  216. aiMesh* pcMesh = pcScene->mMeshes[ pcNode->mMeshes[i] ];
  217. if (iMat == pcMesh->mMaterialIndex)
  218. {
  219. aiOut.push_back(GetMeshVFormat(pcMesh));
  220. }
  221. }
  222. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  223. {
  224. GetVFormatList(pcScene,pcNode->mChildren[i],iMat,aiOut);
  225. }
  226. return;
  227. }
  228. // ------------------------------------------------------------------------------------------------
  229. // Compute the absolute transformation matrices of each node
  230. void ComputeAbsoluteTransform( aiNode* pcNode )
  231. {
  232. if (pcNode->mParent)
  233. {
  234. pcNode->mTransformation = pcNode->mTransformation*pcNode->mParent->mTransformation;
  235. }
  236. for (unsigned int i = 0;i < pcNode->mNumChildren;++i)
  237. {
  238. ComputeAbsoluteTransform(pcNode->mChildren[i]);
  239. }
  240. return;
  241. }
  242. // ------------------------------------------------------------------------------------------------
  243. // Executes the post processing step on the given imported data.
  244. void PretransformVertices::Execute( aiScene* pScene)
  245. {
  246. DefaultLogger::get()->debug("PretransformVerticesProcess begin");
  247. // first compute absolute transformation matrices for all nodes
  248. ComputeAbsoluteTransform(pScene->mRootNode);
  249. // now build a list of output meshes
  250. std::vector<aiMesh*> apcOutMeshes;
  251. apcOutMeshes.reserve(pScene->mNumMaterials*2);
  252. std::list<unsigned int> aiVFormats;
  253. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  254. {
  255. // get the list of all vertex formats for this material
  256. aiVFormats.clear();
  257. GetVFormatList(pScene,pScene->mRootNode,i,aiVFormats);
  258. aiVFormats.sort(std::less<unsigned int>());
  259. aiVFormats.unique();
  260. for (std::list<unsigned int>::const_iterator
  261. j = aiVFormats.begin();
  262. j != aiVFormats.end();++j)
  263. {
  264. unsigned int iVertices = 0;
  265. unsigned int iFaces = 0;
  266. CountVerticesAndFaces(pScene,pScene->mRootNode,i,*j,&iFaces,&iVertices);
  267. if (iFaces && iVertices)
  268. {
  269. apcOutMeshes.push_back(new aiMesh());
  270. aiMesh* pcMesh = apcOutMeshes.back();
  271. pcMesh->mNumFaces = iFaces;
  272. pcMesh->mNumVertices = iVertices;
  273. pcMesh->mFaces = new aiFace[iFaces];
  274. pcMesh->mVertices = new aiVector3D[iVertices];
  275. pcMesh->mMaterialIndex = i;
  276. if ((*j) & 0x1)pcMesh->mNormals = new aiVector3D[iVertices];
  277. if ((*j) & 0x2)
  278. {
  279. pcMesh->mTangents = new aiVector3D[iVertices];
  280. pcMesh->mBitangents = new aiVector3D[iVertices];
  281. }
  282. iFaces = 0;
  283. while ((*j) & (0x100 << iFaces))
  284. {
  285. pcMesh->mTextureCoords[iFaces] = new aiVector3D[iVertices];
  286. if ((*j) & (0x1000 << iFaces))pcMesh->mNumUVComponents[iFaces] = 3;
  287. else pcMesh->mNumUVComponents[iFaces] = 2;
  288. iFaces++;
  289. }
  290. iFaces = 0;
  291. while ((*j) & (0x10000 << iFaces))
  292. pcMesh->mColors[iFaces] = new aiColor4D[iVertices];
  293. // fill the mesh ...
  294. unsigned int aiTemp[2] = {0,0};
  295. CollectData(pScene,pScene->mRootNode,i,*j,pcMesh,aiTemp);
  296. }
  297. }
  298. }
  299. // remove all animations from the scene
  300. for (unsigned int i = 0; i < pScene->mNumAnimations;++i)
  301. delete pScene->mAnimations[i];
  302. pScene->mAnimations = NULL;
  303. pScene->mNumAnimations = 0;
  304. // now delete all meshes in the scene and build a new mesh list
  305. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  306. delete pScene->mMeshes[i];
  307. if (apcOutMeshes.size() != pScene->mNumMeshes)
  308. {
  309. delete[] pScene->mMeshes;
  310. pScene->mNumMeshes = apcOutMeshes.size();
  311. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  312. }
  313. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  314. pScene->mMeshes[i] = apcOutMeshes[i];
  315. // now delete all nodes in the scene and build a new
  316. // flat node graph with a root node and some level 1 children
  317. delete pScene->mRootNode;
  318. pScene->mRootNode = new aiNode();
  319. pScene->mRootNode->mName.Set("<dummy_root>");
  320. if (1 == pScene->mNumMeshes)
  321. {
  322. pScene->mRootNode->mNumMeshes = 1;
  323. pScene->mRootNode->mMeshes = new unsigned int[1];
  324. pScene->mRootNode->mMeshes[0] = 0;
  325. }
  326. else
  327. {
  328. pScene->mRootNode->mNumChildren = pScene->mNumMeshes;
  329. pScene->mRootNode->mChildren = new aiNode*[pScene->mNumMeshes];
  330. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  331. {
  332. aiNode* pcNode = pScene->mRootNode->mChildren[i] = new aiNode();
  333. pcNode->mName.length = sprintf(pcNode->mName.data,"dummy_%i",i);
  334. pcNode->mNumMeshes = 1;
  335. pcNode->mMeshes = new unsigned int[1];
  336. pcNode->mMeshes[0] = i;
  337. pcNode->mParent = pScene->mRootNode;
  338. }
  339. }
  340. DefaultLogger::get()->debug("PretransformVerticesProcess finished. All "
  341. "vertices are in worldspace now");
  342. return;
  343. }