Commit d58c7be117465dc2d55a80d4ec39308c7ea3cc30

henry 2001-11-12T22:28:36

Refactored texture fonts to ba able to load glyphs on the fly...uses glTexSubImage2D. They are now more in line with the rest of the fonts.

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
diff --git a/include/FTGLTextureFont.h b/include/FTGLTextureFont.h
index 589cdd8..cea81e3 100755
--- a/include/FTGLTextureFont.h
+++ b/include/FTGLTextureFont.h
@@ -29,12 +29,12 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 		/**
 		 * Get the total width of the texture that holds this font
 		 */
-		virtual GLsizei TextureWidth() const { return textureWidth;}
+		GLsizei TextureWidth() const { return textureWidth;}
 		
 		/**
 		 * Get the total height of the texture that holds this font
 		 */
-		virtual GLsizei TextureHeight() const { return textureHeight;}
+		GLsizei TextureHeight() const { return textureHeight;}
 		
 		/**
 		 * Renders a string of characters
@@ -52,7 +52,7 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 
 		
 	private:
-		virtual FTGlyph* MakeGlyph( unsigned int g){ return NULL;}
+		virtual FTGlyph* MakeGlyph( unsigned int g);
 				
 		/**
 		 * Constructs the internal glyph cache.
@@ -61,22 +61,6 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 		 * freetype glyphs
 		 */
 		virtual bool MakeGlyphList();
-		
-		/**
-		 * Draw a series of glyphs into texture memory
-		 *
-		 * This function will start with glyph index glyphStart and draw each
-		 * glyph into the texture until it runs out of space in the current
-		 * texture. It will return the index of the last glyph it drew so
-		 * that if more textures are required, we know where to start from.
-		 *
-		 * @param glyphStart	The index of the first glyph to be drawn
-		 * @param textID		The index of the openGLtexture to draw glyphs into
-		 * @param textureWidth	The texture width
-		 * @param textureHeight	The texture height
-		 * @param textMem		A pointer to the texture memory.
-		 */
-		unsigned int FillGlyphs( unsigned int glyphStart, GLuint textID, GLsizei textureWidth, GLsizei textureHeight, unsigned char* textMem);
 
 		/**
 		 * Get the size of a block of memory required to layout the glyphs
@@ -100,7 +84,7 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 		 * @param height	The number of rows of bytes.
 		 * @param data		A pointer to the texture data
 		 */
-		void CreateTexture( GLuint id, GLsizei width, GLsizei height, unsigned char* data);
+		int CreateTexture();
 		
 		/**
 		 * The maximum texture dimension on this OpenGL implemetation
@@ -118,9 +102,9 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 		GLsizei textureHeight;
 		
 		/**
-		 * An array of texture ids
+		 *An array of texture ids
 		 */
-		unsigned long glTextureID[16];
+		unsigned long glTextureID[1024];
 		
 		/**
 		 * The number of textures required to hold the glyphs
@@ -148,6 +132,11 @@ class  FTGL_EXPORT FTGLTextureFont : public FTFont
 		 * glyphs don't overlap in the texture
 		 */
 		int padding;
+		
+		int remGlyphs;
+		int xOffset;
+		int yOffset;
+
 
 };
 
diff --git a/include/FTTextureGlyph.h b/include/FTTextureGlyph.h
index 1b59d05..f0f4862 100755
--- a/include/FTTextureGlyph.h
+++ b/include/FTTextureGlyph.h
@@ -24,15 +24,16 @@ class FTGL_EXPORT FTTextureGlyph : public FTGlyph
 		 * Constructor
 		 *
 		 * @param glyph		The Freetype glyph to be processed
-		 * @param id		The index of the texture that this glyph will
-		 *					be drawn in
-		 * @param data		A pointer to the texture memory
-		 * @param stride	The stride of the texture memory
-		 * @param height	The height (number of rows) of the texture memory
-		 * @param u			The texture co-ord for this glyph
-		 * @param v			The texture co-ord for this glyph
+		 * @param id		The id the texture that this glyph will be
+		 *					drawn in
+		 * @param xOffset	The x offset into the parent texture to draw
+		 *					this glyph
+		 * @param yOffset	The y offset into the parent texture to draw
+		 *					this glyph
+		 * @param width		The width of the parent texture
+		 * @param height	The height (number of rows) of the parent texture
 		 */
-		FTTextureGlyph( FT_Glyph glyph, int id, unsigned char* data, GLsizei stride, GLsizei height, float u, float v);
+		FTTextureGlyph( FT_Glyph glyph, int id, int xOffset, int yOffset, GLsizei width, GLsizei height);
 
 		/**
 		 * Destructor
@@ -47,16 +48,12 @@ class FTGL_EXPORT FTTextureGlyph : public FTGlyph
 		 */
 		virtual float Render( const FT_Vector& pen);
 		
