opengl: Unexport types

This commit is contained in:
Hajime Hoshi 2018-10-30 01:52:59 +09:00
parent e4179aee37
commit ac7bf354a9
8 changed files with 135 additions and 135 deletions

View File

@ -26,14 +26,14 @@ var (
) )
var ( var (
VertexShader ShaderType VertexShader shaderType
FragmentShader ShaderType FragmentShader shaderType
ArrayBuffer BufferType ArrayBuffer bufferType
ElementArrayBuffer BufferType ElementArrayBuffer bufferType
DynamicDraw BufferUsage DynamicDraw bufferUsage
StaticDraw BufferUsage StaticDraw bufferUsage
Triangles Mode Triangles mode
Lines Mode Lines mode
Short DataType Short DataType
Float DataType Float DataType

View File

@ -31,9 +31,9 @@ import (
type ( type (
Texture uint32 Texture uint32
Framebuffer uint32 Framebuffer uint32
Shader uint32 shader uint32
Program uint32 program uint32
Buffer uint32 buffer uint32
) )
var InvalidTexture Texture var InvalidTexture Texture
@ -50,7 +50,7 @@ const (
invalidFramebuffer = (1 << 32) - 1 invalidFramebuffer = (1 << 32) - 1
) )
func getProgramID(p Program) programID { func getProgramID(p program) programID {
return programID(p) return programID(p)
} }
@ -290,8 +290,8 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) {
}) })
} }
func (c *Context) newShader(shaderType ShaderType, source string) (Shader, error) { func (c *Context) newShader(shaderType shaderType, source string) (shader, error) {
var shader Shader var sh shader
if err := c.runOnContextThread(func() error { if err := c.runOnContextThread(func() error {
s := gl.CreateShader(uint32(shaderType)) s := gl.CreateShader(uint32(shaderType))
if s == 0 { if s == 0 {
@ -313,23 +313,23 @@ func (c *Context) newShader(shaderType ShaderType, source string) (Shader, error
} }
return fmt.Errorf("opengl: shader compile failed: %s", log) return fmt.Errorf("opengl: shader compile failed: %s", log)
} }
shader = Shader(s) sh = shader(s)
return nil return nil
}); err != nil { }); err != nil {
return 0, err return 0, err
} }
return shader, nil return sh, nil
} }
func (c *Context) deleteShader(s Shader) { func (c *Context) deleteShader(s shader) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
gl.DeleteShader(uint32(s)) gl.DeleteShader(uint32(s))
return nil return nil
}) })
} }
func (c *Context) newProgram(shaders []Shader) (Program, error) { func (c *Context) newProgram(shaders []shader) (program, error) {
var program Program var pr program
if err := c.runOnContextThread(func() error { if err := c.runOnContextThread(func() error {
p := gl.CreateProgram() p := gl.CreateProgram()
if p == 0 { if p == 0 {
@ -345,22 +345,22 @@ func (c *Context) newProgram(shaders []Shader) (Program, error) {
if v == gl.FALSE { if v == gl.FALSE {
return errors.New("opengl: program error") return errors.New("opengl: program error")
} }
program = Program(p) pr = program(p)
return nil return nil
}); err != nil { }); err != nil {
return 0, err return 0, err
} }
return program, nil return pr, nil
} }
func (c *Context) useProgram(p Program) { func (c *Context) useProgram(p program) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
gl.UseProgram(uint32(p)) gl.UseProgram(uint32(p))
return nil return nil
}) })
} }
func (c *Context) deleteProgram(p Program) { func (c *Context) deleteProgram(p program) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
if !gl.IsProgram(uint32(p)) { if !gl.IsProgram(uint32(p)) {
return nil return nil
@ -370,7 +370,7 @@ func (c *Context) deleteProgram(p Program) {
}) })
} }
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation { func (c *Context) getUniformLocationImpl(p program, location string) uniformLocation {
l, free := gl.Strs(location + "\x00") l, free := gl.Strs(location + "\x00")
uniform := uniformLocation(gl.GetUniformLocation(uint32(p), *l)) uniform := uniformLocation(gl.GetUniformLocation(uint32(p), *l))
free() free()
@ -380,7 +380,7 @@ func (c *Context) getUniformLocationImpl(p Program, location string) uniformLoca
return uniform return uniform
} }
func (c *Context) uniformInt(p Program, location string, v int) { func (c *Context) uniformInt(p program, location string, v int) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
gl.Uniform1i(l, int32(v)) gl.Uniform1i(l, int32(v))
@ -388,7 +388,7 @@ func (c *Context) uniformInt(p Program, location string, v int) {
}) })
} }
func (c *Context) uniformFloat(p Program, location string, v float32) { func (c *Context) uniformFloat(p program, location string, v float32) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
gl.Uniform1f(l, v) gl.Uniform1f(l, v)
@ -396,7 +396,7 @@ func (c *Context) uniformFloat(p Program, location string, v float32) {
}) })
} }
func (c *Context) uniformFloats(p Program, location string, v []float32) { func (c *Context) uniformFloats(p program, location string, v []float32) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
switch len(v) { switch len(v) {
@ -413,7 +413,7 @@ func (c *Context) uniformFloats(p Program, location string, v []float32) {
}) })
} }
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation { func (c *Context) getAttribLocationImpl(p program, location string) attribLocation {
l, free := gl.Strs(location + "\x00") l, free := gl.Strs(location + "\x00")
attrib := attribLocation(gl.GetAttribLocation(uint32(p), *l)) attrib := attribLocation(gl.GetAttribLocation(uint32(p), *l))
free() free()
@ -423,7 +423,7 @@ func (c *Context) getAttribLocationImpl(p Program, location string) attribLocati
return attrib return attrib
} }
func (c *Context) vertexAttribPointer(p Program, location string, size int, dataType DataType, stride int, offset int) { func (c *Context) vertexAttribPointer(p program, location string, size int, dataType DataType, stride int, offset int) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.VertexAttribPointer(uint32(l), int32(size), uint32(dataType), false, int32(stride), gl.PtrOffset(offset)) gl.VertexAttribPointer(uint32(l), int32(size), uint32(dataType), false, int32(stride), gl.PtrOffset(offset))
@ -431,7 +431,7 @@ func (c *Context) vertexAttribPointer(p Program, location string, size int, data
}) })
} }
func (c *Context) enableVertexAttribArray(p Program, location string) { func (c *Context) enableVertexAttribArray(p program, location string) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.EnableVertexAttribArray(uint32(l)) gl.EnableVertexAttribArray(uint32(l))
@ -439,7 +439,7 @@ func (c *Context) enableVertexAttribArray(p Program, location string) {
}) })
} }
func (c *Context) disableVertexAttribArray(p Program, location string) { func (c *Context) disableVertexAttribArray(p program, location string) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.DisableVertexAttribArray(uint32(l)) gl.DisableVertexAttribArray(uint32(l))
@ -447,33 +447,33 @@ func (c *Context) disableVertexAttribArray(p Program, location string) {
}) })
} }
func (c *Context) newArrayBuffer(size int) Buffer { func (c *Context) newArrayBuffer(size int) buffer {
var buffer Buffer var bf buffer
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(ArrayBuffer), b) gl.BindBuffer(uint32(ArrayBuffer), b)
gl.BufferData(uint32(ArrayBuffer), size, nil, uint32(DynamicDraw)) gl.BufferData(uint32(ArrayBuffer), size, nil, uint32(DynamicDraw))
buffer = Buffer(b) bf = buffer(b)
return nil return nil
}) })
return buffer return bf
} }
func (c *Context) newElementArrayBuffer(size int) Buffer { func (c *Context) newElementArrayBuffer(size int) buffer {
var buffer Buffer var bf buffer
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(ElementArrayBuffer), b) gl.BindBuffer(uint32(ElementArrayBuffer), b)
gl.BufferData(uint32(ElementArrayBuffer), size, nil, uint32(DynamicDraw)) gl.BufferData(uint32(ElementArrayBuffer), size, nil, uint32(DynamicDraw))
buffer = Buffer(b) bf = buffer(b)
return nil return nil
}) })
return buffer return bf
} }
func (c *Context) BindBuffer(bufferType BufferType, b Buffer) { func (c *Context) BindBuffer(bufferType bufferType, b buffer) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
gl.BindBuffer(uint32(bufferType), uint32(b)) gl.BindBuffer(uint32(bufferType), uint32(b))
return nil return nil
@ -494,7 +494,7 @@ func (c *Context) ElementArrayBufferSubData(data []uint16) {
}) })
} }
func (c *Context) deleteBuffer(b Buffer) { func (c *Context) deleteBuffer(b buffer) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
bb := uint32(b) bb := uint32(b)
gl.DeleteBuffers(1, &bb) gl.DeleteBuffers(1, &bb)
@ -502,7 +502,7 @@ func (c *Context) deleteBuffer(b Buffer) {
}) })
} }
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) { func (c *Context) DrawElements(mode mode, len int, offsetInBytes int) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(offsetInBytes)) gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(offsetInBytes))
return nil return nil

