-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexgen.cc
More file actions
353 lines (309 loc) · 9.07 KB
/
texgen.cc
File metadata and controls
353 lines (309 loc) · 9.07 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
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
342
343
344
345
346
347
348
349
350
351
352
#include "texgen.h"
#include <cmath>
bool texture_generator::generated_ = false;
GLuint texture_generator::texture_id_ = 0;
void
texture_generator::make_texture()
{
std::unique_ptr<uint8_t[]> texture_data = make_rgba(512, 512);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture_id_);
glBindTexture(GL_TEXTURE_2D, texture_id_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D(GL_TEXTURE_2D, 0,
GL_RGBA,
512, 512, 0,
GL_RGBA, GL_UNSIGNED_BYTE,
texture_data.get());
}
void texture_generator::bind_texture()
{
if (generated_) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_id_);
} else {
make_texture();
generated_ = true;
}
}
std::map<int, std::function<void(cairo_t *)>> get_texture_generator_map()
{
std::map<int, std::function<void(cairo_t *)>> map = {
{texid_tile_background, draw_tile_background},
{texid_left, draw_left_arrow},
{texid_right, draw_right_arrow},
{texid_fwd1, draw_straight_arrow},
{texid_fwd2, draw_straight_arrow2},
{texid_fwd3, draw_straight_arrow3},
{texid_conditional, draw_conditional},
{texid_rep0, [](cairo_t * c){ draw_repeat(c, 0); }},
{texid_rep1, [](cairo_t * c){ draw_repeat(c, 1); }},
{texid_rep2, [](cairo_t * c){ draw_repeat(c, 2); }},
{texid_rep3, [](cairo_t * c){ draw_repeat(c, 3); }},
{texid_rep4, [](cairo_t * c){ draw_repeat(c, 4); }},
{texid_button_lowered_bg, draw_button_background},
{texid_button_raised_bg, draw_pressed_button_background},
{texid_button_halt, draw_abort_button},
{texid_button_pause, draw_pause_button},
{texid_button_run1x, draw_run_button},
{texid_button_run2x, draw_run2_button},
{texid_button_run3x, draw_run3_button},
{texid_solid, draw_solid},
{texid_cross, draw_cross},
{texid_trigger1, draw_trigger1},
{texid_trigger2, draw_trigger2},
{texid_trigger3, draw_trigger3},
{texid_back_icon, draw_back_icon},
{texid_exit_icon, draw_exit_icon}
};
return map;
}
static constexpr int blur_kern_size = 8;
static constexpr double blur_kern_scale = .05;
void
blur_horizontal(std::size_t w, std::size_t h, uint8_t * data, std::size_t stride)
{
std::unique_ptr<uint8_t[]> row(new uint8_t[w * 4]);
for (std::size_t y = 0; y < h; ++y) {
for (std::size_t x = 0; x < w; ++x) {
double acc[4] = {0. , 0., 0., 0.};
for (int xx = - blur_kern_size; xx <= +blur_kern_size; ++xx) {
double f = (std::erf((xx + .5) * blur_kern_scale) - std::erf((xx - .5) * blur_kern_scale)) * .5;
std::size_t xe = (x + xx) & (w - 1);
for (std::size_t c = 0; c < 4; ++c) {
acc[c] += f * data [(xe + y * stride) * 4 + c];
}
}
for (std::size_t c = 0; c < 4; ++c) {
row[x * 4 + c] = std::min(255., acc[c]);
}
}
for (std::size_t x = 0; x < w; ++x) {
data[(x + y * stride) * 4 + 0] = row[x * 4 + 0];
data[(x + y * stride) * 4 + 1] = row[x * 4 + 1];
data[(x + y * stride) * 4 + 2] = row[x * 4 + 2];
data[(x + y * stride) * 4 + 3] = row[x * 4 + 3];
}
}
}
void
blur_vertical(std::size_t w, std::size_t h, uint8_t * data, std::size_t stride)
{
std::unique_ptr<uint8_t[]> row(new uint8_t[w * 4]);
for (std::size_t x = 0; x < w; ++x) {
for (std::size_t y = 0; y < h; ++y) {
double acc[4] = {0. , 0., 0., 0.};
for (int yy = - blur_kern_size; yy <= +blur_kern_size; ++yy) {
double f = (std::erf((yy + .5) * blur_kern_scale) - std::erf((yy - .5) * blur_kern_scale)) * .5;
std::size_t ye = (y + yy) & (h - 1);
for (std::size_t c = 0; c < 4; ++c) {
acc[c] += f * data [(x + ye * stride) * 4 + c];
}
}
for (std::size_t c = 0; c < 4; ++c) {
row[y * 4 + c] = std::min(255., 8 * acc[c]);
}
}
for (std::size_t y = 0; y < w; ++y) {
data[(x + y * stride) * 4 + 0] = row[y * 4 + 0];
data[(x + y * stride) * 4 + 1] = row[y * 4 + 1];
data[(x + y * stride) * 4 + 2] = row[y * 4 + 2];
data[(x + y * stride) * 4 + 3] = row[y * 4 + 3];
}
}
}
void
make_border(std::size_t w, std::size_t h, uint8_t * data, std::size_t stride)
{
for (std::size_t x = 1; x < w - 1; ++x) {
for (std::size_t c = 0; c < 4; ++c) {
data[(x + 0 * stride) * 4 + c] = data[(x + 1 * stride) * 4 + c];
}
for (std::size_t c = 0; c < 4; ++c) {
data[(x + (h - 1) * stride) * 4 + c] = data[(x + (h - 2) * stride) * 4 + c];
}
}
for (std::size_t y = 0; y < h; ++y) {
for (std::size_t c = 0; c < 4; ++c) {
data[(0 + y * stride) * 4 + c] = data[(1 + y * stride) * 4 + c];
}
for (std::size_t c = 0; c < 4; ++c) {
data[((w - 1) + y * stride) * 4 + c] = data[((w-2) + y * stride) * 4 + c];
}
}
}
std::unique_ptr<uint8_t[]>
texture_generator::make_rgba(std::size_t w, std::size_t h)
{
std::unique_ptr<uint8_t[]> data(new uint8_t[w * h * 4]);
std::size_t tile_w = w / 8;
std::size_t tile_h = h / 8;
std::map<int, std::function<void(cairo_t *)>> gen_map = get_texture_generator_map();
for (const auto& gen : gen_map) {
std::size_t index = gen.first;
const std::function<void(cairo_t *)>& fn = gen.second;
std::size_t x = (index % 8) * tile_w;
std::size_t y = (index / 8) * tile_h;
std::size_t xb = ((index + texid_blur_offset) % 8) * tile_w;
std::size_t yb = ((index + texid_blur_offset) / 8) * tile_h;
cairo_draw_gl_rgba(tile_w - 2, tile_h - 2, &data[((x + 1) + (y + 1) * w) * 4], w, fn);
make_border(tile_w, tile_h, &data[(x + y * w) * 4], w);
cairo_draw_gl_rgba(tile_w - 2, tile_h - 2, &data[((xb + 1) + (yb + 1) * w) * 4], w, fn);
blur_horizontal(tile_w - 2, tile_h - 2, &data[((xb + 1) + (yb + 1) * w) * 4], w);
blur_vertical(tile_w - 2, tile_h - 2, &data[((xb + 1) + (yb + 1) * w) * 4], w);
make_border(tile_w, tile_h, &data[(xb + yb * w) * 4], w);
}
return data;
}
void
texture_generator::make_scratch_text(const char * s)
{
std::size_t tile_w = 512 / 8;
std::size_t tile_h = 512 / 8;
std::unique_ptr<uint8_t[]> data(new uint8_t[tile_w * tile_h * 4]);
cairo_draw_gl_rgba(tile_w - 2, tile_h - 2, &data[(1 + 1 * tile_w) * 4], tile_w,
[s](cairo_t * c) { draw_centered_text(c, s, .5); });
make_border(tile_w, tile_h, &data[0], tile_w);
std::size_t x = (texid_scratch % 8) * tile_w;
std::size_t y = (texid_scratch / 8) * tile_h;
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, tile_w, tile_h, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
}
int
texture_generator::get_blur_texture(int texid)
{
return texid + texid_blur_offset;
}
texture_generator::tex_coords
texture_generator::get_tex_coords(std::size_t index)
{
tex_coords c;
double x = (index % 8) / 8.0;
double y = (index / 8) / 8.0;
double w = 1 / 8.0;
double h = 1 / 8.0;
if (index == texid_solid) {
c.x1 = x + w / 2.;
c.y1 = y + h / 2.;
c.x2 = x + w / 2.;
c.y2 = y + h / 2.;
} else {
c.x1 = x + 1 / 512.;
c.y1 = y + 1 / 512.;
c.x2 = x + w - 1 / 512.;
c.y2 = y + h - 1 / 512.;
}
return c;
}
void
texture_generator::make_tex_quad(
std::size_t index,
double x1, double y1, double z1,
double x2, double y2, double z2,
double x3, double y3, double z3,
double x4, double y4, double z4)
{
tex_coords tc = get_tex_coords(index);
glBegin(GL_QUADS);
glTexCoord2f(tc.x1, tc.y1);
glVertex3f(x1, y1, z1);
glTexCoord2f(tc.x2, tc.y1);
glVertex3f(x2, y2, z2);
glTexCoord2f(tc.x2, tc.y2);
glVertex3f(x3, y3, z3);
glTexCoord2f(tc.x1, tc.y2);
glVertex3f(x4, y4, z4);
glEnd();
}
void
texture_generator::make_tex_quad(
std::size_t index,
double x1, double y1, double z1,
double x2, double y2, double z2,
double x3, double y3, double z3,
double x4, double y4, double z4,
double nx, double ny, double nz)
{
tex_coords tc = get_tex_coords(index);
glBegin(GL_QUADS);
glNormal3f(nx, ny, nz);
glTexCoord2f(tc.x1, tc.y1);
glVertex3f(x1, y1, z1);
glTexCoord2f(tc.x2, tc.y1);
glVertex3f(x2, y2, z2);
glTexCoord2f(tc.x2, tc.y2);
glVertex3f(x3, y3, z3);
glTexCoord2f(tc.x1, tc.y2);
glVertex3f(x4, y4, z4);
glEnd();
}
void
texture_generator::make_tex_quad2d(
std::size_t index,
double x1, double y1,
double x2, double y2,
double x3, double y3,
double x4, double y4)
{
tex_coords tc = get_tex_coords(index);
glBegin(GL_QUADS);
glTexCoord2f(tc.x1, tc.y1);
glVertex2f(x1, y1);
glTexCoord2f(tc.x2, tc.y1);
glVertex2f(x2, y2);
glTexCoord2f(tc.x2, tc.y2);
glVertex2f(x3, y3);
glTexCoord2f(tc.x1, tc.y2);
glVertex2f(x4, y4);
glEnd();
}
void
texture_generator::make_box(
double x1, double y1, double z1,
double x2, double y2, double z2)
{
make_tex_quad(texid_solid,
x1, y1, z2,
x1, y2, z2,
x2, y2, z2,
x2, y1, z2,
0, +1, 0);
make_tex_quad(texid_solid,
x1, y1, z1,
x2, y1, z1,
x2, y2, z1,
x1, y2, z1,
0, -1, 0);
make_tex_quad(texid_solid,
x2, y2, z2,
x2, y2, z1,
x2, y1, z1,
x2, y1, z2,
+1, 0, 0);
make_tex_quad(texid_solid,
x1, y2, z2,
x1, y1, z2,
x1, y1, z1,
x1, y2, z1,
-1, 0, 0);
make_tex_quad(texid_solid,
x1, y1, z2,
x2, y1, z2,
x2, y1, z1,
x1, y1, z1,
0, -1, 0);
make_tex_quad(texid_solid,
x1, y2, z2,
x1, y2, z1,
x2, y2, z1,
x2, y2, z2,
0, +1, 0);
}
void
texture_generator::make_solid_tex_coord()
{
tex_coords tc = get_tex_coords(texid_solid);
glTexCoord2f(.5 * (tc.x1 + tc.x2), .5 * (tc.y1 + tc.y2));
}