+	private:
 		/**
-		 * The texture index of the currently active texture
-		 *
-		 * We call glGetIntegerv( GL_TEXTURE_2D_BINDING, activeTextureID);
-		 * to get the currently active texture to try to reduce the number
-		 * of texture bind operations
+		 * Pointer to the 'image' data
 		 */
-		GLint activeTextureID;
-		
-	private:
+		unsigned char* data;
+		 
 		/**
 		 * The width of the glyph 'image'
 		 */
@@ -90,6 +87,16 @@ class FTGL_EXPORT FTTextureGlyph : public FTGlyph
 		 * The texture index that this glyph is contained in.
 		 */
 		int glTextureID;
+
+		/**
+		 * The texture index of the currently active texture
+		 *
+		 * We call glGetIntegerv( GL_TEXTURE_2D_BINDING, activeTextureID);
+		 * to get the currently active texture to try to reduce the number
+		 * of texture bind operations
+		 */
+		GLint activeTextureID;
+		
 };
 
 
diff --git a/src/FTGLTextureFont.cpp b/src/FTGLTextureFont.cpp
index dbbed91..2d623b6 100755
--- a/src/FTGLTextureFont.cpp
+++ b/src/FTGLTextureFont.cpp
@@ -4,6 +4,8 @@
 
 using namespace std;
 
+//#include "mmgr.h"
+
 inline GLuint NextPowerOf2( GLuint in)
 {
      in -= 1;
@@ -22,12 +24,16 @@ FTGLTextureFont::FTGLTextureFont()
 :	maxTextSize(0),
 	textureWidth(0),
 	textureHeight(0),
-	numTextures(1),
+	numTextures(0),
 	textMem(0),
 	glyphHeight(0),
 	glyphWidth(0),
-	padding(1)
-{}
+	padding(1),
+	remGlyphs(0),
+	xOffset(0),
+	yOffset(0)
+{
+}
 
 
 FTGLTextureFont::~FTGLTextureFont()
@@ -36,136 +42,113 @@ FTGLTextureFont::~FTGLTextureFont()
 }
 
 
-bool FTGLTextureFont::MakeGlyphList()
+FTGlyph* FTGLTextureFont::MakeGlyph( unsigned int g)
 {
-	if( !maxTextSize)
-		glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*)&maxTextSize);
-		
-	glyphHeight = ( charSize.Height()) + padding;
-	glyphWidth = ( charSize.Width()) + padding;
+	FT_Glyph* ftGlyph = face.Glyph( g, FT_LOAD_NO_HINTING);
 	
-	GetSize();
-	GLuint totalMem;
-
-	if( textureHeight > (maxTextSize-padding * 2))
+	if( ftGlyph)
 	{
-		numTextures = static_cast<int>( textureHeight / (maxTextSize-padding * 2)) + 1;
-		if( numTextures > 15) // FIXME
-			numTextures = 15;
+		// Estimate the glyph size size - global bbox
+		glyphHeight = ( charSize.Height()) + padding;
+		glyphWidth = ( charSize.Width()) + padding;
 		
-		GLsizei heightRemain = NextPowerOf2( textureHeight % (maxTextSize-padding * 2));
-		totalMem = ((maxTextSize * ( numTextures - 1)) + heightRemain) * textureWidth;
-
-		glGenTextures( numTextures, (GLuint*)&glTextureID[0]);
+		// Is there a current texture
+		if( numTextures == 0)
+		{
+			glTextureID[0] = CreateTexture();
+			xOffset = yOffset = padding;
+			++numTextures;
 
-		textMem = new unsigned char[totalMem]; // GL_ALPHA texture;
-		memset( textMem, 0, totalMem);
-			
-		unsigned int glyphNum = 0;
-		unsigned char* currTextPtr = textMem;
+		}
 		
-		for( int x = 0; x < numTextures - 1; ++x)
+		// will it fit in the current texture
+		if( xOffset > ( textureWidth - glyphWidth))
 		{
-			glyphNum = FillGlyphs( glyphNum, glTextureID[x], textureWidth, maxTextSize, currTextPtr);
-			
-			CreateTexture( x, textureWidth, maxTextSize, currTextPtr);
+			xOffset = padding;
+			yOffset += glyphHeight;
 			
-			currTextPtr += ( textureWidth * maxTextSize);
-			++glyphNum;
+			if( yOffset > ( textureHeight - glyphHeight))
+			{
+				// no - make a new texture
+				glTextureID[numTextures] = CreateTexture();
+				yOffset = padding;
+				++numTextures;
+			}
 		}
 		
-		glyphNum = FillGlyphs( glyphNum, glTextureID[numTextures - 1], textureWidth, heightRemain, currTextPtr);
-		CreateTexture( numTextures - 1, textureWidth, heightRemain, currTextPtr);
+		// yes - load the glyph
+		FTTextureGlyph* tempGlyph = new FTTextureGlyph( *ftGlyph, glTextureID[numTextures - 1],
+															xOffset, yOffset, textureWidth, textureHeight);
+		
+		xOffset += tempGlyph->BBox().x2 - tempGlyph->BBox().x1;
+		
+		--remGlyphs;
+		return tempGlyph;
 	}
