func NewProgram(vertexShaderSource, fragmentShaderSource string) (uint32, error) { vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER) if err != nil { return 0, err } fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER) if err != nil { return 0, err } program := gl.CreateProgram() gl.AttachShader(program, vertexShader) gl.AttachShader(program, fragmentShader) gl.LinkProgram(program) var status int32 gl.GetProgramiv(program, gl.LINK_STATUS, &status) if status == gl.FALSE { 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)) gl.DeleteProgram(program) gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShader) return 0, errors.New(fmt.Sprintf("failed to link program: %v", log)) } gl.DetachShader(program, vertexShader) gl.DetachShader(program, fragmentShader) gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShader) return program, nil }
// Destroying a ShaderProgram. func DestroyShaderProgram(shaderProgram ShaderProgram) { gl.DeleteProgram(uint32(shaderProgram)) }
func InitShader(shader graphics.ShaderSource) (uint32, error) { //vertexshader vertexShaderHandle := gl.CreateShader(gl.VERTEX_SHADER) defer gl.DeleteShader(vertexShaderHandle) vertText := gl.Str(shader.GetVertexSource()) gl.ShaderSource(vertexShaderHandle, 1, &vertText, nil) gl.CompileShader(vertexShaderHandle) //error handling var isVertexCompiled int32 gl.GetShaderiv(vertexShaderHandle, gl.COMPILE_STATUS, &isVertexCompiled) if isVertexCompiled == gl.FALSE { var maxLength int32 gl.GetShaderiv(vertexShaderHandle, gl.INFO_LOG_LENGTH, &maxLength) log := strings.Repeat("\x00", int(maxLength+1)) gl.GetShaderInfoLog(vertexShaderHandle, maxLength, nil, gl.Str(log)) gl.DeleteShader(vertexShaderHandle) fmt.Println("Vertex shader compile error") fmt.Println(log) return 0, errors.New("Vertex shader compile error") } //fragment shader fragmentShaderHandle := gl.CreateShader(gl.FRAGMENT_SHADER) defer gl.DeleteShader(fragmentShaderHandle) fragText := gl.Str(shader.GetFragmentSource()) gl.ShaderSource(fragmentShaderHandle, 1, &fragText, nil) gl.CompileShader(fragmentShaderHandle) //error handling var isFragmentCompiled int32 gl.GetShaderiv(fragmentShaderHandle, gl.COMPILE_STATUS, &isFragmentCompiled) if isFragmentCompiled == gl.FALSE { var maxLength int32 gl.GetShaderiv(fragmentShaderHandle, gl.INFO_LOG_LENGTH, &maxLength) log := strings.Repeat("\x00", int(maxLength+1)) gl.GetShaderInfoLog(fragmentShaderHandle, maxLength, nil, gl.Str(log)) gl.DeleteShader(fragmentShaderHandle) fmt.Println("Fragment shader compile error") fmt.Println(log) return 0, errors.New("Fragment shader compile error") } //program shader shaderHandle := gl.CreateProgram() gl.AttachShader(shaderHandle, vertexShaderHandle) gl.AttachShader(shaderHandle, fragmentShaderHandle) gl.LinkProgram(shaderHandle) //error handling var isLinked int32 gl.GetProgramiv(shaderHandle, gl.LINK_STATUS, &isLinked) if isLinked == gl.FALSE { var maxLength int32 gl.GetProgramiv(shaderHandle, gl.INFO_LOG_LENGTH, &maxLength) log := strings.Repeat("\x00", int(maxLength+1)) gl.GetProgramInfoLog(shaderHandle, maxLength, nil, gl.Str(log)) gl.DeleteProgram(shaderHandle) fmt.Println("Shader linking error") fmt.Println(log) return 0, errors.New("Shader linking error") } return shaderHandle, nil }
//Delete is the implementation of the opengl object interface. Delete this object //from the opengl context func (shader BlankShader) Delete() { gl.DeleteProgram(shader.Handle) }
func (p *Program) Delete() { gl.DeleteVertexArrays(1, &p.vao) p.vao = 0 gl.DeleteProgram(p.program) p.program = 0 }