0
|
1 /* ============================================================================
|
|
2 * Freetype GL - A C OpenGL Freetype engine
|
|
3 * Platform: Any
|
|
4 * WWW: http://code.google.com/p/freetype-gl/
|
|
5 * ----------------------------------------------------------------------------
|
|
6 * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions are met:
|
|
10 *
|
|
11 * 1. Redistributions of source code must retain the above copyright notice,
|
|
12 * this list of conditions and the following disclaimer.
|
|
13 *
|
|
14 * 2. Redistributions in binary form must reproduce the above copyright
|
|
15 * notice, this list of conditions and the following disclaimer in the
|
|
16 * documentation and/or other materials provided with the distribution.
|
|
17 *
|
|
18 * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
|
|
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
21 * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28 *
|
|
29 * The views and conclusions contained in the software and documentation are
|
|
30 * those of the authors and should not be interpreted as representing official
|
|
31 * policies, either expressed or implied, of Nicolas P. Rougier.
|
|
32 * ============================================================================
|
|
33 */
|
|
34 #include <stdio.h>
|
|
35 #include <stdlib.h>
|
|
36 #include <string.h>
|
|
37 #include <assert.h>
|
|
38 #include <limits.h>
|
|
39 #include "opengl.h"
|
|
40 #include "texture-atlas.h"
|
|
41
|
|
42
|
|
43 // ------------------------------------------------------ texture_atlas_new ---
|
|
44 texture_atlas_t *
|
|
45 texture_atlas_new( const size_t width,
|
|
46 const size_t height,
|
|
47 const size_t depth )
|
|
48 {
|
|
49 texture_atlas_t *self = (texture_atlas_t *) malloc( sizeof(texture_atlas_t) );
|
|
50
|
|
51 // We want a one pixel border around the whole atlas to avoid any artefact when
|
|
52 // sampling texture
|
|
53 ivec3 node = {{1,1,width-2}};
|
|
54
|
|
55 assert( (depth == 1) || (depth == 3) || (depth == 4) );
|
|
56 if( self == NULL)
|
|
57 {
|
|
58 fprintf( stderr,
|
|
59 "line %d: No more memory for allocating data\n", __LINE__ );
|
|
60 exit( EXIT_FAILURE );
|
|
61 }
|
|
62 self->nodes = vector_new( sizeof(ivec3) );
|
|
63 self->used = 0;
|
|
64 self->width = width;
|
|
65 self->height = height;
|
|
66 self->depth = depth;
|
|
67 self->id = 0;
|
|
68
|
|
69 vector_push_back( self->nodes, &node );
|
|
70 self->data = (unsigned char *)
|
|
71 calloc( width*height*depth, sizeof(unsigned char) );
|
|
72
|
|
73 if( self->data == NULL)
|
|
74 {
|
|
75 fprintf( stderr,
|
|
76 "line %d: No more memory for allocating data\n", __LINE__ );
|
|
77 exit( EXIT_FAILURE );
|
|
78 }
|
|
79
|
|
80 return self;
|
|
81 }
|
|
82
|
|
83
|
|
84 // --------------------------------------------------- texture_atlas_delete ---
|
|
85 void
|
|
86 texture_atlas_delete( texture_atlas_t *self )
|
|
87 {
|
|
88 assert( self );
|
|
89 vector_delete( self->nodes );
|
|
90 if( self->data )
|
|
91 {
|
|
92 free( self->data );
|
|
93 }
|
|
94 if( self->id )
|
|
95 {
|
|
96 glDeleteTextures( 1, &self->id );
|
|
97 }
|
|
98 free( self );
|
|
99 }
|
|
100
|
|
101
|
|
102 // ----------------------------------------------- texture_atlas_set_region ---
|
|
103 void
|
|
104 texture_atlas_set_region( texture_atlas_t * self,
|
|
105 const size_t x,
|
|
106 const size_t y,
|
|
107 const size_t width,
|
|
108 const size_t height,
|
|
109 const unsigned char * data,
|
|
110 const size_t stride )
|
|
111 {
|
|
112 size_t i;
|
|
113 size_t depth;
|
|
114 size_t charsize;
|
|
115
|
|
116 assert( self );
|
|
117 assert( x > 0);
|
|
118 assert( y > 0);
|
|
119 assert( x < (self->width-1));
|
|
120 assert( (x + width) <= (self->width-1));
|
|
121 assert( y < (self->height-1));
|
|
122 assert( (y + height) <= (self->height-1));
|
|
123
|
|
124 depth = self->depth;
|
|
125 charsize = sizeof(char);
|
|
126 for( i=0; i<height; ++i )
|
|
127 {
|
|
128 memcpy( self->data+((y+i)*self->width + x ) * charsize * depth,
|
|
129 data + (i*stride) * charsize, width * charsize * depth );
|
|
130 }
|
|
131 }
|
|
132
|
|
133
|
|
134 // ------------------------------------------------------ texture_atlas_fit ---
|
|
135 int
|
|
136 texture_atlas_fit( texture_atlas_t * self,
|
|
137 const size_t index,
|
|
138 const size_t width,
|
|
139 const size_t height )
|
|
140 {
|
|
141 ivec3 *node;
|
|
142 int x, y, width_left;
|
|
143 size_t i;
|
|
144
|
|
145 assert( self );
|
|
146
|
|
147 node = (ivec3 *) (vector_get( self->nodes, index ));
|
|
148 x = node->x;
|
|
149 y = node->y;
|
|
150 width_left = width;
|
|
151 i = index;
|
|
152
|
|
153 if ( (x + width) > (self->width-1) )
|
|
154 {
|
|
155 return -1;
|
|
156 }
|
|
157 y = node->y;
|
|
158 while( width_left > 0 )
|
|
159 {
|
|
160 node = (ivec3 *) (vector_get( self->nodes, i ));
|
|
161 if( node->y > y )
|
|
162 {
|
|
163 y = node->y;
|
|
164 }
|
|
165 if( (y + height) > (self->height-1) )
|
|
166 {
|
|
167 return -1;
|
|
168 }
|
|
169 width_left -= node->z;
|
|
170 ++i;
|
|
171 }
|
|
172 return y;
|
|
173 }
|
|
174
|
|
175
|
|
176 // ---------------------------------------------------- texture_atlas_merge ---
|
|
177 void
|
|
178 texture_atlas_merge( texture_atlas_t * self )
|
|
179 {
|
|
180 ivec3 *node, *next;
|
|
181 size_t i;
|
|
182
|
|
183 assert( self );
|
|
184
|
|
185 for( i=0; i< self->nodes->size-1; ++i )
|
|
186 {
|
|
187 node = (ivec3 *) (vector_get( self->nodes, i ));
|
|
188 next = (ivec3 *) (vector_get( self->nodes, i+1 ));
|
|
189 if( node->y == next->y )
|
|
190 {
|
|
191 node->z += next->z;
|
|
192 vector_erase( self->nodes, i+1 );
|
|
193 --i;
|
|
194 }
|
|
195 }
|
|
196 }
|
|
197
|
|
198
|
|
199 // ----------------------------------------------- texture_atlas_get_region ---
|
|
200 ivec4
|
|
201 texture_atlas_get_region( texture_atlas_t * self,
|
|
202 const size_t width,
|
|
203 const size_t height )
|
|
204 {
|
|
205
|
|
206 int y, best_height, best_width, best_index;
|
|
207 ivec3 *node, *prev;
|
|
208 ivec4 region = {{0,0,width,height}};
|
|
209 size_t i;
|
|
210
|
|
211 assert( self );
|
|
212
|
|
213 best_height = INT_MAX;
|
|
214 best_index = -1;
|
|
215 best_width = INT_MAX;
|
|
216 for( i=0; i<self->nodes->size; ++i )
|
|
217 {
|
|
218 y = texture_atlas_fit( self, i, width, height );
|
|
219 if( y >= 0 )
|
|
220 {
|
|
221 node = (ivec3 *) vector_get( self->nodes, i );
|
|
222 if( ( (y + height) < best_height ) ||
|
|
223 ( ((y + height) == best_height) && (node->z < best_width)) )
|
|
224 {
|
|
225 best_height = y + height;
|
|
226 best_index = i;
|
|
227 best_width = node->z;
|
|
228 region.x = node->x;
|
|
229 region.y = y;
|
|
230 }
|
|
231 }
|
|
232 }
|
|
233
|
|
234 if( best_index == -1 )
|
|
235 {
|
|
236 region.x = -1;
|
|
237 region.y = -1;
|
|
238 region.width = 0;
|
|
239 region.height = 0;
|
|
240 return region;
|
|
241 }
|
|
242
|
|
243 node = (ivec3 *) malloc( sizeof(ivec3) );
|
|
244 if( node == NULL)
|
|
245 {
|
|
246 fprintf( stderr,
|
|
247 "line %d: No more memory for allocating data\n", __LINE__ );
|
|
248 exit( EXIT_FAILURE );
|
|
249 }
|
|
250 node->x = region.x;
|
|
251 node->y = region.y + height;
|
|
252 node->z = width;
|
|
253 vector_insert( self->nodes, best_index, node );
|
|
254 free( node );
|
|
255
|
|
256 for(i = best_index+1; i < self->nodes->size; ++i)
|
|
257 {
|
|
258 node = (ivec3 *) vector_get( self->nodes, i );
|
|
259 prev = (ivec3 *) vector_get( self->nodes, i-1 );
|
|
260
|
|
261 if (node->x < (prev->x + prev->z) )
|
|
262 {
|
|
263 int shrink = prev->x + prev->z - node->x;
|
|
264 node->x += shrink;
|
|
265 node->z -= shrink;
|
|
266 if (node->z <= 0)
|
|
267 {
|
|
268 vector_erase( self->nodes, i );
|
|
269 --i;
|
|
270 }
|
|
271 else
|
|
272 {
|
|
273 break;
|
|
274 }
|
|
275 }
|
|
276 else
|
|
277 {
|
|
278 break;
|
|
279 }
|
|
280 }
|
|
281 texture_atlas_merge( self );
|
|
282 self->used += width * height;
|
|
283 return region;
|
|
284 }
|
|
285
|
|
286
|
|
287 // ---------------------------------------------------- texture_atlas_clear ---
|
|
288 void
|
|
289 texture_atlas_clear( texture_atlas_t * self )
|
|
290 {
|
|
291 ivec3 node = {{1,1,1}};
|
|
292
|
|
293 assert( self );
|
|
294 assert( self->data );
|
|
295
|
|
296 vector_clear( self->nodes );
|
|
297 self->used = 0;
|
|
298 // We want a one pixel border around the whole atlas to avoid any artefact when
|
|
299 // sampling texture
|
|
300 node.z = self->width-2;
|
|
301
|
|
302 vector_push_back( self->nodes, &node );
|
|
303 memset( self->data, 0, self->width*self->height*self->depth );
|
|
304 }
|
|
305
|
|
306
|
|
307 // --------------------------------------------------- texture_atlas_upload ---
|
|
308 void
|
|
309 texture_atlas_upload( texture_atlas_t * self )
|
|
310 {
|
|
311 assert( self );
|
|
312 assert( self->data );
|
|
313
|
|
314 if( !self->id )
|
|
315 {
|
|
316 glGenTextures( 1, &self->id );
|
|
317 }
|
|
318
|
|
319 glBindTexture( GL_TEXTURE_2D, self->id );
|
|
320 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
|
|
321 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
|
|
322 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
|
323 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
|
324 if( self->depth == 4 )
|
|
325 {
|
|
326 #ifdef GL_UNSIGNED_INT_8_8_8_8_REV
|
|
327 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, self->width, self->height,
|
|
328 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, self->data );
|
|
329 #else
|
|
330 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, self->width, self->height,
|
|
331 0, GL_RGBA, GL_UNSIGNED_BYTE, self->data );
|
|
332 #endif
|
|
333 }
|
|
334 else if( self->depth == 3 )
|
|
335 {
|
|
336 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, self->width, self->height,
|
|
337 0, GL_RGB, GL_UNSIGNED_BYTE, self->data );
|
|
338 }
|
|
339 else
|
|
340 {
|
|
341 glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, self->width, self->height,
|
|
342 0, GL_ALPHA, GL_UNSIGNED_BYTE, self->data );
|
|
343 }
|
|
344 }
|
|
345
|