-	else
-	{
+	
+	err = face.Error();
+	return NULL;
 
-		textureHeight = NextPowerOf2( textureHeight+padding * 2);
-		totalMem = textureWidth * textureHeight;
-		
-		glGenTextures( numTextures, (GLuint*)&glTextureID[0]);
+}
 
-		textMem = new unsigned char[totalMem]; // GL_ALPHA texture;
-		memset( textMem, 0, totalMem);
 
-		FillGlyphs( 0, glTextureID[0], textureWidth, textureHeight, textMem);
-		CreateTexture( 0, textureWidth, textureHeight, textMem);
-	}
 
-	delete [] textMem;
-	return !err;
-}
 
-unsigned int FTGLTextureFont::FillGlyphs( unsigned int glyphStart, GLuint id, GLsizei width, GLsizei height, unsigned char* textdata)
+bool FTGLTextureFont::MakeGlyphList()
 {
-	int currentTextX = padding;
-	int currentTextY = padding;// + padding;
-	
-	float currTextU = (float)padding / (float)width;
-	float currTextV = (float)padding / (float)height;
-	
-	unsigned int n;
-        
-	for( n = glyphStart; n < numGlyphs; ++n)
-	{
-		FT_Glyph* ftGlyph = face.Glyph( n, FT_LOAD_NO_HINTING);
-		
-		if( ftGlyph)
-		{
-			unsigned char* data = textdata + (( currentTextY * width) + currentTextX);
-			
-			currTextU = (float)currentTextX / (float)width;
-			
-			FTTextureGlyph* tempGlyph = new FTTextureGlyph( *ftGlyph, id, data, width, height, currTextU, currTextV);
-			glyphList->Add( tempGlyph, n);
-
-			currentTextX += glyphWidth;
-			if( currentTextX > ( width - glyphWidth))
-			{
-				currentTextY += glyphHeight;
-				if( currentTextY > ( height - glyphHeight))
-					return n;
-					
-				currentTextX = padding;
-				currTextV = (float)currentTextY / (float)height;
-			}
-		}
-		else
-		{
-			err = face.Error();
-		}
-	}
+	if( !maxTextSize)
+		glGetIntegerv( GL_MAX_TEXTURE_SIZE, (GLint*)&maxTextSize);
 
+	remGlyphs = numGlyphs;
 
-	return n;
+	FTFont::MakeGlyphList();
+	
+	return !err; // FIXME what err?
 }
 
 
 void FTGLTextureFont::GetSize()
 {
 	//work out the max width. Most likely maxTextSize
-	textureWidth = NextPowerOf2( (numGlyphs * glyphWidth) + padding * 2);
+	textureWidth = NextPowerOf2( (remGlyphs * glyphWidth) + padding * 2);
 	if( textureWidth > maxTextSize)
 	{
 		textureWidth = maxTextSize;
 	}
 	
-	int h = static_cast<int>( (textureWidth-padding * 2) / glyphWidth);
+	int h = static_cast<int>( (textureWidth - padding * 2) / glyphWidth);
         
-	textureHeight = (( numGlyphs / h) + 1) * glyphHeight;
+	textureHeight = NextPowerOf2( (( numGlyphs / h) + 1) * glyphHeight);
+	textureHeight = textureHeight > maxTextSize ? maxTextSize : textureHeight;
 }
 
 
-void FTGLTextureFont::CreateTexture( GLuint id, GLsizei width, GLsizei height, unsigned char* data)
-{
+int FTGLTextureFont::CreateTexture()
+{	
+	// calc the size
+	GetSize();
+	
+	// allocate some mem and clear it to black
+	int totalMem = textureWidth * textureHeight;
+	textMem = new unsigned char[totalMem]; // GL_ALPHA texture;
+	memset( textMem, 0, totalMem);
+
+	// Create the blank texture
+	int textID;
+	glGenTextures( 1, (GLuint*)&textID);
+
 	glPixelStorei( GL_UNPACK_ALIGNMENT, 1); //What does this do exactly?
-	glBindTexture( GL_TEXTURE_2D, glTextureID[id]);
+	glBindTexture( GL_TEXTURE_2D, textID);
 	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
 	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
-	glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
+	glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight, 0, GL_ALPHA, GL_UNSIGNED_BYTE, textMem);
+
+	delete [] textMem;
+
+	return textID;
 }
 
 
