2016-11-23 10:29:28 -06:00
|
|
|
package pixelgl
|
|
|
|
|
2016-12-30 18:33:53 -06:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/go-gl/gl/v3.3-core/gl"
|
2017-01-16 18:24:15 -06:00
|
|
|
"github.com/go-gl/mathgl/mgl32"
|
2016-12-30 18:33:53 -06:00
|
|
|
)
|
2016-11-23 10:29:28 -06:00
|
|
|
|
|
|
|
// Texture is an OpenGL texture.
|
|
|
|
type Texture struct {
|
2016-12-14 09:24:31 -06:00
|
|
|
tex binder
|
2016-12-06 09:23:55 -06:00
|
|
|
width, height int
|
2016-11-23 10:29:28 -06:00
|
|
|
}
|
|
|
|
|
2017-01-15 17:53:02 -06:00
|
|
|
// NewTexture creates a new texture with the specified width and height with some initial
|
|
|
|
// pixel values. The pixels must be a sequence of RGBA values.
|
|
|
|
func NewTexture(width, height int, smooth bool, pixels []uint8) *Texture {
|
|
|
|
if len(pixels) != width*height*4 {
|
|
|
|
panic("failed to create new texture: wrong number of pixels")
|
|
|
|
}
|
|
|
|
|
|
|
|
tex := &Texture{
|
2016-12-14 09:24:31 -06:00
|
|
|
tex: binder{
|
|
|
|
restoreLoc: gl.TEXTURE_BINDING_2D,
|
|
|
|
bindFunc: func(obj uint32) {
|
|
|
|
gl.BindTexture(gl.TEXTURE_2D, obj)
|
|
|
|
},
|
|
|
|
},
|
2016-12-06 09:23:55 -06:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
2016-11-24 07:37:11 -06:00
|
|
|
|
2017-01-15 17:53:02 -06:00
|
|
|
gl.GenTextures(1, &tex.tex.obj)
|
2016-12-30 18:33:53 -06:00
|
|
|
|
2017-01-15 17:53:02 -06:00
|
|
|
tex.Begin()
|
|
|
|
defer tex.End()
|
2016-12-30 18:33:53 -06:00
|
|
|
|
|
|
|
gl.TexImage2D(
|
|
|
|
gl.TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
gl.RGBA,
|
|
|
|
int32(width),
|
|
|
|
int32(height),
|
|
|
|
0,
|
|
|
|
gl.RGBA,
|
|
|
|
gl.UNSIGNED_BYTE,
|
|
|
|
gl.Ptr(pixels),
|
|
|
|
)
|
|
|
|
|
2017-01-16 18:24:15 -06:00
|
|
|
borderColor := mgl32.Vec4{0, 0, 0, 0}
|
|
|
|
gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
|
2016-12-30 18:33:53 -06:00
|
|
|
|
|
|
|
if smooth {
|
2017-01-16 18:24:15 -06:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
|
2016-12-30 18:33:53 -06:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
|
|
|
} else {
|
2017-01-16 18:24:15 -06:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
2016-12-30 18:33:53 -06:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
|
|
|
}
|
|
|
|
|
2017-01-15 17:53:02 -06:00
|
|
|
runtime.SetFinalizer(tex, (*Texture).delete)
|
2016-12-18 13:16:27 -06:00
|
|
|
|
2017-01-15 17:53:02 -06:00
|
|
|
return tex
|
2016-11-23 10:29:28 -06:00
|
|
|
}
|
|
|
|
|
2016-12-18 13:16:27 -06:00
|
|
|
func (t *Texture) delete() {
|
2016-12-18 18:11:34 -06:00
|
|
|
DoNoBlock(func() {
|
|
|
|
gl.DeleteTextures(1, &t.tex.obj)
|
2016-11-23 13:06:34 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:23:55 -06:00
|
|
|
// Width returns the width of a texture in pixels.
|
|
|
|
func (t *Texture) Width() int {
|
|
|
|
return t.width
|
|
|
|
}
|
|
|
|
|
|
|
|
// Height returns the height of a texture in pixels.
|
|
|
|
func (t *Texture) Height() int {
|
|
|
|
return t.height
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:33:53 -06:00
|
|
|
// Begin binds a texture. This is necessary before using the texture.
|
|
|
|
func (t *Texture) Begin() {
|
|
|
|
t.tex.bind()
|
|
|
|
}
|
|
|
|
|
|
|
|
// End unbinds a texture and restores the previous one.
|
|
|
|
func (t *Texture) End() {
|
|
|
|
t.tex.restore()
|
2016-11-23 10:29:28 -06:00
|
|
|
}
|