Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

268 linhas
11 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file SkeletonMeshBuilder.cpp
  34. * @brief Implementation of a little class to construct a dummy mesh for a skeleton
  35. */
  36. #include "AssimpPCH.h"
  37. #include "../include/assimp/scene.h"
  38. #include "SkeletonMeshBuilder.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // The constructor processes the given scene and adds a mesh there.
  42. SkeletonMeshBuilder::SkeletonMeshBuilder( aiScene* pScene, aiNode* root, bool bKnobsOnly)
  43. {
  44. // nothing to do if there's mesh data already present at the scene
  45. if( pScene->mNumMeshes > 0 || pScene->mRootNode == NULL)
  46. return;
  47. if (!root)
  48. root = pScene->mRootNode;
  49. mKnobsOnly = bKnobsOnly;
  50. // build some faces around each node
  51. CreateGeometry( root );
  52. // create a mesh to hold all the generated faces
  53. pScene->mNumMeshes = 1;
  54. pScene->mMeshes = new aiMesh*[1];
  55. pScene->mMeshes[0] = CreateMesh();
  56. // and install it at the root node
  57. root->mNumMeshes = 1;
  58. root->mMeshes = new unsigned int[1];
  59. root->mMeshes[0] = 0;
  60. // create a dummy material for the mesh
  61. pScene->mNumMaterials = 1;
  62. pScene->mMaterials = new aiMaterial*[1];
  63. pScene->mMaterials[0] = CreateMaterial();
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. // Recursively builds a simple mesh representation for the given node
  67. void SkeletonMeshBuilder::CreateGeometry( const aiNode* pNode)
  68. {
  69. // add a joint entry for the node.
  70. const unsigned int vertexStartIndex = mVertices.size();
  71. // now build the geometry.
  72. if( pNode->mNumChildren > 0 && !mKnobsOnly)
  73. {
  74. // If the node has children, we build little pointers to each of them
  75. for( unsigned int a = 0; a < pNode->mNumChildren; a++)
  76. {
  77. // find a suitable coordinate system
  78. const aiMatrix4x4& childTransform = pNode->mChildren[a]->mTransformation;
  79. aiVector3D childpos( childTransform.a4, childTransform.b4, childTransform.c4);
  80. float distanceToChild = childpos.Length();
  81. if( distanceToChild < 0.0001f)
  82. continue;
  83. aiVector3D up = aiVector3D( childpos).Normalize();
  84. aiVector3D orth( 1.0f, 0.0f, 0.0f);
  85. if( fabs( orth * up) > 0.99f)
  86. orth.Set( 0.0f, 1.0f, 0.0f);
  87. aiVector3D front = (up ^ orth).Normalize();
  88. aiVector3D side = (front ^ up).Normalize();
  89. unsigned int localVertexStart = mVertices.size();
  90. mVertices.push_back( -front * distanceToChild * 0.1f);
  91. mVertices.push_back( childpos);
  92. mVertices.push_back( -side * distanceToChild * 0.1f);
  93. mVertices.push_back( -side * distanceToChild * 0.1f);
  94. mVertices.push_back( childpos);
  95. mVertices.push_back( front * distanceToChild * 0.1f);
  96. mVertices.push_back( front * distanceToChild * 0.1f);
  97. mVertices.push_back( childpos);
  98. mVertices.push_back( side * distanceToChild * 0.1f);
  99. mVertices.push_back( side * distanceToChild * 0.1f);
  100. mVertices.push_back( childpos);
  101. mVertices.push_back( -front * distanceToChild * 0.1f);
  102. mFaces.push_back( Face( localVertexStart + 0, localVertexStart + 1, localVertexStart + 2));
  103. mFaces.push_back( Face( localVertexStart + 3, localVertexStart + 4, localVertexStart + 5));
  104. mFaces.push_back( Face( localVertexStart + 6, localVertexStart + 7, localVertexStart + 8));
  105. mFaces.push_back( Face( localVertexStart + 9, localVertexStart + 10, localVertexStart + 11));
  106. }
  107. }
  108. else
  109. {
  110. // if the node has no children, it's an end node. Put a little knob there instead
  111. aiVector3D ownpos( pNode->mTransformation.a4, pNode->mTransformation.b4, pNode->mTransformation.c4);
  112. float sizeEstimate = ownpos.Length() * 0.18f;
  113. mVertices.push_back( aiVector3D( -sizeEstimate, 0.0f, 0.0f));
  114. mVertices.push_back( aiVector3D( 0.0f, sizeEstimate, 0.0f));
  115. mVertices.push_back( aiVector3D( 0.0f, 0.0f, -sizeEstimate));
  116. mVertices.push_back( aiVector3D( 0.0f, sizeEstimate, 0.0f));
  117. mVertices.push_back( aiVector3D( sizeEstimate, 0.0f, 0.0f));
  118. mVertices.push_back( aiVector3D( 0.0f, 0.0f, -sizeEstimate));
  119. mVertices.push_back( aiVector3D( sizeEstimate, 0.0f, 0.0f));
  120. mVertices.push_back( aiVector3D( 0.0f, -sizeEstimate, 0.0f));
  121. mVertices.push_back( aiVector3D( 0.0f, 0.0f, -sizeEstimate));
  122. mVertices.push_back( aiVector3D( 0.0f, -sizeEstimate, 0.0f));
  123. mVertices.push_back( aiVector3D( -sizeEstimate, 0.0f, 0.0f));
  124. mVertices.push_back( aiVector3D( 0.0f, 0.0f, -sizeEstimate));
  125. mVertices.push_back( aiVector3D( -sizeEstimate, 0.0f, 0.0f));
  126. mVertices.push_back( aiVector3D( 0.0f, 0.0f, sizeEstimate));
  127. mVertices.push_back( aiVector3D( 0.0f, sizeEstimate, 0.0f));
  128. mVertices.push_back( aiVector3D( 0.0f, sizeEstimate, 0.0f));
  129. mVertices.push_back( aiVector3D( 0.0f, 0.0f, sizeEstimate));
  130. mVertices.push_back( aiVector3D( sizeEstimate, 0.0f, 0.0f));
  131. mVertices.push_back( aiVector3D( sizeEstimate, 0.0f, 0.0f));
  132. mVertices.push_back( aiVector3D( 0.0f, 0.0f, sizeEstimate));
  133. mVertices.push_back( aiVector3D( 0.0f, -sizeEstimate, 0.0f));
  134. mVertices.push_back( aiVector3D( 0.0f, -sizeEstimate, 0.0f));
  135. mVertices.push_back( aiVector3D( 0.0f, 0.0f, sizeEstimate));
  136. mVertices.push_back( aiVector3D( -sizeEstimate, 0.0f, 0.0f));
  137. mFaces.push_back( Face( vertexStartIndex + 0, vertexStartIndex + 1, vertexStartIndex + 2));
  138. mFaces.push_back( Face( vertexStartIndex + 3, vertexStartIndex + 4, vertexStartIndex + 5));
  139. mFaces.push_back( Face( vertexStartIndex + 6, vertexStartIndex + 7, vertexStartIndex + 8));
  140. mFaces.push_back( Face( vertexStartIndex + 9, vertexStartIndex + 10, vertexStartIndex + 11));
  141. mFaces.push_back( Face( vertexStartIndex + 12, vertexStartIndex + 13, vertexStartIndex + 14));
  142. mFaces.push_back( Face( vertexStartIndex + 15, vertexStartIndex + 16, vertexStartIndex + 17));
  143. mFaces.push_back( Face( vertexStartIndex + 18, vertexStartIndex + 19, vertexStartIndex + 20));
  144. mFaces.push_back( Face( vertexStartIndex + 21, vertexStartIndex + 22, vertexStartIndex + 23));
  145. }
  146. unsigned int numVertices = mVertices.size() - vertexStartIndex;
  147. if( numVertices > 0)
  148. {
  149. // create a bone affecting all the newly created vertices
  150. aiBone* bone = new aiBone;
  151. mBones.push_back( bone);
  152. bone->mName = pNode->mName;
  153. // calculate the bone offset matrix by concatenating the inverse transformations of all parents
  154. bone->mOffsetMatrix = aiMatrix4x4( pNode->mTransformation).Inverse();
  155. for( aiNode* parent = pNode->mParent; parent != NULL; parent = parent->mParent)
  156. bone->mOffsetMatrix = aiMatrix4x4( parent->mTransformation).Inverse() * bone->mOffsetMatrix;
  157. // add all the vertices to the bone's influences
  158. bone->mNumWeights = numVertices;
  159. bone->mWeights = new aiVertexWeight[numVertices];
  160. for( unsigned int a = 0; a < numVertices; a++)
  161. bone->mWeights[a] = aiVertexWeight( vertexStartIndex + a, 1.0f);
  162. // HACK: (thom) transform all vertices to the bone's local space. Should be done before adding
  163. // them to the array, but I'm tired now and I'm annoyed.
  164. aiMatrix4x4 boneToMeshTransform = aiMatrix4x4( bone->mOffsetMatrix).Inverse();
  165. for( unsigned int a = vertexStartIndex; a < mVertices.size(); a++)
  166. mVertices[a] = boneToMeshTransform * mVertices[a];
  167. }
  168. // and finally recurse into the children list
  169. for( unsigned int a = 0; a < pNode->mNumChildren; a++)
  170. CreateGeometry( pNode->mChildren[a]);
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. // Creates the mesh from the internally accumulated stuff and returns it.
  174. aiMesh* SkeletonMeshBuilder::CreateMesh()
  175. {
  176. aiMesh* mesh = new aiMesh();
  177. // add points
  178. mesh->mNumVertices = mVertices.size();
  179. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  180. std::copy( mVertices.begin(), mVertices.end(), mesh->mVertices);
  181. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  182. // add faces
  183. mesh->mNumFaces = mFaces.size();
  184. mesh->mFaces = new aiFace[mesh->mNumFaces];
  185. for( unsigned int a = 0; a < mesh->mNumFaces; a++)
  186. {
  187. const Face& inface = mFaces[a];
  188. aiFace& outface = mesh->mFaces[a];
  189. outface.mNumIndices = 3;
  190. outface.mIndices = new unsigned int[3];
  191. outface.mIndices[0] = inface.mIndices[0];
  192. outface.mIndices[1] = inface.mIndices[1];
  193. outface.mIndices[2] = inface.mIndices[2];
  194. // Compute per-face normals ... we don't want the bones to be smoothed ... they're built to visualize
  195. // the skeleton, so it's good if there's a visual difference to the rest of the geometry
  196. aiVector3D nor = ((mVertices[inface.mIndices[2]] - mVertices[inface.mIndices[0]]) ^
  197. (mVertices[inface.mIndices[1]] - mVertices[inface.mIndices[0]]));
  198. if (nor.Length() < 1e-5f) /* ensure that FindInvalidData won't remove us ...*/
  199. nor = aiVector3D(1.f,0.f,0.f);
  200. for (unsigned int n = 0; n < 3; ++n)
  201. mesh->mNormals[inface.mIndices[n]] = nor;
  202. }
  203. // add the bones
  204. mesh->mNumBones = mBones.size();
  205. mesh->mBones = new aiBone*[mesh->mNumBones];
  206. std::copy( mBones.begin(), mBones.end(), mesh->mBones);
  207. // default
  208. mesh->mMaterialIndex = 0;
  209. return mesh;
  210. }
  211. // ------------------------------------------------------------------------------------------------
  212. // Creates a dummy material and returns it.
  213. aiMaterial* SkeletonMeshBuilder::CreateMaterial()
  214. {
  215. aiMaterial* matHelper = new aiMaterial;
  216. // Name
  217. aiString matName( std::string( "SkeletonMaterial"));
  218. matHelper->AddProperty( &matName, AI_MATKEY_NAME);
  219. // Prevent backface culling
  220. const int no_cull = 1;
  221. matHelper->AddProperty(&no_cull,1,AI_MATKEY_TWOSIDED);
  222. return matHelper;
  223. }