1 module sbylib.wrapper.assimp.scene; 2 3 import bindbc.assimp; 4 import sbylib.wrapper.assimp.constants : PostProcessFlag; 5 import sbylib.wrapper.assimp.functions : toArray, indent; 6 import sbylib.wrapper.assimp.node : Node; 7 import sbylib.wrapper.assimp.mesh : Mesh; 8 import sbylib.wrapper.assimp.material : Material; 9 import sbylib.wrapper.assimp.animation : Animation; 10 import sbylib.wrapper.assimp.texture : Texture; 11 import sbylib.wrapper.assimp.light : Light; 12 import sbylib.wrapper.assimp.camera : Camera; 13 14 class Scene { 15 16 private const(aiScene)* scene; 17 18 private this(const(aiScene)* scene) 19 in (scene !is null) 20 { 21 this.scene = scene; 22 } 23 24 static fromFile(string file, PostProcessFlag flags = PostProcessFlag.None) { 25 import std.format : format; 26 import std..string : toStringz; 27 28 auto scene = aiImportFile(file.toStringz, flags); 29 assert(scene !is null, format!"Load error: %s cannot be loaded."(file)); 30 return new Scene(scene); 31 } 32 33 void destroy() { 34 aiReleaseImport(scene); 35 } 36 37 Scene dup() { 38 aiScene* newScene; 39 aiCopyScene(this.scene, &newScene); 40 return new Scene(newScene); 41 } 42 43 uint flags() { 44 return scene.mFlags; 45 } 46 47 Node rootNode() { 48 return Node(scene.mRootNode); 49 } 50 51 auto meshes() { 52 return toArray!((const(aiMesh)* mesh) => Mesh(mesh)) 53 (scene.mNumMeshes, scene.mMeshes); 54 } 55 56 auto materials() { 57 return toArray!((const(aiMaterial)* mat) => Material(mat)) 58 (scene.mNumMaterials, scene.mMaterials); 59 } 60 61 auto animations() { 62 return toArray!((const(aiAnimation)* anim) => Animation(anim)) 63 (scene.mNumAnimations, scene.mAnimations); 64 } 65 66 auto textures() { 67 return toArray!((const(aiTexture)* tex) => Texture(tex)) 68 (scene.mNumTextures, scene.mTextures); 69 } 70 71 auto lights() { 72 return toArray!((const(aiLight)* light) => Light(light)) 73 (scene.mNumLights, scene.mLights); 74 } 75 76 auto cameras() { 77 return toArray!((const(aiCamera)* camera) => Camera(camera)) 78 (scene.mNumCameras, scene.mCameras); 79 } 80 81 override string toString() { 82 import std.algorithm : map; 83 import std.array : array; 84 import sbylib.wrapper.assimp.functions : tree; 85 86 string[] strs; 87 strs ~= rootNode.toString(this); 88 strs ~= lights.map!(l => l.toString()).array; 89 strs ~= cameras.map!(c => c.toString()).array; 90 91 return tree("Scene", strs); 92 } 93 }