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.
|
|
|
|
|
2016-02-25 17:58:19 +01:00
|
|
|
// NOTICE: This file is not maintained well.
|
|
|
|
|
2016-05-07 12:12:19 +02:00
|
|
|
// +build android
|
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"
|
2015-06-20 18:33:28 +02:00
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
mgl "golang.org/x/mobile/gl"
|
2014-12-30 19:04:52 +01:00
|
|
|
)
|
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
type Texture mgl.Texture
|
|
|
|
type Framebuffer mgl.Framebuffer
|
|
|
|
type Shader mgl.Shader
|
|
|
|
type Program mgl.Program
|
2016-02-24 18:21:44 +01:00
|
|
|
type Buffer mgl.Buffer
|
2015-01-16 16:56:29 +01:00
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
var ZeroFramebuffer Framebuffer
|
2015-02-09 02:25:00 +01:00
|
|
|
|
2015-01-16 16:56:29 +01:00
|
|
|
// TODO: Remove this after the GopherJS bug was fixed (#159)
|
|
|
|
func (p Program) Equals(other Program) bool {
|
|
|
|
return p == other
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:13:42 +01:00
|
|
|
type uniformLocation mgl.Uniform
|
|
|
|
type 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-02-26 18:41:38 +01:00
|
|
|
func (p Program) id() programID {
|
|
|
|
return programID(p.Value)
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2016-02-21 14:20:33 +01:00
|
|
|
type context struct {
|
2016-05-07 12:26:35 +02:00
|
|
|
gl mgl.Context
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
2014-12-31 08:12:13 +01:00
|
|
|
|
2014-12-30 19:04:52 +01:00
|
|
|
func NewContext() *Context {
|
2014-12-31 06:57:51 +01:00
|
|
|
c := &Context{
|
2016-02-18 19:06:23 +01:00
|
|
|
Nearest: mgl.NEAREST,
|
|
|
|
Linear: mgl.LINEAR,
|
|
|
|
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-05-07 12:26:35 +02:00
|
|
|
locationCache: newLocationCache(),
|
|
|
|
lastCompositeMode: CompositeModeUnknown,
|
2014-12-31 06:57:51 +01:00
|
|
|
}
|
2014-12-30 19:04:52 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-05-07 12:12:19 +02:00
|
|
|
func (c *Context) SetContext(gl mgl.Context) {
|
|
|
|
c.gl = gl
|
|
|
|
if gl == nil {
|
|
|
|
return
|
2016-02-19 03:43:16 +01:00
|
|
|
}
|
2014-12-30 19:04:52 +01:00
|
|
|
// Textures' pixel formats are alpha premultiplied.
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.Enable(mgl.BLEND)
|
2016-05-07 12:12:19 +02:00
|
|
|
c.BlendFunc(CompositeModeSourceOver)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) IsGLContextNil() bool {
|
|
|
|
return c.gl == nil
|
2014-12-30 19:04:52 +01:00
|
|
|
}
|
2014-12-31 06:57:51 +01: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
|
|
|
|
s, d := c.operations(mode)
|
|
|
|
gl.BlendFunc(mgl.Enum(s), mgl.Enum(d))
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 04:45:19 +01:00
|
|
|
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (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)
|
|
|
|
gl.BindTexture(mgl.TEXTURE_2D, t)
|
2014-12-31 06:57:51 +01:00
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, int(filter))
|
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, int(filter))
|
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
|
|
|
|
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-02-18 19:06:23 +01:00
|
|
|
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) BindTexture(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-02-18 19:06:23 +01:00
|
|
|
gl.DeleteTexture(mgl.Texture(t))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-21 19:14:31 +01:00
|
|
|
func (c *Context) BindZeroFramebuffer() {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-21 19:14:31 +01:00
|
|
|
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(ZeroFramebuffer))
|
|
|
|
}
|
|
|
|
|
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-02-18 19:06:23 +01:00
|
|
|
gl.BindFramebuffer(mgl.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
|
|
|
|
2014-12-31 10:23:18 +01:00
|
|
|
func (c *Context) SetViewport(f Framebuffer, width, height int) error {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2014-12-31 10:23:18 +01:00
|
|
|
gl.Flush()
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f))
|
|
|
|
if err := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER); err != mgl.FRAMEBUFFER_COMPLETE {
|
2015-06-22 16:14:40 +02:00
|
|
|
if e := gl.GetError(); e != 0 {
|
2016-02-24 15:30:43 +01:00
|
|
|
return fmt.Errorf("opengl: glBindFramebuffer failed: %d", e)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
2016-02-23 16:31:25 +01:00
|
|
|
return errors.New("opengl: glBindFramebuffer failed: the context is different?")
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.Viewport(0, 0, width, height)
|
2014-12-31 10:23:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:42:08 +01:00
|
|
|
func (c *Context) FillFramebuffer(r, g, b, a float64) error {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2015-01-28 16:58:56 +01:00
|
|
|
gl.ClearColor(float32(r), float32(g), float32(b), float32(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-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-02-23 16:31:25 +01:00
|
|
|
return Shader{}, errors.New("opengl: glCreateShader failed")
|
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
|
|
|
}
|
|
|
|
|
2015-01-02 15:30:22 +01:00
|
|
|
func (c *Context) GlslHighpSupported() bool {
|
2016-05-07 12:12:19 +02:00
|
|
|
// TODO: Fix this
|
2015-01-02 15:30:22 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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-02-26 19:13:42 +01:00
|
|
|
func (c *Context) getUniformLocation(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
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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:
|
|
|
|
panic("not reach")
|
2014-12-31 12:07:27 +01:00
|
|
|
}
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 19:13:42 +01:00
|
|
|
func (c *Context) getAttribLocation(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
|
|
|
}
|
|
|
|
|
2016-02-17 19:01:41 +01:00
|
|
|
func (c *Context) VertexAttribPointer(p Program, location string, normalize bool, stride int, size int, v 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)
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.VertexAttribPointer(mgl.Attrib(l), size, mgl.SHORT, normalize, stride, v)
|
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-02-23 17:32:56 +01:00
|
|
|
func uint16ToBytes(v []uint16) []byte {
|
2016-02-24 18:17:28 +01:00
|
|
|
b := make([]byte, len(v)*2)
|
2016-02-23 17:32:56 +01:00
|
|
|
for i, x := range v {
|
|
|
|
b[2*i] = byte(x)
|
|
|
|
b[2*i+1] = byte(x >> 8)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func int16ToBytes(v []int16) []byte {
|
2016-02-24 18:17:28 +01:00
|
|
|
b := make([]byte, len(v)*2)
|
2016-02-23 17:32:56 +01:00
|
|
|
for i, x := range v {
|
|
|
|
b[2*i] = byte(uint16(x))
|
|
|
|
b[2*i+1] = byte(uint16(x) >> 8)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-01-17 04:45:19 +01:00
|
|
|
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
b := gl.CreateBuffer()
|
|
|
|
gl.BindBuffer(mgl.Enum(bufferType), b)
|
2014-12-31 13:55:40 +01:00
|
|
|
switch v := v.(type) {
|
2015-01-08 18:28:16 +01:00
|
|
|
case int:
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.BufferInit(mgl.Enum(bufferType), v, mgl.Enum(bufferUsage))
|
|
|
|
return Buffer(b)
|
2014-12-31 13:55:40 +01:00
|
|
|
case []uint16:
|
2016-02-23 17:32:56 +01:00
|
|
|
gl.BufferData(mgl.Enum(bufferType), uint16ToBytes(v), mgl.Enum(bufferUsage))
|
2014-12-31 13:55:40 +01:00
|
|
|
default:
|
|
|
|
panic("not reach")
|
|
|
|
}
|
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
|
|
|
|
2015-01-16 02:37:26 +01:00
|
|
|
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-23 17:32:56 +01:00
|
|
|
gl.BufferSubData(mgl.Enum(bufferType), 0, int16ToBytes(data))
|
2014-12-31 09:53:04 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 04:45:19 +01:00
|
|
|
func (c *Context) DrawElements(mode Mode, len int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DrawElements(mgl.Enum(mode), len, mgl.UNSIGNED_SHORT, 0)
|
2014-12-31 09:53:04 +01:00
|
|
|
}
|