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 enforce!glEnableVertexAttribArray( _posLoc ); 35 if ( textureSupport ) { 36 enforce!glEnableVertexAttribArray( _uvLoc ); 37 } 38 } 39 40 override const @property bool textureSupport () 41 { 42 return _uvLoc >= 0; 43 } 44 45 override void uploadMatrix ( mat4 m ) 46 { 47 enforce!glUniformMatrix4fv( _matrixLoc, 1, GL_TRUE, &m[0][0] ); 48 } 49 50 override void uploadPositionBuffer ( in ArrayBuffer buf ) 51 { 52 buf.bind(); 53 enforce!glVertexAttribPointer( _posLoc, 4, GL_FLOAT, GL_FALSE, 0, null ); 54 } 55 override void uploadUvBuffer ( in ArrayBuffer buf ) 56 { 57 if ( textureSupport ) { 58 buf.bind(); 59 enforce!glVertexAttribPointer( _uvLoc, 2, GL_FLOAT, GL_FALSE, 0, null ); 60 } else { 61 super.uploadUvBuffer( buf ); 62 } 63 } 64 }