示例#1
0
func setupShaders(gl *webgl.Context) *js.Object {
	vs := gl.CreateShader(gl.VERTEX_SHADER)
	gl.ShaderSource(vs, vsSource)
	gl.CompileShader(vs)

	if gl.GetShaderParameter(vs, gl.COMPILE_STATUS).Bool() == false {
		throw(errors.New(gl.GetShaderInfoLog(vs)))
	}

	ps := gl.CreateShader(gl.FRAGMENT_SHADER)
	gl.ShaderSource(ps, psSource)
	gl.CompileShader(ps)

	if gl.GetShaderParameter(ps, gl.COMPILE_STATUS).Bool() == false {
		throw(errors.New(gl.GetShaderInfoLog(ps)))
	}

	program := gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, ps)

	gl.LinkProgram(program)
	gl.UseProgram(program)
	return program
}
示例#2
0
func initShader(gl *webgl.Context, canvas *js.Object) (*js.Object, bool) {
	shader := gl.CreateProgram()
	vertexShader, ok := getShader(gl, gl.VERTEX_SHADER, vertexShaderSource)
	if !ok {
		js.Global.Call("alert", "Error getting vertex shader")
		return nil, false
	}
	fragShader, ok := getShader(gl, gl.FRAGMENT_SHADER, fragShaderSource)
	if !ok {
		js.Global.Call("alert", "Error getting fragment shader")
		return nil, false
	}
	gl.AttachShader(shader, vertexShader)
	gl.AttachShader(shader, fragShader)
	gl.LinkProgram(shader)
	if !gl.GetProgramParameterb(shader, gl.LINK_STATUS) {
		js.Global.Call("alert", "couldnt init shaders :(")
		return nil, false
	}
	gl.UseProgram(shader)

	return shader, true
}