-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.go
103 lines (76 loc) · 2.13 KB
/
program.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package dull
import (
"fmt"
"strings"
"github.com/go-gl/gl/v3.3-core/gl"
)
var vertexShaderSource = `
#version 330 core
layout (location = 0) in vec2 position;
layout (location = 1) in vec2 texCoords;
layout (location = 2) in vec4 color;
out vec2 TexCoords;
out vec4 Color;
void main()
{
vec2 projection = vec2(1.0, -1.0); // Invert y-axis
gl_Position = vec4(projection * position, 0.0, 1.0);
TexCoords = texCoords;
Color = color;
}
`
var fragmentShaderSource = `
#version 330 core
in vec2 TexCoords;
in vec4 Color;
out vec4 color;
uniform sampler2D textur;
void main()
{
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(textur, TexCoords).r);
color = Color * sampled;
}
`
func newProgram() (uint32, error) {
vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
if err != nil {
return 0, err
}
fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
if err != nil {
return 0, err
}
programId := gl.CreateProgram()
gl.AttachShader(programId, vertexShader)
gl.AttachShader(programId, fragmentShader)
gl.LinkProgram(programId)
var status int32
gl.GetProgramiv(programId, gl.LINK_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetProgramiv(programId, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(programId, logLength, nil, gl.Str(log))
return 0, fmt.Errorf("failed to link program: %v", log)
}
gl.DeleteShader(vertexShader)
gl.DeleteShader(fragmentShader)
return programId, nil
}
func compileShader(source string, shaderType uint32) (uint32, error) {
csources, free := gl.Strs(source + "\x00")
defer free()
shader := gl.CreateShader(shaderType)
gl.ShaderSource(shader, 1, csources, nil)
gl.CompileShader(shader)
var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
var logLength int32
gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))
return 0, fmt.Errorf("failed to compile %v: %v", source, log)
}
return shader, nil
}