diff --git a/src/FTTextureGlyph.cpp b/src/FTTextureGlyph.cpp
index 3b07772..05b4aa1 100755
--- a/src/FTTextureGlyph.cpp
+++ b/src/FTTextureGlyph.cpp
@@ -1,15 +1,19 @@
 #include	"FTTextureGlyph.h"
 #include	"FTGL.h"
 
+//#include "mmgr.h"
 
-FTTextureGlyph::FTTextureGlyph( FT_Glyph glyph, int id, unsigned char* data, GLsizei stride, GLsizei height, float u, float v)
+
+FTTextureGlyph::FTTextureGlyph( FT_Glyph glyph, int id, int xOffset, int yOffset, GLsizei width, GLsizei height)
 :	FTGlyph(),
+	data(0),
 	destWidth(0),
 	destHeight(0),
 	numGreys(0),
-	glTextureID(id)
+	glTextureID(id),
+	activeTextureID(0)
 {
-	// This function will always fail if the glyph's format isn't scalable????
+	// FIXME This function will always fail if the glyph's format isn't scalable????
 	err = FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1);
 	if( err || glyph->format != ft_glyph_format_bitmap)
 	{
@@ -19,7 +23,7 @@ FTTextureGlyph::FTTextureGlyph( FT_Glyph glyph, int id, unsigned char* data, GLs
 	FT_BitmapGlyph  bitmap = ( FT_BitmapGlyph)glyph;
 	FT_Bitmap*      source = &bitmap->bitmap;
 
-	//check the pixel mode
+	// FIXME check the pixel mode
 	//ft_pixel_mode_grays
 	    
 	int srcWidth = source->width;
@@ -29,14 +33,20 @@ FTTextureGlyph::FTTextureGlyph( FT_Glyph glyph, int id, unsigned char* data, GLs
     destWidth = srcWidth;
     destHeight = srcHeight;
     
+    data = new unsigned char[destWidth * destHeight];
+
     for(int y = 0; y < srcHeight; ++y)
     {
     	for(int x = 0; x < srcWidth; ++x)
     	{
-			*( data + ( y * stride  + x)) = *( source->buffer + ( y * srcPitch) + x);
+			*( data + ( y * destWidth  + x)) = *( source->buffer + ( y * srcPitch) + x);
     	}    	
     }
 
+	glBindTexture( GL_TEXTURE_2D, glTextureID);
+	glTexSubImage2D( GL_TEXTURE_2D, 0, xOffset, yOffset, destWidth, destHeight, GL_ALPHA, GL_UNSIGNED_BYTE, data);
+
+
 //		0    
 //		+----+
 //		|    |
@@ -45,18 +55,21 @@ FTTextureGlyph::FTTextureGlyph( FT_Glyph glyph, int id, unsigned char* data, GLs
 //		+----+
 //		     1
 	
-	uv[0].x = u;
-	uv[0].y = v;
-	uv[1].x = uv[0].x + ( (float)destWidth / (float)stride);
-	uv[1].y = uv[0].y + ( (float)destHeight / (float)height);
-
-	bBox = FTBBox( glyph);
+	// FIXME ????
+	uv[0].x = static_cast<float>(xOffset) / static_cast<float>(width);
+	uv[0].y = static_cast<float>(yOffset) / static_cast<float>(height);
+	uv[1].x = static_cast<float>( xOffset + destWidth) / static_cast<float>(width);
+	uv[1].y = static_cast<float>( yOffset + destHeight) / static_cast<float>(height);
+	
 	numGreys = source->num_grays;
 	advance = glyph->advance.x >> 16;
+	bBox = FTBBox( glyph);
 
  	pos.x = bitmap->left;
 	pos.y = bitmap->top;
 	
+	delete [] data;
+	
 // discard glyph image (bitmap or not)
 	// Is this the right place to do this?
 	FT_Done_Glyph( glyph);
@@ -64,9 +77,7 @@ FTTextureGlyph::FTTextureGlyph( FT_Glyph glyph, int id, unsigned char* data, GLs
 
 
 FTTextureGlyph::~FTTextureGlyph()
-{
-
-}
+{}
 
 
 float FTTextureGlyph::Render( const FT_Vector& pen)