-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.go
80 lines (70 loc) · 2.21 KB
/
load.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
package main
import (
"fmt"
"strings"
"github.com/fzipp/geom"
"github.com/go-gl/gl/v4.1-core/gl"
)
func loadShaders(vertexShaderCode, fragmentShaderCode string) (vs uint32, fs uint32, err error) {
vs, err = loadShader(vertexShaderCode, gl.VERTEX_SHADER)
if err != nil {
return vs, 0, fmt.Errorf("could not load vertex shader: %s", err)
}
fs, err = loadShader(fragmentShaderCode, gl.FRAGMENT_SHADER)
if err != nil {
return vs, fs, fmt.Errorf("could not load fragment shader: %s", err)
}
return vs, fs, nil
}
func loadShader(shaderCode string, shaderType uint32) (shader uint32, err error) {
shader = gl.CreateShader(shaderType)
csources, free := gl.Strs(shaderCode + "\x00")
gl.ShaderSource(shader, 1, csources, nil)
free()
gl.CompileShader(shader)
var status int32
gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
if status == gl.FALSE {
return shader, fmt.Errorf("failed to compile GL shader: %v", getShaderInfoLog(shader))
}
return shader, nil
}
func loadProgram(shaders ...uint32) (program uint32, err error) {
p := gl.CreateProgram()
for _, shader := range shaders {
gl.AttachShader(p, shader)
}
gl.LinkProgram(p)
var status int32
gl.GetProgramiv(p, gl.LINK_STATUS, &status)
if status == gl.FALSE {
return p, fmt.Errorf("failed to link GL program: %v", getProgramInfoLog(p))
}
gl.ValidateProgram(p)
gl.GetProgramiv(p, gl.VALIDATE_STATUS, &status)
if status == gl.FALSE {
return p, fmt.Errorf("failed to validate GL program: %v", getProgramInfoLog(p))
}
for _, shader := range shaders {
gl.DeleteShader(shader)
}
return p, nil
}
func getProgramInfoLog(program uint32) string {
var logLength int32
gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))
return log
}
func getShaderInfoLog(program uint32) string {
var logLength int32
gl.GetShaderiv(program, gl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
gl.GetShaderInfoLog(program, logLength, nil, gl.Str(log))
return log
}
func loadMatrix(m *geom.Mat4, program uint32, name string) {
loc := gl.GetUniformLocation(program, gl.Str(name+"\x00"))
gl.UniformMatrix4fv(loc, 1, false, &m[0][0])
}