-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshader.go
71 lines (57 loc) · 1.97 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
package noodle
//Shader holds the shaders
type Shader struct {
program WebGLShaderProgram
}
//LoadShaderFromURL loads a shader from a URL
func LoadShaderFromURL(vertURL, fragURL string) (*Shader, error) {
//Load the vertext shader
vertCode, err := DownloadString(vertURL)
if err != nil {
return nil, err
}
//Load the frag shader
fragCode, err := DownloadString(fragURL)
return LoadShader(vertCode, fragCode)
}
//LoadShader loads a shader from code
func LoadShader(vertCode, fragCode string) (*Shader, error) {
vertex, err := GL.NewShader(GlVertexShader, vertCode)
defer GL.DeleteShader(vertex)
if err != nil {
return nil, err
}
fragment, err := GL.NewShader(GlFragmentShader, fragCode)
defer GL.DeleteShader(fragment)
if err != nil {
return nil, err
}
program, err := GL.NewProgram([]WebGLShader{vertex, fragment})
if err != nil {
return nil, err
}
return &Shader{program}, nil
}
//GetProgram gets the shader program
func (shader *Shader) GetProgram() WebGLShaderProgram {
return shader.program
}
//GetUniformLocation returns the location of a specific uniform variable which is part of a given WebGLProgram.
func (shader *Shader) GetUniformLocation(location string) WebGLUniformLocation {
return GL.GetUniformLocation(shader.program, location)
}
//GetAttribLocation gets a location of an attribute
func (shader *Shader) GetAttribLocation(attribute string) WebGLAttributeLocation {
return GL.GetAttribLocation(shader.program, attribute)
}
//BindVertexData binds a buffer of vertex data to an attribute
func (shader *Shader) BindVertexData(attribute string, bufferLocation GLEnum, buffer WebGLBuffer, bufferSize int, bufferType GLEnum, normalize bool, stride int, offset int) {
pos := shader.GetAttribLocation(attribute)
GL.BindBuffer(bufferLocation, buffer)
GL.VertexAttribPointer(pos, bufferSize, bufferType, normalize, stride, offset)
GL.EnableVertexAttribArray(pos)
}
//Use tells GL to use this shader
func (shader *Shader) Use() {
GL.UseProgram(shader.program)
}