assimpAppMesh.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. }
  37. const char* AssimpAppMesh::getName(bool allowFixed)
  38. {
  39. // Some exporters add a 'PIVOT' or unnamed node between the mesh and the
  40. // actual object node. Detect this and return the object node name instead
  41. // of the pivot node.
  42. const char* nodeName = appNode->getName();
  43. if ( dStrEqual(nodeName, "null") || dStrEndsWith(nodeName, "PIVOT") )
  44. nodeName = appNode->getParentName();
  45. // If all geometry is being fixed to the same size, append the size
  46. // to the name
  47. //return allowFixed && fixedSizeEnabled ? avar("%s %d", nodeName, fixedSize) : nodeName;
  48. return nodeName;
  49. }
  50. MatrixF AssimpAppMesh::getMeshTransform(F32 time)
  51. {
  52. return appNode->getNodeTransform(time);
  53. }
  54. void AssimpAppMesh::lockMesh(F32 t, const MatrixF& objOffset)
  55. {
  56. // After this function, the following are expected to be populated:
  57. // points, normals, uvs, primitives, indices
  58. // There is also colors and uv2s but those don't seem to be required.
  59. points.reserve(mMeshData->mNumVertices);
  60. uvs.reserve(mMeshData->mNumVertices);
  61. normals.reserve(mMeshData->mNumVertices);
  62. bool noUVFound = false;
  63. for (U32 i = 0; i<mMeshData->mNumVertices; i++)
  64. {
  65. // Points and Normals
  66. aiVector3D pt = mMeshData->mVertices[i];
  67. aiVector3D nrm;
  68. if (mMeshData->HasNormals())
  69. nrm = mMeshData->mNormals[i];
  70. else
  71. nrm.Set(0, 0, 0);
  72. Point3F tmpVert;
  73. Point3F tmpNormal;
  74. tmpVert = Point3F(pt.x, pt.y, pt.z);
  75. tmpNormal = Point3F(nrm.x, nrm.y, nrm.z);
  76. //AssimpAppNode::convertPoint(tmpVert);
  77. //AssimpAppNode::convertPoint(tmpNormal);
  78. //objOffset.mulP(tmpVert);
  79. points.push_back(tmpVert);
  80. if (mMeshData->HasTextureCoords(0))
  81. {
  82. uvs.push_back(Point2F(mMeshData->mTextureCoords[0][i].x, mMeshData->mTextureCoords[0][i].y));
  83. }
  84. else
  85. {
  86. // I don't know if there's any solution to this issue.
  87. // If it's not mapped, it's not mapped.
  88. noUVFound = true;
  89. uvs.push_back(Point2F(1, 1));
  90. }
  91. // UV2s
  92. if (mMeshData->HasTextureCoords(1))
  93. {
  94. uv2s.push_back(Point2F(mMeshData->mTextureCoords[1][i].x, mMeshData->mTextureCoords[1][i].y));
  95. }
  96. // Vertex Colors
  97. if (mMeshData->HasVertexColors(0))
  98. {
  99. LinearColorF vColor(mMeshData->mColors[0][i].r,
  100. mMeshData->mColors[0][i].g,
  101. mMeshData->mColors[0][i].b,
  102. mMeshData->mColors[0][i].a);
  103. colors.push_back(vColor.toColorI());
  104. }
  105. //uvs.push_back(mModel->mVerts[i].texcoord);
  106. normals.push_back(tmpNormal);
  107. //edgeVerts.push_back(mModel->mVerts[i].edge);
  108. }
  109. U32 numFaces = mMeshData->mNumFaces;
  110. U32 primCount = 0;
  111. primitives.reserve(numFaces);
  112. //Fetch the number of indices
  113. U32 indicesCount = 0;
  114. for (U32 i = 0; i < numFaces; i++)
  115. {
  116. indicesCount += mMeshData->mFaces[i].mNumIndices;
  117. }
  118. indices.reserve(indicesCount);
  119. /*U32 idxCount = 0;
  120. for (U32 j = 0; j<mModel->mMaterials.size(); j++)
  121. {
  122. MikuModel::Material &mat = mModel->mMaterials[j];
  123. U32 nextIdxCount = idxCount + mat.numIndices;
  124. primitives.increment();
  125. TSDrawPrimitive& primitive = primitives.last();
  126. primitive.start = indices.size();
  127. primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | j;
  128. primitive.numElements = mat.numIndices;
  129. for (U32 i = idxCount; i<nextIdxCount; i++)
  130. {
  131. indices.push_back(mModel->mIndices[i]);
  132. }
  133. idxCount = nextIdxCount;
  134. }*/
  135. for ( U32 n = 0; n < mMeshData->mNumFaces; ++n)
  136. {
  137. const struct aiFace* face = &mMeshData->mFaces[n];
  138. if ( face->mNumIndices == 3 )
  139. {
  140. // Create TSMesh primitive
  141. primitives.increment();
  142. TSDrawPrimitive& primitive = primitives.last();
  143. primitive.start = indices.size();
  144. primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | (S32)mMeshData->mMaterialIndex;
  145. //primitive.numElements = face->mNumIndices;//3;
  146. primitive.numElements = 3;
  147. if (Con::getBoolVariable("$Assimp::FlipNormals", true))
  148. {
  149. U32 indexCount = face->mNumIndices;
  150. for (S32 ind = indexCount - 1; ind >= 0; ind--)
  151. {
  152. U32 index = face->mIndices[ind];
  153. indices.push_back(index);
  154. }
  155. }
  156. else
  157. {
  158. U32 indexCount = face->mNumIndices;
  159. for (U32 ind = 0; ind < indexCount; ind++)
  160. {
  161. U32 index = face->mIndices[ind];
  162. indices.push_back(index);
  163. }
  164. }
  165. // Load the indices in.
  166. //indices.push_back(face->mIndices[0]);
  167. //indices.push_back(face->mIndices[1]);
  168. //indices.push_back(face->mIndices[2]);
  169. }
  170. else
  171. {
  172. Con::printf("[ASSIMP] Non-Triangle Face Found. Indices: %d", face->mNumIndices);
  173. }
  174. }
  175. U32 boneCount = mMeshData->mNumBones;
  176. bones.setSize(boneCount);
  177. for (U32 b = 0; b < boneCount; b++)
  178. {
  179. String name = mMeshData->mBones[b]->mName.C_Str();
  180. MatrixF boneTransform;
  181. boneTransform.setRow(0, Point4F(mMeshData->mBones[b]->mOffsetMatrix.a1, mMeshData->mBones[b]->mOffsetMatrix.a2, mMeshData->mBones[b]->mOffsetMatrix.a3, mMeshData->mBones[b]->mOffsetMatrix.a4));
  182. boneTransform.setRow(1, Point4F(mMeshData->mBones[b]->mOffsetMatrix.b1, mMeshData->mBones[b]->mOffsetMatrix.b2, mMeshData->mBones[b]->mOffsetMatrix.b3, mMeshData->mBones[b]->mOffsetMatrix.b4));
  183. boneTransform.setRow(2, Point4F(mMeshData->mBones[b]->mOffsetMatrix.c1, mMeshData->mBones[b]->mOffsetMatrix.c2, mMeshData->mBones[b]->mOffsetMatrix.c3, mMeshData->mBones[b]->mOffsetMatrix.c4));
  184. boneTransform.setRow(3, Point4F(mMeshData->mBones[b]->mOffsetMatrix.d1, mMeshData->mBones[b]->mOffsetMatrix.d2, mMeshData->mBones[b]->mOffsetMatrix.d3, mMeshData->mBones[b]->mOffsetMatrix.d4));
  185. //boneTransform.inverse();
  186. //AssimpAppNode::convertMat(boneTransform); // Will need a better animated reference shape to determine if this is needed.
  187. //boneTransform.inverse();
  188. initialTransforms.push_back(boneTransform);
  189. //Weights
  190. U32 numWeights = mMeshData->mBones[b]->mNumWeights;
  191. weight.setSize(numWeights);
  192. vertexIndex.setSize(numWeights);
  193. boneIndex.setSize(numWeights);
  194. for (U32 w = 0; w < numWeights; ++w)
  195. {
  196. aiVertexWeight* aiWeight = mMeshData->mBones[b]->mWeights;
  197. weight[w] = aiWeight->mWeight;
  198. vertexIndex[w] = aiWeight->mVertexId;
  199. boneIndex[w] = b;
  200. //vertWeight. = aiWeight->
  201. }
  202. //= mNumWeights
  203. }
  204. if ( noUVFound )
  205. Con::warnf("[ASSIMP] No UV Data for mesh.");
  206. }
  207. void AssimpAppMesh::lookupSkinData()
  208. {
  209. }
  210. F32 AssimpAppMesh::getVisValue(F32 t)
  211. {
  212. return 1.0f;
  213. }