Hash :
be451805
Author :
Date :
2001-08-06T05:43:36
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
#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;
// float Uinc = (float)glyphWidth / (float)textureSize;
// float Vinc = (float)glyphHeight / (float)textureSize;
// int currGlyph = 0;
int glyphIndex;
for( int n = 0; n <= numGlyphs; ++n)
{
glyphIndex = FT_Get_Char_Index( *ftFace, n);
err = FT_Load_Glyph( *ftFace, glyphIndex, FT_LOAD_DEFAULT);
if( err)
{ }
FT_Glyph ftGlyph;
err = FT_Get_Glyph( (*ftFace)->glyph, &ftGlyph);
if( err)
{}
unsigned char* data = textMem + ( ( currentTextY * textureSize) + currentTextX);
currTextU = (float)currentTextX / (float)textureSize;
tempGlyph = new FTTextureGlyph( ftGlyph, glyphIndex, 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);
}
bool FTGLTextureFont::CreateTexture()
{
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxTextSize);
glGenTextures( 1, &glTextureID);
// 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();
}