Add padding around every glyph in a Font texture, fixing the fringing seen with scaled text (actually fixes issue #321)

This commit is contained in:
Bill Meltsner
2012-01-08 20:59:52 -06:00
parent 2077408351
commit 21516fc658
2 changed files with 20 additions and 7 deletions
+19 -7
View File
@@ -57,7 +57,7 @@ namespace opengl
void Font::createTexture() void Font::createTexture()
{ {
texture_x = texture_y = rowHeight = 0; texture_x = texture_y = rowHeight = TEXTURE_PADDING;
GLuint t; GLuint t;
glGenTextures(1, &t); glGenTextures(1, &t);
textures.push_back(t); textures.push_back(t);
@@ -70,6 +70,7 @@ namespace opengl
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLint format = (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA); GLint format = (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA);
// Initialize the texture
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D,
0, 0,
GL_RGBA, GL_RGBA,
@@ -79,6 +80,17 @@ namespace opengl
format, format,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
NULL); NULL);
// Fill the texture with transparent black
std::vector<GLubyte> emptyData(TEXTURE_WIDTH * TEXTURE_HEIGHT * (type == FONT_TRUETYPE ? 2 : 4), 0);
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
(GLsizei)TEXTURE_WIDTH,
(GLsizei)TEXTURE_HEIGHT,
format,
GL_UNSIGNED_BYTE,
&emptyData[0]);
} }
Font::Glyph * Font::addGlyph(int glyph) Font::Glyph * Font::addGlyph(int glyph)
@@ -94,13 +106,13 @@ namespace opengl
g->spacing = gd->getAdvance(); g->spacing = gd->getAdvance();
int w = gd->getWidth(); int w = gd->getWidth();
int h = gd->getHeight(); int h = gd->getHeight();
if (texture_x + w > TEXTURE_WIDTH) if (texture_x + w + TEXTURE_PADDING > TEXTURE_WIDTH)
{ // out of space - new row! { // out of space - new row!
texture_x = 0; texture_x = TEXTURE_PADDING;
texture_y += rowHeight; texture_y += rowHeight;
rowHeight = 0; rowHeight = TEXTURE_PADDING;
} }
if (texture_y + h > TEXTURE_HEIGHT) if (texture_y + h + TEXTURE_PADDING > TEXTURE_HEIGHT)
{ // totally out of space - new texture! { // totally out of space - new texture!
createTexture(); createTexture();
} }
@@ -135,8 +147,8 @@ namespace opengl
delete q; delete q;
delete gd; delete gd;
texture_x += w; texture_x += (w + TEXTURE_PADDING);
rowHeight = std::max(rowHeight, h); rowHeight = std::max(rowHeight, h + TEXTURE_PADDING);
glyphs[glyph] = g; glyphs[glyph] = g;
return g; return g;
+1
View File
@@ -68,6 +68,7 @@ namespace opengl
static const int TEXTURE_WIDTH = 512; static const int TEXTURE_WIDTH = 512;
static const int TEXTURE_HEIGHT = 512; static const int TEXTURE_HEIGHT = 512;
static const int TEXTURE_PADDING = 1;
int texture_x, texture_y; int texture_x, texture_y;
int rowHeight; int rowHeight;