Ejemplo n.º 1
0
func compileShaders() (err error) {
	var glStatus gl.Int

	makeShader := func(stage gl.Enum, src string) (shader gl.Uint) {
		shader = gl.CreateShader(stage)
		logLastGlError("gl.CreateShader()")
		glStr := gl.Util.CStringArray(src)
		defer gl.Util.CStringArrayFree(glStr)
		gl.ShaderSource(shader, gl.Sizei(len(glStr)), &glStr[0], nil)
		logLastGlError("gl.ShaderSource()")
		gl.CompileShader(shader)
		if gl.GetShaderiv(shader, gl.COMPILE_STATUS, &glStatus); glStatus == 0 {
			panic("Shader compilation failed: " + src)
		}
		return
	}

	glVertShader := makeShader(gl.VERTEX_SHADER, srcVertShader)
	defer gl.DeleteShader(glVertShader)
	glFragShader := makeShader(gl.FRAGMENT_SHADER, srcFragShader)
	defer gl.DeleteShader(glFragShader)

	shaderProg = gl.CreateProgram()
	for stageName, shader := range map[string]gl.Uint{"vert": glVertShader, "frag": glFragShader} {
		gl.AttachShader(shaderProg, shader)
		logLastGlError("gl.AttachShader " + stageName)
		defer gl.DetachShader(shaderProg, shader)
	}

	gl.LinkProgram(shaderProg)
	if gl.GetProgramiv(shaderProg, gl.LINK_STATUS, &glStatus); glStatus == 0 {
		panic("Program linking failed...")
	}

	glAttName := gl.Util.CString("aPos")
	defer gl.Util.CStringFree(glAttName)
	progPosAttrLoc = gl.Uint(gl.GetAttribLocation(shaderProg, glAttName))
	logLastGlError("gl.GetAttribLocation()")

	return
}
Ejemplo n.º 2
0
//	Compiles this shader object. This is a convenience short-hand for calling gl.CompileShader(),
//	checking gl.COMPILE_STATUS and obtaining gl.GetShaderInfoLog().
func (me *Shader) Compile(name string) (err error) {
	if gl.CompileShader(me.GlHandle); me.ParamInt(gl.COMPILE_STATUS) == gl.FALSE {
		err = errf("%s'%s'.Compile() error: %s\n", me.StageName(), name, me.InfoLog(name))
	}
	return
}