assimpAppMesh.cpp 7.7 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& objectOffset)
  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 = mMeshData->mNormals[i];
  68. Point3F tmpVert;
  69. Point3F tmpNormal;
  70. if (Con::getBoolVariable("$Assimp::SwapYZ", false))
  71. {
  72. tmpVert = Point3F(pt.x, pt.z, pt.y);
  73. tmpNormal = Point3F(nrm.x, nrm.z, nrm.y);
  74. }
  75. else
  76. {
  77. tmpVert = Point3F(pt.x, pt.y, pt.z);
  78. tmpNormal = Point3F(nrm.x, nrm.y, nrm.z);
  79. }
  80. //objectOffset.mulP(tmpVert);
  81. points.push_back(tmpVert);
  82. if (mMeshData->HasTextureCoords(0))
  83. {
  84. uvs.push_back(Point2F(mMeshData->mTextureCoords[0][i].x, mMeshData->mTextureCoords[0][i].y));
  85. }
  86. else
  87. {
  88. // I don't know if there's any solution to this issue.
  89. // If it's not mapped, it's not mapped.
  90. noUVFound = true;
  91. uvs.push_back(Point2F(1, 1));
  92. }
  93. // UV2s
  94. if (mMeshData->HasTextureCoords(1))
  95. {
  96. uv2s.push_back(Point2F(mMeshData->mTextureCoords[1][i].x, mMeshData->mTextureCoords[1][i].y));
  97. }
  98. // Vertex Colors
  99. if (mMeshData->HasVertexColors(0))
  100. {
  101. LinearColorF vColor(mMeshData->mColors[0][i].r,
  102. mMeshData->mColors[0][i].g,
  103. mMeshData->mColors[0][i].b,
  104. mMeshData->mColors[0][i].a);
  105. colors.push_back(vColor.toColorI());
  106. }
  107. //uvs.push_back(mModel->mVerts[i].texcoord);
  108. normals.push_back(tmpNormal);
  109. //edgeVerts.push_back(mModel->mVerts[i].edge);
  110. }
  111. U32 numFaces = mMeshData->mNumFaces;
  112. U32 primCount = 0;
  113. primitives.reserve(numFaces);
  114. //Fetch the number of indices
  115. U32 indicesCount = 0;
  116. for (U32 i = 0; i < numFaces; i++)
  117. {
  118. indicesCount += mMeshData->mFaces[i].mNumIndices;
  119. }
  120. indices.reserve(indicesCount);
  121. /*U32 idxCount = 0;
  122. for (U32 j = 0; j<mModel->mMaterials.size(); j++)
  123. {
  124. MikuModel::Material &mat = mModel->mMaterials[j];
  125. U32 nextIdxCount = idxCount + mat.numIndices;
  126. primitives.increment();
  127. TSDrawPrimitive& primitive = primitives.last();
  128. primitive.start = indices.size();
  129. primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | j;
  130. primitive.numElements = mat.numIndices;
  131. for (U32 i = idxCount; i<nextIdxCount; i++)
  132. {
  133. indices.push_back(mModel->mIndices[i]);
  134. }
  135. idxCount = nextIdxCount;
  136. }*/
  137. for ( U32 n = 0; n < mMeshData->mNumFaces; ++n)
  138. {
  139. const struct aiFace* face = &mMeshData->mFaces[n];
  140. if ( face->mNumIndices == 3 )
  141. {
  142. // Create TSMesh primitive
  143. primitives.increment();
  144. TSDrawPrimitive& primitive = primitives.last();
  145. primitive.start = indices.size();
  146. primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | (S32)mMeshData->mMaterialIndex;
  147. //primitive.numElements = face->mNumIndices;//3;
  148. primitive.numElements = 3;
  149. if (Con::getBoolVariable("$Assimp::FlipNormals", true))
  150. {
  151. U32 indexCount = face->mNumIndices;
  152. for (S32 ind = indexCount - 1; ind >= 0; ind--)
  153. {
  154. U32 index = face->mIndices[ind];
  155. indices.push_back(index);
  156. }
  157. }
  158. else
  159. {
  160. U32 indexCount = face->mNumIndices;
  161. for (U32 ind = 0; ind < indexCount; ind++)
  162. {
  163. U32 index = face->mIndices[ind];
  164. indices.push_back(index);
  165. }
  166. }
  167. // Load the indices in.
  168. //indices.push_back(face->mIndices[0]);
  169. //indices.push_back(face->mIndices[1]);
  170. //indices.push_back(face->mIndices[2]);
  171. }
  172. else
  173. {
  174. Con::printf("[ASSIMP] Non-Triangle Face Found. Indices: %d", face->mNumIndices);
  175. }
  176. }
  177. U32 boneCount = mMeshData->mNumBones;
  178. bones.setSize(boneCount);
  179. for (U32 b = 0; b < boneCount; b++)
  180. {
  181. String name = mMeshData->mBones[b]->mName.C_Str();
  182. MatrixF boneTransform;
  183. for (U32 m = 0; m < 16; ++m)
  184. {
  185. boneTransform[m] = *mMeshData->mBones[b]->mOffsetMatrix[m];
  186. }
  187. //initialTransforms.push_back(boneTransform);
  188. initialTransforms.push_back(MatrixF::Identity);
  189. //Weights
  190. U32 numWeights = mMeshData->mBones[b]->mNumWeights;
  191. weight.setSize(numWeights);
  192. vertexIndex.setSize(numWeights);
  193. for (U32 w = 0; w < numWeights; ++w)
  194. {
  195. aiVertexWeight* aiWeight = mMeshData->mBones[b]->mWeights;
  196. weight[w] = aiWeight->mWeight;
  197. vertexIndex[w] = aiWeight->mVertexId;
  198. boneIndex[w] = b;
  199. //vertWeight. = aiWeight->
  200. }
  201. //= mNumWeights
  202. }
  203. if ( noUVFound )
  204. Con::warnf("[ASSIMP] No UV Data for mesh.");
  205. }
  206. void AssimpAppMesh::lookupSkinData()
  207. {
  208. }
  209. F32 AssimpAppMesh::getVisValue(F32 t)
  210. {
  211. return 1.0f;
  212. }