-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtext_shader.go
66 lines (50 loc) · 1.5 KB
/
text_shader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package three
import (
"github.com/go-gl/gl"
"github.com/go-gl/glh"
)
// MakeTextShader creates a new shader program
// for text rendering purposes.
func MakeTextShader() gl.Program {
vertSource := loadTextVertexShader()
fragSource := loadTextFragmentShader()
return glh.NewProgram(
glh.Shader{Type: gl.VERTEX_SHADER, Program: string(vertSource)},
glh.Shader{Type: gl.FRAGMENT_SHADER, Program: string(fragSource)},
)
}
func loadTextVertexShader() string {
vertex := `
#version 330
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec2 vertexPosition_screenspace;
layout(location = 1) in vec2 vertexUV;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
void main(){
// Output position of the vertex, in clip space
// map [0..800][0..600] to [-1..1][-1..1]
vec2 vertexPosition_homoneneousspace = vertexPosition_screenspace - vec2(400,300); // [0..800][0..600] -> [-400..400][-300..300]
vertexPosition_homoneneousspace /= vec2(400,300);
gl_Position = vec4(vertexPosition_homoneneousspace,0,1);
// UV of the vertex. No special space for this one.
UV = vertexUV;
}`
return vertex
}
func loadTextFragmentShader() string {
fragment := `
#version 330
// Interpolated values from the vertex shaders
in vec2 UV;
// Ouput data
out vec4 color;
// Values that stay constant for the whole mesh.
uniform vec3 diffuse;
uniform sampler2D textureSampler;
void main(){
color = vec4(diffuse, 1.0) * texture(textureSampler, UV);
}
`
return fragment
}