func ShaderSource(name string, shader gl.Uint, source string, defines map[string]interface{}, logPrint bool) { var src []*gl.Char var i, l = 1, len(defines) var lines = make([]string, (l*5)+3) var joined string lines[0] = "#version 420 core\n" for dk, dv := range defines { lines[i+0] = "#define " lines[i+1] = dk lines[i+2] = " " lines[i+3] = fmt.Sprintf("%v", dv) lines[i+4] = "\n" i = i + 5 } lines[i] = "#line 1\n" lines[i+1] = source joined = strings.Join(lines, "") src = gl.GLStringArray(lines...) defer gl.GLStringArrayFree(src) gl.ShaderSource(shader, gl.Sizei(len(src)), &src[0], nil) PanicIfErrors("ShaderSource(name=%v source=%v)", name, joined) if logPrint { log.Printf("\n\n------------------------------\n%s\n\n", joined) } }
// Loads a shader from a string func LoadShader(typ g.Enum, s string) g.Uint { shader := g.CreateShader(typ) cst := g.GLString(s) defer g.GLStringFree(cst) g.ShaderSource(shader, 1, &cst, nil) g.CompileShader(shader) var status g.Int g.GetShaderiv(shader, g.COMPILE_STATUS, &status) if status == g.FALSE { var length g.Int g.GetShaderiv(shader, g.INFO_LOG_LENGTH, &length) log.Println(int(length)) logStr := g.GLStringAlloc(g.Sizei(length + 1)) // TODO: defer g.GetShaderInfoLog(shader, g.Sizei(length), nil, logStr) log.Println(g.GoString(logStr)) } return shader }
func glSetShaderSource(shader gl.Uint, source string) { var src = gl.GLStringArray(source) defer gl.GLStringArrayFree(src) gl.ShaderSource(shader, gl.Sizei(len(src)), &src[0], nil) }