ebiten/internal/opengl/texture.go

115 lines
2.8 KiB
Go
Raw Normal View History

2014-12-09 15:16:04 +01:00
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
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"
2013-06-19 03:31:44 +02:00
"image"
2014-12-07 11:25:49 +01:00
"image/draw"
2013-06-15 10:07:14 +02:00
)
func NextPowerOf2(x uint64) uint64 {
x -= 1
x |= (x >> 1)
x |= (x >> 2)
x |= (x >> 4)
x |= (x >> 8)
x |= (x >> 16)
x |= (x >> 32)
return x + 1
}
func AdjustSizeForTexture(size int) int {
return int(NextPowerOf2(uint64(size)))
}
2014-12-07 11:25:49 +01:00
func adjustImageForTexture(img image.Image) *image.NRGBA {
width, height := img.Bounds().Size().X, img.Bounds().Size().Y
adjustedImageBounds := image.Rectangle{
image.ZP,
image.Point{
AdjustSizeForTexture(width),
AdjustSizeForTexture(height),
2014-12-07 11:25:49 +01:00
},
}
2014-12-07 20:22:50 +01:00
if nrgba, ok := img.(*image.NRGBA); ok && img.Bounds() == adjustedImageBounds {
2014-12-07 11:25:49 +01:00
return nrgba
}
adjustedImage := image.NewNRGBA(adjustedImageBounds)
dstBounds := image.Rectangle{
image.ZP,
img.Bounds().Size(),
}
draw.Draw(adjustedImage, dstBounds, img, image.ZP, draw.Src)
return adjustedImage
}
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
}
func (t *Texture) Native() gl.Texture {
return t.native
}
func (t *Texture) Width() int {
return t.width
}
func (t *Texture) Height() int {
return t.height
}
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter int) gl.Texture {
2014-12-06 07:47:48 +01:00
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
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter)
2013-06-19 16:51:41 +02:00
2014-12-08 14:51:40 +01:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
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
}
func NewTexture(width, height int, filter int) (*Texture, error) {
w := AdjustSizeForTexture(width)
h := AdjustSizeForTexture(height)
2014-12-07 20:22:50 +01:00
native := createNativeTexture(w, h, nil, filter)
return &Texture{native, width, height}, nil
2013-10-17 04:21:57 +02:00
}
2013-10-27 11:27:59 +01:00
func NewTextureFromImage(img image.Image, filter int) (*Texture, error) {
origSize := img.Bounds().Size()
2014-12-07 11:25:49 +01:00
adjustedImage := adjustImageForTexture(img)
2013-12-18 10:05:28 +01:00
size := adjustedImage.Bounds().Size()
2014-12-07 20:22:50 +01:00
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
return &Texture{native, origSize.X, origSize.Y}, nil
2013-10-27 11:27:59 +01:00
}
2014-01-08 10:03:21 +01:00
func (t *Texture) Dispose() {
2014-12-06 07:47:48 +01:00
t.native.Delete()
}