-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.go
More file actions
264 lines (200 loc) · 4.76 KB
/
texture.go
File metadata and controls
264 lines (200 loc) · 4.76 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"image"
"image/draw"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"os"
"errors"
"runtime"
"github.com/go-gl/gl/v3.3-core/gl"
)
var lastActiveTexture uint32 = 0
type TextureManager struct {
textures map[string]*Texture
}
func NewTextureManager() *TextureManager {
return &TextureManager{make(map[string]*Texture)}
}
func (tm *TextureManager) Get(name string) (tex *Texture, wasLoaded bool) {
tex, wasLoaded = tm.textures[name]
if !wasLoaded {
tex = NewTexture()
tm.textures[name] = tex
}
return
}
func (tm *TextureManager) MustGet(name string) *Texture {
tex, found := tm.textures[name]
if !found {
log.Panicf(`Texture "%s" was not found in memory`, name)
}
return tex
}
func (tm *TextureManager) Has(name string) bool {
_, found := tm.textures[name]
return found
}
func (tm *TextureManager) Load(name, path string) (*Texture, error) {
tex, wasLoaded := tm.Get(name)
if !wasLoaded {
log.Debugf(`Loading texture "%s" <- "%s"`, name, path)
err := tex.SetImgFile(path)
if err != nil {
tm.Unload(name)
return nil, err
}
}
return tex, nil
}
func (tm *TextureManager) MustLoad(name, path string) *Texture {
tex, err := tm.Load(name, path)
if err != nil {
log.Panicf(`Could not load texture "%s" ("%s")`, path, name)
}
return tex
}
func (tm *TextureManager) Reload(name, path string) (*Texture, error) {
log.Debugf(`Reloading texture "%s" <- "%s"`, name, path)
tex, _ := tm.Get(name)
err := tex.SetImgFile(path)
return tex, err
}
func (tm *TextureManager) Take(name string) *Texture {
tex, found := tm.textures[name]
if !found {
return nil
}
delete(tm.textures, name)
return tex
}
func (tm *TextureManager) MustTake(name string) *Texture {
tex := tm.Take(name)
if tex == nil {
log.Panicf(`Texture "%s" was not found in memory`, name)
}
return tex
}
func (tm *TextureManager) Unload(name string) {
log.Debugf(`Unloading texture "%s"`, name)
tex, found := tm.textures[name]
if found {
tex.Delete()
delete(tm.textures, name)
}
}
type Texture struct {
id uint32
lastSize image.Point
}
func NewTexture() *Texture {
gl.Enable(gl.TEXTURE_2D)
var texId uint32
gl.GenTextures(1, &texId)
texture := &Texture{texId, image.Pt(0, 0)}
runtime.SetFinalizer(texture, func(t *Texture){
t.Delete()
})
return texture
}
func NewTextureImg(img image.Image) (*Texture, error) {
tex := NewTexture()
err := tex.SetImg(img)
return tex, err
}
func NewTextureImgFile(path string) (*Texture, error) {
tex := NewTexture()
err := tex.SetImgFile(path)
return tex, err
}
func (t *Texture) ActiveTexture(i uint32) {
if i < 0 {
i = 0
} else if i > MaxTextureUnits() - 1 {
i = MaxTextureUnits() - 1
}
gl.ActiveTexture(gl.TEXTURE0 + i)
gl.BindTexture(gl.TEXTURE_2D, t.id)
}
func (t *Texture) Uniform(sp *ShaderProgram, name string) {
uniformLocation := sp.GetUniformLocation(name)
lastActiveTexture++;
if lastActiveTexture > MaxTextureUnits() - 1 {
lastActiveTexture = 0
}
t.ActiveTexture(lastActiveTexture)
gl.Uniform1i(uniformLocation, int32(lastActiveTexture))
}
func (t *Texture) Bind() {
if t.id != 0 {
gl.BindTexture(gl.TEXTURE_2D, t.id)
}
}
func (t *Texture) Delete() {
if t.id != 0 {
gl.DeleteTextures(1, &t.id)
t.id = 0
t.lastSize = image.Pt(0, 0)
}
}
func (t Texture) SetImg(img image.Image) error {
rgba := image.NewRGBA(img.Bounds())
if rgba.Stride != rgba.Rect.Size().X * 4 {
return errors.New("unsupported stride")
}
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
gl.BindTexture(gl.TEXTURE_2D, t.id)
if t.lastSize == rgba.Rect.Size() {
gl.TexSubImage2D(
gl.TEXTURE_2D,
0,
int32(0),
int32(0),
int32(rgba.Rect.Size().X),
int32(rgba.Rect.Size().Y),
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix),
)
} else {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(rgba.Rect.Size().X),
int32(rgba.Rect.Size().Y),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix),
)
t.lastSize = rgba.Rect.Size()
}
gl.GenerateMipmap(gl.TEXTURE_2D)
gl.BindTexture(gl.TEXTURE_2D, 0)
return nil
}
func (t Texture) SetImgFile(path string) error {
imgfile, err := os.Open(path)
if err != nil {
return err
}
img, _, err := image.Decode(imgfile)
if err != nil {
return err
}
return t.SetImg(img)
}
func MaxTextureUnits() uint32 {
var texUnits int32
gl.GetIntegerv(gl.MAX_TEXTURE_IMAGE_UNITS, &texUnits)
if texUnits < 16 {
log.Warning("GPU only supporting %v texture units (should be 16 min.)", texUnits)
}
return uint32(texUnits)
}