-
Notifications
You must be signed in to change notification settings - Fork 0
/
texturebuilder.go
341 lines (269 loc) · 6.63 KB
/
texturebuilder.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package glui
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
type TextureBuilder struct {
nComp int
width int
height int
free []Rect
data []byte
dirty bool
}
func NewTextureBuilder(nComp int, initWidth int, initHeight int) *TextureBuilder {
return &TextureBuilder{
nComp,
initWidth,
initHeight,
[]Rect{Rect{0, 0, initWidth, initHeight}},
make([]byte, nComp*initWidth*initHeight),
true,
}
}
func (tb *TextureBuilder) HaveFreeSpace(w, h int) bool {
b := false
for _, f := range tb.free {
if f.W >= w && f.H >= h {
b = true
break
}
}
return b
}
func (tb *TextureBuilder) Build(data []byte, w int, h int) (int, int) {
// look for a free that is able to accomodate
horFailCount := 0
verFailCount := 0
// free rect with smallest waste wins
bestFree := -1
bestWaste := w*h
for i, f := range tb.free {
horFail := f.W < w
verFail := f.H < h
if horFail {
horFailCount++
}
if verFail {
verFailCount++
}
if !horFail && !verFail {
waste := (f.W - w)*h + f.W*(f.H - h)
if bestFree == -1 || waste < bestWaste {
bestFree = i
bestWaste = waste
}
}
}
if bestFree != -1 {
i := bestFree
f := tb.free[i]
tb.setData(f.X, f.Y, data, w, h)
if f.W == w && f.H == h {
if len(tb.free) == 1 {
tb.free = []Rect{}
} else {
tb.free = append(tb.free[0:i], tb.free[i+1:]...)
}
} else if (f.W - w) < (f.H - h) {
tb.free[i] = Rect{f.X, f.Y + h, w, f.H - h}
tb.free = append(tb.free, Rect{f.X + w, f.Y, f.W - w, f.H})
} else {
tb.free[i] = Rect{f.X + w, f.Y, f.W - w, h}
tb.free = append(tb.free, Rect{f.X, f.Y + h, f.W, f.H - h})
}
tb.filterEmptyFree()
return f.X, f.Y
} else {
if horFailCount > verFailCount {
tb.growRight()
} else {
tb.growDown()
}
tb.dirty = true
return tb.Build(data, w, h)
}
}
func (tb *TextureBuilder) filterEmptyFree() {
free := make([]Rect, 0)
for _, fr := range tb.free {
if fr.W > 0 && fr.H > 0 {
free = append(free, fr)
}
}
tb.free = free
}
func (tb *TextureBuilder) BuildBordered(data []byte, t int) (int, int) {
s := 2*t+1
return tb.Build(data, s, s)
}
func (tb *TextureBuilder) Free(x, y, w, h int) {
if w > 0 && h > 0 {
tb.free = append(tb.free, Rect{x, y, w, h})
}
tb.Defrag()
}
func (tb *TextureBuilder) dumpFree(fname string) {
f, err := os.Create(fname)
if err != nil {
panic("unable to debug defrag")
}
for _, fr := range tb.free {
fmt.Fprintf(f, "%d %d\n", fr.X, fr.Y)
fmt.Fprintf(f, "%d %d\n", fr.Right(), fr.Y)
fmt.Fprintf(f, "%d %d\n", fr.Right(), fr.Bottom())
fmt.Fprintf(f, "%d %d\n", fr.X, fr.Bottom())
fmt.Fprintf(f, "%d %d\n\n", fr.X, fr.Y)
}
}
func (tb *TextureBuilder) Defrag() {
tb.free = defragFreeRects(tb.free)
}
func aIsBetterThanB(a []Rect, b []Rect) bool {
a = sortRects(a[:])
b = sortRects(b[:])
for i := 0; i < len(a); i++ {
if i >= len(b) {
return false
}
rA := a[i]
rB := b[i]
rAWH := rA.W*rA.H
rBWH := rB.W*rB.H
if rAWH > rBWH {
return true
} else if rBWH > rAWH {
return false
}
}
return true
}
func defragFreeRects(free []Rect) []Rect {
// no further improvements possible
if len(free) < 1 {
return free
}
removeTwo := func(lst []Rect, i, j int) []Rect {
if i > j {
i, j = j, i
}
newLst := make([]Rect, len(lst) - 2)
copy(newLst[0:i], lst[0:i])
copy(newLst[i:j-1], lst[i+1:j])
if j < len(lst) - 1 {
copy(newLst[j-1:], lst[j+1:])
}
return newLst
}
// first simple search for common edges
for i, r := range free {
for j, r_ := range free {
if i == j {
continue
}
rNew, ok := r.MergeAlongEdge(r_)
if ok {
if r.Area() + r_.Area() != rNew.Area() {
fmt.Println(rNew, r_, r)
panic("area not preserved")
}
free = append(removeTwo(free, i, j), rNew)
// this is a guaranteed improvement, and we return now
return defragFreeRects(free)
}
}
}
// now search for partially overlapping edges
improved := false
freeTries := make([][]Rect, 0) // different possibilities
for i, r := range free {
for j, r_ := range free {
if i == j {
continue
}
rNew, rRem, ok := r.MergeAlongPartialEdge(r_)
if ok {
freeTries = append(freeTries, append(removeTwo(free, i, j), rNew, rRem))
}
}
}
for _, try := range freeTries {
if aIsBetterThanB(try, free) {
free = try
improved = true
}
}
if improved {
return defragFreeRects(free)
} else {
return free
}
}
func (tb *TextureBuilder) setData(x int, y int, data []byte, w int, h int) {
// newer method with larger contiguous copies
for i := x; i < x+w; i++ {
dst0 := (i*tb.height + y)*tb.nComp
dst1 := (i*tb.height + y+h)*tb.nComp
src0 := ((i-x)*h + (y - y))*tb.nComp
src1 := ((i-x)*h + (y + h - y))*tb.nComp
copy(tb.data[dst0 : dst1 : dst1], data[src0 : src1 : src1])
}
//old method for reference
// for i := x; i < x+w; i++ {
// for j := y; j < y+h; j++ {
// for c := 0; c < tb.nComp; c++ {
// dst := (i*tb.height + j)*tb.nComp + c
// src := ((i-x)*h + (j - y))*tb.nComp + c
// tb.data[dst] = data[src]
// }
// }
// }
tb.dirty = true
}
func (tb *TextureBuilder) growRight() {
oldWidth := tb.width
tb.width = oldWidth*2
oldData := tb.data
tb.data = make([]byte, tb.nComp*tb.width*tb.height)
tb.setData(0, 0, oldData, oldWidth, tb.height)
tb.free = append(tb.free, Rect{oldWidth, 0, oldWidth, tb.height})
tb.dirty = true
tb.Defrag()
}
func (tb *TextureBuilder) growDown() {
oldHeight := tb.height
tb.height = oldHeight*2
oldData := tb.data
tb.data = make([]byte, tb.nComp*tb.width*tb.height)
tb.setData(0, 0, oldData, tb.width, oldHeight)
tb.free = append(tb.free, Rect{0, oldHeight, tb.width, oldHeight})
tb.dirty = true
tb.Defrag()
}
func (tb *TextureBuilder) ToImage(fname string) error {
return DataToImage(tb.data, tb.width, tb.height, fname)
}
func DataToImage(data []byte, width, height int, fname string) error {
f, err := os.Create(fname)
if err != nil {
return err
}
r := image.Rect(0, 0, width, height)
img := image.NewRGBA(r)
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
src := i*height + j
c := color.RGBA{
data[src*4+0],
data[src*4+1],
data[src*4+2],
data[src*4+3],
}
img.Set(i, j, c)
}
}
return png.Encode(f, img)
}