FBXConverter.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 FBXDocument.cpp
  34. * @brief Implementation of the FBX DOM classes
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXParser.h"
  39. #include "FBXConverter.h"
  40. #include "FBXDocument.h"
  41. #include "FBXUtil.h"
  42. namespace Assimp {
  43. namespace FBX {
  44. namespace {
  45. /** Dummy class to encapsulate the conversion process */
  46. class Converter
  47. {
  48. public:
  49. Converter(aiScene* out, const Document& doc)
  50. : out(out)
  51. , doc(doc)
  52. {
  53. //ConvertRootNode();
  54. // hack to process all meshes
  55. BOOST_FOREACH(const ObjectMap::value_type& v,doc.Objects()) {
  56. const Object* ob = v.second->Get();
  57. if(!ob) {
  58. continue;
  59. }
  60. const MeshGeometry* geo = dynamic_cast<const MeshGeometry*>(ob);
  61. if(geo) {
  62. ConvertMesh(*geo);
  63. }
  64. }
  65. // dummy root node
  66. out->mRootNode = new aiNode();
  67. out->mRootNode->mNumMeshes = static_cast<unsigned int>(meshes.size());
  68. out->mRootNode->mMeshes = new unsigned int[meshes.size()];
  69. for(unsigned int i = 0; i < out->mRootNode->mNumMeshes; ++i) {
  70. out->mRootNode->mMeshes[i] = i;
  71. }
  72. TransferDataToScene();
  73. }
  74. ~Converter()
  75. {
  76. std::for_each(meshes.begin(),meshes.end(),Util::delete_fun<aiMesh>());
  77. }
  78. private:
  79. // ------------------------------------------------------------------------------------------------
  80. // find scene root and trigger recursive scene conversion
  81. void ConvertRootNode()
  82. {
  83. }
  84. // ------------------------------------------------------------------------------------------------
  85. // MeshGeometry -> aiMesh
  86. void ConvertMesh(const MeshGeometry& mesh)
  87. {
  88. const std::vector<aiVector3D>& vertices = mesh.GetVertices();
  89. const std::vector<unsigned int>& faces = mesh.GetFaceIndexCounts();
  90. if(vertices.empty() || faces.empty()) {
  91. return;
  92. }
  93. aiMesh* out_mesh = new aiMesh();
  94. meshes.push_back(out_mesh);
  95. // copy vertices
  96. out_mesh->mNumVertices = static_cast<size_t>(vertices.size());
  97. out_mesh->mVertices = new aiVector3D[vertices.size()];
  98. std::copy(vertices.begin(),vertices.end(),out_mesh->mVertices);
  99. // generate dummy faces
  100. out_mesh->mNumFaces = static_cast<size_t>(faces.size());
  101. aiFace* fac = out_mesh->mFaces = new aiFace[faces.size()]();
  102. unsigned int cursor = 0;
  103. BOOST_FOREACH(unsigned int pcount, faces) {
  104. aiFace& f = *fac++;
  105. f.mNumIndices = pcount;
  106. f.mIndices = new unsigned int[pcount];
  107. switch(pcount)
  108. {
  109. case 1:
  110. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  111. break;
  112. case 2:
  113. out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  114. break;
  115. case 3:
  116. out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  117. break;
  118. default:
  119. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  120. break;
  121. }
  122. for (unsigned int i = 0; i < pcount; ++i) {
  123. f.mIndices[i] = cursor++;
  124. }
  125. }
  126. // copy normals
  127. const std::vector<aiVector3D>& normals = mesh.GetVertices();
  128. if(normals.size()) {
  129. ai_assert(normals.size() == vertices.size());
  130. out_mesh->mNormals = new aiVector3D[vertices.size()];
  131. std::copy(normals.begin(),normals.end(),out_mesh->mNormals);
  132. }
  133. // copy tangents - assimp requires both tangents and bitangents (binormals)
  134. // to be present, or neither of them. Compute binormals from normals
  135. // and tangents if needed.
  136. const std::vector<aiVector3D>& tangents = mesh.GetTangents();
  137. const std::vector<aiVector3D>* binormals = &mesh.GetBinormals();
  138. if(tangents.size()) {
  139. std::vector<aiVector3D> tempBinormals;
  140. if (!binormals->size()) {
  141. if (normals.size()) {
  142. tempBinormals.resize(normals.size());
  143. for (unsigned int i = 0; i < tangents.size(); ++i) {
  144. tempBinormals[i] = normals[i] ^ tangents[i];
  145. }
  146. binormals = &tempBinormals;
  147. }
  148. else {
  149. binormals = NULL;
  150. }
  151. }
  152. if(binormals) {
  153. ai_assert(tangents.size() == vertices.size() && binormals->size() == vertices.size());
  154. out_mesh->mTangents = new aiVector3D[vertices.size()];
  155. std::copy(tangents.begin(),tangents.end(),out_mesh->mTangents);
  156. out_mesh->mBitangents = new aiVector3D[vertices.size()];
  157. std::copy(binormals->begin(),binormals->end(),out_mesh->mBitangents);
  158. }
  159. }
  160. // copy texture coords
  161. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  162. const std::vector<aiVector2D>& uvs = mesh.GetTextureCoords(i);
  163. if(uvs.empty()) {
  164. break;
  165. }
  166. aiVector3D* out_uv = out_mesh->mTextureCoords[i] = new aiVector3D[vertices.size()];
  167. BOOST_FOREACH(const aiVector2D& v, uvs) {
  168. *out_uv++ = aiVector3D(v.x,v.y,0.0f);
  169. }
  170. out_mesh->mNumUVComponents[i] = 2;
  171. }
  172. // copy vertex colors
  173. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
  174. const std::vector<aiColor4D>& colors = mesh.GetVertexColors(i);
  175. if(colors.empty()) {
  176. break;
  177. }
  178. out_mesh->mColors[i] = new aiColor4D[vertices.size()];
  179. std::copy(colors.begin(),colors.end(),out_mesh->mColors[i]);
  180. }
  181. }
  182. // ------------------------------------------------------------------------------------------------
  183. // copy generated meshes, animations, lights, cameras and textures to the output scene
  184. void TransferDataToScene()
  185. {
  186. ai_assert(!out->mMeshes && !out->mNumMeshes);
  187. // note: the trailing () ensures initialization with NULL - not
  188. // many C++ users seem to know this, so pointing it out to avoid
  189. // confusion why this code works.
  190. out->mMeshes = new aiMesh*[meshes.size()]();
  191. out->mNumMeshes = static_cast<unsigned int>(meshes.size());
  192. std::swap_ranges(meshes.begin(),meshes.end(),out->mMeshes);
  193. }
  194. private:
  195. std::vector<aiMesh*> meshes;
  196. aiScene* const out;
  197. const FBX::Document& doc;
  198. };
  199. } // !anon
  200. // ------------------------------------------------------------------------------------------------
  201. void ConvertToAssimpScene(aiScene* out, const Document& doc)
  202. {
  203. Converter converter(out,doc);
  204. }
  205. } // !FBX
  206. } // !Assimp
  207. #endif