View File

@ -28,13 +28,13 @@ import (
type ( type (
Texture js.Value Texture js.Value
Framebuffer js.Value Framebuffer js.Value
Shader js.Value shader js.Value
Buffer js.Value buffer js.Value
uniformLocation js.Value uniformLocation js.Value
attribLocation int attribLocation int
programID int programID int
Program struct { program struct {
value js.Value value js.Value
id programID id programID
} }
@ -42,7 +42,7 @@ type (
var InvalidTexture = Texture(js.Null()) var InvalidTexture = Texture(js.Null())
func getProgramID(p Program) programID { func getProgramID(p program) programID {
return p.id return p.id
} }
@ -72,13 +72,13 @@ var (
func init() { func init() {
// Accessing the prototype is rquired on Safari. // Accessing the prototype is rquired on Safari.
c := js.Global().Get("WebGLRenderingContext").Get("prototype") c := js.Global().Get("WebGLRenderingContext").Get("prototype")
VertexShader = ShaderType(c.Get("VERTEX_SHADER").Int()) VertexShader = shaderType(c.Get("VERTEX_SHADER").Int())
FragmentShader = ShaderType(c.Get("FRAGMENT_SHADER").Int()) FragmentShader = shaderType(c.Get("FRAGMENT_SHADER").Int())
ArrayBuffer = BufferType(c.Get("ARRAY_BUFFER").Int()) ArrayBuffer = bufferType(c.Get("ARRAY_BUFFER").Int())
ElementArrayBuffer = BufferType(c.Get("ELEMENT_ARRAY_BUFFER").Int()) ElementArrayBuffer = bufferType(c.Get("ELEMENT_ARRAY_BUFFER").Int())
DynamicDraw = BufferUsage(c.Get("DYNAMIC_DRAW").Int()) DynamicDraw = bufferUsage(c.Get("DYNAMIC_DRAW").Int())
Triangles = Mode(c.Get("TRIANGLES").Int()) Triangles = mode(c.Get("TRIANGLES").Int())
Lines = Mode(c.Get("LINES").Int()) Lines = mode(c.Get("LINES").Int())
Short = DataType(c.Get("SHORT").Int()) Short = DataType(c.Get("SHORT").Int())
Float = DataType(c.Get("FLOAT").Int()) Float = DataType(c.Get("FLOAT").Int())
@ -285,11 +285,11 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) {
gl.Call("deleteFramebuffer", js.Value(f)) gl.Call("deleteFramebuffer", js.Value(f))
} }
func (c *Context) newShader(shaderType ShaderType, source string) (Shader, error) { func (c *Context) newShader(shaderType shaderType, source string) (shader, error) {
gl := c.gl gl := c.gl
s := gl.Call("createShader", int(shaderType)) s := gl.Call("createShader", int(shaderType))
if s == js.Null() { if s == js.Null() {
return Shader(js.Null()), fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) return shader(js.Null()), fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
} }
gl.Call("shaderSource", js.Value(s), source) gl.Call("shaderSource", js.Value(s), source)
@ -297,21 +297,21 @@ func (c *Context) newShader(shaderType ShaderType, source string) (Shader, error
if !gl.Call("getShaderParameter", js.Value(s), compileStatus).Bool() { if !gl.Call("getShaderParameter", js.Value(s), compileStatus).Bool() {
log := gl.Call("getShaderInfoLog", js.Value(s)) log := gl.Call("getShaderInfoLog", js.Value(s))
return Shader(js.Null()), fmt.Errorf("opengl: shader compile failed: %s", log) return shader(js.Null()), fmt.Errorf("opengl: shader compile failed: %s", log)
} }
return Shader(s), nil return shader(s), nil
} }
func (c *Context) deleteShader(s Shader) { func (c *Context) deleteShader(s shader) {
gl := c.gl gl := c.gl
gl.Call("deleteShader", js.Value(s)) gl.Call("deleteShader", js.Value(s))
} }
func (c *Context) newProgram(shaders []Shader) (Program, error) { func (c *Context) newProgram(shaders []shader) (program, error) {
gl := c.gl gl := c.gl
v := gl.Call("createProgram") v := gl.Call("createProgram")
if v == js.Null() { if v == js.Null() {
return Program{}, errors.New("opengl: glCreateProgram failed") return program{}, errors.New("opengl: glCreateProgram failed")
} }
for _, shader := range shaders { for _, shader := range shaders {
@ -319,23 +319,23 @@ func (c *Context) newProgram(shaders []Shader) (Program, error) {
} }
gl.Call("linkProgram", v) gl.Call("linkProgram", v)
if !gl.Call("getProgramParameter", v, linkStatus).Bool() { if !gl.Call("getProgramParameter", v, linkStatus).Bool() {
return Program{}, errors.New("opengl: program error") return program{}, errors.New("opengl: program error")
} }
id := c.lastProgramID id := c.lastProgramID
c.lastProgramID++ c.lastProgramID++
return Program{ return program{
value: v, value: v,
id: id, id: id,
}, nil }, nil
} }
func (c *Context) useProgram(p Program) { func (c *Context) useProgram(p program) {
gl := c.gl gl := c.gl
gl.Call("useProgram", p.value) gl.Call("useProgram", p.value)
} }
func (c *Context) deleteProgram(p Program) { func (c *Context) deleteProgram(p program) {
gl := c.gl gl := c.gl
if !gl.Call("isProgram", p.value).Bool() { if !gl.Call("isProgram", p.value).Bool() {
return return
@ -343,18 +343,18 @@ func (c *Context) deleteProgram(p Program) {
gl.Call("deleteProgram", p.value) gl.Call("deleteProgram", p.value)
} }
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation { func (c *Context) getUniformLocationImpl(p program, location string) uniformLocation {
gl := c.gl gl := c.gl
return uniformLocation(gl.Call("getUniformLocation", p.value, location)) return uniformLocation(gl.Call("getUniformLocation", p.value, location))
} }
func (c *Context) uniformInt(p Program, location string, v int) { func (c *Context) uniformInt(p program, location string, v int) {
gl := c.gl gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location) l := c.locationCache.GetUniformLocation(c, p, location)
gl.Call("uniform1i", js.Value(l), v) gl.Call("uniform1i", js.Value(l), v)
} }
func (c *Context) uniformFloat(p Program, location string, v float32) { func (c *Context) uniformFloat(p program, location string, v float32) {
gl := c.gl gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location) l := c.locationCache.GetUniformLocation(c, p, location)
gl.Call("uniform1f", js.Value(l), v) gl.Call("uniform1f", js.Value(l), v)
@ -364,7 +364,7 @@ var (
float32Array = js.Global().Get("Float32Array") float32Array = js.Global().Get("Float32Array")
) )
func (c *Context) uniformFloats(p Program, location string, v []float32) { func (c *Context) uniformFloats(p program, location string, v []float32) {
gl := c.gl gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location) l := c.locationCache.GetUniformLocation(c, p, location)
switch len(v) { switch len(v) {
@ -381,46 +381,46 @@ func (c *Context) uniformFloats(p Program, location string, v []float32) {
} }
} }
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation { func (c *Context) getAttribLocationImpl(p program, location string) attribLocation {
gl := c.gl gl := c.gl
return attribLocation(gl.Call("getAttribLocation", p.value, location).Int()) return attribLocation(gl.Call("getAttribLocation", p.value, location).Int())
} }
func (c *Context) vertexAttribPointer(p Program, location string, size int, dataType DataType, stride int, offset int) { func (c *Context) vertexAttribPointer(p program, location string, size int, dataType DataType, stride int, offset int) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("vertexAttribPointer", int(l), size, int(dataType), false, stride, offset) gl.Call("vertexAttribPointer", int(l), size, int(dataType), false, stride, offset)
} }
func (c *Context) enableVertexAttribArray(p Program, location string) { func (c *Context) enableVertexAttribArray(p program, location string) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("enableVertexAttribArray", int(l)) gl.Call("enableVertexAttribArray", int(l))
} }
func (c *Context) disableVertexAttribArray(p Program, location string) { func (c *Context) disableVertexAttribArray(p program, location string) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("disableVertexAttribArray", int(l)) gl.Call("disableVertexAttribArray", int(l))
} }
func (c *Context) newArrayBuffer(size int) Buffer { func (c *Context) newArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.Call("createBuffer") b := gl.Call("createBuffer")
gl.Call("bindBuffer", int(ArrayBuffer), js.Value(b)) gl.Call("bindBuffer", int(ArrayBuffer), js.Value(b))
gl.Call("bufferData", int(ArrayBuffer), size, int(DynamicDraw)) gl.Call("bufferData", int(ArrayBuffer), size, int(DynamicDraw))
return Buffer(b) return buffer(b)
} }
func (c *Context) newElementArrayBuffer(size int) Buffer { func (c *Context) newElementArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.Call("createBuffer") b := gl.Call("createBuffer")
gl.Call("bindBuffer", int(ElementArrayBuffer), js.Value(b)) gl.Call("bindBuffer", int(ElementArrayBuffer), js.Value(b))
gl.Call("bufferData", int(ElementArrayBuffer), size, int(DynamicDraw)) gl.Call("bufferData", int(ElementArrayBuffer), size, int(DynamicDraw))
return Buffer(b) return buffer(b)
} }
func (c *Context) BindBuffer(bufferType BufferType, b Buffer) { func (c *Context) BindBuffer(bufferType bufferType, b buffer) {
gl := c.gl gl := c.gl
gl.Call("bindBuffer", int(bufferType), js.Value(b)) gl.Call("bindBuffer", int(bufferType), js.Value(b))
} }
@ -439,12 +439,12 @@ func (c *Context) ElementArrayBufferSubData(data []uint16) {
arr.Release() arr.Release()
} }
func (c *Context) deleteBuffer(b Buffer) { func (c *Context) deleteBuffer(b buffer) {
gl := c.gl gl := c.gl
gl.Call("deleteBuffer", js.Value(b)) gl.Call("deleteBuffer", js.Value(b))
} }
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) { func (c *Context) DrawElements(mode mode, len int, offsetInBytes int) {
gl := c.gl gl := c.gl
gl.Call("drawElements", int(mode), len, unsignedShort, offsetInBytes) gl.Call("drawElements", int(mode), len, unsignedShort, offsetInBytes)
} }

View File

@ -28,9 +28,9 @@ import (
type ( type (
Texture mgl.Texture Texture mgl.Texture
Framebuffer mgl.Framebuffer Framebuffer mgl.Framebuffer
Shader mgl.Shader shader mgl.Shader
Program mgl.Program program mgl.Program
Buffer mgl.Buffer buffer mgl.Buffer
) )
var InvalidTexture Texture var InvalidTexture Texture
@ -47,7 +47,7 @@ var (
invalidFramebuffer = Framebuffer(mgl.Framebuffer{(1 << 32) - 1}) invalidFramebuffer = Framebuffer(mgl.Framebuffer{(1 << 32) - 1})
) )
func getProgramID(p Program) programID { func getProgramID(p program) programID {
return programID(p.Value) return programID(p.Value)
} }
@ -241,11 +241,11 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) {
gl.DeleteFramebuffer(mgl.Framebuffer(f)) gl.DeleteFramebuffer(mgl.Framebuffer(f))
} }
func (c *Context) newShader(shaderType ShaderType, source string) (Shader, error) { func (c *Context) newShader(shaderType shaderType, source string) (shader, error) {
gl := c.gl gl := c.gl
s := gl.CreateShader(mgl.Enum(shaderType)) s := gl.CreateShader(mgl.Enum(shaderType))
if s.Value == 0 { if s.Value == 0 {
return Shader{}, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) return shader{}, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
} }
gl.ShaderSource(s, source) gl.ShaderSource(s, source)
gl.CompileShader(s) gl.CompileShader(s)
@ -253,21 +253,21 @@ func (c *Context) newShader(shaderType ShaderType, source string) (Shader, error
v := gl.GetShaderi(s, mgl.COMPILE_STATUS) v := gl.GetShaderi(s, mgl.COMPILE_STATUS)
if v == mgl.FALSE { if v == mgl.FALSE {
log := gl.GetShaderInfoLog(s) log := gl.GetShaderInfoLog(s)
return Shader{}, fmt.Errorf("opengl: shader compile failed: %s", log) return shader{}, fmt.Errorf("opengl: shader compile failed: %s", log)
} }
return Shader(s), nil return shader(s), nil
} }
func (c *Context) deleteShader(s Shader) { func (c *Context) deleteShader(s shader) {
gl := c.gl gl := c.gl
gl.DeleteShader(mgl.Shader(s)) gl.DeleteShader(mgl.Shader(s))
} }
func (c *Context) newProgram(shaders []Shader) (Program, error) { func (c *Context) newProgram(shaders []shader) (program, error) {
gl := c.gl gl := c.gl
p := gl.CreateProgram() p := gl.CreateProgram()
if p.Value == 0 { if p.Value == 0 {
return Program{}, errors.New("opengl: glCreateProgram failed") return program{}, errors.New("opengl: glCreateProgram failed")
} }
for _, shader := range shaders { for _, shader := range shaders {
@ -276,17 +276,17 @@ func (c *Context) newProgram(shaders []Shader) (Program, error) {
gl.LinkProgram(p) gl.LinkProgram(p)
v := gl.GetProgrami(p, mgl.LINK_STATUS) v := gl.GetProgrami(p, mgl.LINK_STATUS)
if v == mgl.FALSE { if v == mgl.FALSE {
return Program{}, errors.New("opengl: program error") return program{}, errors.New("opengl: program error")
} }
return Program(p), nil return program(p), nil
} }
func (c *Context) useProgram(p Program) { func (c *Context) useProgram(p program) {
gl := c.gl gl := c.gl
gl.UseProgram(mgl.Program(p)) gl.UseProgram(mgl.Program(p))
} }
func (c *Context) deleteProgram(p Program) { func (c *Context) deleteProgram(p program) {
gl := c.gl gl := c.gl
if !gl.IsProgram(mgl.Program(p)) { if !gl.IsProgram(mgl.Program(p)) {
return return
@ -294,7 +294,7 @@ func (c *Context) deleteProgram(p Program) {
gl.DeleteProgram(mgl.Program(p)) gl.DeleteProgram(mgl.Program(p))
} }
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation { func (c *Context) getUniformLocationImpl(p program, location string) uniformLocation {
gl := c.gl gl := c.gl
u := uniformLocation(gl.GetUniformLocation(mgl.Program(p), location)) u := uniformLocation(gl.GetUniformLocation(mgl.Program(p), location))
if u.Value == -1 { if u.Value == -1 {
@ -303,17 +303,17 @@ func (c *Context) getUniformLocationImpl(p Program, location string) uniformLoca
return u return u
} }
func (c *Context) uniformInt(p Program, location string, v int) { func (c *Context) uniformInt(p program, location string, v int) {
gl := c.gl gl := c.gl
gl.Uniform1i(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v) gl.Uniform1i(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
} }
func (c *Context) uniformFloat(p Program, location string, v float32) { func (c *Context) uniformFloat(p program, location string, v float32) {
gl := c.gl gl := c.gl
gl.Uniform1f(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v) gl.Uniform1f(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
} }
func (c *Context) uniformFloats(p Program, location string, v []float32) { func (c *Context) uniformFloats(p program, location string, v []float32) {
gl := c.gl gl := c.gl
l := mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)) l := mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location))
switch len(v) { switch len(v) {
@ -328,7 +328,7 @@ func (c *Context) uniformFloats(p Program, location string, v []float32) {
} }
} }
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation { func (c *Context) getAttribLocationImpl(p program, location string) attribLocation {
gl := c.gl gl := c.gl
a := attribLocation(gl.GetAttribLocation(mgl.Program(p), location)) a := attribLocation(gl.GetAttribLocation(mgl.Program(p), location))
if a.Value == ^uint(0) { if a.Value == ^uint(0) {
@ -337,41 +337,41 @@ func (c *Context) getAttribLocationImpl(p Program, location string) attribLocati
return a return a
} }
func (c *Context) vertexAttribPointer(p Program, location string, size int, dataType DataType, stride int, offset int) { func (c *Context) vertexAttribPointer(p program, location string, size int, dataType DataType, stride int, offset int) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.VertexAttribPointer(mgl.Attrib(l), size, mgl.Enum(dataType), false, stride, offset) gl.VertexAttribPointer(mgl.Attrib(l), size, mgl.Enum(dataType), false, stride, offset)
} }
func (c *Context) enableVertexAttribArray(p Program, location string) { func (c *Context) enableVertexAttribArray(p program, location string) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.EnableVertexAttribArray(mgl.Attrib(l)) gl.EnableVertexAttribArray(mgl.Attrib(l))
} }
func (c *Context) disableVertexAttribArray(p Program, location string) { func (c *Context) disableVertexAttribArray(p program, location string) {
gl := c.gl gl := c.gl
l := c.locationCache.GetAttribLocation(c, p, location) l := c.locationCache.GetAttribLocation(c, p, location)
gl.DisableVertexAttribArray(mgl.Attrib(l)) gl.DisableVertexAttribArray(mgl.Attrib(l))
} }
func (c *Context) newArrayBuffer(size int) Buffer { func (c *Context) newArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.CreateBuffer() b := gl.CreateBuffer()
gl.BindBuffer(mgl.Enum(ArrayBuffer), b) gl.BindBuffer(mgl.Enum(ArrayBuffer), b)
gl.BufferInit(mgl.Enum(ArrayBuffer), size, mgl.Enum(DynamicDraw)) gl.BufferInit(mgl.Enum(ArrayBuffer), size, mgl.Enum(DynamicDraw))
return Buffer(b) return buffer(b)
} }
func (c *Context) newElementArrayBuffer(size int) Buffer { func (c *Context) newElementArrayBuffer(size int) buffer {
gl := c.gl gl := c.gl
b := gl.CreateBuffer() b := gl.CreateBuffer()
gl.BindBuffer(mgl.Enum(ElementArrayBuffer), b) gl.BindBuffer(mgl.Enum(ElementArrayBuffer), b)
gl.BufferInit(mgl.Enum(ElementArrayBuffer), size, mgl.Enum(DynamicDraw)) gl.BufferInit(mgl.Enum(ElementArrayBuffer), size, mgl.Enum(DynamicDraw))
return Buffer(b) return buffer(b)
} }
func (c *Context) BindBuffer(bufferType BufferType, b Buffer) { func (c *Context) BindBuffer(bufferType bufferType, b buffer) {
gl := c.gl gl := c.gl
gl.BindBuffer(mgl.Enum(bufferType), mgl.Buffer(b)) gl.BindBuffer(mgl.Enum(bufferType), mgl.Buffer(b))
} }
@ -386,12 +386,12 @@ func (c *Context) ElementArrayBufferSubData(data []uint16) {
gl.BufferSubData(mgl.Enum(ElementArrayBuffer), 0, uint16sToBytes(data)) gl.BufferSubData(mgl.Enum(ElementArrayBuffer), 0, uint16sToBytes(data))
} }
func (c *Context) deleteBuffer(b Buffer) { func (c *Context) deleteBuffer(b buffer) {
gl := c.gl gl := c.gl
gl.DeleteBuffer(mgl.Buffer(b)) gl.DeleteBuffer(mgl.Buffer(b))
} }
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) { func (c *Context) DrawElements(mode mode, len int, offsetInBytes int) {
gl := c.gl gl := c.gl
gl.DrawElements(mgl.Enum(mode), len, mgl.UNSIGNED_SHORT, offsetInBytes) gl.DrawElements(mgl.Enum(mode), len, mgl.UNSIGNED_SHORT, offsetInBytes)
} }

View File

@ -28,7 +28,7 @@ func newLocationCache() *locationCache {
} }
} }
func (c *locationCache) GetUniformLocation(context *Context, p Program, location string) uniformLocation { func (c *locationCache) GetUniformLocation(context *Context, p program, location string) uniformLocation {
id := getProgramID(p) id := getProgramID(p)
if _, ok := c.uniformLocationCache[id]; !ok { if _, ok := c.uniformLocationCache[id]; !ok {
c.uniformLocationCache[id] = map[string]uniformLocation{} c.uniformLocationCache[id] = map[string]uniformLocation{}
@ -41,7 +41,7 @@ func (c *locationCache) GetUniformLocation(context *Context, p Program, location
return l return l
} }
func (c *locationCache) GetAttribLocation(context *Context, p Program, location string) attribLocation { func (c *locationCache) GetAttribLocation(context *Context, p program, location string) attribLocation {
id := getProgramID(p) id := getProgramID(p)
if _, ok := c.attribLocationCache[id]; !ok { if _, ok := c.attribLocationCache[id]; !ok {
c.attribLocationCache[id] = map[string]attribLocation{} c.attribLocationCache[id] = map[string]attribLocation{}

View File

@ -54,12 +54,12 @@ func (a *arrayBufferLayout) totalBytes() int {
} }
// newArrayBuffer creates OpenGL's buffer object for the array buffer. // newArrayBuffer creates OpenGL's buffer object for the array buffer.
func (a *arrayBufferLayout) newArrayBuffer() Buffer { func (a *arrayBufferLayout) newArrayBuffer() buffer {
return GetContext().newArrayBuffer(a.totalBytes() * IndicesNum) return GetContext().newArrayBuffer(a.totalBytes() * IndicesNum)
} }
// enable binds the array buffer the given program to use the array buffer. // enable binds the array buffer the given program to use the array buffer.
func (a *arrayBufferLayout) enable(program Program) { func (a *arrayBufferLayout) enable(program program) {
for _, p := range a.parts { for _, p := range a.parts {
GetContext().enableVertexAttribArray(program, p.name) GetContext().enableVertexAttribArray(program, p.name)
} }
@ -72,7 +72,7 @@ func (a *arrayBufferLayout) enable(program Program) {
} }
// disable stops using the array buffer. // disable stops using the array buffer.
func (a *arrayBufferLayout) disable(program Program) { func (a *arrayBufferLayout) disable(program program) {
// TODO: Disabling should be done in reversed order? // TODO: Disabling should be done in reversed order?
for _, p := range a.parts { for _, p := range a.parts {
GetContext().disableVertexAttribArray(program, p.name) GetContext().disableVertexAttribArray(program, p.name)
@ -112,20 +112,20 @@ func ArrayBufferLayoutTotalBytes() int {
// openGLState is a state for // openGLState is a state for
type openGLState struct { type openGLState struct {
// arrayBuffer is OpenGL's array buffer (vertices data). // arrayBuffer is OpenGL's array buffer (vertices data).
arrayBuffer Buffer arrayBuffer buffer
// elementArrayBuffer is OpenGL's element array buffer (indices data). // elementArrayBuffer is OpenGL's element array buffer (indices data).
elementArrayBuffer Buffer elementArrayBuffer buffer
// programNearest is OpenGL's program for rendering a texture with nearest filter. // programNearest is OpenGL's program for rendering a texture with nearest filter.
programNearest Program programNearest program
// programLinear is OpenGL's program for rendering a texture with linear filter. // programLinear is OpenGL's program for rendering a texture with linear filter.
programLinear Program programLinear program
programScreen Program programScreen program
lastProgram Program lastProgram program
lastProjectionMatrix []float32 lastProjectionMatrix []float32
lastColorMatrix []float32 lastColorMatrix []float32
lastColorMatrixTranslation []float32 lastColorMatrixTranslation []float32
@ -137,8 +137,8 @@ var (
// theOpenGLState is the OpenGL state in the current process. // theOpenGLState is the OpenGL state in the current process.
theOpenGLState openGLState theOpenGLState openGLState
zeroBuffer Buffer zeroBuffer buffer
zeroProgram Program zeroProgram program
) )
const ( const (
@ -189,31 +189,31 @@ func (s *openGLState) reset() error {
} }
} }
shaderVertexModelviewNative, err := GetContext().newShader(VertexShader, shader(shaderVertexModelview)) shaderVertexModelviewNative, err := GetContext().newShader(VertexShader, shaderStr(shaderVertexModelview))
if err != nil { if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err)) panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
} }
defer GetContext().deleteShader(shaderVertexModelviewNative) defer GetContext().deleteShader(shaderVertexModelviewNative)
shaderFragmentNearestNative, err := GetContext().newShader(FragmentShader, shader(shaderFragmentNearest)) shaderFragmentNearestNative, err := GetContext().newShader(FragmentShader, shaderStr(shaderFragmentNearest))
if err != nil { if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err)) panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
} }
defer GetContext().deleteShader(shaderFragmentNearestNative) defer GetContext().deleteShader(shaderFragmentNearestNative)
shaderFragmentLinearNative, err := GetContext().newShader(FragmentShader, shader(shaderFragmentLinear)) shaderFragmentLinearNative, err := GetContext().newShader(FragmentShader, shaderStr(shaderFragmentLinear))
if err != nil { if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err)) panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
} }
defer GetContext().deleteShader(shaderFragmentLinearNative) defer GetContext().deleteShader(shaderFragmentLinearNative)
shaderFragmentScreenNative, err := GetContext().newShader(FragmentShader, shader(shaderFragmentScreen)) shaderFragmentScreenNative, err := GetContext().newShader(FragmentShader, shaderStr(shaderFragmentScreen))
if err != nil { if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err)) panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
} }
defer GetContext().deleteShader(shaderFragmentScreenNative) defer GetContext().deleteShader(shaderFragmentScreenNative)
s.programNearest, err = GetContext().newProgram([]Shader{ s.programNearest, err = GetContext().newProgram([]shader{
shaderVertexModelviewNative, shaderVertexModelviewNative,
shaderFragmentNearestNative, shaderFragmentNearestNative,
}) })
@ -221,7 +221,7 @@ func (s *openGLState) reset() error {
return err return err
} }
s.programLinear, err = GetContext().newProgram([]Shader{ s.programLinear, err = GetContext().newProgram([]shader{
shaderVertexModelviewNative, shaderVertexModelviewNative,
shaderFragmentLinearNative, shaderFragmentLinearNative,
}) })
@ -229,7 +229,7 @@ func (s *openGLState) reset() error {
return err return err
} }
s.programScreen, err = GetContext().newProgram([]Shader{ s.programScreen, err = GetContext().newProgram([]shader{
shaderVertexModelviewNative, shaderVertexModelviewNative,
shaderFragmentScreenNative, shaderFragmentScreenNative,
}) })
@ -268,7 +268,7 @@ func UseProgram(proj []float32, texture Texture, dstW, dstH, srcW, srcH int, col
func (s *openGLState) useProgram(proj []float32, texture Texture, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) { func (s *openGLState) useProgram(proj []float32, texture Texture, dstW, dstH, srcW, srcH int, colorM *affine.ColorM, filter graphics.Filter) {
c := GetContext() c := GetContext()
var program Program var program program
switch filter { switch filter {
case graphics.FilterNearest: case graphics.FilterNearest:
program = s.programNearest program = s.programNearest

View File

@ -27,7 +27,7 @@ const (
shaderFragmentScreen shaderFragmentScreen
) )
func shader(id shaderID) string { func shaderStr(id shaderID) string {
if id == shaderVertexModelview { if id == shaderVertexModelview {
return shaderStrVertex return shaderStrVertex
} }

View File

@ -15,10 +15,10 @@
package opengl package opengl
type ( type (
ShaderType int shaderType int
BufferType int bufferType int
BufferUsage int bufferUsage int
Mode int mode int
operation int operation int
) )