annotate src/texture-atlas.c @ 62:301805d68a97 default tip

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