go.mobile/gl/glutil: OpenGL utilities package

No tests yet. I am working on a gltest package that can
create a windowless context on a variety of platforms for
testing GL code, but given current priorities it is a
couple of weeks away.

LGTM=nigeltao
R=nigeltao
CC=golang-codereviews
https://golang.org/cl/135450043
This commit is contained in:
David Crawshaw 2014-09-05 14:02:43 -04:00
parent 0a1cd6409e
commit 1d1714ebe9
1 changed files with 58 additions and 0 deletions

58
gl/glutil/glutil.go Normal file
View File

@ -0,0 +1,58 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package glutil implements OpenGL utility functions.
package glutil
import (
"fmt"
"code.google.com/p/go.mobile/gl"
)
// CreateProgram creates, compiles, and links a gl.Program.
func CreateProgram(vertexSrc, fragmentSrc string) (gl.Program, error) {
program := gl.CreateProgram()
if program == 0 {
return 0, fmt.Errorf("glutil: no programs available")
}
vertexShader, err := loadShader(gl.VERTEX_SHADER, vertexSrc)
if err != nil {
return 0, err
}
fragmentShader, err := loadShader(gl.FRAGMENT_SHADER, fragmentSrc)
if err != nil {
gl.DeleteShader(vertexShader)
return 0, err
}
gl.AttachShader(program, vertexShader)
gl.AttachShader(program, fragmentShader)
gl.LinkProgram(program)
// Flag shaders for deletion when program is unlinked.
gl.DeleteShader(vertexShader)
gl.DeleteShader(fragmentShader)
if gl.GetProgrami(program, gl.LINK_STATUS) == 0 {
defer gl.DeleteProgram(program)
return 0, fmt.Errorf("glutil: %s", gl.GetProgramInfoLog(program))
}
return program, nil
}
func loadShader(shaderType gl.Enum, src string) (gl.Shader, error) {
shader := gl.CreateShader(shaderType)
if shader == 0 {
return 0, fmt.Errorf("glutil: could not create shader (type %v)", shaderType)
}
gl.ShaderSource(shader, src)
gl.CompileShader(shader)
if gl.GetShaderi(shader, gl.COMPILE_STATUS) == 0 {
defer gl.DeleteShader(shader)
return 0, fmt.Errorf("shader compile: %s", gl.GetShaderInfoLog(shader))
}
return shader, nil
}