sduclos
unread,Mar 21, 2014, 6:54:50 PM3/21/14Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to freetype-gl, Nicolas Rougier
Bonjours M. Rougier,
The trick is to render all text with a single call to
glDrawArrays(GL_TRIANGLE_STRIP, ..),
The simplified code bellow show how to make that happen for a string:
---------------------------------->8--------------------------------------------------
static GArray *_fill_ftglBuf(GArray *ftglBuf, const char *str)
{
int pen_x = 0;
int pen_y = 0;
if (NULL == ftglBuf) {
ftglBuf = g_array_new(FALSE, FALSE, sizeof(_freetype_gl_vertex_t));
} else {
g_array_set_size(ftglBuf, 0);
}
int len = strlen(str);
for (int i=0; i<len; ++i) {
GLfloat x0 = pen_x + glyph->offset_x;
GLfloat y0 = pen_y + glyph->offset_y;
GLfloat x1 = x0 + glyph->width;
GLfloat y1 = y0 - (glyph->height);
GLfloat s0 = glyph->s0;
GLfloat t0 = glyph->t0;
GLfloat s1 = glyph->s1;
GLfloat t1 = glyph->t1;
// GL_TRIANGLE_STRIP
GLfloat z0 = 0.0;
_freetype_gl_vertex_t vertices[4] = {
{x0,y0,z0, s0,t0},
{x0,y1,z0, s0,t1},
{x1,y0,z0, s1,t0},
{x1,y1,z0, s1,t1}
};
// connect glyphs with degenerated triangle (GPU skip those)
if (0 < ftglBuf->len) {
// dup last vertex
ftglBuf = g_array_append_vals(ftglBuf, &vertices[0], 1);
}
// glyph strip
ftglBuf = g_array_append_vals(ftglBuf, &vertices[0], 4);
// dup 3rd vertex
ftglBuf = g_array_append_vals(ftglBuf, &vertices[2], 1);
pen_x += glyph->advance_x;
pen_y += glyph->advance_y;
}
return ftglBuf;
}
---------------------------------->8--------------------------------------------------
The GPU discard degenerated triangle. So the trick is to link glyph strip using
degenerated strip.
Note how vertex in vertices are organize to form a strip.
Also note how degenerated strip are appended to glyph.
Finally there is no core code to change in freetype_gl (texture-atlas,
texture-font
vector, vec123.h)
I use glib GArray to store stuff but anything could do.
Salutation,
Sylvain.