Use go-gl/glow instead of go-gl/gl (#112)

This commit is contained in:
Hajime Hoshi 2015-01-29 00:58:56 +09:00
parent d321dfe7fd
commit 2544d74ba0
4 changed files with 95 additions and 71 deletions

View File

@ -9,7 +9,7 @@ before_install:
- sudo add-apt-repository 'deb http://us.archive.ubuntu.com/ubuntu/ utopic main restricted universe multiverse'
- sudo add-apt-repository 'deb http://us.archive.ubuntu.com/ubuntu/ utopic-updates main restricted universe multiverse'
- sudo apt-get update -qq
- sudo apt-get install -qq libglew-dev libglfw3-dev libopenal-dev libalut-dev
- sudo apt-get install -qq libglew-dev libglfw3-dev libopenal-dev libalut-dev # libglew-dev is required by node-gl.
- export NODE_PATH=$(npm config get prefix)/lib/node_modules
- npm install --global gl

View File

@ -19,22 +19,22 @@ package opengl
import (
"errors"
"fmt"
"github.com/go-gl/gl"
"github.com/go-gl/glow/gl-core/3.2/gl"
)
type Texture gl.Texture
type Framebuffer gl.Framebuffer
type Shader gl.Shader
type Program gl.Program
type Buffer gl.Buffer
type Texture uint32
type Framebuffer uint32
type Shader uint32
type Program uint32
type Buffer uint32
// TODO: Remove this after the GopherJS bug was fixed (#159)
func (p Program) Equals(other Program) bool {
return p == other
}
type UniformLocation gl.UniformLocation
type AttribLocation gl.AttribLocation
type UniformLocation int32
type AttribLocation int32
type ProgramID int
@ -62,24 +62,31 @@ func NewContext() *Context {
}
func (c *Context) init() {
gl.Init()
if err := gl.Init(); err != nil {
panic(err)
}
// Textures' pixel formats are alpha premultiplied.
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
}
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) {
t := gl.GenTexture()
var t uint32
gl.GenTextures(1, &t)
if t < 0 {
return 0, errors.New("glGenTexture failed")
}
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
t.Bind(gl.TEXTURE_2D)
gl.BindTexture(gl.TEXTURE_2D, t)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int32(filter))
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int32(filter))
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
var p interface{}
if pixels != nil {
p = pixels
}
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
return Texture(t), nil
}
@ -87,10 +94,10 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
gl.Flush()
gl.Framebuffer(f).Bind()
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f))
pixels := make([]uint8, 4*width*height)
gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels))
if e := gl.GetError(); e != gl.NO_ERROR {
return nil, errors.New(fmt.Sprintf("gl error: %d", e))
}
@ -98,22 +105,24 @@ func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8,
}
func (c *Context) BindTexture(t Texture) {
gl.Texture(t).Bind(gl.TEXTURE_2D)
gl.BindTexture(gl.TEXTURE_2D, uint32(t))
}
func (c *Context) DeleteTexture(t Texture) {
gl.Texture(t).Delete()
tt := uint32(t)
gl.DeleteTextures(1, &tt)
}
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, p)
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
}
func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
f := gl.GenFramebuffer()
f.Bind()
var f uint32
gl.GenFramebuffers(1, &f)
gl.BindFramebuffer(gl.FRAMEBUFFER, f)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, gl.Texture(texture), 0)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0)
if gl.CheckFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE {
return 0, errors.New("creating framebuffer failed")
}
@ -123,7 +132,7 @@ func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
func (c *Context) SetViewport(f Framebuffer, width, height int) error {
gl.Flush()
gl.Framebuffer(f).Bind()
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f))
err := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
if err != gl.FRAMEBUFFER_COMPLETE {
if gl.GetError() != 0 {
@ -131,42 +140,48 @@ func (c *Context) SetViewport(f Framebuffer, width, height int) error {
}
return errors.New("glBindFramebuffer failed: the context is different?")
}
gl.Viewport(0, 0, width, height)
gl.Viewport(0, 0, int32(width), int32(height))
return nil
}
func (c *Context) FillFramebuffer(r, g, b, a float64) error {
gl.ClearColor(gl.GLclampf(r), gl.GLclampf(g), gl.GLclampf(b), gl.GLclampf(a))
gl.ClearColor(float32(r), float32(g), float32(b), float32(a))
gl.Clear(gl.COLOR_BUFFER_BIT)
return nil
}
func (c *Context) DeleteFramebuffer(f Framebuffer) {
gl.Framebuffer(f).Delete()
ff := uint32(f)
gl.DeleteFramebuffers(1, &ff)
}
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
s := gl.CreateShader(gl.GLenum(shaderType))
s := gl.CreateShader(uint32(shaderType))
if s == 0 {
println(gl.GetError())
return 0, errors.New("glCreateShader failed")
}
s.Source(source)
s.Compile()
glSource := gl.Str(source + "\x00")
gl.ShaderSource(uint32(s), 1, &glSource, nil)
gl.CompileShader(s)
if s.Get(gl.COMPILE_STATUS) == gl.FALSE {
log := ""
if s.Get(gl.INFO_LOG_LENGTH) != 0 {
log = s.GetInfoLog()
var v int32
gl.GetShaderiv(s, gl.COMPILE_STATUS, &v)
if v == gl.FALSE {
log := []uint8{}
gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v)
if v != 0 {
log = make([]uint8, int(v))
gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log)))
}
return 0, errors.New(fmt.Sprintf("shader compile failed: %s", log))
return 0, errors.New(fmt.Sprintf("shader compile failed: %s", string(log)))
}
return Shader(s), nil
}
func (c *Context) DeleteShader(s Shader) {
gl.Shader(s).Delete()
gl.DeleteShader(uint32(s))
}
func (c *Context) GlslHighpSupported() bool {
@ -180,68 +195,77 @@ func (c *Context) NewProgram(shaders []Shader) (Program, error) {
}
for _, shader := range shaders {
p.AttachShader(gl.Shader(shader))
gl.AttachShader(p, uint32(shader))
}
p.Link()
if p.Get(gl.LINK_STATUS) == gl.FALSE {
gl.LinkProgram(p)
var v int32
gl.GetProgramiv(p, gl.LINK_STATUS, &v)
if v == gl.FALSE {
return 0, errors.New("program error")
}
return Program(p), nil
}
func (c *Context) UseProgram(p Program) {
gl.Program(p).Use()
gl.UseProgram(uint32(p))
}
func (c *Context) GetUniformLocation(p Program, location string) UniformLocation {
return UniformLocation(gl.Program(p).GetUniformLocation(location))
u := UniformLocation(gl.GetUniformLocation(uint32(p), gl.Str(location+"\x00")))
if u == -1 {
panic("invalid uniform location: " + location)
}
return u
}
func (c *Context) UniformInt(p Program, location string, v int) {
l := gl.UniformLocation(GetUniformLocation(c, p, location))
l.Uniform1i(v)
l := int32(GetUniformLocation(c, p, location))
gl.Uniform1i(l, int32(v))
}
func (c *Context) UniformFloats(p Program, location string, v []float32) {
l := gl.UniformLocation(GetUniformLocation(c, p, location))
l := int32(GetUniformLocation(c, p, location))
switch len(v) {
case 4:
l.Uniform4fv(1, v)
gl.Uniform4fv(l, 1, (*float32)(gl.Ptr(v)))
case 16:
v2 := [16]float32{}
copy(v2[:], v)
l.UniformMatrix4fv(false, v2)
gl.UniformMatrix4fv(l, 1, false, (*float32)(gl.Ptr(v)))
default:
panic("not reach")
}
}
func (c *Context) GetAttribLocation(p Program, location string) AttribLocation {
return AttribLocation(gl.Program(p).GetAttribLocation(location))
a := AttribLocation(gl.GetAttribLocation(uint32(p), gl.Str(location+"\x00")))
if a == -1 {
panic("invalid attrib location: " + location)
}
return a
}
func (c *Context) VertexAttribPointer(p Program, location string, signed bool, normalize bool, stride int, size int, v uintptr) {
l := gl.AttribLocation(GetAttribLocation(c, p, location))
t := gl.GLenum(gl.SHORT)
func (c *Context) VertexAttribPointer(p Program, location string, signed bool, normalize bool, stride int, size int, v int) {
l := GetAttribLocation(c, p, location)
t := gl.SHORT
if !signed {
t = gl.UNSIGNED_SHORT
}
l.AttribPointer(uint(size), t, normalize, stride, v)
gl.VertexAttribPointer(uint32(l), int32(size), uint32(t), normalize, int32(stride), gl.PtrOffset(v))
}
func (c *Context) EnableVertexAttribArray(p Program, location string) {
l := gl.AttribLocation(GetAttribLocation(c, p, location))
l.EnableArray()
l := GetAttribLocation(c, p, location)
gl.EnableVertexAttribArray(uint32(l))
}
func (c *Context) DisableVertexAttribArray(p Program, location string) {
l := gl.AttribLocation(GetAttribLocation(c, p, location))
l.DisableArray()
l := GetAttribLocation(c, p, location)
gl.DisableVertexAttribArray(uint32(l))
}
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer {
b := gl.GenBuffer()
b.Bind(gl.GLenum(bufferType))
var b uint32
gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(bufferType), b)
size := 0
ptr := v
switch v := v.(type) {
@ -255,21 +279,21 @@ func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage Bu
default:
panic("not reach")
}
gl.BufferData(gl.GLenum(bufferType), size, ptr, gl.GLenum(bufferUsage))
gl.BufferData(uint32(bufferType), size, gl.Ptr(ptr), uint32(bufferUsage))
return Buffer(b)
}
func (c *Context) BindElementArrayBuffer(b Buffer) {
gl.Buffer(b).Bind(gl.ELEMENT_ARRAY_BUFFER)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b))
}
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
const int16Size = 2
gl.BufferSubData(gl.GLenum(bufferType), 0, int16Size*len(data), data)
gl.BufferSubData(uint32(bufferType), 0, int16Size*len(data), gl.Ptr(data))
}
func (c *Context) DrawElements(mode Mode, len int) {
gl.DrawElements(gl.GLenum(mode), len, gl.UNSIGNED_SHORT, uintptr(0))
gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(0))
}
func (c *Context) Flush() {

View File

@ -301,14 +301,14 @@ func (c *Context) GetAttribLocation(p Program, location string) AttribLocation {
return AttribLocation(gl.GetAttribLocation(p.Object, location))
}
func (c *Context) VertexAttribPointer(p Program, location string, signed bool, normalize bool, stride int, size int, v uintptr) {
func (c *Context) VertexAttribPointer(p Program, location string, signed bool, normalize bool, stride int, size int, v int) {
gl := c.gl
l := GetAttribLocation(c, p, location)
t := gl.SHORT
if !signed {
t = gl.UNSIGNED_SHORT
}
gl.VertexAttribPointer(int(l), size, t, normalize, stride, int(v))
gl.VertexAttribPointer(int(l), size, t, normalize, stride, v)
}
func (c *Context) EnableVertexAttribArray(p Program, location string) {

View File

@ -176,8 +176,8 @@ func useProgramForTexture(c *opengl.Context, projectionMatrix []float32, texture
c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "tex_coord")
c.VertexAttribPointer(program, "vertex", true, false, int16Size*4, 2, uintptr(int16Size*0))
c.VertexAttribPointer(program, "tex_coord", true, true, int16Size*4, 2, uintptr(int16Size*2))
c.VertexAttribPointer(program, "vertex", true, false, int16Size*4, 2, int16Size*0)
c.VertexAttribPointer(program, "tex_coord", true, true, int16Size*4, 2, int16Size*2)
return func() {
c.DisableVertexAttribArray(program, "tex_coord")
@ -200,8 +200,8 @@ func useProgramForLines(c *opengl.Context, projectionMatrix []float32) programFi
c.EnableVertexAttribArray(program, "color")
// TODO: Change to floats?
c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, uintptr(int16Size*0))
c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, uintptr(int16Size*2))
c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, int16Size*0)
c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, int16Size*2)
return func() {
c.DisableVertexAttribArray(program, "color")
@ -223,8 +223,8 @@ func useProgramForRects(c *opengl.Context, projectionMatrix []float32) programFi
c.EnableVertexAttribArray(program, "vertex")
c.EnableVertexAttribArray(program, "color")
c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, uintptr(int16Size*0))
c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, uintptr(int16Size*2))
c.VertexAttribPointer(program, "vertex", true, false, int16Size*6, 2, int16Size*0)
c.VertexAttribPointer(program, "color", false, true, int16Size*6, 4, int16Size*2)
return func() {
c.DisableVertexAttribArray(program, "color")