-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexture.go
148 lines (127 loc) · 3.42 KB
/
texture.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package glhf
import (
"runtime"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/mathgl/mgl32"
"github.com/gopxl/mainthread/v2"
)
// Texture is an OpenGL texture.
type Texture struct {
tex binder
width, height int
smooth bool
}
// 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 (one byte per component).
func NewTexture(width, height int, smooth bool, pixels []uint8) *Texture {
tex := &Texture{
tex: binder{
restoreLoc: gl.TEXTURE_BINDING_2D,
bindFunc: func(obj uint32) {
gl.BindTexture(gl.TEXTURE_2D, obj)
},
},
width: width,
height: height,
}
gl.GenTextures(1, &tex.tex.obj)
tex.Begin()
defer tex.End()
// initial data
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(width),
int32(height),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(pixels),
)
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)
tex.SetSmooth(smooth)
runtime.SetFinalizer(tex, (*Texture).delete)
return tex
}
func (t *Texture) delete() {
mainthread.CallNonBlock(func() {
gl.DeleteTextures(1, &t.tex.obj)
})
}
// ID returns the OpenGL ID of this Texture.
func (t *Texture) ID() uint32 {
return t.tex.obj
}
// Width returns the width of the Texture in pixels.
func (t *Texture) Width() int {
return t.width
}
// Height returns the height of the Texture in pixels.
func (t *Texture) Height() int {
return t.height
}
// SetPixels sets the content of a sub-region of the Texture. Pixels must be an RGBA byte sequence.
func (t *Texture) SetPixels(x, y, w, h int, pixels []uint8) {
if len(pixels) != w*h*4 {
panic("set pixels: wrong number of pixels")
}
gl.TexSubImage2D(
gl.TEXTURE_2D,
0,
int32(x),
int32(y),
int32(w),
int32(h),
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(pixels),
)
}
// Pixels returns the content of a sub-region of the Texture as an RGBA byte sequence.
func (t *Texture) Pixels(x, y, w, h int) []uint8 {
pixels := make([]uint8, t.width*t.height*4)
gl.GetTexImage(
gl.TEXTURE_2D,
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(pixels),
)
subPixels := make([]uint8, w*h*4)
for i := 0; i < h; i++ {
row := pixels[(i+y)*t.width*4+x*4 : (i+y)*t.width*4+(x+w)*4]
subRow := subPixels[i*w*4 : (i+1)*w*4]
copy(subRow, row)
}
return subPixels
}
// SetSmooth sets whether the Texture should be drawn "smoothly" or "pixely".
//
// It affects how the Texture is drawn when zoomed. Smooth interpolates between the neighbour
// pixels, while pixely always chooses the nearest pixel.
func (t *Texture) SetSmooth(smooth bool) {
t.smooth = smooth
if smooth {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
} else {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
}
}
// Smooth returns whether the Texture is set to be drawn "smooth" or "pixely".
func (t *Texture) Smooth() bool {
return t.smooth
}
// Begin binds the Texture. This is necessary before using the Texture.
func (t *Texture) Begin() {
t.tex.bind()
}
// End unbinds the Texture and restores the previous one.
func (t *Texture) End() {
t.tex.restore()
}