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 * This source is based on the article by Jukka Jylänki :
|
|
35 * "A Thousand Ways to Pack the Bin - A Practical Approach to
|
|
36 * Two-Dimensional Rectangle Bin Packing", February 27, 2010.
|
|
37 *
|
|
38 * More precisely, this is an implementation of the Skyline Bottom-Left
|
|
39 * algorithm based on C++ sources provided by Jukka Jylänki at:
|
|
40 * http://clb.demon.fi/files/RectangleBinPack/
|
|
41 *
|
|
42 * ============================================================================
|
|
43 */
|
|
44 #ifndef __TEXTURE_ATLAS_H__
|
|
45 #define __TEXTURE_ATLAS_H__
|
|
46
|
|
47 #include <stdlib.h>
|
|
48
|
|
49 #ifdef __cplusplus
|
|
50 extern "C" {
|
|
51 #endif
|
|
52
|
|
53 #include "vector.h"
|
|
54 #include "vec234.h"
|
|
55
|
|
56 /**
|
|
57 * @file texture-atlas.h
|
|
58 * @author Nicolas Rougier (Nicolas.Rougier@inria.fr)
|
|
59 *
|
|
60 * @defgroup texture-atlas Texture atlas
|
|
61 *
|
|
62 * A texture atlas is used to pack several small regions into a single texture.
|
|
63 *
|
|
64 * The actual implementation is based on the article by Jukka Jylänki : "A
|
|
65 * Thousand Ways to Pack the Bin - A Practical Approach to Two-Dimensional
|
|
66 * Rectangle Bin Packing", February 27, 2010.
|
|
67 * More precisely, this is an implementation of the Skyline Bottom-Left
|
|
68 * algorithm based on C++ sources provided by Jukka Jylänki at:
|
|
69 * http://clb.demon.fi/files/RectangleBinPack/
|
|
70 *
|
|
71 *
|
|
72 * Example Usage:
|
|
73 * @code
|
|
74 * #include "texture-atlas.h"
|
|
75 *
|
|
76 * ...
|
|
77 *
|
|
78 * / Creates a new atlas of 512x512 with a depth of 1
|
|
79 * texture_atlas_t * atlas = texture_atlas_new( 512, 512, 1 );
|
|
80 *
|
|
81 * // Allocates a region of 20x20
|
|
82 * ivec4 region = texture_atlas_get_region( atlas, 20, 20 );
|
|
83 *
|
|
84 * // Fill region with some data
|
|
85 * texture_atlas_set_region( atlas, region.x, region.y, region.width, region.height, data, stride )
|
|
86 *
|
|
87 * ...
|
|
88 *
|
|
89 * @endcode
|
|
90 *
|
|
91 * @{
|
|
92 */
|
|
93
|
|
94
|
|
95 /**
|
|
96 * A texture atlas is used to pack several small regions into a single texture.
|
|
97 */
|
|
98 typedef struct
|
|
99 {
|
|
100 /**
|
|
101 * Allocated nodes
|
|
102 */
|
|
103 vector_t * nodes;
|
|
104
|
|
105 /**
|
|
106 * Width (in pixels) of the underlying texture
|
|
107 */
|
|
108 size_t width;
|
|
109
|
|
110 /**
|
|
111 * Height (in pixels) of the underlying texture
|
|
112 */
|
|
113 size_t height;
|
|
114
|
|
115 /**
|
|
116 * Depth (in bytes) of the underlying texture
|
|
117 */
|
|
118 size_t depth;
|
|
119
|
|
120 /**
|
|
121 * Allocated surface size
|
|
122 */
|
|
123 size_t used;
|
|
124
|
|
125 /**
|
|
126 * Texture identity (OpenGL)
|
|
127 */
|
|
128 unsigned int id;
|
|
129
|
|
130 /**
|
|
131 * Atlas data
|
|
132 */
|
|
133 unsigned char * data;
|
|
134
|
|
135 } texture_atlas_t;
|
|
136
|
|
137
|
|
138
|
|
139 /**
|
|
140 * Creates a new empty texture atlas.
|
|
141 *
|
|
142 * @param width width of the atlas
|
|
143 * @param height height of the atlas
|
|
144 * @param depth bit depth of the atlas
|
|
145 * @return a new empty texture atlas.
|
|
146 *
|
|
147 */
|
|
148 texture_atlas_t *
|
|
149 texture_atlas_new( const size_t width,
|
|
150 const size_t height,
|
|
151 const size_t depth );
|
|
152
|
|
153
|
|
154 /**
|
|
155 * Deletes a texture atlas.
|
|
156 *
|
|
157 * @param self a texture atlas structure
|
|
158 *
|
|
159 */
|
|
160 void
|
|
161 texture_atlas_delete( texture_atlas_t * self );
|
|
162
|
|
163
|
|
164 /**
|
|
165 * Upload atlas to video memory.
|
|
166 *
|
|
167 * @param self a texture atlas structure
|
|
168 *
|
|
169 */
|
|
170 void
|
|
171 texture_atlas_upload( texture_atlas_t * self );
|
|
172
|
|
173
|
|
174 /**
|
|
175 * Allocate a new region in the atlas.
|
|
176 *
|
|
177 * @param self a texture atlas structure
|
|
178 * @param width width of the region to allocate
|
|
179 * @param height height of the region to allocate
|
|
180 * @return Coordinates of the allocated region
|
|
181 *
|
|
182 */
|
|
183 ivec4
|
|
184 texture_atlas_get_region( texture_atlas_t * self,
|
|
185 const size_t width,
|
|
186 const size_t height );
|
|
187
|
|
188
|
|
189 /**
|
|
190 * Upload data to the specified atlas region.
|
|
191 *
|
|
192 * @param self a texture atlas structure
|
|
193 * @param x x coordinate the region
|
|
194 * @param y y coordinate the region
|
|
195 * @param width width of the region
|
|
196 * @param height height of the region
|
|
197 * @param data data to be uploaded into the specified region
|
|
198 * @param stride stride of the data
|
|
199 *
|
|
200 */
|
|
201 void
|
|
202 texture_atlas_set_region( texture_atlas_t * self,
|
|
203 const size_t x,
|
|
204 const size_t y,
|
|
205 const size_t width,
|
|
206 const size_t height,
|
|
207 const unsigned char *data,
|
|
208 const size_t stride );
|
|
209
|
|
210 /**
|
|
211 * Remove all allocated regions from the atlas.
|
|
212 *
|
|
213 * @param self a texture atlas structure
|
|
214 */
|
|
215 void
|
|
216 texture_atlas_clear( texture_atlas_t * self );
|
|
217
|
|
218
|
|
219 /** @} */
|
|
220
|
|
221 #ifdef __cplusplus
|
|
222 }
|
|
223 #endif
|
|
224
|
|
225 #endif /* __TEXTURE_ATLAS_H__ */
|