1 /**
2  * Copyright:       Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3  * Authors:         $(Gabriel Gheorghe)
4  * License:         $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5  * Source:          $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/model/impl.d)
6  * Documentation:
7  * Coverage:
8 **/
9 module liberty.model.impl;
10 
11 import liberty.graphics.constants;
12 import liberty.graphics.engine;
13 import liberty.graphics.factory;
14 import liberty.material.impl;
15 import liberty.model.data;
16 import liberty.scene.services;
17 
18 ///
19 class Model : IGfxRendererFactory, IDrawable {
20   // Used to Store wireframe global state
21   private bool tempWireframeEnabled;
22   ///
23   bool cullingEnabled = true;
24   ///
25   bool wireframeEnabled;
26   ///
27   bool transparencyEnabled = true;
28   ///
29   bool fakeLightingEnabled;
30   ///
31   RawModel rawModel;
32   ///
33   Material[] materials;
34 
35   ///
36   this(RawModel rawModel, Material[] materials) {
37     this.rawModel = rawModel;
38     this.materials = materials;
39   }
40 
41   /// Returns reference to this so it can be used in a stream.
42   typeof(this) swapMaterials(Material[] materialsLhs, Material[] materialsRhs) {
43     materials = (materials == materialsLhs) ? materialsRhs : materialsLhs;
44     return this;
45   }
46 
47   /// Returns reference to this so it can be used in a stream.
48   typeof(this) swapMaterials(Material[2][] arr) {
49     foreach (i, ref material; materials)
50       material = (material == arr[i][0]) ? arr[i][1] : arr[i][0];
51 
52     return this;
53   }
54 
55   /// Render the model to the screen by calling specific draw method from $(D IGfxRendererFactory)
56   void draw() {
57     // Send culling type to graphics engine
58     GfxEngine.backend.setCullingEnabled(cullingEnabled);
59 
60     // Send alpha blend to graphics engine
61     transparencyEnabled
62       ? GfxEngine.backend.enableAlphaBlend
63       : GfxEngine.backend.disableBlend;
64 
65     // Store wireframe global state
66     tempWireframeEnabled = GfxEngine.backend.getOptions.wireframeEnabled;
67 
68     // Send wireframe type to graphics engine
69     GfxEngine.backend.setWireframeEnabled(tempWireframeEnabled ? !wireframeEnabled : wireframeEnabled);
70 
71     // Render
72     rawModel.useIndices
73       ? drawElements(GfxDrawMode.TRIANGLES, GfxVectorType.UINT, rawModel.vertexCount)
74       : drawArrays(GfxDrawMode.TRIANGLES, rawModel.vertexCount);
75 
76     // Restore wireframe global state using the stored boolean
77     GfxEngine.backend.setWireframeEnabled(tempWireframeEnabled);
78   }
79 }