1 module des.gl.simple.loader;
2 
3 import derelict.assimp3.assimp;
4 import derelict.assimp3.types;
5 
6 import des.math.linear;
7 import des.util.data.type;
8 import des.util.stdext..string;
9 
10 import des.gl.simple.meshobj;
11 
12 import std..string;
13 
14 ///
15 class SceneLoader
16 {
17     ///
18     this( string fname )
19     {
20         if( !DerelictASSIMP3.isLoaded )
21             DerelictASSIMP3.load();
22 
23         auto scene = aiImportFile( fname.toStringz, 0 );
24 
25         foreach( i; 0 .. scene.mNumMeshes )
26             meshes ~= convMesh( scene.mMeshes[i] );
27     }
28 
29     ///
30     MeshData[] meshes;
31 
32     ///
33     MeshData meshByName( string name, lazy MeshData def=null )
34     {
35         foreach( m; meshes )
36             if( m.name == name ) return m;
37         return def;
38     }
39 
40 protected:
41 
42     MeshData convMesh( in aiMesh* m )
43     {
44         auto ret = new MeshData;
45         ret.name = toDStringFix( m.mName.data );
46 
47         auto cnt = m.mNumVertices;
48 
49         ret.vertices = getTypedArray!vec3( cnt,
50                 cast(void*)(m.mVertices) ).arr.dup;
51 
52         if( m.mNormals !is null )
53             ret.normals = getTypedArray!vec3( cnt,
54                     cast(void*)(m.mNormals) ).arr.dup;
55 
56         if( m.mTangents !is null )
57             ret.tangents = getTypedArray!vec3( cnt,
58                     cast(void*)(m.mTangents) ).arr.dup;
59 
60         if( m.mBitangents !is null )
61             ret.bitangents = getTypedArray!vec3( cnt,
62                     cast(void*)(m.mBitangents) ).arr.dup;
63 
64         foreach( i; 0 .. AI_MAX_NUMBER_OF_COLOR_SETS )
65             if( m.mColors[i] !is null )
66             {
67                 ret.colors ~= getTypedArray!vec4( cnt,
68                         cast(void*)(m.mColors[i]) ).arr.dup;
69             }
70 
71         foreach( i; 0 .. AI_MAX_NUMBER_OF_TEXTURECOORDS )
72             if( m.mTextureCoords[i] !is null )
73             {
74                 ret.texcrds ~= getTypedArray!vec3( cnt,
75                         cast(void*)(m.mTextureCoords[i]) ).arr.dup;
76                 ret.texcrdsdims ~= m.mNumUVComponents[i];
77             }
78 
79         foreach( i; 0 .. m.mNumFaces )
80         {
81             auto f = m.mFaces[i];
82             ret.indices ~= getTypedArray!uint( f.mNumIndices,
83                     cast(void*)f.mIndices ).arr;
84         }
85 
86         return ret;
87     }
88 }
89