// loads shader objects and then attaches them to a program func LoadShaderProgram(vertShader string, fragShader string, attribs []string) (*Program, error) { // create the program var prog uint32 = gl.CreateProgram() var p *Program = &Program{prog, make(map[string]int32)} // create the vertex shader var vs uint32 = gl.CreateShader(gl.VERTEX_SHADER) ShaderSource(vs, vertShader) gl.CompileShader(vs) var success int32 = gl.FALSE gl.GetShaderiv(vs, gl.COMPILE_STATUS, &success) if success == gl.FALSE { return nil, fmt.Errorf("Failed to compile the vertex shader!\n%s", GetShaderInfoLog(vs)) } // create the fragment shader var fs uint32 = gl.CreateShader(gl.FRAGMENT_SHADER) ShaderSource(fs, fragShader) gl.CompileShader(fs) success = gl.FALSE gl.GetShaderiv(fs, gl.COMPILE_STATUS, &success) if success == gl.FALSE { return nil, fmt.Errorf("Failed to compile the fragment shader!\n%s", GetShaderInfoLog(fs)) } // attach the shaders to the program and link gl.AttachShader(prog, vs) gl.AttachShader(prog, fs) for i := 1; i <= len(attribs); i++ { var attr_array []uint8 = StringToArray(attribs[i-1]) gl.BindAttribLocation(prog, uint32(i), &attr_array[0]) } gl.LinkProgram(prog) success = gl.FALSE gl.GetProgramiv(prog, gl.LINK_STATUS, &success) if success == gl.FALSE { return nil, fmt.Errorf("Failed to link the program!\n%s", p.GetProgramInfoLog()) } // at this point the shaders can be deleted gl.DeleteShader(vs) gl.DeleteShader(fs) return p, nil }
func (self *ShaderInfo) CompileShader() (uint32, error) { content, err := ioutil.ReadFile(self.Path) if err != nil { return 0, err } contentString := bytes.NewBuffer(content).String() glContentString := gl.Str(contentString + "\x00") shader := gl.CreateShader(self.ShaderType) gl.ShaderSource(shader, 1, &glContentString, nil) gl.CompileShader(shader) var status int32 gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log)) return 0, fmt.Errorf("failed to compile %v: %v", contentString, log) } return shader, nil }
func compileShader(shaderCode string, shaderType uint32) uint32 { shader := gl.CreateShader(shaderType) shaderChars := gl.Str(shaderCode + "\x00") gl.ShaderSource(shader, 1, &shaderChars, nil) gl.CompileShader(shader) checkShaderCompileErrors(shader) return shader }
func compileShaders() []uint32 { // create the vertex shader vertexShader := gl.CreateShader(gl.VERTEX_SHADER) shaderSourceChars := gl.Str(vertexShaderSource + "\x00") gl.ShaderSource(vertexShader, 1, &shaderSourceChars, nil) gl.CompileShader(vertexShader) checkShaderCompileErrors(vertexShader) // create the fragment shader fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER) shaderSourceChars = gl.Str(fragmentShaderSource + "\x00") gl.ShaderSource(fragmentShader, 1, &shaderSourceChars, nil) gl.CompileShader(fragmentShader) checkShaderCompileErrors(fragmentShader) return []uint32{vertexShader, fragmentShader} }
func createProgram() uint32 { vs := gl.CreateShader(gl.VERTEX_SHADER) cvertexShader := gl.Str(vertexShader) gl.ShaderSource(vs, 1, &cvertexShader, nil) gl.CompileShader(vs) fs := gl.CreateShader(gl.FRAGMENT_SHADER) cfragmentShader := gl.Str(fragmentShader) gl.ShaderSource(fs, 1, &cfragmentShader, nil) gl.CompileShader(fs) shaderProgram := gl.CreateProgram() gl.AttachShader(shaderProgram, fs) gl.AttachShader(shaderProgram, vs) gl.LinkProgram(shaderProgram) return shaderProgram }
func (me *Core) AddProgram(vertFile, fragFile string) int { vertexShader := gl.Str(readWholeFile(vertFile) + "\x00") fragmentShader := gl.Str(readWholeFile(fragFile) + "\x00") n := me.nbProg vs := gl.CreateShader(gl.VERTEX_SHADER) gl.ShaderSource(vs, 1, &vertexShader, nil) gl.CompileShader(vs) fs := gl.CreateShader(gl.FRAGMENT_SHADER) gl.ShaderSource(fs, 1, &fragmentShader, nil) gl.CompileShader(fs) me.Progs[n] = gl.CreateProgram() gl.AttachShader(me.Progs[n], fs) gl.AttachShader(me.Progs[n], vs) gl.LinkProgram(me.Progs[n]) me.nbProg++ return n }
func NewShader(src string, sType uint32) (*Shader, error) { handle := gl.CreateShader(sType) glSrc := gl.Str(src + "\x00") gl.ShaderSource(handle, 1, &glSrc, nil) gl.CompileShader(handle) err := getGlError(handle, gl.COMPILE_STATUS, gl.GetShaderiv, gl.GetShaderInfoLog, "SHADER::COMPILE_FAILURE::") if err != nil { return nil, err } return &Shader{handle: handle}, nil }
func compileShader(source string, shaderType uint32) (uint32, error) { shader := gl.CreateShader(shaderType) csource := gl.Str(source) gl.ShaderSource(shader, 1, &csource, nil) gl.CompileShader(shader) var status int32 gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log)) return 0, fmt.Errorf("failed to compile %v: %v", source, log) } return shader, nil }