Bug fix: image.Pixels didn't work on WebGL

This commit is contained in:
Hajime Hoshi 2015-01-17 18:22:58 +09:00
parent 76de4ae39d
commit 43872582c3
6 changed files with 21 additions and 16 deletions

View File

@ -210,7 +210,7 @@ func (i *Image) At(x, y int) color.Color {
if i.pixels == nil {
ui.Use(func(c *opengl.Context) {
var err error
i.pixels, err = i.inner.texture.Pixels(c)
i.pixels, err = i.inner.framebuffer.Pixels(c)
if err != nil {
panic(err)
}

View File

@ -168,7 +168,7 @@ func TestImageDotByDotInversion(t *testing.T) {
c0 := img0.At(i, j).(color.RGBA)
c1 := img1.At(w-i-1, h-j-1).(color.RGBA)
if c0 != c1 {
t.Errorf("c0 should equal to c1 but not: c0: %v, c1: %v", c0, c1)
t.Errorf("img0.At(%[1]d, %[2]d) should equal to img1.At(%[1]d, %[2]d) but not: %[3]v vs %[4]v", i, j, c0, c1)
}
}
}

View File

@ -112,3 +112,9 @@ func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQu
projectionMatrix := f.projectionMatrix()
return shader.DrawTexture(c, t.native, projectionMatrix, quads, geo, clr)
}
func (f *Framebuffer) Pixels(c *opengl.Context) ([]uint8, error) {
w, h := f.Size()
w, h = internal.NextPowerOf2Int(w), internal.NextPowerOf2Int(h)
return c.FramebufferPixels(f.native, w, h)
}

View File

@ -90,8 +90,3 @@ func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.Filte
func (t *Texture) Dispose(c *opengl.Context) {
c.DeleteTexture(t.native)
}
func (t *Texture) Pixels(c *opengl.Context) ([]uint8, error) {
w, h := internal.NextPowerOf2Int(t.width), internal.NextPowerOf2Int(t.height)
return c.TexturePixels(t.native, w, h)
}

View File

@ -75,12 +75,13 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterTyp
return Texture(t), nil
}
func (c *Context) TexturePixels(t Texture, width, height int) ([]uint8, error) {
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
gl.Flush()
// TODO: Use glGetTexLevelParameteri and GL_TEXTURE_WIDTH?
gl.Framebuffer(f).Bind()
pixels := make([]uint8, 4*width*height)
gl.Texture(t).Bind(gl.TEXTURE_2D)
gl.GetTexImage(gl.TEXTURE_2D, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
if e := gl.GetError(); e != gl.NO_ERROR {
return nil, errors.New(fmt.Sprintf("gl error: %d", e))
}

View File

@ -54,6 +54,8 @@ type context struct {
gl *webgl.Context
}
var lastFramebuffer Framebuffer
func NewContext(gl *webgl.Context) *Context {
c := &Context{
Nearest: FilterType(gl.NEAREST),
@ -101,12 +103,14 @@ func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterTyp
return Texture{t}, nil
}
func (c *Context) TexturePixels(t Texture, width, height int) ([]uint8, error) {
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
gl := c.gl
gl.Flush()
// TODO: Use glGetTexLevelParameteri and GL_TEXTURE_WIDTH?
lastFramebuffer = Framebuffer{nil}
gl.BindFramebuffer(gl.FRAMEBUFFER, f.Object)
pixels := js.Global.Get("Uint8Array").New(4 * width * height)
gl.BindTexture(gl.TEXTURE_2D, t.Object)
gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
if e := gl.GetError(); e != gl.NO_ERROR {
return nil, errors.New(fmt.Sprintf("gl error: %d", e))
@ -132,6 +136,7 @@ func (c *Context) GlslHighpSupported() bool {
func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
gl := c.gl
f := gl.CreateFramebuffer()
lastFramebuffer = Framebuffer{nil}
gl.BindFramebuffer(gl.FRAMEBUFFER, f)
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t.Object, 0)
@ -142,8 +147,6 @@ func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
return Framebuffer{f}, nil
}
var lastFramebuffer Framebuffer
func (c *Context) SetViewport(f Framebuffer, width, height int) error {
gl := c.gl
// TODO: Fix this after the GopherJS bug was fixed (#159)