2013-10-25 21:15:27 +02:00
|
|
|
package shader
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
import (
|
2014-12-06 07:47:48 +01:00
|
|
|
"github.com/go-gl/gl"
|
|
|
|
"log"
|
2013-06-15 10:07:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type shader struct {
|
2014-12-06 07:47:48 +01:00
|
|
|
native gl.Shader
|
|
|
|
shaderType gl.GLenum
|
2013-12-12 16:31:00 +01:00
|
|
|
source string
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
type shaderId int
|
|
|
|
|
|
|
|
const (
|
|
|
|
shaderVertex shaderId = iota
|
|
|
|
shaderFragment
|
|
|
|
shaderColorMatrix
|
|
|
|
shaderSolidColor
|
|
|
|
)
|
|
|
|
|
|
|
|
var shaders = map[shaderId]*shader{
|
2014-12-06 14:56:57 +01:00
|
|
|
shaderVertex: {
|
2014-12-06 07:47:48 +01:00
|
|
|
shaderType: gl.VERTEX_SHADER,
|
2013-06-15 10:07:14 +02:00
|
|
|
source: `
|
2013-12-12 16:31:00 +01:00
|
|
|
uniform mat4 projection_matrix;
|
|
|
|
uniform mat4 modelview_matrix;
|
2013-12-13 21:34:27 +01:00
|
|
|
attribute vec2 vertex;
|
|
|
|
attribute vec2 tex_coord;
|
|
|
|
varying vec2 vertex_out_tex_coord;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
void main(void) {
|
2013-12-13 21:34:27 +01:00
|
|
|
vertex_out_tex_coord = tex_coord;
|
2013-06-15 10:07:14 +02:00
|
|
|
gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 0, 1);
|
|
|
|
}
|
|
|
|
`,
|
2013-12-12 16:31:00 +01:00
|
|
|
},
|
2014-12-06 14:56:57 +01:00
|
|
|
shaderFragment: {
|
2014-12-06 07:47:48 +01:00
|
|
|
shaderType: gl.FRAGMENT_SHADER,
|
2013-06-15 10:07:14 +02:00
|
|
|
source: `
|
2013-12-12 16:31:00 +01:00
|
|
|
uniform sampler2D texture;
|
2013-12-13 21:34:27 +01:00
|
|
|
varying vec2 vertex_out_tex_coord;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
void main(void) {
|
2013-12-13 21:34:27 +01:00
|
|
|
gl_FragColor = texture2D(texture, vertex_out_tex_coord);
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
`,
|
2013-12-12 16:31:00 +01:00
|
|
|
},
|
2014-12-06 14:56:57 +01:00
|
|
|
shaderColorMatrix: {
|
2014-12-06 07:47:48 +01:00
|
|
|
shaderType: gl.FRAGMENT_SHADER,
|
2013-06-15 10:07:14 +02:00
|
|
|
source: `
|
2013-12-12 16:31:00 +01:00
|
|
|
uniform sampler2D texture;
|
|
|
|
uniform mat4 color_matrix;
|
|
|
|
uniform vec4 color_matrix_translation;
|
2013-12-13 21:34:27 +01:00
|
|
|
varying vec2 vertex_out_tex_coord;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
void main(void) {
|
2013-12-13 21:34:27 +01:00
|
|
|
vec4 color = texture2D(texture, vertex_out_tex_coord);
|
2013-06-15 10:07:14 +02:00
|
|
|
gl_FragColor = (color_matrix * color) + color_matrix_translation;
|
|
|
|
}
|
|
|
|
`,
|
2013-12-12 16:31:00 +01:00
|
|
|
},
|
2014-12-06 14:56:57 +01:00
|
|
|
shaderSolidColor: {
|
2014-12-06 07:47:48 +01:00
|
|
|
shaderType: gl.FRAGMENT_SHADER,
|
2013-12-12 16:31:00 +01:00
|
|
|
source: `
|
|
|
|
uniform vec4 color;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
void main(void) {
|
|
|
|
gl_FragColor = color;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
func (s *shader) compile() {
|
2014-12-06 07:47:48 +01:00
|
|
|
s.native = gl.CreateShader(s.shaderType)
|
2013-12-12 16:31:00 +01:00
|
|
|
if s.native == 0 {
|
|
|
|
panic("glCreateShader failed")
|
|
|
|
}
|
|
|
|
|
2014-12-06 07:47:48 +01:00
|
|
|
s.native.Source(s.source)
|
|
|
|
s.native.Compile()
|
2013-06-15 10:07:14 +02:00
|
|
|
|
2014-12-06 07:47:48 +01:00
|
|
|
if s.native.Get(gl.COMPILE_STATUS) == gl.FALSE {
|
2013-06-15 10:07:14 +02:00
|
|
|
s.showShaderLog()
|
2013-12-12 16:31:00 +01:00
|
|
|
panic("shader compile failed")
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *shader) showShaderLog() {
|
2014-12-06 07:47:48 +01:00
|
|
|
if s.native.Get(gl.INFO_LOG_LENGTH) == 0 {
|
2013-06-15 10:07:14 +02:00
|
|
|
return
|
|
|
|
}
|
2014-12-06 07:47:48 +01:00
|
|
|
log.Fatalf("shader error: %s\n", s.native.GetInfoLog())
|
2013-12-12 16:31:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *shader) delete() {
|
2014-12-06 07:47:48 +01:00
|
|
|
s.native.Delete()
|
2013-06-15 10:07:14 +02:00
|
|
|
}
|