2014-12-31 13:55:40 +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.
|
|
|
|
|
|
|
|
// +build js
|
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-06-14 20:35:35 +02:00
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
"github.com/gopherjs/gopherjs/js"
|
|
|
|
"github.com/gopherjs/webgl"
|
|
|
|
)
|
|
|
|
|
2017-07-02 15:18:49 +02:00
|
|
|
// Note that `type Texture *js.Object` doesn't work.
|
|
|
|
// There is no way to get the internal object in that case.
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
type Texture struct {
|
2015-02-28 17:26:05 +01:00
|
|
|
*js.Object
|
2015-01-15 15:26:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Framebuffer struct {
|
2015-02-28 17:26:05 +01:00
|
|
|
*js.Object
|
2015-01-15 15:26:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Shader struct {
|
2015-02-28 17:26:05 +01:00
|
|
|
*js.Object
|
2015-01-15 15:26:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Program struct {
|
2015-02-28 17:26:05 +01:00
|
|
|
*js.Object
|
2015-01-15 15:26:05 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 04:45:19 +01:00
|
|
|
type Buffer struct {
|
2015-02-28 17:26:05 +01:00
|
|
|
*js.Object
|
2015-01-17 04:45:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-02 15:18:49 +02:00
|
|
|
func (t Texture) equals(other Texture) bool {
|
|
|
|
return t.Object == other.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f Framebuffer) equals(other Framebuffer) bool {
|
|
|
|
return f.Object == other.Object
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:13:42 +01:00
|
|
|
type uniformLocation struct {
|
2015-02-28 17:26:05 +01:00
|
|
|
*js.Object
|
2015-01-15 15:26:05 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 19:13:42 +01:00
|
|
|
type attribLocation int
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2016-02-26 18:41:38 +01:00
|
|
|
type programID int
|
2015-01-12 15:16:34 +01:00
|
|
|
|
2016-07-09 16:14:24 +02:00
|
|
|
var (
|
|
|
|
invalidTexture = Texture{}
|
|
|
|
invalidFramebuffer = Framebuffer{}
|
|
|
|
)
|
2016-06-17 21:46:33 +02:00
|
|
|
|
2016-02-26 18:41:38 +01:00
|
|
|
func (p Program) id() programID {
|
|
|
|
return programID(p.Get("__ebiten_programId").Int())
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 11:11:37 +02:00
|
|
|
func init() {
|
2016-11-25 17:17:22 +01:00
|
|
|
// Accessing the prototype is rquired on Safari.
|
|
|
|
c := js.Global.Get("WebGLRenderingContext").Get("prototype")
|
2016-07-03 11:11:37 +02:00
|
|
|
Nearest = Filter(c.Get("NEAREST").Int())
|
|
|
|
Linear = Filter(c.Get("LINEAR").Int())
|
|
|
|
VertexShader = ShaderType(c.Get("VERTEX_SHADER").Int())
|
|
|
|
FragmentShader = ShaderType(c.Get("FRAGMENT_SHADER").Int())
|
|
|
|
ArrayBuffer = BufferType(c.Get("ARRAY_BUFFER").Int())
|
|
|
|
ElementArrayBuffer = BufferType(c.Get("ELEMENT_ARRAY_BUFFER").Int())
|
|
|
|
DynamicDraw = BufferUsage(c.Get("DYNAMIC_DRAW").Int())
|
|
|
|
StaticDraw = BufferUsage(c.Get("STATIC_DRAW").Int())
|
|
|
|
Triangles = Mode(c.Get("TRIANGLES").Int())
|
|
|
|
Lines = Mode(c.Get("LINES").Int())
|
2016-10-22 07:51:23 +02:00
|
|
|
Short = DataType(c.Get("SHORT").Int())
|
|
|
|
Float = DataType(c.Get("FLOAT").Int())
|
2016-07-03 11:11:37 +02:00
|
|
|
|
|
|
|
zero = operation(c.Get("ZERO").Int())
|
|
|
|
one = operation(c.Get("ONE").Int())
|
|
|
|
srcAlpha = operation(c.Get("SRC_ALPHA").Int())
|
|
|
|
dstAlpha = operation(c.Get("DST_ALPHA").Int())
|
|
|
|
oneMinusSrcAlpha = operation(c.Get("ONE_MINUS_SRC_ALPHA").Int())
|
|
|
|
oneMinusDstAlpha = operation(c.Get("ONE_MINUS_DST_ALPHA").Int())
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
type context struct {
|
2016-05-31 19:33:31 +02:00
|
|
|
gl *webgl.Context
|
2017-01-20 18:47:31 +01:00
|
|
|
loseContext *js.Object
|
2016-05-31 19:33:31 +02:00
|
|
|
lastProgramID programID
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func Init() error {
|
2015-01-27 14:02:23 +01:00
|
|
|
var gl *webgl.Context
|
|
|
|
|
|
|
|
if js.Global.Get("require") == js.Undefined {
|
|
|
|
// TODO: Define id?
|
|
|
|
canvas := js.Global.Get("document").Call("querySelector", "canvas")
|
|
|
|
var err error
|
|
|
|
gl, err = webgl.NewContext(canvas, &webgl.ContextAttributes{
|
|
|
|
Alpha: true,
|
|
|
|
PremultipliedAlpha: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-30 19:09:27 +02:00
|
|
|
return err
|
2015-01-27 14:02:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-06 07:35:21 +01:00
|
|
|
// TODO: Now Ebiten with headless-gl doesn't work well (#141).
|
2015-01-27 14:02:23 +01:00
|
|
|
// Use headless-gl for testing.
|
2016-01-19 19:43:13 +01:00
|
|
|
options := map[string]bool{
|
|
|
|
"alpha": true,
|
|
|
|
"premultipliedAlpha": true,
|
|
|
|
}
|
|
|
|
webglContext := js.Global.Call("require", "gl").Invoke(16, 16, options)
|
2015-01-27 14:02:23 +01:00
|
|
|
gl = &webgl.Context{Object: webglContext}
|
|
|
|
}
|
2016-07-03 17:55:04 +02:00
|
|
|
c := &Context{}
|
2015-01-02 07:20:05 +01:00
|
|
|
c.gl = gl
|
2017-01-20 18:47:31 +01:00
|
|
|
// Getting an extension might fail after the context is lost, so
|
|
|
|
// it is required to get the extension here.
|
|
|
|
c.loseContext = gl.GetExtension("WEBGL_lose_context")
|
|
|
|
if c.loseContext != nil {
|
|
|
|
// This testing function name is temporary.
|
|
|
|
js.Global.Set("_ebiten_loseContextForTesting", func() {
|
|
|
|
c.loseContext.Call("loseContext")
|
|
|
|
})
|
|
|
|
}
|
2017-05-30 19:09:27 +02:00
|
|
|
theContext = c
|
|
|
|
return nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
gl := c.gl
|
|
|
|
gl.Enable(gl.BLEND)
|
|
|
|
c.BlendFunc(CompositeModeSourceOver)
|
2016-06-17 21:46:33 +02:00
|
|
|
f := gl.GetParameter(gl.FRAMEBUFFER_BINDING)
|
|
|
|
c.screenFramebuffer = Framebuffer{f}
|
2016-06-17 23:25:40 +02:00
|
|
|
return nil
|
2016-06-09 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
2016-02-29 18:16:32 +01:00
|
|
|
func (c *Context) BlendFunc(mode CompositeMode) {
|
|
|
|
if c.lastCompositeMode == mode {
|
2016-02-28 17:44:09 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-29 18:16:32 +01:00
|
|
|
c.lastCompositeMode = mode
|
2016-07-03 11:11:37 +02:00
|
|
|
s, d := operations(mode)
|
2016-02-28 16:25:16 +01:00
|
|
|
gl := c.gl
|
|
|
|
gl.BlendFunc(int(s), int(d))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 04:45:19 +01:00
|
|
|
func (c *Context) NewTexture(width, height int, pixels []uint8, filter Filter) (Texture, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
|
|
|
t := gl.CreateTexture()
|
2015-01-04 14:25:58 +01:00
|
|
|
if t == nil {
|
2016-02-23 16:31:25 +01:00
|
|
|
return Texture{nil}, errors.New("opengl: glGenTexture failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
2016-07-09 12:15:53 +02:00
|
|
|
if err := c.BindTexture(Texture{t}); err != nil {
|
|
|
|
return Texture{}, err
|
|
|
|
}
|
2014-12-31 13:55:40 +01:00
|
|
|
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
|
|
|
|
|
2015-01-20 15:58:58 +01:00
|
|
|
// TODO: Can we use glTexSubImage2D with linear filtering?
|
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
|
|
|
|
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
|
|
|
// GLenum type, ArrayBufferView? pixels);
|
2015-01-02 15:30:22 +01:00
|
|
|
var p interface{}
|
|
|
|
if pixels != nil {
|
|
|
|
p = pixels
|
|
|
|
}
|
|
|
|
gl.Call("texImage2D", gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, p)
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
return Texture{t}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 15:47:34 +02:00
|
|
|
func (c *Context) bindFramebufferImpl(f Framebuffer) error {
|
2015-02-19 18:02:23 +01:00
|
|
|
gl := c.gl
|
2016-05-31 19:33:31 +02:00
|
|
|
gl.BindFramebuffer(gl.FRAMEBUFFER, f.Object)
|
2016-06-18 15:47:34 +02:00
|
|
|
return nil
|
2015-02-19 18:02:23 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 10:22:58 +01:00
|
|
|
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2015-01-17 10:22:58 +01:00
|
|
|
|
2015-02-19 18:02:23 +01:00
|
|
|
c.bindFramebuffer(f)
|
2015-01-17 10:22:58 +01:00
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
pixels := js.Global.Get("Uint8Array").New(4 * width * height)
|
|
|
|
gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
|
|
|
if e := gl.GetError(); e != gl.NO_ERROR {
|
2016-02-23 16:31:25 +01:00
|
|
|
return nil, errors.New(fmt.Sprintf("opengl: error: %d", e))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
return pixels.Interface().([]uint8), nil
|
|
|
|
}
|
|
|
|
|
2016-07-09 12:15:53 +02:00
|
|
|
func (c *Context) bindTextureImpl(t Texture) error {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.BindTexture(gl.TEXTURE_2D, t.Object)
|
2016-07-09 12:15:53 +02:00
|
|
|
return nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteTexture(t Texture) {
|
|
|
|
gl := c.gl
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsTexture(t.Object) {
|
|
|
|
return
|
|
|
|
}
|
2016-07-09 22:04:25 +02:00
|
|
|
if c.lastTexture == t {
|
|
|
|
c.lastTexture = invalidTexture
|
|
|
|
}
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.DeleteTexture(t.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-06-12 16:54:36 +02:00
|
|
|
func (c *Context) IsTexture(t Texture) bool {
|
|
|
|
gl := c.gl
|
2017-01-21 17:37:08 +01:00
|
|
|
b := gl.IsTexture(t.Object)
|
|
|
|
return b
|
2016-06-12 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
2015-01-20 15:58:58 +01:00
|
|
|
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
|
2015-01-02 15:30:22 +01:00
|
|
|
gl := c.gl
|
2015-01-20 15:58:58 +01:00
|
|
|
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
|
|
|
// GLsizei width, GLsizei height,
|
|
|
|
// GLenum format, GLenum type, ArrayBufferView? pixels);
|
|
|
|
gl.Call("texSubImage2D", gl.TEXTURE_2D, 0, 0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, p)
|
2015-01-02 15:30:22 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
|
|
|
f := gl.CreateFramebuffer()
|
2015-02-19 18:02:23 +01:00
|
|
|
c.bindFramebuffer(Framebuffer{f})
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t.Object, 0)
|
2016-02-05 19:48:15 +01:00
|
|
|
s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
|
|
|
|
if s != gl.FRAMEBUFFER_COMPLETE {
|
2016-02-23 16:31:25 +01:00
|
|
|
return Framebuffer{nil}, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
return Framebuffer{f}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-06-18 15:47:34 +02:00
|
|
|
func (c *Context) setViewportImpl(width, height int) error {
|
|
|
|
gl := c.gl
|
|
|
|
gl.Viewport(0, 0, width, height)
|
2014-12-31 13:55:40 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:42:08 +01:00
|
|
|
func (c *Context) FillFramebuffer(r, g, b, a float64) error {
|
2015-01-15 15:26:05 +01:00
|
|
|
// TODO: Use f?
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
|
|
|
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 := c.gl
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsFramebuffer(f.Object) {
|
|
|
|
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 01:16:16 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
2016-06-03 20:40:56 +02:00
|
|
|
}
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.DeleteFramebuffer(f.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
|
|
|
|
gl := c.gl
|
|
|
|
s := gl.CreateShader(int(shaderType))
|
2015-01-04 14:25:58 +01:00
|
|
|
if s == nil {
|
2016-11-25 16:57:02 +01:00
|
|
|
return Shader{nil}, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gl.ShaderSource(s, source)
|
|
|
|
gl.CompileShader(s)
|
|
|
|
|
|
|
|
if !gl.GetShaderParameterb(s, gl.COMPILE_STATUS) {
|
|
|
|
log := gl.GetShaderInfoLog(s)
|
2016-11-25 16:57:02 +01:00
|
|
|
return Shader{nil}, fmt.Errorf("opengl: shader compile failed: %s", log)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2015-01-15 15:26:05 +01:00
|
|
|
return Shader{s}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteShader(s Shader) {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.DeleteShader(s.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
|
|
|
gl := c.gl
|
|
|
|
p := gl.CreateProgram()
|
2015-01-04 14:25:58 +01:00
|
|
|
if p == nil {
|
2016-02-23 16:31:25 +01:00
|
|
|
return Program{nil}, errors.New("opengl: glCreateProgram failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2016-02-26 19:05:12 +01:00
|
|
|
p.Set("__ebiten_programId", c.lastProgramID)
|
|
|
|
c.lastProgramID++
|
2014-12-31 13:55:40 +01:00
|
|
|
|
|
|
|
for _, shader := range shaders {
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.AttachShader(p, shader.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
gl.LinkProgram(p)
|
|
|
|
if !gl.GetProgramParameterb(p, gl.LINK_STATUS) {
|
2016-02-23 16:31:25 +01:00
|
|
|
return Program{nil}, errors.New("opengl: program error")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2015-01-15 15:26:05 +01:00
|
|
|
return Program{p}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) UseProgram(p Program) {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.UseProgram(p.Object)
|
2014-12-31 13:55:40 +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(p.Object) {
|
|
|
|
return
|
|
|
|
}
|
2016-07-03 17:23:45 +02:00
|
|
|
gl.DeleteProgram(p.Object)
|
|
|
|
}
|
|
|
|
|
2016-06-03 20:50:28 +02:00
|
|
|
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation {
|
2015-01-12 15:16:34 +01:00
|
|
|
gl := c.gl
|
2016-02-26 19:13:42 +01:00
|
|
|
return uniformLocation{gl.GetUniformLocation(p.Object, location)}
|
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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.Uniform1i(l.Object, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) UniformFloats(p Program, location string, v []float32) {
|
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
2015-01-03 07:52:02 +01:00
|
|
|
switch len(v) {
|
|
|
|
case 4:
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.Call("uniform4fv", l.Object, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
case 16:
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.UniformMatrix4fv(l.Object, false, v)
|
2015-01-03 07:21:47 +01:00
|
|
|
default:
|
|
|
|
panic("not reach")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 20:50:28 +02:00
|
|
|
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation {
|
2015-01-12 15:16:34 +01:00
|
|
|
gl := c.gl
|
2016-02-26 19:13:42 +01:00
|
|
|
return attribLocation(gl.GetAttribLocation(p.Object, location))
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2016-10-22 07:51:23 +02:00
|
|
|
func (c *Context) VertexAttribPointer(p Program, location string, size int, dataType DataType, normalize bool, stride int, offset int) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2016-10-22 07:51:23 +02:00
|
|
|
gl.VertexAttribPointer(int(l), size, int(dataType), normalize, stride, offset)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) EnableVertexAttribArray(p Program, location string) {
|
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2015-01-03 07:56:54 +01:00
|
|
|
gl.EnableVertexAttribArray(int(l))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DisableVertexAttribArray(p Program, location string) {
|
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2015-01-03 07:56:54 +01:00
|
|
|
gl.DisableVertexAttribArray(int(l))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 04:45:19 +01:00
|
|
|
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsage BufferUsage) Buffer {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
|
|
|
b := gl.CreateBuffer()
|
|
|
|
gl.BindBuffer(int(bufferType), b)
|
2015-01-17 04:45:19 +01:00
|
|
|
gl.BufferData(int(bufferType), v, int(bufferUsage))
|
|
|
|
return Buffer{b}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) BindElementArrayBuffer(b Buffer) {
|
|
|
|
gl := c.gl
|
|
|
|
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-11-01 18:06:37 +01:00
|
|
|
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2016-10-22 12:16:16 +02:00
|
|
|
gl.BufferSubData(int(bufferType), 0, data)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
func (c *Context) DeleteBuffer(b Buffer) {
|
|
|
|
gl := c.gl
|
|
|
|
gl.DeleteBuffer(b.Object)
|
|
|
|
}
|
|
|
|
|
2016-06-03 05:41:18 +02:00
|
|
|
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2016-06-03 05:41:18 +02:00
|
|
|
gl.DrawElements(int(mode), len, gl.UNSIGNED_SHORT, offsetInBytes)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2016-06-06 19:24:36 +02:00
|
|
|
|
|
|
|
func (c *Context) Flush() {
|
|
|
|
gl := c.gl
|
|
|
|
gl.Flush()
|
|
|
|
}
|
2017-01-19 18:20:41 +01:00
|
|
|
|
2017-01-20 18:47:31 +01:00
|
|
|
func (c *Context) IsContextLost() bool {
|
2017-01-19 18:20:41 +01:00
|
|
|
gl := c.gl
|
|
|
|
return gl.IsContextLost()
|
|
|
|
}
|
2017-01-20 18:47:31 +01:00
|
|
|
|
|
|
|
func (c *Context) RestoreContext() {
|
|
|
|
if c.loseContext != nil {
|
|
|
|
c.loseContext.Call("restoreContext")
|
|
|
|
}
|
|
|
|
}
|