assimpAppMesh.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. bool AssimpAppMesh::fixedSizeEnabled = false;
  32. S32 AssimpAppMesh::fixedSize = 2;
  33. //------------------------------------------------------------------------------
  34. AssimpAppMesh::AssimpAppMesh(const struct aiMesh* mesh, AssimpAppNode* node)
  35. : mMeshData(mesh), appNode(node)
  36. {
  37. Con::printf("[ASSIMP] Mesh Created: %s", getName());
  38. // See if it's a skinned mesh
  39. mIsSkinMesh = false;
  40. for (U32 b = 0; b < mesh->mNumBones; b++)
  41. if (mMeshData->mBones[b]->mNumWeights > 0)
  42. {
  43. mIsSkinMesh = true;
  44. break;
  45. }
  46. }
  47. const char* AssimpAppMesh::getName(bool allowFixed)
  48. {
  49. // Some exporters add a 'PIVOT' or unnamed node between the mesh and the
  50. // actual object node. Detect this and return the object node name instead
  51. // of the pivot node.
  52. const char* nodeName = appNode->getName();
  53. if ( dStrEqual(nodeName, "null") || dStrEndsWith(nodeName, "PIVOT") )
  54. nodeName = appNode->getParentName();
  55. // If all geometry is being fixed to the same size, append the size
  56. // to the name
  57. return allowFixed && fixedSizeEnabled ? avar("%s %d", nodeName, fixedSize) : nodeName;
  58. }
  59. MatrixF AssimpAppMesh::getMeshTransform(F32 time)
  60. {
  61. return appNode->getNodeTransform(time);
  62. }
  63. void AssimpAppMesh::lockMesh(F32 t, const MatrixF& objOffset)
  64. {
  65. // After this function, the following are expected to be populated:
  66. // points, normals, uvs, primitives, indices
  67. // There is also colors and uv2s but those don't seem to be required.
  68. points.reserve(mMeshData->mNumVertices);
  69. uvs.reserve(mMeshData->mNumVertices);
  70. normals.reserve(mMeshData->mNumVertices);
  71. bool flipNormals = ColladaUtils::getOptions().invertNormals;
  72. bool noUVFound = false;
  73. for (U32 i = 0; i<mMeshData->mNumVertices; i++)
  74. {
  75. // Points and Normals
  76. aiVector3D pt = mMeshData->mVertices[i];
  77. aiVector3D nrm;
  78. if (mMeshData->HasNormals())
  79. nrm = mMeshData->mNormals[i];
  80. else
  81. nrm.Set(0, 0, 0);
  82. Point3F tmpVert;
  83. Point3F tmpNormal;
  84. tmpVert = Point3F(pt.x, pt.y, pt.z);
  85. tmpNormal = Point3F(nrm.x, nrm.y, nrm.z);
  86. if (flipNormals)
  87. tmpNormal *= -1.0f;
  88. objOffset.mulP(tmpVert);
  89. points.push_back(tmpVert);
  90. if (mMeshData->HasTextureCoords(0))
  91. {
  92. uvs.push_back(Point2F(mMeshData->mTextureCoords[0][i].x, mMeshData->mTextureCoords[0][i].y));
  93. }
  94. else
  95. {
  96. // I don't know if there's any solution to this issue.
  97. // If it's not mapped, it's not mapped.
  98. noUVFound = true;
  99. uvs.push_back(Point2F(1, 1));
  100. }
  101. // UV2s
  102. if (mMeshData->HasTextureCoords(1))
  103. {
  104. uv2s.push_back(Point2F(mMeshData->mTextureCoords[1][i].x, mMeshData->mTextureCoords[1][i].y));
  105. }
  106. // Vertex Colors
  107. if (mMeshData->HasVertexColors(0))
  108. {
  109. LinearColorF vColor(mMeshData->mColors[0][i].r,
  110. mMeshData->mColors[0][i].g,
  111. mMeshData->mColors[0][i].b,
  112. mMeshData->mColors[0][i].a);
  113. colors.push_back(vColor.toColorI());
  114. }
  115. //uvs.push_back(mModel->mVerts[i].texcoord);
  116. normals.push_back(tmpNormal);
  117. //edgeVerts.push_back(mModel->mVerts[i].edge);
  118. }
  119. U32 numFaces = mMeshData->mNumFaces;
  120. //primitives.reserve(numFaces);
  121. //Fetch the number of indices
  122. U32 indicesCount = 0;
  123. for (U32 i = 0; i < numFaces; i++)
  124. {
  125. indicesCount += mMeshData->mFaces[i].mNumIndices;
  126. }
  127. indices.reserve(indicesCount);
  128. // Create TSMesh primitive
  129. primitives.increment();
  130. TSDrawPrimitive& primitive = primitives.last();
  131. primitive.start = 0;
  132. primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | (S32)mMeshData->mMaterialIndex;
  133. primitive.numElements = indicesCount;
  134. for ( U32 n = 0; n < mMeshData->mNumFaces; ++n)
  135. {
  136. const struct aiFace* face = &mMeshData->mFaces[n];
  137. if ( face->mNumIndices == 3 )
  138. {
  139. U32 indexCount = face->mNumIndices;
  140. for (U32 ind = 0; ind < indexCount; ind++)
  141. {
  142. U32 index = face->mIndices[ind];
  143. indices.push_back(index);
  144. }
  145. }
  146. else
  147. {
  148. Con::printf("[ASSIMP] Non-Triangle Face Found. Indices: %d", face->mNumIndices);
  149. }
  150. }
  151. U32 boneCount = mMeshData->mNumBones;
  152. bones.setSize(boneCount);
  153. // Count the total number of weights for all of the bones.
  154. U32 totalWeights = 0;
  155. U32 nonZeroWeights = 0;
  156. for (U32 b = 0; b < boneCount; b++)
  157. totalWeights += mMeshData->mBones[b]->mNumWeights;
  158. // Assimp gives weights sorted by bone index. We need them in vertex order.
  159. Vector<F32> tmpWeight;
  160. Vector<S32> tmpBoneIndex;
  161. Vector<S32> tmpVertexIndex;
  162. tmpWeight.setSize(totalWeights);
  163. tmpBoneIndex.setSize(totalWeights);
  164. tmpVertexIndex.setSize(totalWeights);
  165. for (U32 b = 0; b < boneCount; b++)
  166. {
  167. String name = mMeshData->mBones[b]->mName.C_Str();
  168. aiNode* nodePtr = AssimpAppNode::findChildNodeByName(mMeshData->mBones[b]->mName.C_Str(), appNode->mScene->mRootNode);
  169. if (!nodePtr)
  170. bones[b] = new AssimpAppNode(appNode->mScene, appNode->mNode);
  171. else
  172. bones[b] = new AssimpAppNode(appNode->mScene, nodePtr);
  173. MatrixF boneTransform;
  174. AssimpAppNode::assimpToTorqueMat(mMeshData->mBones[b]->mOffsetMatrix, boneTransform);
  175. Point3F boneScale = boneTransform.getScale();
  176. Point3F bonePos = boneTransform.getPosition();
  177. if (boneScale != Point3F::One && ColladaUtils::getOptions().ignoreNodeScale)
  178. {
  179. Point3F scaleMult = Point3F::One / boneScale;
  180. boneTransform.scale(scaleMult);
  181. bonePos /= scaleMult;
  182. }
  183. bonePos *= ColladaUtils::getOptions().unit * ColladaUtils::getOptions().formatScaleFactor;
  184. boneTransform.setPosition(bonePos);
  185. initialTransforms.push_back(boneTransform);
  186. //Weights
  187. U32 numWeights = mMeshData->mBones[b]->mNumWeights;
  188. for (U32 w = 0; w < numWeights; ++w)
  189. {
  190. aiVertexWeight* aiWeight = &mMeshData->mBones[b]->mWeights[w];
  191. if (aiWeight->mWeight > 0.0f)
  192. {
  193. tmpWeight[nonZeroWeights] = aiWeight->mWeight;
  194. tmpVertexIndex[nonZeroWeights] = aiWeight->mVertexId;
  195. tmpBoneIndex[nonZeroWeights] = b;
  196. nonZeroWeights++;
  197. }
  198. }
  199. }
  200. weight.setSize(nonZeroWeights);
  201. vertexIndex.setSize(nonZeroWeights);
  202. boneIndex.setSize(nonZeroWeights);
  203. // Copy the weights to our vectors in vertex order and
  204. // normalize vertex weights (force weights for each vert to sum to 1)
  205. U32 nextWeight = 0;
  206. for (U32 i = 0; i < mMeshData->mNumVertices; ++i)
  207. {
  208. U32 vertStart = nextWeight;
  209. F32 invTotalWeight = 0;
  210. for (U32 ind = 0; ind < nonZeroWeights; ++ind)
  211. {
  212. if (tmpVertexIndex[ind] == i)
  213. {
  214. weight[nextWeight] = tmpWeight[ind];
  215. invTotalWeight += tmpWeight[ind];
  216. vertexIndex[nextWeight] = tmpVertexIndex[ind];
  217. boneIndex[nextWeight] = tmpBoneIndex[ind];
  218. nextWeight++;
  219. }
  220. }
  221. // Now normalize the vertex weights
  222. if (invTotalWeight > 0.0)
  223. {
  224. invTotalWeight = 1.0f / invTotalWeight;
  225. for (U32 ind = vertStart; ind < nextWeight; ++ind)
  226. weight[ind] *= invTotalWeight;
  227. }
  228. }
  229. if ( noUVFound )
  230. Con::warnf("[ASSIMP] No UV Data for mesh.");
  231. }
  232. void AssimpAppMesh::lookupSkinData()
  233. { // This function is intentionally left blank. The skin data - bones, weights and indexes are
  234. // processed in lockMesh() with the rest of the mesh data.
  235. }
  236. F32 AssimpAppMesh::getVisValue(F32 t)
  237. {
  238. return 1.0f;
  239. }