示例#1
0
文件: opengl3.go 项目: emlai/go-sdl2
func createprogram() gl.Uint {
	// VERTEX SHADER
	vs := gl.CreateShader(gl.VERTEX_SHADER)
	vs_source := gl.GLString(vertexShaderSource)
	gl.ShaderSource(vs, 1, &vs_source, nil)
	gl.CompileShader(vs)
	var vs_status gl.Int
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &vs_status)
	fmt.Printf("Compiled Vertex Shader: %v\n", vs_status)

	// FRAGMENT SHADER
	fs := gl.CreateShader(gl.FRAGMENT_SHADER)
	fs_source := gl.GLString(fragmentShaderSource)
	gl.ShaderSource(fs, 1, &fs_source, nil)
	gl.CompileShader(fs)
	var fstatus gl.Int
	gl.GetShaderiv(fs, gl.COMPILE_STATUS, &fstatus)
	fmt.Printf("Compiled Fragment Shader: %v\n", fstatus)

	// CREATE PROGRAM
	program := gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	fragoutstring := gl.GLString("outColor")
	defer gl.GLStringFree(fragoutstring)
	gl.BindFragDataLocation(program, gl.Uint(0), fragoutstring)

	gl.LinkProgram(program)
	var linkstatus gl.Int
	gl.GetProgramiv(program, gl.LINK_STATUS, &linkstatus)
	fmt.Printf("Program Link: %v\n", linkstatus)

	return program
}
示例#2
0
func createShader(name string, shaderType int) (gl.Uint, error) {
	data, err := fileRead(name)
	if err != nil {
		return 0, err
	}
	if len(data) == 0 {
		return 0, errors.New("No shader code.")
	}
	var shader gl.Uint
	switch shaderType {
	case VertexShaderType:
		shader = gl.CreateShader(gl.VERTEX_SHADER)
	case FragmentShaderType:
		shader = gl.CreateShader(gl.FRAGMENT_SHADER)
	default:
		return 0, errors.New("Unknown ShaderType.")
	}
	src := gl.GLStringArray(string(data))
	defer gl.GLStringArrayFree(src)
	gl.ShaderSource(shader, gl.Sizei(1), &src[0], nil)
	gl.CompileShader(shader)

	// Similar to print_log in the C code example
	var length gl.Int
	gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &length)
	if length > 1 {
		glString := gl.GLStringAlloc(gl.Sizei(length))
		defer gl.GLStringFree(glString)
		gl.GetShaderInfoLog(shader, gl.Sizei(length), nil, glString)
		return 0, errors.New(fmt.Sprintf("Shader log: %s", gl.GoString(glString)))
	}
	return shader, nil
}
示例#3
0
func initResources() {
	var compileOk gl.Int
	// Vertex Shader
	vs = gl.CreateShader(gl.VERTEX_SHADER)
	vsSrc := gl.GLStringArray(vsSource)
	defer gl.GLStringArrayFree(vsSrc)
	gl.ShaderSource(vs, gl.Sizei(len(vsSrc)), &vsSrc[0], nil)
	gl.CompileShader(vs)
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &compileOk)
	if compileOk == 0 {
		errNum := gl.GetError()
		fmt.Printf("Error in vertex shader: %d\n", errNum)
	}

	// Fragment Shader
	fs = gl.CreateShader(gl.FRAGMENT_SHADER)
	fsSrc := gl.GLStringArray(fsSource)
	defer gl.GLStringArrayFree(fsSrc)
	gl.ShaderSource(fs, gl.Sizei(1), &fsSrc[0], nil)
	gl.CompileShader(fs)
	gl.GetShaderiv(fs, gl.COMPILE_STATUS, &compileOk)
	if compileOk == 0 {
		errNum := gl.GetError()
		fmt.Printf("Error in fragment shader: %d\n", errNum)
	}

	// GLSL program
	program = gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &compileOk)
	if compileOk == 0 {
		fmt.Printf("Error in program.\n")

	}

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := gl.GLString("coord2d")
	defer gl.GLStringFree(attributeName)
	attributeTemp := gl.GetAttribLocation(program, attributeName)
	if attributeTemp == -1 {
		fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName))
	}
	attributeCoord2d = gl.Uint(attributeTemp)
}
示例#4
0
文件: shader.go 项目: klkblake/sge
func NewShader(type_ gl33.Enum) *Shader {
	shader := new(Shader)
	shader.Id = gl33.CreateShader(type_)
	if shader.Id == 0 {
		panic(gl33.GetError())
	}
	return shader
}
示例#5
0
func LoadShader(data string, shaderType ShaderType) (*Shader, error) {
	var id gl.Uint
	switch shaderType {
	case VertexShader:
		id = gl.CreateShader(gl.VERTEX_SHADER)
	case FragmentShader:
		id = gl.CreateShader(gl.FRAGMENT_SHADER)
	default:
		return nil, ErrUnknownShader
	}
	shader := &Shader{id: id}
	src := gl.GLStringArray(data)
	length := gl.Int(-1)
	gl.ShaderSource(shader.id, gl.Sizei(1), &src[0], &length)
	gl.GLStringArrayFree(src)
	return shader, nil
}