Exporter.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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 following
  9. 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. /** @file Exporter.cpp
  35. Assimp export interface. While it's public interface bears many similarities
  36. to the import interface (in fact, it is largely symmetric), the internal
  37. implementations differs a lot. Exporters are considered stateless and are
  38. simple callbacks which we maintain in a global list along with their
  39. description strings.
  40. */
  41. #include "AssimpPCH.h"
  42. #ifndef ASSIMP_BUILD_NO_EXPORT
  43. #include "DefaultIOStream.h"
  44. #include "DefaultIOSystem.h"
  45. namespace Assimp {
  46. // ------------------------------------------------------------------------------------------------
  47. // Exporter worker function prototypes. Should not be necessary to #ifndef them, it's just a prototype
  48. void ExportSceneCollada(aiExportDataBlob*, const aiScene*);
  49. void ExportScene3DS(aiExportDataBlob*, const aiScene*) { }
  50. /// Function pointer type of a Export worker function
  51. typedef void (*fpExportFunc)(aiExportDataBlob*, const aiScene*);
  52. // ------------------------------------------------------------------------------------------------
  53. /// Internal description of an Assimp export format option
  54. struct ExportFormatEntry
  55. {
  56. /// Public description structure to be returned by aiGetExportFormatDescription()
  57. aiExportFormatDesc mDescription;
  58. // Worker function to do the actual exporting
  59. fpExportFunc mExportFunction;
  60. // Constructor to fill all entries
  61. ExportFormatEntry( const char* pId, const char* pDesc, fpExportFunc pFunction)
  62. {
  63. mDescription.id = pId;
  64. mDescription.description = pDesc;
  65. mExportFunction = pFunction;
  66. }
  67. };
  68. // ------------------------------------------------------------------------------------------------
  69. // global array of all export formats which Assimp supports in its current build
  70. ExportFormatEntry gExporters[] =
  71. {
  72. #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
  73. ExportFormatEntry( "collada", "Collada .dae", &ExportSceneCollada),
  74. #endif
  75. #ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
  76. ExportFormatEntry( "3ds", "Autodesk .3ds", &ExportScene3DS),
  77. #endif
  78. };
  79. class ExporterPimpl {
  80. public:
  81. ExporterPimpl()
  82. : blob()
  83. , mIOSystem(new Assimp::DefaultIOSystem())
  84. , mIsDefaultIOHandler(true)
  85. {
  86. }
  87. ~ExporterPimpl()
  88. {
  89. delete blob;
  90. }
  91. public:
  92. aiExportDataBlob* blob;
  93. boost::shared_ptr< Assimp::IOSystem > mIOSystem;
  94. bool mIsDefaultIOHandler;
  95. };
  96. } // end of namespace Assimp
  97. using namespace Assimp;
  98. // ------------------------------------------------------------------------------------------------
  99. Exporter :: Exporter()
  100. : pimpl(new ExporterPimpl())
  101. {
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. Exporter :: ~Exporter()
  105. {
  106. delete pimpl;
  107. }
  108. // ------------------------------------------------------------------------------------------------
  109. void Exporter :: SetIOHandler( IOSystem* pIOHandler)
  110. {
  111. pimpl->mIsDefaultIOHandler = !pIOHandler;
  112. pimpl->mIOSystem.reset(pIOHandler);
  113. }
  114. // ------------------------------------------------------------------------------------------------
  115. IOSystem* Exporter :: GetIOHandler() const
  116. {
  117. return pimpl->mIOSystem.get();
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. bool Exporter :: IsDefaultIOHandler() const
  121. {
  122. return pimpl->mIsDefaultIOHandler;
  123. }
  124. // ------------------------------------------------------------------------------------------------
  125. const aiExportDataBlob* Exporter :: ExportToBlob( const aiScene* pScene, const char* pFormatId )
  126. {
  127. if (pimpl->blob) {
  128. delete pimpl->blob;
  129. pimpl->blob = NULL;
  130. }
  131. /* TODO (ALEX)
  132. boost::shared_ptr<IOSystem*> old = pimpl->mIOSystem;
  133. BlobIOSystem* blobio;
  134. pimpl->mIOSystem = blobio = new BlobIOSystem();
  135. if (AI_SUCCESS != Export(pScene,pFormatId)) {
  136. pimpl->mIOSystem = old;
  137. return NULL;
  138. }
  139. pimpl->blob = blobio->GetBlob();
  140. pimpl->mIOSystem = old;
  141. return pimpl->blob;
  142. */
  143. return NULL;
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. aiReturn Exporter :: Export( const aiScene* pScene, const char* pFormatId, const char* pPath )
  147. {
  148. return AI_FAILURE;
  149. }
  150. // ------------------------------------------------------------------------------------------------
  151. const aiExportDataBlob* Exporter :: GetBlob() const
  152. {
  153. return pimpl->blob;
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. const aiExportDataBlob* Exporter :: GetOrphanedBlob() const
  157. {
  158. const aiExportDataBlob* tmp = pimpl->blob;
  159. pimpl->blob = NULL;
  160. return tmp;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. size_t Exporter :: aiGetExportFormatCount() const
  164. {
  165. return ::aiGetExportFormatCount();
  166. }
  167. // ------------------------------------------------------------------------------------------------
  168. const aiExportFormatDesc* Exporter :: aiGetExportFormatDescription( size_t pIndex ) const
  169. {
  170. return ::aiGetExportFormatDescription(pIndex);
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. ASSIMP_API size_t aiGetExportFormatCount(void)
  174. {
  175. return sizeof( Assimp::gExporters) / sizeof( Assimp::ExportFormatEntry);
  176. }
  177. // ------------------------------------------------------------------------------------------------
  178. ASSIMP_API const aiExportFormatDesc* aiGetExportFormatDescription( size_t pIndex)
  179. {
  180. if( pIndex >= aiGetExportFormatCount() )
  181. return NULL;
  182. return &Assimp::gExporters[pIndex].mDescription;
  183. }
  184. // ------------------------------------------------------------------------------------------------
  185. ASSIMP_API aiReturn aiExportScene( const aiScene* pScene, const char* pFormatId, const char* pFileName )
  186. {
  187. return ::aiExportSceneEx(pScene,pFormatId,pFileName,NULL);
  188. }
  189. // ------------------------------------------------------------------------------------------------
  190. ASSIMP_API aiReturn aiExportSceneEx( const aiScene* pScene, const char* pFormatId, const char* pFileName, const aiFileIO* pIO)
  191. {
  192. return AI_FAILURE;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. ASSIMP_API const C_STRUCT aiExportDataBlob* aiExportSceneToBlob( const aiScene* pScene, const char* pFormatId )
  196. {
  197. // find a suitable exporter
  198. const Assimp::ExportFormatEntry* exporter = NULL;
  199. for( size_t a = 0; a < aiGetExportFormatCount(); ++a )
  200. {
  201. if( strcmp( Assimp::gExporters[a].mDescription.id, pFormatId) == 0 )
  202. exporter = Assimp::gExporters + a;
  203. }
  204. if( !exporter )
  205. return NULL;
  206. aiExportDataBlob* blob = new aiExportDataBlob;
  207. exporter->mExportFunction( blob, pScene);
  208. if( !blob->data || blob->size == 0 )
  209. {
  210. delete blob;
  211. return NULL;
  212. }
  213. return blob;
  214. }
  215. // ------------------------------------------------------------------------------------------------
  216. ASSIMP_API C_STRUCT void aiReleaseExportData( const aiExportDataBlob* pData )
  217. {
  218. delete pData;
  219. }
  220. #endif // !ASSIMP_BUILD_NO_EXPORT