-
Notifications
You must be signed in to change notification settings - Fork 1
/
atlas.cpp
272 lines (233 loc) · 6.26 KB
/
atlas.cpp
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
/* Copyright 2011 Pyarelal Knowles, under GNU LGPL (see LICENCE.txt) */
#include "prec.h"
#include "atlas.h"
#include "util.h"
#include "includegl.h"
#include "shader.h"
#include "shaderutil.h"
#include "imgpng.h"
#include "vbomesh.h"
#include "resources.h"
using namespace std;
EMBED(atlasBlitVert, atlasBlit.vert);
EMBED(atlasBlitFrag, atlasBlit.frag);
static Shader shader("atlasBlit.vert", "atlasBlit.frag");
bool TextureAtlas::Tex::setUniform(Shader* program, const std::string& name) const
{
bool ok = true;
ok = ok && program->set(name + ".pos", npos);
ok = ok && program->set(name + ".size", nsize);
return ok;
}
TextureAtlas::TextureAtlas()
{
dirty = false;
size = vec2i(0);
}
TextureAtlas::~TextureAtlas()
{
atlas.release();
}
bool TextureAtlas::isDirty()
{
return dirty;
}
void TextureAtlas::recursivePack(vec2i packOffset, vec2i packSize)
{
//get the next biggest source that fits
Source* s;
while (true)
{
//return if we've run out of items to pack
if (cur >= (int)sources.size())
return;
s = &sources[sources.size() - cur - 1];
++cur;
//check that it fits and has not already been packed
if (!s->packed && s->size.x <= packSize.x && s->size.y <= packSize.y)
break;
}
//place the texture
s->packed = true;
s->pos = packOffset;
//we're left with an L shape. split to give the largest area
//possible, then recurse into the biggest first
int a1 = (packSize.x - s->size.x) * packSize.y;
int a2 = (packSize.y - s->size.y) * packSize.x;
int curBack = cur;
if (a1 < a2)
{
recursivePack(packOffset + vec2i(s->size.x, 0), vec2i(packSize.x - s->size.x, packSize.y));
cur = curBack;
recursivePack(packOffset + vec2i(0, s->size.y), vec2i(s->size.x, packSize.y - s->size.y));
}
else
{
recursivePack(packOffset + vec2i(0, s->size.y), vec2i(packSize.x, packSize.y - s->size.y));
cur = curBack;
recursivePack(packOffset + vec2i(s->size.x, 0), vec2i(packSize.x - s->size.x, s->size.y));
}
}
bool TextureAtlas::createPack(vec2i testSize)
{
sort(sources.begin(), sources.end());
cur = 0;
//clear packed flags
for (int i = 0; i < (int)sources.size(); ++i)
sources[i].packed = false;
recursivePack(vec2i(0), testSize);
//see if everything was packed
bool done = true;
for (int i = 0; i < (int)sources.size(); ++i)
done = done && sources[i].packed;
return done;
}
void TextureAtlas::upload()
{
FrameBuffer fbo;
fbo.colour[0] = &atlas;
fbo.resize(size);
fbo.attach();
fbo.bind();
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_BLEND);
vec3f quadData[] = {
vec3f(0, 0, 0),
vec3f(0, 1, 0),
vec3f(1, 0, 0),
vec3f(1, 1, 0)
};
VertexBuffer quad;
quad.buffer(quadData, sizeof(quadData));
//instead of loading the shader from disk, use the embedded text included with BINDATA()
static bool setSource = false;
if (!setSource)
{
Shader::include("atlasBlit.vert", RESOURCE(atlasBlitVert));
Shader::include("atlasBlit.frag", RESOURCE(atlasBlitFrag));
setSource = true;
}
shader.use();
shader.attrib("osVert", quad, GL_FLOAT, sizeof(vec3f));
texMap.clear();
//render all the sources
vec2f fsize(size);
for (int i = 0; i < (int)sources.size(); ++i)
{
texMap[sources[i].name] = (int)textures.size();
Tex t;
t.size = sources[i].size;
t.npos = vec2f(sources[i].pos)/fsize;
t.nsize = vec2f(t.size)/fsize;
textures.push_back(t);
shader.set("pos", t.npos);
shader.set("size", t.nsize);
glBindTexture(GL_TEXTURE_2D, sources[i].tex);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
shader.unuse();
fbo.unbind();
fbo.colour[0] = NULL; //don't want fbo release()ing atlas
fbo.release();
quad.release();
dirty = false;
#if 0
QI::ImagePNG test;
test.readTexture(atlas);
test.saveImage("atlas.png");
printf("saved atlas.png for debugging\n");
#endif
}
void TextureAtlas::cleanup(bool deleteSource)
{
if (deleteSource)
{
for (int i = 0; i < (int)sources.size(); ++i)
{
glDeleteTextures(1, &sources[i].tex);
}
}
sources.clear();
}
void TextureAtlas::add(std::string name, GLuint tex)
{
assert(tex != 0);
dirty = true;
Source source;
source.tex = tex;
source.name = name;
//FIXME: may want to pass in texture size instead
//for now, packing a texture atlas is considered pre-processing and I don't care about speed
source.size.x = 0;
source.size.y = 0;
glBindTexture(GL_TEXTURE_2D, tex);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &source.size.x);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &source.size.y);
glBindTexture(GL_TEXTURE_2D, 0);
sources.push_back(source);
}
bool TextureAtlas::has(std::string name)
{
return texMap.find(name) != texMap.end();
}
TextureAtlas::Tex TextureAtlas::get(std::string name)
{
return textures[texMap[name]];
}
bool TextureAtlas::pack()
{
//get the initial test size for packing
int totalPixels = 0;
for (int i = 0; i < (int)sources.size(); ++i)
{
if (sources[i].size.x <= 0 || sources[i].size.y <= 0)
printf("WARNING: non-positive texture size in TextureAtlas::pack()\n");
totalPixels += sources[i].size.x * sources[i].size.y;
}
size = vec2i(32);
//brute force attempt to pack with increasing powers of two textures
/*
bool success = false;
int perfectWidth = 1 << (ceilLog2(totalPixels) / 2);
for (int w = perfectWidth; w < TEXTURE_ATLAS_MAX_SIZE; w *= 2)
{
for (int h = 1; h >= 0; --h)
{
size = vec2i(w>>h, w);
success = createPack(size);
if (success)
break;
}
if (success)
break;
}
*/
//binary search for best size
int canary = 0;
bool success = false;
int perfectWidth = 1 << (ceilLog2(totalPixels) / 2);
int w = perfectWidth;
int s;
for (s = w; s >= 4; s /= 2) //size must be a multiple of 4
{
size = vec2i(w);
success = createPack(size);
if (success)
w -= s;
else
w += s;
++canary;
}
if (!success) //make sure search ends on a success
{
w += s;
size = vec2i(w);
success = createPack(size);
++canary;
}
//even if unsuccessful, still upload and just let the atlas be invalid, returning false
//no need to make the user's day any worse
upload();
return success;
}