2014-12-30 19:04:52 +01:00
|
|
|
// Copyright 2014 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-25 17:32:33 +01:00
|
|
|
// +build android ios
|
2014-12-31 10:23:18 +01:00
|
|
|
|
2014-12-30 19:04:52 +01:00
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
2014-12-31 06:57:51 +01:00
|
|
|
"errors"
|
2014-12-31 07:11:19 +01:00
|
|
|
"fmt"
|
2016-11-01 18:06:37 +01:00
|
|
|
"math"
|
2015-06-20 18:33:28 +02:00
|
|
|
|
2016-11-01 18:06:37 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/endian"
|
2016-02-18 19:06:23 +01:00
|
|
|
mgl "golang.org/x/mobile/gl"
|
2014-12-30 19:04:52 +01:00
|
|
|
)
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
type (
|
|
|
|
Texture mgl.Texture
|
|
|
|
Framebuffer mgl.Framebuffer
|
|
|
|
Shader mgl.Shader
|
|
|
|
Program mgl.Program
|
|
|
|
Buffer mgl.Buffer
|
|
|
|
)
|
2015-01-16 16:56:29 +01:00
|
|
|
|
2018-02-18 18:49:00 +01:00
|
|
|
var InvalidTexture Texture
|
2017-07-02 15:18:49 +02:00
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
type (
|
|
|
|
uniformLocation mgl.Uniform
|
|
|
|
attribLocation mgl.Attrib
|
|
|
|
)
|
2015-01-12 15:16:34 +01:00
|
|
|
|
2016-02-26 18:41:38 +01:00
|
|
|
type programID uint32
|
2015-01-12 15:16:34 +01:00
|
|
|
|
2016-07-09 16:14:24 +02:00
|
|
|
var (
|
|
|
|
invalidTexture = Texture(mgl.Texture{})
|
|
|
|
invalidFramebuffer = Framebuffer(mgl.Framebuffer{(1 << 32) - 1})
|
|
|
|
)
|
2016-06-17 21:46:33 +02:00
|
|
|
|
2018-02-18 17:45:03 +01:00
|
|
|
func getProgramID(p Program) programID {
|
2016-02-26 18:41:38 +01:00
|
|
|
return programID(p.Value)
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2016-07-03 11:11:37 +02:00
|
|
|
func init() {
|
|
|
|
VertexShader = mgl.VERTEX_SHADER
|
|
|
|
FragmentShader = mgl.FRAGMENT_SHADER
|
|
|
|
ArrayBuffer = mgl.ARRAY_BUFFER
|
|
|
|
ElementArrayBuffer = mgl.ELEMENT_ARRAY_BUFFER
|
|
|
|
DynamicDraw = mgl.DYNAMIC_DRAW
|
|
|
|
StaticDraw = mgl.STATIC_DRAW
|
|
|
|
Triangles = mgl.TRIANGLES
|
|
|
|
Lines = mgl.LINES
|
2016-10-22 07:51:23 +02:00
|
|
|
Short = mgl.SHORT
|
|
|
|
Float = mgl.FLOAT
|
2016-07-03 11:11:37 +02:00
|
|
|
|
|
|
|
zero = mgl.ZERO
|
|
|
|
one = mgl.ONE
|
|
|
|
srcAlpha = mgl.SRC_ALPHA
|
|
|
|
dstAlpha = mgl.DST_ALPHA
|
|
|
|
oneMinusSrcAlpha = mgl.ONE_MINUS_SRC_ALPHA
|
|
|
|
oneMinusDstAlpha = mgl.ONE_MINUS_DST_ALPHA
|
|
|
|
}
|
|
|
|
|
2016-02-21 14:20:33 +01:00
|
|
|
type context struct {
|
2016-07-03 18:04:27 +02:00
|
|
|
gl mgl.Context
|
|
|
|
worker mgl.Worker
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
2014-12-31 08:12:13 +01:00
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func Init() {
|
2016-07-03 18:04:27 +02:00
|
|
|
c := &Context{}
|
2016-05-19 16:37:58 +02:00
|
|
|
c.gl, c.worker = mgl.NewContext()
|
2017-05-30 19:09:27 +02:00
|
|
|
theContext = c
|
2014-12-30 19:04:52 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:25:35 +02:00
|
|
|
func (c *Context) DoWork(chError <-chan error, chDone <-chan struct{}) error {
|
|
|
|
// TODO: Check this is called on the rendering thread
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-chError:
|
|
|
|
return err
|
|
|
|
case <-c.worker.WorkAvailable():
|
|
|
|
c.worker.DoWork()
|
2016-07-22 19:09:48 +02:00
|
|
|
case <-chDone:
|
|
|
|
break loop
|
2016-07-03 18:25:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:30:14 +02:00
|
|
|
func (c *Context) Reset() error {
|
2016-06-09 18:19:10 +02:00
|
|
|
c.locationCache = newLocationCache()
|
2016-07-09 16:14:24 +02:00
|
|
|
c.lastTexture = invalidTexture
|
2016-06-17 21:46:33 +02:00
|
|
|
c.lastFramebuffer = invalidFramebuffer
|
2016-06-09 18:19:10 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
|
|
|
c.lastCompositeMode = CompositeModeUnknown
|
|
|
|
c.gl.Enable(mgl.BLEND)
|
|
|
|
c.BlendFunc(CompositeModeSourceOver)
|
2016-06-17 21:46:33 +02:00
|
|
|
f := c.gl.GetInteger(mgl.FRAMEBUFFER_BINDING)
|
|
|
|
c.screenFramebuffer = Framebuffer(mgl.Framebuffer{uint32(f)})
|
2016-06-18 12:55:04 +02:00
|
|
|
// TODO: Need to update screenFramebufferWidth/Height?
|
2016-06-17 23:25:40 +02:00
|
|
|
return nil
|
2016-06-09 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
2016-05-07 12:12:19 +02:00
|
|
|
func (c *Context) BlendFunc(mode CompositeMode) {
|
|
|
|
gl := c.gl
|
|
|
|
if c.lastCompositeMode == mode {
|
|
|
|
return
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
2016-05-07 12:12:19 +02:00
|
|
|
c.lastCompositeMode = mode
|
2016-07-03 11:11:37 +02:00
|
|
|
s, d := operations(mode)
|
2016-05-07 12:12:19 +02:00
|
|
|
gl.BlendFunc(mgl.Enum(s), mgl.Enum(d))
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
func (c *Context) NewTexture(width, height int, pixels []uint8) (Texture, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
t := gl.CreateTexture()
|
2016-02-24 18:17:28 +01:00
|
|
|
if t.Value <= 0 {
|
2016-02-24 16:26:16 +01:00
|
|
|
return Texture{}, errors.New("opengl: creating texture failed")
|
2014-12-31 06:57:51 +01:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4)
|
2017-09-24 17:11:19 +02:00
|
|
|
c.BindTexture(Texture(t))
|
2014-12-31 06:57:51 +01:00
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, mgl.NEAREST)
|
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, mgl.NEAREST)
|
2014-12-31 06:57:51 +01:00
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
var p []uint8
|
2015-01-28 16:58:56 +01:00
|
|
|
if pixels != nil {
|
|
|
|
p = pixels
|
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.TexImage2D(mgl.TEXTURE_2D, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, p)
|
2014-12-31 06:57:51 +01:00
|
|
|
|
|
|
|
return Texture(t), nil
|
|
|
|
}
|
2014-12-31 07:22:15 +01:00
|
|
|
|
2017-09-24 17:14:25 +02:00
|
|
|
func (c *Context) bindFramebufferImpl(f Framebuffer) {
|
2016-05-31 19:33:31 +02:00
|
|
|
gl := c.gl
|
|
|
|
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f))
|
|
|
|
}
|
|
|
|
|
2015-01-17 10:22:58 +01:00
|
|
|
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2015-01-02 17:14:36 +01:00
|
|
|
gl.Flush()
|
2015-01-17 10:22:58 +01:00
|
|
|
|
2016-05-31 19:33:31 +02:00
|
|
|
c.bindFramebuffer(f)
|
2015-01-17 10:22:58 +01:00
|
|
|
|
2014-12-31 10:23:18 +01:00
|
|
|
pixels := make([]uint8, 4*width*height)
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.ReadPixels(pixels, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE)
|
|
|
|
if e := gl.GetError(); e != mgl.NO_ERROR {
|
2016-02-24 15:30:43 +01:00
|
|
|
return nil, fmt.Errorf("opengl: glReadPixels: %d", e)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
return pixels, nil
|
|
|
|
}
|
|
|
|
|
2017-09-24 17:11:19 +02:00
|
|
|
func (c *Context) bindTextureImpl(t Texture) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.BindTexture(mgl.TEXTURE_2D, mgl.Texture(t))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteTexture(t Texture) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsTexture(mgl.Texture(t)) {
|
|
|
|
return
|
|
|
|
}
|
2016-07-09 22:04:25 +02:00
|
|
|
if c.lastTexture == t {
|
|
|
|
c.lastTexture = invalidTexture
|
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DeleteTexture(mgl.Texture(t))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 16:54:36 +02:00
|
|
|
func (c *Context) IsTexture(t Texture) bool {
|
|
|
|
gl := c.gl
|
|
|
|
return gl.IsTexture(mgl.Texture(t))
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:58:58 +01:00
|
|
|
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.TexSubImage2D(mgl.TEXTURE_2D, 0, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, p)
|
2015-01-20 15:58:58 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 07:22:15 +01:00
|
|
|
func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
f := gl.CreateFramebuffer()
|
2016-02-24 18:17:28 +01:00
|
|
|
if f.Value <= 0 {
|
2016-02-24 16:26:16 +01:00
|
|
|
return Framebuffer{}, errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false")
|
2016-02-24 15:30:43 +01:00
|
|
|
}
|
2016-05-31 19:33:31 +02:00
|
|
|
c.bindFramebuffer(Framebuffer(f))
|
2014-12-31 07:22:15 +01:00
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.FramebufferTexture2D(mgl.FRAMEBUFFER, mgl.COLOR_ATTACHMENT0, mgl.TEXTURE_2D, mgl.Texture(texture), 0)
|
|
|
|
s := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER)
|
|
|
|
if s != mgl.FRAMEBUFFER_COMPLETE {
|
2016-02-05 19:48:15 +01:00
|
|
|
if s != 0 {
|
2016-02-24 15:30:43 +01:00
|
|
|
return Framebuffer{}, fmt.Errorf("opengl: creating framebuffer failed: %v", s)
|
2016-02-05 19:48:15 +01:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
if e := gl.GetError(); e != mgl.NO_ERROR {
|
2016-02-24 15:30:43 +01:00
|
|
|
return Framebuffer{}, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
|
2016-02-05 19:48:15 +01:00
|
|
|
}
|
2016-02-24 15:30:43 +01:00
|
|
|
return Framebuffer{}, fmt.Errorf("opengl: creating framebuffer failed: unknown error")
|
2014-12-31 07:22:15 +01:00
|
|
|
}
|
|
|
|
return Framebuffer(f), nil
|
|
|
|
}
|
2014-12-31 08:12:13 +01:00
|
|
|
|
2017-09-24 17:17:07 +02:00
|
|
|
func (c *Context) setViewportImpl(width, height int) {
|
2016-06-18 15:47:34 +02:00
|
|
|
gl := c.gl
|
|
|
|
gl.Viewport(0, 0, width, height)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2017-12-02 14:25:02 +01:00
|
|
|
func (c *Context) FillFramebuffer(r, g, b, a float32) error {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2017-12-02 19:51:42 +01:00
|
|
|
gl.ClearColor(adjustForClearColor(r),
|
|
|
|
adjustForClearColor(g),
|
|
|
|
adjustForClearColor(b),
|
|
|
|
adjustForClearColor(a))
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.Clear(mgl.COLOR_BUFFER_BIT)
|
2014-12-31 10:23:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteFramebuffer(f Framebuffer) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsFramebuffer(mgl.Framebuffer(f)) {
|
|
|
|
return
|
|
|
|
}
|
2016-07-06 19:08:28 +02:00
|
|
|
// If a framebuffer to be deleted is bound, a newly bound framebuffer
|
2016-06-03 20:40:56 +02:00
|
|
|
// will be a default framebuffer.
|
|
|
|
// https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml
|
|
|
|
if c.lastFramebuffer == f {
|
2016-06-17 21:46:33 +02:00
|
|
|
c.lastFramebuffer = invalidFramebuffer
|
2016-06-05 00:47:11 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
2016-06-03 20:40:56 +02:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DeleteFramebuffer(mgl.Framebuffer(f))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 08:12:13 +01:00
|
|
|
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
s := gl.CreateShader(mgl.Enum(shaderType))
|
|
|
|
if s.Value == 0 {
|
2016-11-25 16:57:02 +01:00
|
|
|
return Shader{}, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.ShaderSource(s, source)
|
2015-01-28 16:58:56 +01:00
|
|
|
gl.CompileShader(s)
|
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
v := gl.GetShaderi(s, mgl.COMPILE_STATUS)
|
|
|
|
if v == mgl.FALSE {
|
|
|
|
log := gl.GetShaderInfoLog(s)
|
2016-02-24 15:30:43 +01:00
|
|
|
return Shader{}, fmt.Errorf("opengl: shader compile failed: %s", log)
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
|
|
|
return Shader(s), nil
|
|
|
|
}
|
|
|
|
|
2014-12-31 10:23:18 +01:00
|
|
|
func (c *Context) DeleteShader(s Shader) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DeleteShader(mgl.Shader(s))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 08:48:25 +01:00
|
|
|
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2014-12-31 08:12:13 +01:00
|
|
|
p := gl.CreateProgram()
|
2016-02-18 19:06:23 +01:00
|
|
|
if p.Value == 0 {
|
2016-02-23 16:31:25 +01:00
|
|
|
return Program{}, errors.New("opengl: glCreateProgram failed")
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 08:48:25 +01:00
|
|
|
for _, shader := range shaders {
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.AttachShader(p, mgl.Shader(shader))
|
2014-12-31 08:48:25 +01:00
|
|
|
}
|
2015-01-28 16:58:56 +01:00
|
|
|
gl.LinkProgram(p)
|
2016-02-18 19:06:23 +01:00
|
|
|
v := gl.GetProgrami(p, mgl.LINK_STATUS)
|
|
|
|
if v == mgl.FALSE {
|
2016-02-23 16:31:25 +01:00
|
|
|
return Program{}, errors.New("opengl: program error")
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
|
|
|
return Program(p), nil
|
|
|
|
}
|
2014-12-31 09:45:23 +01:00
|
|
|
|
2014-12-31 10:23:18 +01:00
|
|
|
func (c *Context) UseProgram(p Program) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.UseProgram(mgl.Program(p))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
func (c *Context) DeleteProgram(p Program) {
|
|
|
|
gl := c.gl
|
2016-07-12 19:07:35 +02:00
|
|
|
if !gl.IsProgram(mgl.Program(p)) {
|
|
|
|
return
|
|
|
|
}
|
2016-07-03 17:23:45 +02:00
|
|
|
gl.DeleteProgram(mgl.Program(p))
|
|
|
|
}
|
|
|
|
|
2016-06-03 20:50:28 +02:00
|
|
|
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:13:42 +01:00
|
|
|
u := uniformLocation(gl.GetUniformLocation(mgl.Program(p), location))
|
2016-02-18 19:06:23 +01:00
|
|
|
if u.Value == -1 {
|
2015-01-28 16:58:56 +01:00
|
|
|
panic("invalid uniform location: " + location)
|
|
|
|
}
|
|
|
|
return u
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 07:52:02 +01:00
|
|
|
func (c *Context) UniformInt(p Program, location string, v int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
gl.Uniform1i(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
|
2015-01-03 07:52:02 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 03:46:46 +01:00
|
|
|
func (c *Context) UniformFloat(p Program, location string, v float32) {
|
|
|
|
gl := c.gl
|
|
|
|
gl.Uniform1f(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
|
|
|
|
}
|
|
|
|
|
2015-01-03 07:52:02 +01:00
|
|
|
func (c *Context) UniformFloats(p Program, location string, v []float32) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location))
|
2015-01-03 07:52:02 +01:00
|
|
|
switch len(v) {
|
2017-12-11 15:07:01 +01:00
|
|
|
case 2:
|
|
|
|
gl.Uniform2fv(l, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
case 4:
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.Uniform4fv(l, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
case 16:
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.UniformMatrix4fv(l, v)
|
2015-01-03 07:21:47 +01:00
|
|
|
default:
|
2017-12-11 15:07:01 +01:00
|
|
|
panic("not reached")
|
2014-12-31 12:07:27 +01:00
|
|
|
}
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 20:50:28 +02:00
|
|
|
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:13:42 +01:00
|
|
|
a := attribLocation(gl.GetAttribLocation(mgl.Program(p), location))
|
2016-02-18 19:06:23 +01:00
|
|
|
if a.Value == ^uint(0) {
|
2015-01-28 16:58:56 +01:00
|
|
|
panic("invalid attrib location: " + location)
|
|
|
|
}
|
|
|
|
return a
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 11:21:59 +01:00
|
|
|
func (c *Context) VertexAttribPointer(p Program, location string, size int, dataType DataType, stride int, offset int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2017-12-03 11:21:59 +01:00
|
|
|
gl.VertexAttribPointer(mgl.Attrib(l), size, mgl.Enum(dataType), false, stride, offset)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) EnableVertexAttribArray(p Program, location string) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.EnableVertexAttribArray(mgl.Attrib(l))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DisableVertexAttribArray(p Program, location string) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DisableVertexAttribArray(mgl.Attrib(l))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2016-10-22 11:45:32 +02:00
|
|
|
func uint16ToBytes(v []uint16) []uint8 {
|
2016-10-22 12:16:16 +02:00
|
|
|
// TODO: Consider endian?
|
2016-10-22 11:45:32 +02:00
|
|
|
b := make([]uint8, len(v)*2)
|
2016-02-23 17:32:56 +01:00
|
|
|
for i, x := range v {
|
2016-10-22 11:45:32 +02:00
|
|
|
b[2*i] = uint8(x)
|
|
|
|
b[2*i+1] = uint8(x >> 8)
|
2016-02-23 17:32:56 +01:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:29:56 +02:00
|
|
|
func (c *Context) NewArrayBuffer(size int) Buffer {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
b := gl.CreateBuffer()
|
2017-08-31 18:29:56 +02:00
|
|
|
gl.BindBuffer(mgl.Enum(ArrayBuffer), b)
|
|
|
|
gl.BufferInit(mgl.Enum(ArrayBuffer), size, mgl.Enum(DynamicDraw))
|
|
|
|
return Buffer(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewElementArrayBuffer(indices []uint16) Buffer {
|
|
|
|
gl := c.gl
|
|
|
|
b := gl.CreateBuffer()
|
|
|
|
gl.BindBuffer(mgl.Enum(ElementArrayBuffer), b)
|
|
|
|
gl.BufferData(mgl.Enum(ElementArrayBuffer), uint16ToBytes(indices), mgl.Enum(StaticDraw))
|
2015-01-17 04:45:19 +01:00
|
|
|
return Buffer(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) BindElementArrayBuffer(b Buffer) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.BindBuffer(mgl.ELEMENT_ARRAY_BUFFER, mgl.Buffer(b))
|
2014-12-31 09:45:23 +01:00
|
|
|
}
|
2014-12-31 09:53:04 +01:00
|
|
|
|
2016-11-01 18:06:37 +01:00
|
|
|
func float32ToBytes(v []float32) []byte {
|
|
|
|
b := make([]byte, len(v)*4)
|
|
|
|
if endian.IsLittle() {
|
|
|
|
for i, x := range v {
|
|
|
|
bits := math.Float32bits(x)
|
|
|
|
b[4*i] = uint8(bits)
|
|
|
|
b[4*i+1] = uint8(bits >> 8)
|
|
|
|
b[4*i+2] = uint8(bits >> 16)
|
|
|
|
b[4*i+3] = uint8(bits >> 24)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: Test this
|
|
|
|
for i, x := range v {
|
|
|
|
bits := math.Float32bits(x)
|
|
|
|
b[4*i] = uint8(bits >> 24)
|
|
|
|
b[4*i+1] = uint8(bits >> 16)
|
|
|
|
b[4*i+2] = uint8(bits >> 8)
|
|
|
|
b[4*i+3] = uint8(bits)
|
|
|
|
}
|
2016-10-25 17:20:41 +02:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:06:37 +01:00
|
|
|
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-11-01 18:06:37 +01:00
|
|
|
gl.BufferSubData(mgl.Enum(bufferType), 0, float32ToBytes(data))
|
2014-12-31 09:53:04 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
func (c *Context) DeleteBuffer(b Buffer) {
|
|
|
|
gl := c.gl
|
|
|
|
gl.DeleteBuffer(mgl.Buffer(b))
|
|
|
|
}
|
|
|
|
|
2016-06-03 05:41:18 +02:00
|
|
|
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-06-03 05:41:18 +02:00
|
|
|
gl.DrawElements(mgl.Enum(mode), len, mgl.UNSIGNED_SHORT, offsetInBytes)
|
2014-12-31 09:53:04 +01:00
|
|
|
}
|
2016-06-06 19:24:36 +02:00
|
|
|
|
|
|
|
func (c *Context) Flush() {
|
|
|
|
gl := c.gl
|
|
|
|
gl.Flush()
|
|
|
|
}
|