ebiten/graphics/opengl/texture.go

62 lines
1.6 KiB
Go
Raw Normal View History

2014-05-01 14:42:57 +02:00
package opengl
2013-06-15 10:07:14 +02:00
import (
2014-12-06 07:47:48 +01:00
"github.com/go-gl/gl"
2014-12-05 14:16:58 +01:00
"github.com/hajimehoshi/ebiten/graphics"
2013-06-19 03:31:44 +02:00
"image"
2013-06-15 10:07:14 +02:00
)
2014-01-08 06:37:07 +01:00
type Texture struct {
2014-12-06 07:47:48 +01:00
native gl.Texture
2014-01-08 10:03:21 +01:00
width int
height int
2014-01-08 06:37:07 +01:00
}
2014-12-06 07:47:48 +01:00
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter graphics.Filter) gl.Texture {
nativeTexture := gl.GenTexture()
2013-10-19 11:47:34 +02:00
if nativeTexture < 0 {
2013-06-19 03:31:44 +02:00
panic("glGenTexture failed")
}
2014-12-06 07:47:48 +01:00
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
nativeTexture.Bind(gl.TEXTURE_2D)
defer gl.Texture(0).Bind(gl.TEXTURE_2D)
2013-10-26 19:25:41 +02:00
2014-12-06 07:47:48 +01:00
glFilter := 0
2013-10-27 13:27:16 +01:00
switch filter {
2013-12-18 10:05:28 +01:00
case graphics.FilterLinear:
2014-12-06 07:47:48 +01:00
glFilter = gl.LINEAR
2013-12-18 10:05:28 +01:00
case graphics.FilterNearest:
2014-12-06 07:47:48 +01:00
glFilter = gl.NEAREST
2013-10-27 13:27:16 +01:00
default:
panic("not reached")
}
2014-12-06 07:47:48 +01:00
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, glFilter)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, glFilter)
2013-06-19 16:51:41 +02:00
2014-12-06 07:47:48 +01:00
ptr := interface{}(nil)
2013-10-25 02:30:39 +02:00
if pixels != nil {
2014-12-06 07:47:48 +01:00
ptr = pixels
2013-10-25 02:30:39 +02:00
}
2014-12-06 07:47:48 +01:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, ptr)
2013-06-15 10:07:14 +02:00
2014-01-08 10:47:38 +01:00
return nativeTexture
2013-10-20 17:17:05 +02:00
}
2014-12-06 07:47:48 +01:00
func createTexture(width, height int, filter graphics.Filter) (*Texture, error) {
w := graphics.AdjustSizeForTexture(width)
h := graphics.AdjustSizeForTexture(height)
2014-12-06 14:56:57 +01:00
native := createNativeTexture(w, h, nil, filter)
2014-01-08 06:37:07 +01:00
return &Texture{native, width, height}, nil
2013-10-17 04:21:57 +02:00
}
2013-10-27 11:27:59 +01:00
2014-12-06 07:47:48 +01:00
func createTextureFromImage(img image.Image, filter graphics.Filter) (*Texture, error) {
adjustedImage := graphics.AdjustImageForTexture(img)
2013-12-18 10:05:28 +01:00
size := adjustedImage.Bounds().Size()
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
2014-01-08 06:37:07 +01:00
return &Texture{native, size.X, size.Y}, nil
2013-10-27 11:27:59 +01:00
}
2014-01-08 10:03:21 +01:00
2014-05-02 17:06:20 +02:00
func (t *Texture) dispose() {
2014-12-06 07:47:48 +01:00
t.native.Delete()
}