示例#1
0
文件: cube.go 项目: depy/examples
func newProgram(vertexShaderSource, fragmentShaderSource string) (uint32, error) {
	vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
	if err != nil {
		return 0, err
	}

	fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
	if err != nil {
		return 0, err
	}

	program := gl.CreateProgram()

	gl.AttachShader(program, vertexShader)
	gl.AttachShader(program, fragmentShader)
	gl.LinkProgram(program)

	var status int32
	gl.GetProgramiv(program, gl.LINK_STATUS, &status)
	if status == gl.FALSE {
		var logLength int32
		gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)

		log := strings.Repeat("\x00", int(logLength+1))
		gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))

		return 0, fmt.Errorf("failed to link program: %v", log)
	}

	gl.DeleteShader(vertexShader)
	gl.DeleteShader(fragmentShader)

	return program, nil
}
示例#2
0
// 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
}
示例#3
0
func NewProgram(shaderSource map[string]string) (*GLSLProgram, error) {
	var hasVertexShader bool
	var hasFragmentShader bool

	vertexShaderSource, ok := shaderSource["vertex"]
	var vertexShader uint32
	if ok {

		vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
		if err != nil {
			return nil, err
		}
		hasVertexShader = true
	}

	fragmentShaderSource, ok := shaderSource["fragment"]
	var fragmentShader uint32
	if ok {

		fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
		if err != nil {
			return nil, err
		}
		hasFragmentShader = true
	}

	program := gl.CreateProgram()

	if !(hasVertexShader && hasFragmentShader) {
		return nil, fmt.Errorf("Need vertex shader: %v, and fragment shader: %v", hasVertexShader, hasFragmentShader)
	}

	gl.AttachShader(program, vertexShader)
	gl.AttachShader(program, fragmentShader)
	gl.LinkProgram(program)

	var status int32
	gl.GetProgramiv(program, gl.LINK_STATUS, &status)
	if status == gl.FALSE {
		var logLength int32
		gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)

		log := strings.Repeat("\x00", int(logLength+1))
		gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))

		return nil, fmt.Errorf("Failed to link program(%v): %v", program, log)
	}

	gl.DeleteShader(vertexShader)
	gl.DeleteShader(fragmentShader)
	p := &GLSLProgram{program, vertexShaderSource, fragmentShaderSource, true}
	return p, nil
}
示例#4
0
文件: gl.go 项目: alex-ac/redbook-go
func LoadShaders(shaders []ShaderInfo) (uint32, error) {
	program := gl.CreateProgram()

	for _, shaderInfo := range shaders {
		shader, err := shaderInfo.CompileShader()
		if err != nil {
			return 0, err
		}
		gl.AttachShader(program, shader)
		gl.DeleteShader(shader)
	}

	gl.LinkProgram(program)

	var status int32
	gl.GetProgramiv(program, gl.LINK_STATUS, &status)
	if status == gl.FALSE {
		var logLength int32
		gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)

		log := strings.Repeat("\x00", int(logLength+1))
		gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))

		return 0, fmt.Errorf("Failed to link program: %v", log)
	}

	return program, nil
}
示例#5
0
func newProgram(shaders ...uint32) (uint32, error) {
	program := gl.CreateProgram()

	for _, shader := range shaders {
		gl.AttachShader(program, shader)
	}
	gl.LinkProgram(program)

	var status int32
	gl.GetProgramiv(program, gl.LINK_STATUS, &status)
	if status == gl.FALSE {
		var logLength int32
		gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)

		log := strings.Repeat("\x00", int(logLength+1))
		gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))

		return 0, errors.New(fmt.Sprintf("failed to link program: %v", log))
	}

	for _, shader := range shaders {
		gl.DeleteShader(shader)
	}

	return program, nil
}
示例#6
0
文件: triangle.go 项目: depy/examples
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
}
/*
 * Link the provided shaders in the order they were given and return the linked program.
 */
func linkShaders(shaders []uint32) uint32 {
	program := gl.CreateProgram()
	for _, shader := range shaders {
		gl.AttachShader(program, shader)
	}
	gl.LinkProgram(program)
	checkProgramLinkErrors(program)

	return program
}
示例#8
0
func newProgram(vertexShaderFilename, fragmentShaderFilename string) (uint32, error) {
	program := gl.CreateProgram()

	//vertexShaderSource, err := shaders.Asset(vertexShaderFilename)
	//if err != nil {
	//	return 0, err
	//}

	vertexShader, err := shaders.Compile(vertexShaderSource, gl.VERTEX_SHADER)
	if err != nil {
		return 0, err
	}

	//fragmentShaderSource, err := shaders.Asset(fragmentShaderFilename)
	//if err != nil {
	//	return 0, err
	//}
	fragmentShader, err := shaders.Compile(fragmentShaderSource, gl.FRAGMENT_SHADER)
	if err != nil {
		return 0, err
	}

	gl.AttachShader(program, vertexShader)
	gl.AttachShader(program, fragmentShader)
	gl.LinkProgram(program)

	var status int32
	gl.GetProgramiv(program, gl.LINK_STATUS, &status)
	if status == gl.FALSE {
		var logLength int32
		gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength)

		log := strings.Repeat("\x00", int(logLength+1))
		gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log))

		return 0, errors.New(fmt.Sprintf("failed to link program: %v", log))
	}

	gl.DeleteShader(vertexShader)
	gl.DeleteShader(fragmentShader)

	return program, nil
}
示例#9
0
文件: core.go 项目: Nekony/go-gl-test
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
}
/*
 * Link the provided shaders in the order they were given and return the linked program.
 */
func linkShaders(shaders []uint32) uint32 {
	program := gl.CreateProgram()
	for _, shader := range shaders {
		gl.AttachShader(program, shader)
	}
	gl.LinkProgram(program)
	checkProgramLinkErrors(program)

	// shader objects are not needed after they are linked into a program object
	for _, shader := range shaders {
		gl.DeleteShader(shader)
	}

	return program
}
示例#11
0
func (prog *Program) Attach(shaders ...*Shader) {
	for _, shader := range shaders {
		gl.AttachShader(prog.handle, shader.handle)
		prog.shaders = append(prog.shaders, shader)
	}
}