1 module evael.graphics.shapes.Quad; 2 3 import evael.graphics.GraphicsDevice; 4 import evael.graphics.Drawable; 5 import evael.graphics.shaders.Shader; 6 import evael.graphics.Vertex; 7 import evael.graphics.Texture; 8 9 import evael.utils.Math; 10 import evael.utils.Color; 11 import evael.utils.Size; 12 13 /** 14 * Quad. 15 * Renders indexed quad. 16 */ 17 class Quad(Type) : Drawable 18 { 19 private Type[] m_vertices; 20 private int[] m_indices; 21 22 private uint m_trianglesNumber; 23 24 /** 25 * Quad constructor. 26 * Params: 27 * graphicsDevice : graphics device 28 * vertices : quad vertices 29 * indices : quad indices 30 */ 31 @nogc 32 public this(GraphicsDevice graphicsDevice, Type[] vertices, int[] indices) nothrow 33 { 34 super(0, 0, 0); 35 36 this.m_graphicsDevice = graphicsDevice; 37 this.m_vertices = vertices; 38 this.m_indices = indices; 39 40 this.initialize(); 41 } 42 43 /** 44 * Quad destructor. 45 */ 46 public void dispose() 47 { 48 49 } 50 51 /** 52 * Initializes quad. 53 */ 54 @nogc 55 public void initialize() nothrow 56 { 57 this.m_vao = this.m_graphicsDevice.generateVAO(); 58 this.m_vertexBuffer = this.m_graphicsDevice.createVertexBuffer(Type.sizeof * this.m_vertices.length, this.m_vertices.ptr); 59 this.m_indexBuffer = this.m_graphicsDevice.createIndexBuffer(uint.sizeof * this.m_indices.length, this.m_indices.ptr); 60 61 this.m_graphicsDevice.setVertexBuffer!(Type)(this.m_vertexBuffer); 62 63 this.m_graphicsDevice.bindVAO(0); 64 65 this.m_trianglesNumber = this.m_indices.length / 3; 66 } 67 68 public override void draw(in float deltaTime, mat4 view, mat4 projection) 69 { 70 if (!this.m_isVisible) 71 return; 72 73 this.m_graphicsDevice.enableShader(this.m_shader); 74 75 if (this.m_texture !is null) 76 { 77 this.m_graphicsDevice.bindTexture(this.m_texture); 78 } 79 80 mat4 translation = translationMatrix(this.m_position); 81 mat4 rotation = this.m_rotation.toMatrix4x4(); 82 mat4 scale = scaleMatrix(this.m_scale); 83 mat4 model = translation * rotation * scale; 84 85 this.m_graphicsDevice.setMatrix(this.m_shader.modelMatrix, model.arrayof.ptr); 86 this.m_graphicsDevice.setMatrix(this.m_shader.viewMatrix, view.arrayof.ptr); 87 this.m_graphicsDevice.setMatrix(this.m_shader.projectionMatrix, projection.arrayof.ptr); 88 89 this.m_graphicsDevice.bindVAO(this.m_vao); 90 91 this.m_graphicsDevice.drawIndexedPrimitives!(PrimitiveType.Triangle)(this.m_trianglesNumber); 92 93 this.m_graphicsDevice.bindVAO(0); 94 95 this.m_graphicsDevice.disableShader(); 96 } 97 98 public void draw(in float deltaTime) 99 { 100 this.draw(deltaTime, this.m_graphicsDevice.viewMatrix, this.m_graphicsDevice.projectionMatrix); 101 } 102 103 @nogc 104 @property nothrow 105 { 106 public Type[] vertices() 107 { 108 return this.m_vertices; 109 } 110 111 public int[] indices() 112 { 113 return this.m_indices; 114 } 115 } 116 }