2
0

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