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.vertex.threedim;
8 
9 /// GLSL source code of 3d vert shader.
10 enum ThreeDimVertexShaderSource = import("g4d/shader/vertex/threedim.glsl");
11 
12 /// A template for the shader program that uses 3d vert shader.
13 template ThreeDimVertexShader ()
14 {
15     import g4d.gl.buffer,
16            g4d.gl.lib;
17     import gl3n.linalg;
18 
19     override const pure @property string vertexSource ()
20     {
21         return ThreeDimVertexShaderSource;
22     }
23 
24     protected GLint _matrixLoc;
25     protected GLint _posLoc;
26     protected GLint _uvLoc;
27 
28     protected override void initVertexShader ()
29     {
30         _matrixLoc = getUniformLoc( "matrix" );
31         _posLoc    = getAttribLoc( "pos" );
32         _uvLoc     = getAttribLoc( "uv" );
33     }
34 
35     override const @property bool textureSupport ()
36     {
37         return _uvLoc >= 0;
38     }
39 
40     override void uploadMatrix ( mat4 m )
41     {
42         enforce!glUniformMatrix4fv( _matrixLoc, 1, GL_TRUE, &m[0][0] );
43     }
44 
45     override void uploadPositionBuffer ( in ArrayBuffer buf )
46     {
47         buf.bind();
48         enforce!glEnableVertexAttribArray( _posLoc );
49         enforce!glVertexAttribPointer( _posLoc, 4, GL_FLOAT, GL_FALSE, 0, null );
50     }
51     override void uploadUvBuffer ( in ArrayBuffer buf )
52     {
53         if ( textureSupport ) {
54             buf.bind();
55             enforce!glEnableVertexAttribArray( _uvLoc );
56             enforce!glVertexAttribPointer( _uvLoc, 2, GL_FLOAT, GL_FALSE, 0, null );
57         } else {
58             super.uploadUvBuffer( buf );
59         }
60     }
61 }