ebiten/graphics/opengl/texture/texture.go

88 lines
2.1 KiB
Go
Raw Normal View History

2013-10-27 11:27:59 +01:00
package texture
2013-06-15 10:07:14 +02:00
// #cgo LDFLAGS: -framework OpenGL
//
// #include <OpenGL/gl.h>
import "C"
import (
2013-10-27 11:27:59 +01:00
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
2013-06-19 03:31:44 +02:00
"image"
2013-06-15 10:07:14 +02:00
"unsafe"
)
2013-10-27 11:27:59 +01:00
type Native C.GLuint
2013-10-27 13:27:16 +01:00
type Filter int
const (
FilterLinear = iota
FilterNearest
)
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
filter Filter) Native {
2013-10-19 11:47:34 +02:00
nativeTexture := C.GLuint(0)
2013-10-20 17:17:05 +02:00
2013-10-19 11:47:34 +02:00
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
if nativeTexture < 0 {
2013-06-19 03:31:44 +02:00
panic("glGenTexture failed")
}
C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 4)
2013-10-19 11:47:34 +02:00
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(nativeTexture))
2013-10-26 19:25:41 +02:00
defer C.glBindTexture(C.GL_TEXTURE_2D, 0)
2013-10-27 13:27:16 +01:00
glFilter := C.GLint(0)
switch filter {
case FilterLinear:
glFilter = C.GL_LINEAR
case FilterNearest:
glFilter = C.GL_NEAREST
default:
panic("not reached")
}
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, glFilter)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, glFilter)
2013-06-19 16:51:41 +02:00
2013-10-25 02:30:39 +02:00
ptr := unsafe.Pointer(nil)
if pixels != nil {
ptr = unsafe.Pointer(&pixels[0])
}
2013-06-19 03:31:44 +02:00
C.glTexImage2D(C.GL_TEXTURE_2D, 0, C.GL_RGBA,
C.GLsizei(textureWidth), C.GLsizei(textureHeight),
2013-10-25 02:30:39 +02:00
0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr)
2013-06-15 10:07:14 +02:00
2013-10-27 11:27:59 +01:00
return Native(nativeTexture)
2013-10-20 17:17:05 +02:00
}
2013-10-27 14:58:56 +01:00
func create(textureWidth, textureHeight int, filter Filter) (
interface{}, error) {
2013-11-26 15:05:54 +01:00
return createNativeTexture(textureWidth, textureHeight, nil, filter), nil
2013-10-25 02:30:39 +02:00
}
2013-10-26 16:18:23 +02:00
func createFromImage(img *image.NRGBA) (interface{}, error) {
2013-10-25 02:30:39 +02:00
size := img.Bounds().Size()
2013-10-27 13:27:16 +01:00
return createNativeTexture(size.X, size.Y, img.Pix, FilterLinear), nil
2013-10-20 17:17:05 +02:00
}
// TODO: Rename them
2013-10-27 13:27:16 +01:00
func New(width, height int, filter Filter) (*gtexture.Texture, error) {
2013-11-26 13:07:37 +01:00
native, err := create(gtexture.AdjustSize(width), gtexture.AdjustSize(height), filter)
if err != nil {
return nil, err
2013-10-27 13:27:16 +01:00
}
2013-11-26 13:07:37 +01:00
return gtexture.New(native, width, height), nil
2013-10-17 04:21:57 +02:00
}
2013-10-27 11:27:59 +01:00
2013-10-27 14:12:28 +01:00
func NewEmpty(width, height int) (*gtexture.Texture, error) {
2013-11-26 13:07:37 +01:00
return gtexture.New(nil, width, height), nil
2013-10-27 14:12:28 +01:00
}
2013-10-27 11:27:59 +01:00
func NewFromImage(img image.Image) (*gtexture.Texture, error) {
2013-11-26 13:07:37 +01:00
native, err := createFromImage(gtexture.AdjustImage(img))
if err != nil {
return nil, err
}
size := img.Bounds().Size()
return gtexture.New(native, size.X, size.Y), nil
2013-10-27 11:27:59 +01:00
}