Hash :
4fcbcd3c
Author :
Date :
2001-08-08T01:30:04
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
#include "GL/gl.h"
#include "FTGLTextureFont.h"
#include "FTGlyphContainer.h"
#include "FTGL.h"
#include "FTTextureGlyph.h"
typedef unsigned long UInt32; // a mac thing?
inline UInt32 NextPowerOf2( UInt32 in)
{
in -= 1;
in |= in >> 16;
in |= in >> 8;
in |= in >> 4;
in |= in >> 2;
in |= in >> 1;
return in + 1;
}
FTGLTextureFont::FTGLTextureFont()
: glTextureID(0),
textMem(0),
padding(15),
tempGlyph(0),
maxTextSize(0),
textureSize(0),
glyphHeight(0),
glyphWidth(0),
horizGlyphs(0),
vertGlyphs(0)
{}
FTGLTextureFont::~FTGLTextureFont()
{
glDeleteTextures( 1, &glTextureID);
delete [] textMem;
}
bool FTGLTextureFont::MakeGlyphList()
{
FT_Face* ftFace = face.Face();
CreateTexture();
int currentTextX = padding;
int currentTextY = padding + padding;
float currTextU = (float)padding / (float)textureSize;
float currTextV = (float)padding / (float)textureSize;
// numGlyphs = 256; // FIXME hack
for( int n = 0; n <= numGlyphs; ++n)
{
err = FT_Load_Glyph( *ftFace, n, FT_LOAD_DEFAULT);
FT_Glyph ftGlyph;
err = FT_Get_Glyph( (*ftFace)->glyph, &ftGlyph);
unsigned char* data = textMem + ( ( currentTextY * textureSize) + currentTextX);
currTextU = (float)currentTextX / (float)textureSize;
tempGlyph = new FTTextureGlyph( ftGlyph, n, data, textureSize, currTextU, currTextV);
glyphList->Add( tempGlyph);
currentTextX += glyphWidth;
if( currentTextX > ( textureSize - glyphWidth))
{
currentTextY += glyphHeight;
currentTextX = padding;
currTextV = (float)currentTextY / (float)textureSize;
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //What does this do exactly?
glBindTexture( GL_TEXTURE_2D, glTextureID);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, textureSize, textureSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, textMem);
return !err;
}
bool FTGLTextureFont::CreateTexture()
{
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxTextSize);
glGenTextures( 1, &glTextureID);
// There is a fixed relationship between ppem, point size, and the
// resulting pixel size. The real dimension of the glyphs are not
// covered by this. The field `bbox' in FT_Face gives a maximal bounding
// box large enough to hold all bboxes of single glyphs.
// calc the area required for this font
glyphHeight = ( charSize.Height()) + padding;
glyphWidth = ( charSize.Width()) + padding;
//FIXME
// textureSize = 1024;
int t;
for( t = 64; t <= maxTextSize; t *=2)
{
int h = static_cast<int>( t / glyphWidth);
if( t > ( ( numGlyphs / h) * glyphHeight))
break;
}
textureSize = t;
horizGlyphs = static_cast<int>( textureSize / glyphWidth);
vertGlyphs = static_cast<int>(( numGlyphs / horizGlyphs) + 1);
// build the texture.
textMem = new unsigned char[ textureSize * textureSize]; // GL_ALPHA texture;
//FIXME
for( int i = 0; i < ( textureSize * textureSize); ++i)
textMem[i] = 0;
}
void FTGLTextureFont::render( const char* string)
{
glBindTexture( GL_TEXTURE_2D, glTextureID);
// QUADS are faster!? Less function call overhead?
glBegin( GL_QUADS);
FTFont::render( string);
glEnd();
}