2018-12-29 19:52:30 +00:00
|
|
|
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"
|
2018-12-29 19:52:30 +00:00
|
|
|
)
|
|
|
|
|
2019-01-01 01:42:00 +00:00
|
|
|
type texture struct {
|
2018-12-31 17:59:58 +00:00
|
|
|
size math.Vec2i
|
|
|
|
glTexture uint32
|
2018-12-29 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 01:42:00 +00:00
|
|
|
func newTextureFromRgba(colors []math.Color4b, size math.Vec2i) (graphics.Texture, error) {
|
2018-12-31 17:59:58 +00:00
|
|
|
var glLastTexture int32
|
|
|
|
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &glLastTexture)
|
2018-12-29 19:52:30 +00:00
|
|
|
|
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-29 19:52:30 +00:00
|
|
|
|
2018-12-31 17:59:58 +00:00
|
|
|
gl.BindTexture(gl.TEXTURE_2D, uint32(glLastTexture))
|
2019-01-01 01:42:00 +00:00
|
|
|
return &texture{size, glTexture}, nil
|
2018-12-29 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 01:42:00 +00:00
|
|
|
func newTextureFromRgb(colors []math.Color3b, size math.Vec2i) (graphics.Texture, error) {
|
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))
|
2019-01-01 01:42:00 +00:00
|
|
|
return &texture{size, glTexture}, nil
|
2018-12-31 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 01:42:00 +00:00
|
|
|
func (t *texture) Id() graphics.TextureId {
|
|
|
|
return graphics.TextureId(t.glTexture)
|
2018-12-31 00:34:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 01:42:00 +00:00
|
|
|
func (t *texture) Size() math.Vec2i {
|
2018-12-31 00:34:53 +00:00
|
|
|
return t.size
|
|
|
|
}
|
2018-12-29 19:52:30 +00:00
|
|
|
|
2019-01-01 01:42:00 +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-29 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 17:59:58 +00:00
|
|
|
gl.DeleteTextures(1, &t.glTexture)
|
|
|
|
t.glTexture = 0
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
return nil
|
|
|
|
}
|