lazarus/platform/texture.go

66 lines
1.8 KiB
Go
Raw Normal View History

package platform
import (
"unsafe"
2018-12-31 00:34:53 +00:00
"github.com/FooSoft/lazarus/math"
"github.com/go-gl/gl/v2.1/gl"
)
type Texture struct {
2018-12-31 17:59:58 +00:00
size math.Vec2i
glTexture uint32
}
2018-12-31 18:54:33 +00:00
func newTextureFromRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error) {
2018-12-31 17:59:58 +00:00
var glLastTexture int32
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture)
2018-12-31 17:59:58 +00:00
var glTexture uint32
gl.GenTextures(1, &glTexture)
gl.BindTexture(gl.TEXTURE_2D, glTexture)
2018-12-31 00:34:53 +00:00
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0)
2018-12-31 17:59:58 +00:00
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(size.X), int32(size.Y), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&colors[0]))
2018-12-31 17:59:58 +00:00
gl.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture))
return &Texture{size, glTexture}, nil
}
2018-12-31 18:54:33 +00:00
func newTextureFromRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error) {
var glLastTexture int32
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture)
var glTexture uint32
gl.GenTextures(1, &glTexture)
gl.BindTexture(gl.TEXTURE_2D, glTexture)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0)
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 1)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGB, int32(size.X), int32(size.Y), 0, gl.RGB, gl.UNSIGNED_BYTE, unsafe.Pointer(&colors[0]))
gl.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture))
return &Texture{size, glTexture}, nil
}
2018-12-31 00:34:53 +00:00
func (t *Texture) Handle() Handle {
2018-12-31 17:59:58 +00:00
return Handle(t.glTexture)
2018-12-31 00:34:53 +00:00
}
func (t *Texture) Size() math.Vec2i {
return t.size
}
2018-12-31 00:34:53 +00:00
func (t *Texture) Destroy() error {
2018-12-31 17:59:58 +00:00
if t == nil || t.glTexture == 0 {
return nil
}
2018-12-31 17:59:58 +00:00
gl.DeleteTextures(1, &t.glTexture)
t.glTexture = 0
return nil
}