ebiten/graphics/opengl/texture/texture.go

104 lines
2.9 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-12-18 10:05:28 +01:00
"github.com/hajimehoshi/go-ebiten/graphics"
2014-01-08 10:03:21 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
2014-01-08 10:47:38 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
2014-01-08 10:03:21 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
2013-06-19 03:31:44 +02:00
"image"
2013-06-15 10:07:14 +02:00
"unsafe"
)
2014-01-08 06:37:07 +01:00
type Texture struct {
2014-01-08 10:47:38 +01:00
native C.GLuint
2014-01-08 10:03:21 +01:00
width int
height int
2014-01-08 06:37:07 +01:00
}
2014-01-10 13:28:50 +01:00
func glMatrix(matrix [4][4]float64) [16]float32 {
result := [16]float32{}
for j := 0; j < 4; j++ {
for i := 0; i < 4; i++ {
result[i+j*4] = float32(matrix[i][j])
}
}
return result
}
2013-10-27 13:27:16 +01:00
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
2014-01-08 10:47:38 +01:00
filter graphics.Filter) C.GLuint {
2013-10-19 11:47:34 +02:00
nativeTexture := C.GLuint(0)
2013-10-20 17:17:05 +02:00
C.glGenTextures(1, &nativeTexture)
2013-10-19 11:47:34 +02:00
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 {
2013-12-18 10:05:28 +01:00
case graphics.FilterLinear:
2013-10-27 13:27:16 +01:00
glFilter = C.GL_LINEAR
2013-12-18 10:05:28 +01:00
case graphics.FilterNearest:
2013-10-27 13:27:16 +01:00
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
2014-01-08 10:47:38 +01:00
return nativeTexture
2013-10-20 17:17:05 +02:00
}
2014-01-08 06:37:07 +01:00
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
2014-01-07 17:04:14 +01:00
native := createNativeTexture(
graphics.AdjustSizeForTexture(width),
graphics.AdjustSizeForTexture(height), 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-01-08 06:37:07 +01:00
func CreateFromImage(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-01-08 10:47:38 +01:00
func (t *Texture) CreateRenderTarget() *rendertarget.RenderTarget {
return rendertarget.CreateFromTexture(
rendertarget.NativeTexture(t.native), t.width, t.height)
2014-01-08 10:03:21 +01:00
}
2014-01-10 13:28:50 +01:00
func (t *Texture) Draw(projectionMatrix [4][4]float64, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
2014-01-08 10:03:21 +01:00
quad := graphics.TextureQuadForTexture(t.width, t.height)
2014-01-08 10:47:38 +01:00
shader.DrawTexture(shader.NativeTexture(t.native),
2014-01-10 13:28:50 +01:00
glMatrix(projectionMatrix), []graphics.TextureQuad{quad},
2014-01-08 10:03:21 +01:00
geometryMatrix, colorMatrix)
}
2014-01-10 13:28:50 +01:00
func (t *Texture) DrawParts(parts []graphics.TexturePart, projectionMatrix [4][4]float64,
2014-01-08 10:03:21 +01:00
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
quads := graphics.TextureQuadsForTextureParts(parts, t.width, t.height)
2014-01-08 10:47:38 +01:00
shader.DrawTexture(shader.NativeTexture(t.native),
2014-01-10 13:28:50 +01:00
glMatrix(projectionMatrix), quads,
2014-01-08 10:03:21 +01:00
geometryMatrix, colorMatrix)
}
func (t *Texture) Dispose() {
C.glDeleteTextures(1, &t.native)
}