func (p *Program) compileShader(stype uint32, source string) (shader uint32, err error) { csources, free := gl.Strs(source) shader = gl.CreateShader(stype) gl.ShaderSource(shader, 1, csources, nil) free() gl.CompileShader(shader) var status int32 if gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status); status == gl.FALSE { var length int32 gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &length) log := strings.Repeat("\x00", int(length+1)) gl.GetShaderInfoLog(shader, length, nil, gl.Str(log)) err = fmt.Errorf("ERROR shader compile:\n%s", log) } return }
func compileShader(source string, shaderType uint32) (uint32, error) { shader := gl.CreateShader(shaderType) csource, free := gl.Strs(source) gl.ShaderSource(shader, 1, csource, nil) free() 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 }
// LoadShaderProgram loads shader objects and then attaches them to a program func LoadShaderProgram(vertShader, fragShader string) (uint32, error) { // create the program prog := gl.CreateProgram() // create the vertex shader vs := gl.CreateShader(gl.VERTEX_SHADER) cVertShader, free := gl.Strs(vertShader + "\x00") gl.ShaderSource(vs, 1, cVertShader, nil) gl.CompileShader(vs) free() var status int32 gl.GetShaderiv(vs, gl.COMPILE_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetShaderiv(vs, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetShaderInfoLog(vs, logLength, nil, gl.Str(log)) err := fmt.Sprintf("Failed to compile the vertex shader!\n%s", log) fmt.Println(err) return 0, errors.New(err) } defer gl.DeleteShader(vs) // create the fragment shader fs := gl.CreateShader(gl.FRAGMENT_SHADER) cFragShader, free := gl.Strs(fragShader + "\x00") gl.ShaderSource(fs, 1, cFragShader, nil) gl.CompileShader(fs) free() gl.GetShaderiv(fs, gl.COMPILE_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetShaderiv(fs, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetShaderInfoLog(fs, logLength, nil, gl.Str(log)) err := fmt.Sprintf("Failed to compile the fragment shader!\n%s", log) fmt.Println(err) return 0, errors.New(err) } defer gl.DeleteShader(fs) // attach the shaders to the program and link // attach the shaders to the program and link gl.AttachShader(prog, vs) gl.AttachShader(prog, fs) gl.LinkProgram(prog) gl.GetProgramiv(prog, gl.LINK_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetProgramiv(prog, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetProgramInfoLog(prog, logLength, nil, gl.Str(log)) error := fmt.Sprintf("Failed to link the program!\n%s", log) fmt.Println(error) return 0, errors.New(error) } return prog, nil }