1 // Written in the D programming language.
2 /++
3  + Authors: KanzakiKino
4  + Copyright: KanzakiKino 2018
5  + License: LGPL-3.0
6 ++/
7 module g4d.shader.matrix;
8 import gl3n.linalg;
9 
10 /// A matrix data for the shader program.
11 struct ShaderMatrix
12 {
13     protected mat4 _cache;
14     protected bool _needRecalc = false;
15 
16     protected mat4 _projection = mat4.identity;
17 
18     protected vec3 _late = vec3(0,0,0);
19     protected vec3 _rota = vec3(0,0,0);
20     protected vec3 _form = vec3(1,1,1);
21 
22     /// Projection matrix.
23     const @property projection () { return _projection; }
24 
25     ///
26     @property void projection ( mat4 m )
27     {
28         _projection = m;
29         _needRecalc = true;
30     }
31 
32     /// Size of translation.
33     const @property late () { return _late; }
34     /// Amount of rotation.
35     const @property rota () { return _rota; }
36     /// Ratio of transformation.
37     const @property form () { return _form; }
38 
39     ///
40     @property void late ( vec3 v )
41     {
42         _late       = v;
43         _needRecalc = true;
44     }
45     ///
46     @property void rota ( vec3 v )
47     {
48         _rota       = v;
49         _needRecalc = true;
50     }
51     ///
52     @property void form ( vec3 v )
53     {
54         _form       = v;
55         _needRecalc = true;
56     }
57 
58     /// Cache of the calculation.
59     /// If recalculating is needed, calculates before return.
60     @property cache ()
61     {
62         if ( _needRecalc ) {
63             calc();
64         }
65         return _cache;
66     }
67 
68     /// Recalculates the matrix.
69     void calc ()
70     {
71         _cache = mat4.scaling( _form.x, _form.y, _form.z );
72 
73         _cache.rotatex( _rota.x );
74         _cache.rotatey( _rota.y );
75         _cache.rotatez( _rota.z );
76 
77         _cache.translate( _late );
78 
79         _cache = _projection * _cache;
80 
81         _needRecalc = false;
82     }
83 }