-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshader.go
83 lines (61 loc) · 1.67 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
package gosigl
import (
"fmt"
opengl "github.com/go-gl/gl/v4.1-core/gl"
"strings"
)
type ShaderType uint32
const VertexShader = ShaderType(opengl.VERTEX_SHADER)
const FragmentShader = ShaderType(opengl.FRAGMENT_SHADER)
type Context struct {
context uint32
}
func (ctx *Context) Id() uint32 {
return ctx.context
}
func (ctx *Context) AddShader(source string, shaderType ShaderType) error {
shader, err := ctx.compileShader(source, shaderType)
if err != nil {
return err
}
opengl.AttachShader(ctx.context, shader)
return nil
}
func (ctx *Context) Finalize() {
opengl.LinkProgram(ctx.context)
}
func (ctx *Context) UseProgram() {
opengl.UseProgram(ctx.context)
}
func (ctx *Context) GetUniform(name string) int32 {
return opengl.GetUniformLocation(ctx.context, opengl.Str(name+"\x00"))
}
func (ctx *Context) compileShader(source string, shaderType ShaderType) (uint32, error) {
shader := opengl.CreateShader(uint32(shaderType))
csources, free := opengl.Strs(source)
opengl.ShaderSource(shader, 1, csources, nil)
free()
opengl.CompileShader(shader)
var status int32
opengl.GetShaderiv(shader, opengl.COMPILE_STATUS, &status)
if status == opengl.FALSE {
var logLength int32
opengl.GetShaderiv(shader, opengl.INFO_LOG_LENGTH, &logLength)
log := strings.Repeat("\x00", int(logLength+1))
opengl.GetShaderInfoLog(shader, logLength, nil, opengl.Str(log))
return 0, fmt.Errorf("failed to compile %v: %v", source, log)
}
return shader, nil
}
func (ctx *Context) Destroy() {
opengl.DeleteShader(ctx.Id())
}
func NewShader() Context {
if err := opengl.Init(); err != nil {
panic(err)
}
context := Context{
context: opengl.CreateProgram(),
}
return context
}