1 module gfm.assimp.scene;
2 
3 import std.string;
4 
5 import derelict.assimp3.assimp;
6 
7 import gfm.assimp.assimp;
8 
9 /// ASSIMP scene wrapper.
10 class AssimpScene
11 {
12     public
13     {
14         /// Import mesh from a file.
15         /// The ASSIMP library must have been loaded.
16         /// Throws: AssimpException on error.
17         this(Assimp assimp, string path, uint postProcessFlags = 0)
18         {
19             _assimp = assimp;
20             _scene = aiImportFile(toStringz(path), postProcessFlags);
21             if (_scene is null)
22                 assimp.throwAssimpException("aiImportFile");
23         }
24 
25         /// Import mesh from a memory area.
26         /// The ASSIMP library must have been loaded.
27         /// Throws: AssimpException on error.
28         this(Assimp assimp, ubyte[] data, uint postProcessFlags = 0)
29         {
30             _assimp = assimp;
31             _scene = aiImportFileFromMemory(cast(const(char)*)(data.ptr),
32                                             cast(uint)data.length,
33                                             postProcessFlags,
34                                             null);
35             if (_scene is null)
36                 assimp.throwAssimpException("aiImportFileFromMemory");
37         }
38 
39         /// Import mesh from a memory area using specified import properties.
40         /// The ASSIMP library must have been loaded.
41         /// Throws: AssimpException on error.
42         this(Assimp assimp, ubyte[] data, aiPropertyStore* props, uint postProcessFlags = 0)
43         {
44             _assimp = assimp;
45             _scene = aiImportFileFromMemoryWithProperties(cast(const(char)*)(data.ptr),
46                                                           cast(uint)data.length,
47                                                           postProcessFlags,
48                                                           null,
49                                                           props);
50             if (_scene is null)
51                 assimp.throwAssimpException("aiImportFileFromMemoryWithProperties");
52         }
53 
54         /// Releases the ASSIMP scene resource.
55         ~this()
56         {
57             if (_scene !is null)
58             {
59                 debug ensureNotInGC("AssimpScene");
60                 aiReleaseImport(_scene);
61                 _scene = null;
62             }
63         }
64 
65         /// Apply post-processing separately, to separate loading from post-processing.
66         /// Throws: AssimpException on error.
67         void applyPostProcessing(uint postProcessFlags)
68         {
69             const(aiScene)* newScene = aiApplyPostProcessing(_scene, postProcessFlags);
70             if (newScene is null)
71                 _assimp.throwAssimpException("aiApplyPostProcessing");
72             _scene = newScene;
73         }
74 
75         /// Returns: Wrapped ASSIMP scene handle.
76         const(aiScene)* scene()
77         {
78             return _scene;
79         }
80     }
81 
82     private
83     {
84         Assimp _assimp;
85         const(aiScene)* _scene;
86     }
87 }