1 /**
2 * Copyright: Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
3 * Authors: $(Gabriel Gheorghe)
4 * License: $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
5 * Source: $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/text/shader.d)
6 * Documentation:
7 * Coverage:
8 **/
9 module liberty.text.shader;
10
11 version (none) :
12
13 import liberty.math.vector;
14 import liberty.graphics.shader;
15
16 /**
17 *
18 **/
19 final class TextShader : ShaderProgram {
20 private {
21 static immutable FONT_VERTEX = GFX_SHADER_CORE_VERSION ~ q{
22 layout (location = 0) in vec3 lPosition;
23 layout (location = 1) in vec2 lTexCoord;
24
25 out vec2 tTexCoord;
26
27 uniform vec2 uTranslation;
28
29 void main() {
30 tTexCoord = lTexCoord;
31
32 gl_Position = vec4(lPosition + vec3(uTranslation.x, uTranslation.y, 1.0)
33 * vec3(2.0, -2.0, 1.0), 1.0);
34 }
35 };
36
37 static immutable FONT_FRAGMENT = GFX_SHADER_CORE_VERSION ~ q{
38 in vec2 tTexCoord;
39
40 uniform vec3 uColor;
41 uniform sampler2D uFontAtlas;
42
43 void main() {
44 gl_FragColor = vec4(uColor, texture(uFontAtlas, tTexCoord).a);
45 }
46 };
47 }
48
49 /**
50 *
51 **/
52 this() {
53 this
54 .compileShaders(FONT_VERTEX, FONT_FRAGMENT)
55 .linkShaders()
56 .bindAttribute("lPosition")
57 .bindAttribute("lTexCoord")
58 .bind()
59 .addUniform("uTranslation")
60 .addUniform("uColor")
61 .addUniform("uFontAtlas")
62 .loadFontAtlas(0)
63 .unbind();
64 }
65
66 /**
67 *
68 * Returns reference to this so it can be used in a stream.
69 **/
70 typeof(this) loadTranslation(Vector2F matrix) {
71 super.loadUniform("uTranslation", matrix);
72 return this;
73 }
74
75 /**
76 *
77 * Returns reference to this so it can be used in a stream.
78 **/
79 typeof(this) loadColor(Vector3F matrix) {
80 super.loadUniform("uColor", matrix);
81 return this;
82 }
83
84 /**
85 *
86 * Returns reference to this so it can be used in a stream.
87 **/
88 typeof(this) loadFontAtlas(int id) {
89 super.loadUniform("uFontAtlas", id);
90 return this;
91 }
92 }