1 module evael.graphics.shapes.Circle; 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 * Circle. 15 * Renders circle. 16 */ 17 class Circle : Drawable 18 { 19 private uint m_divisions; 20 private uint m_radius; 21 22 /** 23 * Circle constructor. 24 * Params: 25 * graphicsDevice : graphics device 26 * divisions : circle divisions 27 * radius : circle raddius 28 */ 29 @nogc @safe 30 public this(GraphicsDevice graphicsDevice, in uint divisions, in uint radius) pure nothrow 31 { 32 super(); 33 34 this.m_graphicsDevice = graphicsDevice; 35 36 // We need two additional points, both are origin 37 this.m_divisions = divisions + 2; 38 39 this.m_radius = radius; 40 } 41 42 /** 43 * Initializes circle. 44 * TODO: @nogc 45 */ 46 public void initialize() nothrow 47 { 48 import std.math; 49 50 auto vertices = new VertexPositionColor!3[this.m_divisions]; 51 immutable color = Color(0, 130, 206, 160); 52 immutable originPoint = VertexPositionColor!3(vec3(0, 2, 0), Color(0, 120, 180, 130)); 53 54 vertices[0] = originPoint; 55 56 // 2 * PI, we can add / this.m_divisions 57 auto circleRadians = 2 * PI / (this.m_divisions - 2); 58 59 for(int i = 1; i < (this.m_divisions - 1); i++) 60 { 61 vertices[i] = VertexPositionColor!3( 62 vec3(this.m_radius * cos (i * circleRadians), 2, this.m_radius * sin (i * circleRadians)), color 63 ); 64 } 65 66 vertices[$ - 1] = vertices[1]; 67 68 this.m_vertexBuffer = this.m_graphicsDevice.createVertexBuffer(VertexPositionColor!3.sizeof * vertices.length, vertices.ptr); 69 } 70 71 public override void draw(in float deltaTime, mat4 view, mat4 projection) 72 { 73 if (!this.m_isVisible) 74 return; 75 76 gl.DepthMask(false); 77 78 this.m_graphicsDevice.enableShader(this.m_shader); 79 80 //this.m_graphicsDevice.bindTexture(this.m_texture); 81 82 mat4 model = translationMatrix(this.m_position); 83 84 if(this.m_scale.x != 1.0f || this.m_scale.z != 1.0f) 85 { 86 model *= scaleMatrix(this.m_scale); 87 } 88 89 this.m_graphicsDevice.setMatrix(this.m_shader.modelMatrix, model.arrayof.ptr); 90 this.m_graphicsDevice.setMatrix(this.m_shader.viewMatrix, view.arrayof.ptr); 91 this.m_graphicsDevice.setMatrix(this.m_shader.projectionMatrix, projection.arrayof.ptr); 92 93 this.m_graphicsDevice.setVertexBuffer!(VertexPositionColor!3)(this.m_vertexBuffer); 94 this.m_graphicsDevice.drawPrimitives!(PrimitiveType.TriangleFan)(this.m_divisions); 95 96 this.m_graphicsDevice.disableShader(); 97 98 gl.DepthMask(true); 99 } 100 101 public void draw(in float deltaTime) 102 { 103 this.draw(deltaTime, this.m_graphicsDevice.viewMatrix, this.m_graphicsDevice.projectionMatrix); 104 } 105 }