diff --git a/image.go b/image.go index 417d32e2c..48f80d340 100644 --- a/image.go +++ b/image.go @@ -179,7 +179,7 @@ func (i *Image) ReplacePixels(p []uint8) error { if len(p) != l { return errors.New(fmt.Sprintf("p's length must be %d", l)) } - return i.texture.ReplacePixels(glContext, p) + return i.framebuffer.ReplacePixels(glContext, i.texture, p) } // A DrawImageOptions represents options to render an image on an image. diff --git a/internal/graphics/framebuffer.go b/internal/graphics/framebuffer.go index c349fb280..f6155eabf 100644 --- a/internal/graphics/framebuffer.go +++ b/internal/graphics/framebuffer.go @@ -129,3 +129,14 @@ func (f *Framebuffer) Pixels(c *opengl.Context) ([]uint8, error) { w, h = int(NextPowerOf2Int32(int32(w))), int(NextPowerOf2Int32(int32(h))) return c.FramebufferPixels(f.native, w, h) } + +func (f *Framebuffer) ReplacePixels(c *opengl.Context, t *Texture, p []uint8) error { + // Filling with non black or white color is required here for glTexSubImage2D. + // Very mysterious but this actually works (Issue #186) + if err := f.Fill(c, color.RGBA{0, 0, 0x01, 0xff}); err != nil { + return err + } + c.BindTexture(t.native) + c.TexSubImage2D(p, t.width, t.height) + return nil +} diff --git a/internal/graphics/texture.go b/internal/graphics/texture.go index 36d7c1a5f..3a5417be6 100644 --- a/internal/graphics/texture.go +++ b/internal/graphics/texture.go @@ -89,9 +89,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) ReplacePixels(c *opengl.Context, p []uint8) error { - c.BindTexture(t.native) - c.TexSubImage2D(p, t.width, t.height) - return nil -}