1 // Written in the D programming language.
2 /++
3  + Authors: KanzakiKino
4  + Copyright: KanzakiKino 2018
5  + License: LGPL-3.0
6 ++/
7 module g4d.element.shape.regular;
8 import g4d.element.base,
9        g4d.gl.buffer,
10        g4d.math.ngon,
11        g4d.shader.base;
12 import gl3n.linalg;
13 import std.math;
14 
15 /// An element of polygons.
16 class RegularNgonElement ( size_t _N ) : Element
17     if ( _N >= 3 )
18 {
19     /// Length of vertexes.
20     alias N = _N;
21 
22     protected ArrayBuffer _pos;
23 
24     ///
25     this ()
26     {
27         clear();
28     }
29 
30     ///
31     void clear ()
32     {
33         _pos = new ArrayBuffer( new float[N*4] );
34     }
35 
36     ///
37     void resize ( float size )
38     {
39         auto verts = genRegularNgonVertexes( N, size );
40         foreach ( i,v; verts ) {
41             _pos.overwrite( v.vector, i*4 );
42         }
43     }
44 
45     ///
46     void draw ( Shader s )
47     {
48         const saver = ShaderStateSaver( s );
49         s.uploadPositionBuffer( _pos );
50         s.applyMatrix();
51         s.drawFan( N );
52     }
53 
54     /// To prove this type.
55     enum this_is_a_regular_ngon_class_of_g4d = true;
56 }
57 
58 /// Checks if T is RegularNgonElement.
59 enum isRegularNgon(T) =
60     __traits(hasMember,T,"this_is_a_regular_ngon_class_of_g4d");