-
Notifications
You must be signed in to change notification settings - Fork 2
/
shader.go
83 lines (65 loc) · 1.87 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package examples
import (
"fmt"
"os"
"strings"
"github.com/go-gl/gl/v2.1/gl"
)
func CheckGLErrors() {
for err := gl.GetError(); err != 0; err = gl.GetError() {
switch err {
case gl.NO_ERROR:
// ok
case gl.INVALID_ENUM:
panic("Invalid enum")
case gl.INVALID_VALUE:
panic("Invalid value")
case gl.INVALID_OPERATION:
panic("Invalid operation")
case gl.INVALID_FRAMEBUFFER_OPERATION:
panic("Invalid Framebuffer Operation")
case gl.OUT_OF_MEMORY:
panic("Out of memory")
}
}
}
func CheckError(obj uint32, status uint32, getiv func(uint32, uint32, *int32), getInfoLog func(uint32, int32, *int32, *uint8)) bool {
var success int32
getiv(obj, status, &success)
if success == gl.FALSE {
var length int32
getiv(obj, gl.INFO_LOG_LENGTH, &length)
log := strings.Repeat("\x00", int(length+1))
getInfoLog(obj, length, nil, gl.Str(log))
fmt.Fprintln(os.Stderr, "GL Error:", log)
return true
}
return false
}
func CompileShader(typ uint32, source string) uint32 {
shader := gl.CreateShader(typ)
sources, free := gl.Strs(source + "\x00")
defer free()
gl.ShaderSource(shader, 1, sources, nil)
gl.CompileShader(shader)
if CheckError(shader, gl.COMPILE_STATUS, gl.GetShaderiv, gl.GetShaderInfoLog) {
panic("Error compiling shader")
}
return shader
}
func LinkProgram(vshader, fshader uint32) uint32 {
p := gl.CreateProgram()
gl.AttachShader(p, vshader)
gl.AttachShader(p, fshader)
gl.LinkProgram(p)
if CheckError(p, gl.LINK_STATUS, gl.GetProgramiv, gl.GetProgramInfoLog) {
panic("Error linking shader program")
}
return p
}
func SetAttribute(program uint32, name string, size int32, gltype uint32, stride int32, offset int) {
var index uint32 = uint32(gl.GetAttribLocation(program, gl.Str(name+"\x00")))
gl.EnableVertexAttribArray(index)
gl.VertexAttribPointer(index, size, gltype, false, stride, gl.PtrOffset(offset))
CheckGLErrors()
}