SkeletonMeshBuilder.cpp 10 KB

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