You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

194 lines
6.9 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2009, 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. /**
  35. * Provides facilities for dynamically loading the Assimp library.
  36. *
  37. * Currently requires Tango, but there is no reason why Phobos could not be
  38. * supported too.
  39. */
  40. module assimp.loader;
  41. import assimp.api;
  42. import tango.io.Stdout;
  43. import tango.sys.SharedLib;
  44. const uint ASSIMP_BINDINGS_MAJOR = 2;
  45. const uint ASSIMP_BINDINGS_MINOR = 0;
  46. /**
  47. * Loader class for dynamically loading the Assimp library.
  48. *
  49. * The library is »reference-counted«, meaning that the library is not
  50. * unloaded on a call to <code>unload()</code> if there are still other
  51. * references to it.
  52. */
  53. struct Assimp {
  54. public:
  55. /**
  56. * Loads the library if it is not already loaded and increases the
  57. * reference counter.
  58. *
  59. * The library file (<code>libassimp.so</code> on POSIX systems,
  60. * <code>Assimp32.dll</code> on Win32) is loaded via Tango's SharedLib
  61. * class.
  62. */
  63. static void load() {
  64. if ( m_sRefCount == 0 ) {
  65. version ( Posix ) {
  66. version ( OSX ) {
  67. m_sLibrary = SharedLib.load( "libassimp.dylib" );
  68. } else {
  69. m_sLibrary = SharedLib.load( "libassimp.so" );
  70. }
  71. }
  72. version ( Win32 ) {
  73. m_sLibrary = SharedLib.load( "Assimp32.dll" );
  74. }
  75. // Versioning
  76. mixin( bindCode( "aiGetLegalString" ) );
  77. mixin( bindCode( "aiGetVersionMinor" ) );
  78. mixin( bindCode( "aiGetVersionMajor" ) );
  79. mixin( bindCode( "aiGetVersionRevision" ) );
  80. mixin( bindCode( "aiGetCompileFlags" ) );
  81. // Check for version mismatch between the external, dynamically loaded
  82. // library and the version the bindings were created against.
  83. uint libMajor = aiGetVersionMajor();
  84. uint libMinor = aiGetVersionMinor();
  85. if ( ( libMajor < ASSIMP_BINDINGS_MAJOR ) ||
  86. ( libMinor < ASSIMP_BINDINGS_MINOR ) ) {
  87. Stdout.format(
  88. "WARNING: Assimp version too old (loaded library: {}.{}, " ~
  89. "bindings: {}.{})!",
  90. libMajor,
  91. libMinor,
  92. ASSIMP_BINDINGS_MAJOR,
  93. ASSIMP_BINDINGS_MINOR
  94. ).newline;
  95. }
  96. if ( libMajor > ASSIMP_BINDINGS_MAJOR ) {
  97. Stdout.format(
  98. "WARNING: Assimp version too new (loaded library: {}.{}, " ~
  99. "bindings: {}.{})!",
  100. libMajor,
  101. libMinor,
  102. ASSIMP_BINDINGS_MAJOR,
  103. ASSIMP_BINDINGS_MINOR
  104. ).newline;
  105. }
  106. // General API
  107. mixin( bindCode( "aiImportFile" ) );
  108. mixin( bindCode( "aiImportFileEx" ) );
  109. mixin( bindCode( "aiImportFileFromMemory" ) );
  110. mixin( bindCode( "aiApplyPostProcessing" ) );
  111. mixin( bindCode( "aiGetPredefinedLogStream" ) );
  112. mixin( bindCode( "aiAttachLogStream" ) );
  113. mixin( bindCode( "aiEnableVerboseLogging" ) );
  114. mixin( bindCode( "aiDetachLogStream" ) );
  115. mixin( bindCode( "aiDetachAllLogStreams" ) );
  116. mixin( bindCode( "aiReleaseImport" ) );
  117. mixin( bindCode( "aiGetErrorString" ) );
  118. mixin( bindCode( "aiIsExtensionSupported" ) );
  119. mixin( bindCode( "aiGetExtensionList" ) );
  120. mixin( bindCode( "aiGetMemoryRequirements" ) );
  121. mixin( bindCode( "aiSetImportPropertyInteger" ) );
  122. mixin( bindCode( "aiSetImportPropertyFloat" ) );
  123. mixin( bindCode( "aiSetImportPropertyString" ) );
  124. // Mathematical functions
  125. mixin( bindCode( "aiCreateQuaternionFromMatrix" ) );
  126. mixin( bindCode( "aiDecomposeMatrix" ) );
  127. mixin( bindCode( "aiTransposeMatrix4" ) );
  128. mixin( bindCode( "aiTransposeMatrix3" ) );
  129. mixin( bindCode( "aiTransformVecByMatrix3" ) );
  130. mixin( bindCode( "aiTransformVecByMatrix4" ) );
  131. mixin( bindCode( "aiMultiplyMatrix4" ) );
  132. mixin( bindCode( "aiMultiplyMatrix3" ) );
  133. mixin( bindCode( "aiIdentityMatrix3" ) );
  134. mixin( bindCode( "aiIdentityMatrix4" ) );
  135. // Material system
  136. mixin( bindCode( "aiGetMaterialProperty" ) );
  137. mixin( bindCode( "aiGetMaterialFloatArray" ) );
  138. mixin( bindCode( "aiGetMaterialIntegerArray" ) );
  139. mixin( bindCode( "aiGetMaterialColor" ) );
  140. mixin( bindCode( "aiGetMaterialString" ) );
  141. mixin( bindCode( "aiGetMaterialTextureCount" ) );
  142. mixin( bindCode( "aiGetMaterialTexture" ) );
  143. }
  144. ++m_sRefCount;
  145. }
  146. /**
  147. * Decreases the reference counter and unloads the library if this was the
  148. * last reference.
  149. */
  150. static void unload() {
  151. assert( m_sRefCount > 0 );
  152. --m_sRefCount;
  153. if ( m_sRefCount == 0 ) {
  154. m_sLibrary.unload();
  155. }
  156. }
  157. private:
  158. /// Current number of references to the library.
  159. static uint m_sRefCount;
  160. /// Library handle.
  161. static SharedLib m_sLibrary;
  162. }
  163. /**
  164. * Private helper function which constructs the bind command for a symbol to
  165. * keep the code DRY.
  166. */
  167. private char[] bindCode( char[] symbol ) {
  168. return symbol ~ " = cast( typeof( " ~ symbol ~
  169. " ) )m_sLibrary.getSymbol( `" ~ symbol ~ "` );";
  170. }