-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextureAtlas.sj
575 lines (512 loc) · 15.2 KB
/
textureAtlas.sj
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* Freetype GL - A C OpenGL Freetype engine
*
* Distributed under the OSI-approved BSD 2-Clause License. See accompanying
* file `LICENSE` for more details.
*/
--cstruct--
/**
* Tuple of 4 ints.
*
* Each field can be addressed using several aliases:
* - First component: <b>x</b>, <b>r</b>, <b>red</b> or <b>vstart</b>
* - Second component: <b>y</b>, <b>g</b>, <b>green</b> or <b>vcount</b>
* - Third component: <b>z</b>, <b>b</b>, <b>blue</b>, <b>width</b> or <b>istart</b>
* - Fourth component: <b>w</b>, <b>a</b>, <b>alpha</b>, <b>height</b> or <b>icount</b>
*
*/
typedef union
{
int data[4]; /**< All compoments at once */
struct {
int x; /**< Alias for first component */
int y; /**< Alias for second component */
int z; /**< Alias for third component */
int w; /**< Alias for fourht component */
};
struct {
int x_; /**< Alias for first component */
int y_; /**< Alias for second component */
int width; /**< Alias for third component */
int height; /**< Alias for fourth component */
};
struct {
int r; /**< Alias for first component */
int g; /**< Alias for second component */
int b; /**< Alias for third component */
int a; /**< Alias for fourth component */
};
struct {
int red; /**< Alias for first component */
int green; /**< Alias for second component */
int blue; /**< Alias for third component */
int alpha; /**< Alias for fourth component */
};
struct {
int vstart; /**< Alias for first component */
int vcount; /**< Alias for second component */
int istart; /**< Alias for third component */
int icount; /**< Alias for fourth component */
};
} ivec4;
/**
* Tuple of 3 ints.
*
* Each field can be addressed using several aliases:
* - First component: <b>x</b>, <b>r</b> or <b>red</b>
* - Second component: <b>y</b>, <b>g</b> or <b>green</b>
* - Third component: <b>z</b>, <b>b</b> or <b>blue</b>
*
*/
typedef union
{
int data[3]; /**< All compoments at once */
struct {
int x; /**< Alias for first component */
int y; /**< Alias for second component */
int z; /**< Alias for third component */
};
struct {
int r; /**< Alias for first component */
int g; /**< Alias for second component */
int b; /**< Alias for third component */
};
struct {
int red; /**< Alias for first component */
int green; /**< Alias for second component */
int blue; /**< Alias for third component */
};
} ivec3;
/**
* Tuple of 2 ints.
*
* Each field can be addressed using several aliases:
* - First component: <b>x</b>, <b>s</b> or <b>start</b>
* - Second component: <b>y</b>, <b>t</b> or <b>end</b>
*
*/
typedef union
{
int data[2]; /**< All compoments at once */
struct {
int x; /**< Alias for first component */
int y; /**< Alias for second component */
};
struct {
int s; /**< Alias for first component */
int t; /**< Alias for second component */
};
struct {
int start; /**< Alias for first component */
int end; /**< Alias for second component */
};
} ivec2;
/**
* Tuple of 4 floats.
*
* Each field can be addressed using several aliases:
* - First component: <b>x</b>, <b>left</b>, <b>r</b> or <b>red</b>
* - Second component: <b>y</b>, <b>top</b>, <b>g</b> or <b>green</b>
* - Third component: <b>z</b>, <b>width</b>, <b>b</b> or <b>blue</b>
* - Fourth component: <b>w</b>, <b>height</b>, <b>a</b> or <b>alpha</b>
*/
typedef union
{
float data[4]; /**< All compoments at once */
struct {
float x; /**< Alias for first component */
float y; /**< Alias for second component */
float z; /**< Alias for third component */
float w; /**< Alias for fourth component */
};
struct {
float left; /**< Alias for first component */
float top; /**< Alias for second component */
float width; /**< Alias for third component */
float height; /**< Alias for fourth component */
};
struct {
float r; /**< Alias for first component */
float g; /**< Alias for second component */
float b; /**< Alias for third component */
float a; /**< Alias for fourth component */
};
struct {
float red; /**< Alias for first component */
float green; /**< Alias for second component */
float blue; /**< Alias for third component */
float alpha; /**< Alias for fourth component */
};
} vec4;
/**
* Tuple of 3 floats
*
* Each field can be addressed using several aliases:
* - First component: <b>x</b>, <b>r</b> or <b>red</b>
* - Second component: <b>y</b>, <b>g</b> or <b>green</b>
* - Third component: <b>z</b>, <b>b</b> or <b>blue</b>
*/
typedef union
{
float data[3]; /**< All compoments at once */
struct {
float x; /**< Alias for first component */
float y; /**< Alias fo second component */
float z; /**< Alias fo third component */
};
struct {
float r; /**< Alias for first component */
float g; /**< Alias fo second component */
float b; /**< Alias fo third component */
};
struct {
float red; /**< Alias for first component */
float green; /**< Alias fo second component */
float blue; /**< Alias fo third component */
};
} vec3;
/**
* Tuple of 2 floats
*
* Each field can be addressed using several aliases:
* - First component: <b>x</b> or <b>s</b>
* - Second component: <b>y</b> or <b>t</b>
*/
typedef union
{
float data[2]; /**< All components at once */
struct {
float x; /**< Alias for first component */
float y; /**< Alias for second component */
};
struct {
float s; /**< Alias for first component */
float t; /**< Alias for second component */
};
} vec2;
/**
* A texture atlas is used to pack several small regions into a single texture.
*/
typedef struct texture_atlas_t
{
/**
* Allocated nodes
*/
vector_t * nodes;
/**
* Width (in pixels) of the underlying texture
*/
size_t width;
/**
* Height (in pixels) of the underlying texture
*/
size_t height;
/**
* Depth (in bytes) of the underlying texture
*/
size_t depth;
/**
* Allocated surface size
*/
size_t used;
/**
* Texture identity (OpenGL)
*/
unsigned int id;
/**
* Atlas data
*/
unsigned char * data;
} texture_atlas_t;
--cstruct--
--cdefine--
/**
* Creates a new empty texture atlas.
*
* @param width width of the atlas
* @param height height of the atlas
* @param depth bit depth of the atlas
* @return a new empty texture atlas.
*
*/
texture_atlas_t *
texture_atlas_new( const size_t width,
const size_t height,
const size_t depth );
/**
* Deletes a texture atlas.
*
* @param self a texture atlas structure
*
*/
void
texture_atlas_delete( texture_atlas_t * self );
/**
* Allocate a new region in the atlas.
*
* @param self a texture atlas structure
* @param width width of the region to allocate
* @param height height of the region to allocate
* @return Coordinates of the allocated region
*
*/
ivec4
texture_atlas_get_region( texture_atlas_t * self,
const size_t width,
const size_t height );
/**
* Upload data to the specified atlas region.
*
* @param self a texture atlas structure
* @param x x coordinate the region
* @param y y coordinate the region
* @param width width of the region
* @param height height of the region
* @param data data to be uploaded into the specified region
* @param stride stride of the data
*
*/
void
texture_atlas_set_region( texture_atlas_t * self,
const size_t x,
const size_t y,
const size_t width,
const size_t height,
const unsigned char *data,
const size_t stride );
/**
* Remove all allocated regions from the atlas.
*
* @param self a texture atlas structure
*/
void
texture_atlas_clear( texture_atlas_t * self );
--cdefine--
--cfunction--
#include(<stdio.h>)
#include(<stdlib.h>)
#include(<string.h>)
#include(<assert.h>)
#include(<limits.h>)
// ------------------------------------------------------ texture_atlas_new ---
texture_atlas_t *
texture_atlas_new( const size_t width,
const size_t height,
const size_t depth )
{
texture_atlas_t *self = (texture_atlas_t *) malloc( sizeof(texture_atlas_t) );
// We want a one pixel border around the whole atlas to avoid any artefact when
// sampling texture
ivec3 node = {{1,1, (int)width-2}};
assert( (depth == 1) || (depth == 3) || (depth == 4) );
if( self == NULL)
{
halt("line %d: No more memory for allocating data\n", __LINE__ );
}
self->nodes = vector_new( sizeof(ivec3) );
self->used = 0;
self->width = width;
self->height = height;
self->depth = depth;
self->id = 0;
vector_push_back( self->nodes, &node );
self->data = (unsigned char *)
calloc( width*height*depth, sizeof(unsigned char) );
if( self->data == NULL)
{
halt("line %d: No more memory for allocating data\n", __LINE__ );
}
return self;
}
// --------------------------------------------------- texture_atlas_delete ---
void
texture_atlas_delete( texture_atlas_t *self )
{
assert( self );
vector_delete( self->nodes );
if( self->data )
{
free( self->data );
}
free( self );
}
// ----------------------------------------------- texture_atlas_set_region ---
void
texture_atlas_set_region( texture_atlas_t * self,
const size_t x,
const size_t y,
const size_t width,
const size_t height,
const unsigned char * data,
const size_t stride )
{
size_t i;
size_t depth;
size_t charsize;
assert( self );
assert( x > 0);
assert( y > 0);
assert( x < (self->width-1));
assert( (x + width) <= (self->width-1));
assert( y < (self->height-1));
assert( (y + height) <= (self->height-1));
//prevent copying data from undefined position
//and prevent memcpy's undefined behavior when count is zero
assert(height == 0 || (data != NULL && width > 0));
depth = self->depth;
charsize = sizeof(char);
for( i=0; i<height; ++i )
{
memcpy( self->data+((y+i)*self->width + x ) * charsize * depth,
data + (i*stride) * charsize, width * charsize * depth );
}
}
// ------------------------------------------------------ texture_atlas_fit ---
int
texture_atlas_fit( texture_atlas_t * self,
const size_t index,
const size_t width,
const size_t height )
{
ivec3 *node;
int x, y, width_left;
size_t i;
assert( self );
node = (ivec3 *) (vector_get( self->nodes, index ));
x = node->x;
y = node->y;
width_left = (int)width;
i = index;
if ( (x + width) > (self->width-1) )
{
return -1;
}
y = node->y;
while( width_left > 0 )
{
node = (ivec3 *) (vector_get( self->nodes, i ));
if( node->y > y )
{
y = node->y;
}
if( (y + height) > (self->height-1) )
{
return -1;
}
width_left -= node->z;
++i;
}
return y;
}
// ---------------------------------------------------- texture_atlas_merge ---
void
texture_atlas_merge( texture_atlas_t * self )
{
ivec3 *node, *next;
size_t i;
assert( self );
for( i=0; i< self->nodes->size-1; ++i )
{
node = (ivec3 *) (vector_get( self->nodes, i ));
next = (ivec3 *) (vector_get( self->nodes, i+1 ));
if( node->y == next->y )
{
node->z += next->z;
vector_erase( self->nodes, i+1 );
--i;
}
}
}
// ----------------------------------------------- texture_atlas_get_region ---
ivec4
texture_atlas_get_region( texture_atlas_t * self,
const size_t width,
const size_t height )
{
int y, best_index;
size_t best_height, best_width;
ivec3 *node, *prev;
ivec4 region = {{0,0,(int)width,(int)height}};
size_t i;
assert( self );
best_height = UINT_MAX;
best_index = -1;
best_width = UINT_MAX;
for( i=0; i<self->nodes->size; ++i )
{
y = texture_atlas_fit( self, i, width, height );
if( y >= 0 )
{
node = (ivec3 *) vector_get( self->nodes, i );
if( ( (y + height) < best_height ) ||
( ((y + height) == best_height) && (node->z > 0 && (size_t)node->z < best_width)) )
{
best_height = y + height;
best_index = (int)i;
best_width = node->z;
region.x = node->x;
region.y = y;
}
}
}
if( best_index == -1 )
{
region.x = -1;
region.y = -1;
region.width = 0;
region.height = 0;
return region;
}
node = (ivec3 *) malloc( sizeof(ivec3) );
if( node == NULL)
{
halt("line %d: No more memory for allocating data\n", __LINE__ );
}
node->x = region.x;
node->y = (int)(region.y + height);
node->z = (int)width;
vector_insert( self->nodes, best_index, node );
free( node );
for(i = best_index+1; i < self->nodes->size; ++i)
{
node = (ivec3 *) vector_get( self->nodes, i );
prev = (ivec3 *) vector_get( self->nodes, i-1 );
if (node->x < (prev->x + prev->z) )
{
int shrink = prev->x + prev->z - node->x;
node->x += shrink;
node->z -= shrink;
if (node->z <= 0)
{
vector_erase( self->nodes, i );
--i;
}
else
{
break;
}
}
else
{
break;
}
}
texture_atlas_merge( self );
self->used += width * height;
return region;
}
// ---------------------------------------------------- texture_atlas_clear ---
void
texture_atlas_clear( texture_atlas_t * self )
{
ivec3 node = {{1,1,1}};
assert( self );
assert( self->data );
vector_clear( self->nodes );
self->used = 0;
// We want a one pixel border around the whole atlas to avoid any artefact when
// sampling texture
node.z = (int)(self->width-2);
vector_push_back( self->nodes, &node );
memset( self->data, 0, self->width*self->height*self->depth );
}
--cfunction--