Ejemplo n.º 1
0
Archivo: gl.go Proyecto: gmacd/rt
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
}
Ejemplo n.º 2
0
//
// Load Shader
// Load vertex and fragment shader and return the compiled program.
//
// @param vertexShaderSource (string) path to the vertex shader file
// @param fragmentShaderSource (string) path to the fragment shader file
//
// @return program (uint32) a pointer to the shader program
// @return error (error) the error (if any)
//
func LoadShader(vertexShaderSource, fragmentShaderSource string) (uint32, error) {
	// Loads the Vertex shader file
	vertexShader, err := BuildShader(vertexShaderSource, gl.VERTEX_SHADER)
	if err != nil {
		return 0, err
	}

	// Loads the fragment shader file
	fragmentShader, err := BuildShader(fragmentShaderSource, gl.FRAGMENT_SHADER)
	if err != nil {
		return 0, err
	}

	// Creates the Program
	program := gl.CreateProgram()

	// Attaches the Shaders to the program
	gl.AttachShader(program, vertexShader)
	gl.AttachShader(program, fragmentShader)

	// Links the program
	gl.LinkProgram(program)

	// Gets any error that happened when linking the program
	var status int32
	gl.GetProgramiv(program, gl.LINK_STATUS, &status)

	// If there was any error, parse the C error and return it as a Go error
	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)
	}

	// Deletes the shaders
	gl.DeleteShader(vertexShader)
	gl.DeleteShader(fragmentShader)

	// returns the program
	return program, nil
}
Ejemplo n.º 3
0
Archivo: hello.go Proyecto: rdterner/gl
func makeProgram(vertexShader uint32, fragmentShader uint32) uint32 {

	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))

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

	return program
}
Ejemplo n.º 4
0
func createProgram(vertexShaderFile, fragmentShaderFile string, uniforms, attributes []string) (*Shader, error) {

	// Configure the vertex and fragment shaders
	vertexShaderSource, err := ioutil.ReadFile(vertexShaderFile)
	vertexShaderSource = append(vertexShaderSource, 0x00)
	fragmentShaderSource, err := ioutil.ReadFile(fragmentShaderFile)
	fragmentShaderSource = append(fragmentShaderSource, 0x00)

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

	fragmentShader, err := compileShader(string(fragmentShaderSource), gl.FRAGMENT_SHADER)
	if err != nil {
		return nil, 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 nil, fmt.Errorf("failed to link program: %v", log)
	}

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

	gl.UseProgram(program)

	programUniforms := make(map[string]int32)
	for _, uniformName := range uniforms {
		if uniformName != "" {
			glstr := gl.Str(uniformName + "\x00")
			programUniforms[uniformName] = gl.GetUniformLocation(program, glstr)
		}
	}

	programAttributes := make(map[string]uint32)
	for _, attribName := range attributes {
		if attribName != "" {
			glstr := gl.Str(attribName + "\x00")
			programAttributes[attribName] = uint32(gl.GetAttribLocation(program, glstr))
		}
	}
	return &Shader{
		program:    program,
		uniforms:   programUniforms,
		attributes: programAttributes,
	}, nil
}