lazarus/platform/texture.go

75 lines
2.0 KiB
Go
Raw Normal View History

package platform
import (
"unsafe"
2019-01-01 01:30:59 +00:00
"github.com/FooSoft/lazarus/graphics"
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
glTexture uint32
size math.Vec2i
}
func NewTextureFromRgba(colors []math.Color4b, size math.Vec2i) (graphics.Texture, error) {
if !WindowIsCreated() {
return nil, ErrWindowNotExists
}
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{glTexture, size}, nil
}
func NewTextureFromRgb(colors []math.Color3b, size math.Vec2i) (graphics.Texture, error) {
if !WindowIsCreated() {
return nil, ErrWindowNotExists
}
2018-12-31 18:54:33 +00:00
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{glTexture, size}, nil
2018-12-31 18:54:33 +00:00
}
func (t *texture) Id() graphics.TextureId {
return graphics.TextureId(t.glTexture)
2018-12-31 00:34:53 +00:00
}
func (t *texture) Size() math.Vec2i {
2018-12-31 00:34:53 +00:00
return t.size
}
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
}