assimpAppMesh.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "ts/collada/colladaExtensions.h"
  24. #include "ts/assimp/assimpAppMesh.h"
  25. #include "ts/assimp/assimpAppNode.h"
  26. // assimp include files.
  27. #include <assimp/cimport.h>
  28. #include <assimp/scene.h>
  29. #include <assimp/postprocess.h>
  30. #include <assimp/types.h>
  31. //------------------------------------------------------------------------------
  32. AssimpAppMesh::AssimpAppMesh(const struct aiMesh* mesh, AssimpAppNode* node)
  33. : mMeshData(mesh), appNode(node)
  34. {
  35. Con::printf("[ASSIMP] Mesh Created: %s", getName());
  36. // See if it's a skinned mesh
  37. mIsSkinMesh = false;
  38. for (U32 b = 0; b < mesh->mNumBones; b++)
  39. if (mMeshData->mBones[b]->mNumWeights > 0)
  40. {
  41. mIsSkinMesh = true;
  42. break;
  43. }
  44. }
  45. const char* AssimpAppMesh::getName(bool allowFixed)
  46. {
  47. // Some exporters add a 'PIVOT' or unnamed node between the mesh and the
  48. // actual object node. Detect this and return the object node name instead
  49. // of the pivot node.
  50. const char* nodeName = appNode->getName();
  51. if ( dStrEqual(nodeName, "null") || dStrEndsWith(nodeName, "PIVOT") )
  52. nodeName = appNode->getParentName();
  53. // If all geometry is being fixed to the same size, append the size
  54. // to the name
  55. //return allowFixed && fixedSizeEnabled ? avar("%s %d", nodeName, fixedSize) : nodeName;
  56. return nodeName;
  57. }
  58. MatrixF AssimpAppMesh::getMeshTransform(F32 time)
  59. {
  60. return appNode->getNodeTransform(time);
  61. }
  62. void AssimpAppMesh::lockMesh(F32 t, const MatrixF& objOffset)
  63. {
  64. // After this function, the following are expected to be populated:
  65. // points, normals, uvs, primitives, indices
  66. // There is also colors and uv2s but those don't seem to be required.
  67. points.reserve(mMeshData->mNumVertices);
  68. uvs.reserve(mMeshData->mNumVertices);
  69. normals.reserve(mMeshData->mNumVertices);
  70. bool noUVFound = false;
  71. for (U32 i = 0; i<mMeshData->mNumVertices; i++)
  72. {
  73. // Points and Normals
  74. aiVector3D pt = mMeshData->mVertices[i];
  75. aiVector3D nrm;
  76. if (mMeshData->HasNormals())
  77. nrm = mMeshData->mNormals[i];
  78. else
  79. nrm.Set(0, 0, 0);
  80. Point3F tmpVert;
  81. Point3F tmpNormal;
  82. tmpVert = Point3F(pt.x, pt.y, pt.z);
  83. tmpNormal = Point3F(nrm.x, nrm.y, nrm.z);
  84. objOffset.mulP(tmpVert);
  85. points.push_back(tmpVert);
  86. if (mMeshData->HasTextureCoords(0))
  87. {
  88. uvs.push_back(Point2F(mMeshData->mTextureCoords[0][i].x, mMeshData->mTextureCoords[0][i].y));
  89. }
  90. else
  91. {
  92. // I don't know if there's any solution to this issue.
  93. // If it's not mapped, it's not mapped.
  94. noUVFound = true;
  95. uvs.push_back(Point2F(1, 1));
  96. }
  97. // UV2s
  98. if (mMeshData->HasTextureCoords(1))
  99. {
  100. uv2s.push_back(Point2F(mMeshData->mTextureCoords[1][i].x, mMeshData->mTextureCoords[1][i].y));
  101. }
  102. // Vertex Colors
  103. if (mMeshData->HasVertexColors(0))
  104. {
  105. LinearColorF vColor(mMeshData->mColors[0][i].r,
  106. mMeshData->mColors[0][i].g,
  107. mMeshData->mColors[0][i].b,
  108. mMeshData->mColors[0][i].a);
  109. colors.push_back(vColor.toColorI());
  110. }
  111. //uvs.push_back(mModel->mVerts[i].texcoord);
  112. normals.push_back(tmpNormal);
  113. //edgeVerts.push_back(mModel->mVerts[i].edge);
  114. }
  115. U32 numFaces = mMeshData->mNumFaces;
  116. //primitives.reserve(numFaces);
  117. //Fetch the number of indices
  118. U32 indicesCount = 0;
  119. for (U32 i = 0; i < numFaces; i++)
  120. {
  121. indicesCount += mMeshData->mFaces[i].mNumIndices;
  122. }
  123. indices.reserve(indicesCount);
  124. // Create TSMesh primitive
  125. primitives.increment();
  126. TSDrawPrimitive& primitive = primitives.last();
  127. primitive.start = 0;
  128. primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | (S32)mMeshData->mMaterialIndex;
  129. primitive.numElements = indicesCount;
  130. for ( U32 n = 0; n < mMeshData->mNumFaces; ++n)
  131. {
  132. const struct aiFace* face = &mMeshData->mFaces[n];
  133. if ( face->mNumIndices == 3 )
  134. {
  135. if (Con::getBoolVariable("$Assimp::FlipNormals", true))
  136. {
  137. U32 indexCount = face->mNumIndices;
  138. for (S32 ind = indexCount - 1; ind >= 0; ind--)
  139. {
  140. U32 index = face->mIndices[ind];
  141. indices.push_back(index);
  142. }
  143. }
  144. else
  145. {
  146. U32 indexCount = face->mNumIndices;
  147. for (U32 ind = 0; ind < indexCount; ind++)
  148. {
  149. U32 index = face->mIndices[ind];
  150. indices.push_back(index);
  151. }
  152. }
  153. }
  154. else
  155. {
  156. Con::printf("[ASSIMP] Non-Triangle Face Found. Indices: %d", face->mNumIndices);
  157. }
  158. }
  159. U32 boneCount = mMeshData->mNumBones;
  160. bones.setSize(boneCount);
  161. // Count the total number of weights for all of the bones.
  162. U32 totalWeights = 0;
  163. U32 nonZeroWeights = 0;
  164. for (U32 b = 0; b < boneCount; b++)
  165. totalWeights += mMeshData->mBones[b]->mNumWeights;
  166. // Assimp gives weights sorted by bone index. We need them in vertex order.
  167. Vector<F32> tmpWeight;
  168. Vector<S32> tmpBoneIndex;
  169. Vector<S32> tmpVertexIndex;
  170. tmpWeight.setSize(totalWeights);
  171. tmpBoneIndex.setSize(totalWeights);
  172. tmpVertexIndex.setSize(totalWeights);
  173. for (U32 b = 0; b < boneCount; b++)
  174. {
  175. String name = mMeshData->mBones[b]->mName.C_Str();
  176. aiNode* nodePtr = AssimpAppNode::findChildNodeByName(mMeshData->mBones[b]->mName.C_Str(), appNode->mScene->mRootNode);
  177. if (!nodePtr)
  178. bones[b] = new AssimpAppNode(appNode->mScene, appNode->mNode);
  179. else
  180. bones[b] = new AssimpAppNode(appNode->mScene, nodePtr);
  181. MatrixF boneTransform;
  182. AssimpAppNode::assimpToTorqueMat(mMeshData->mBones[b]->mOffsetMatrix, boneTransform);
  183. initialTransforms.push_back(boneTransform);
  184. //Weights
  185. U32 numWeights = mMeshData->mBones[b]->mNumWeights;
  186. for (U32 w = 0; w < numWeights; ++w)
  187. {
  188. aiVertexWeight* aiWeight = &mMeshData->mBones[b]->mWeights[w];
  189. if (aiWeight->mWeight > 0.0f)
  190. {
  191. tmpWeight[nonZeroWeights] = aiWeight->mWeight;
  192. tmpVertexIndex[nonZeroWeights] = aiWeight->mVertexId;
  193. tmpBoneIndex[nonZeroWeights] = b;
  194. nonZeroWeights++;
  195. }
  196. }
  197. }
  198. weight.setSize(nonZeroWeights);
  199. vertexIndex.setSize(nonZeroWeights);
  200. boneIndex.setSize(nonZeroWeights);
  201. // Copy the weights to our vectors in vertex order
  202. U32 nextWeight = 0;
  203. for (U32 i = 0; i < mMeshData->mNumVertices; i++)
  204. {
  205. for (U32 ind = 0; ind < nonZeroWeights; ind++)
  206. {
  207. if (tmpVertexIndex[ind] == i)
  208. {
  209. weight[nextWeight] = tmpWeight[ind];
  210. vertexIndex[nextWeight] = tmpVertexIndex[ind];
  211. boneIndex[nextWeight] = tmpBoneIndex[ind];
  212. nextWeight++;
  213. }
  214. }
  215. }
  216. if ( noUVFound )
  217. Con::warnf("[ASSIMP] No UV Data for mesh.");
  218. }
  219. void AssimpAppMesh::lookupSkinData()
  220. { // This function is intentionally left blank. The skin data - bones, weights and indexes are
  221. // processed in lockMesh() with the rest of the mesh data.
  222. }
  223. F32 AssimpAppMesh::getVisValue(F32 t)
  224. {
  225. return 1.0f;
  226. }