Add opengl.Texture.Pixels

This commit is contained in:
Hajime Hoshi 2014-12-31 15:11:19 +09:00
parent cc9b874dd0
commit b1d4ce2120
2 changed files with 19 additions and 11 deletions

View File

@ -16,8 +16,6 @@ package graphics
import ( import (
"errors" "errors"
"fmt"
"github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"image" "image"
@ -90,17 +88,10 @@ func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.Filte
} }
func (t *Texture) Dispose() { func (t *Texture) Dispose() {
gl.Texture(t.native).Delete() t.native.Delete()
} }
func (t *Texture) Pixels() ([]uint8, error) { func (t *Texture) Pixels() ([]uint8, error) {
w, h := internal.NextPowerOf2Int(t.width), internal.NextPowerOf2Int(t.height) w, h := internal.NextPowerOf2Int(t.width), internal.NextPowerOf2Int(t.height)
pixels := make([]uint8, 4*w*h) return t.native.Pixels(w, h)
gl.Texture(t.native).Bind(gl.TEXTURE_2D)
gl.GetTexImage(gl.TEXTURE_2D, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
if e := gl.GetError(); e != gl.NO_ERROR {
// TODO: Use glu.ErrorString
return nil, errors.New(fmt.Sprintf("gl error: %d", e))
}
return pixels, nil
} }

View File

@ -16,6 +16,7 @@ package opengl
import ( import (
"errors" "errors"
"fmt"
"github.com/go-gl/gl" "github.com/go-gl/gl"
) )
@ -33,6 +34,22 @@ type Context struct {
type Texture gl.Texture type Texture gl.Texture
func (t Texture) Pixels(width, height int) ([]uint8, error) {
// TODO: Use glGetTexLevelParameteri and GL_TEXTURE_WIDTH?
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)
if e := gl.GetError(); e != gl.NO_ERROR {
// TODO: Use glu.ErrorString
return nil, errors.New(fmt.Sprintf("gl error: %d", e))
}
return pixels, nil
}
func (t Texture) Delete() {
gl.Texture(t).Delete()
}
func NewContext() *Context { func NewContext() *Context {
c := &Context{ c := &Context{
Nearest: filterNearest, Nearest: filterNearest,