starting to use freetype's rasterizer and gl texturing.
authorDerek Foreman <manmower@gmail.com>
Thu, 29 May 2003 22:27:11 +0000 (22:27 +0000)
committerDerek Foreman <manmower@gmail.com>
Thu, 29 May 2003 22:27:11 +0000 (22:27 +0000)
it's funny.

glft/font.c
glft/font.h
glft/render.c
glft/render.h
glft/test.c

index 9a2944078914bf5deb648771703d13501863d690..b9779d8aa5e2bd8daef0b3cecd71709e57eef73b 100644 (file)
@@ -21,7 +21,7 @@ struct GHashTable *glyph_map = NULL;
 void dest_glyph_map_value(gpointer key, gpointer val, gpointer data)
 {
     struct GlftGlyph *g = val;
-    glDeleteLists(g->dlist, 1);    
+    glDeleteTextures(1, &g->tnum);    
     free(g);
 }
 
@@ -287,9 +287,9 @@ struct GlftGlyph *GlftFontGlyph(struct GlftFont *font, const char *c)
     if (!g) {
         g = malloc(sizeof(struct GlftGlyph));
         g->w = w;
-        g->dlist = glGenLists(1);
+        glGenTextures(1, &g->tnum);
 
-        GlftRenderGlyph(font->face, g->dlist);
+        GlftRenderGlyph(font->face, g->tnum);
 
         if (!(font->spacing == FC_PROPORTIONAL)) {
             g->width = font->max_advance_width;
index 5b149561f9b82b662ad04e4add5598a73e818cf3..0059913f93b06738bb8b297a435775b25195d398 100644 (file)
@@ -48,7 +48,7 @@ struct GlftGlyph {
     /* The character in UCS-4 encoding */
     FcChar32 w;
     /* OpenGL display list for the character */
-    unsigned int dlist;
+    unsigned int tnum;
     /* The FT_Face glyph */
     FT_UInt glyph;
 
index 91bd9ba758e312031d6aac00917898ccdfa97453..d425244200e8752cb20216ae6fd07bffbeba2d1f 100644 (file)
@@ -8,83 +8,38 @@
 
 #define TOFLOAT(x) (((x) >> 6) + ((x) & 63)/64.0)
 
-#include FT_OUTLINE_H
-
-struct GlftWalkState {
-    int drawing;
-    float x, y;
-};
-
-static struct GlftWalkState state;
-
-int GlftMoveToFunc(FT_Vector *to, void *user)
-{
-    state.x = TOFLOAT(to->x);
-    state.y = TOFLOAT(to->y);
-    if (state.drawing) {
-        glEnd();
-    }
-    glBegin(GL_LINE_STRIP);
-    glVertex2f(state.x, state.y);
-    state.drawing = 1;
-    return 0;
-}
-
-int GlftLineToFunc(FT_Vector *to, void *user)
-{
-    state.x = TOFLOAT(to->x);
-    state.y = TOFLOAT(to->y);
-    glVertex2f(state.x, state.y);
-    return 0;
-}
-
-int GlftConicToFunc(FT_Vector *c, FT_Vector *to, void *user)
-{
-    float t, u, x, y;
-
-    for (t = 0, u = 1; t < 1.0; t += 1.0/TPOINTS, u = 1.0-t) {
-        x = u*u*state.x + 2*t*u*TOFLOAT(c->x) + t*t*TOFLOAT(to->x);
-        y = u*u*state.y + 2*t*u*TOFLOAT(c->y) + t*t*TOFLOAT(to->y);
-        glVertex2f(x, y);
-    }
-    state.x = TOFLOAT(to->x);
-    state.y = TOFLOAT(to->y);
-    glVertex2f(state.x, state.y);
-    return 0;
-}
-
-int GlftCubicToFunc(FT_Vector *c1, FT_Vector *c2, FT_Vector *to, void 
-*user)
-{
-    GlftLineToFunc(to, user);
-    g_message("cubic not currently rendered properly\n");
-    return 0;
-}
-
-FT_Outline_Funcs GlftFuncs = {
-    GlftMoveToFunc,
-    GlftLineToFunc,
-    GlftConicToFunc,
-    GlftCubicToFunc,
-    0,
-    0
-};
-
-void GlftRenderGlyph(FT_Face face, unsigned int dlist)
+void GlftRenderGlyph(FT_Face face, unsigned int tnum)
 {
+    unsigned char *padbuf;
+    int padx = 1, pady = 1, i;
     int err;
     FT_GlyphSlot slot = face->glyph;
 
-    state.x = 0;
-    state.y = 0;
-    state.drawing = 0;
-
-    glNewList(dlist, GL_COMPILE);
-    err = FT_Outline_Decompose(&slot->outline, &GlftFuncs, NULL);
-    g_assert(!err);
-    if (state.drawing)
-        glEnd();
-    glEndList();
+    err = FT_Render_Glyph( slot, ft_render_mode_normal );
+        g_assert(!err);
+    printf("bitmap with dims %d, %d\n", slot->bitmap.rows, 
+           slot->bitmap.width);
+    while (padx < slot->bitmap.width)
+       padx <<= 1;
+    while (pady < slot->bitmap.rows)
+       pady <<= 1;
+    printf("padding to %d, %d\n", padx, pady);
+    padbuf = g_new(unsigned char, padx * pady);
+    for (i = 0; i < slot->bitmap.rows; i++)
+        memcpy(padbuf + i*padx,
+               slot->bitmap.buffer + i*slot->bitmap.width,
+               slot->bitmap.width);
+    glBindTexture(GL_TEXTURE_2D, tnum);
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, padx, pady,
+                 0, GL_GREEN, GL_UNSIGNED_BYTE, padbuf);
+
+    g_free(padbuf);
 }
 
 void GlftRenderString(struct GlftFont *font, const char *str, int bytes,
@@ -105,7 +60,22 @@ void GlftRenderString(struct GlftFont *font, const char *str, int bytes,
         g = GlftFontGlyph(font, c);
         if (g) {
             glTranslatef(GlftFontAdvance(font, p, g), 0.0, 0.0);
-            glCallList(g->dlist);
+            glBindTexture(GL_TEXTURE_2D, g->tnum);
+            glBegin(GL_QUADS);
+            glColor3f(1.0, 1.0, 1.0);
+
+            glTexCoord2i(0, 1);
+            glVertex2i(g->x, g->y);
+
+            glTexCoord2i(1, 1);
+            glVertex2i(g->x + g->width, g->y);
+
+            glTexCoord2i(1, 0);
+            glVertex2i(g->x + g->width ,g->y + g->height);
+
+            glTexCoord2i(0, 0);
+            glVertex2i(g->x, g->y + g->height);
+            glEnd();
         } else
             glTranslatef(font->max_advance_width, 0.0, 0.0);
         p = g;
index 9a3c1518582e06f30c3b97661d769107e3837a40..191a53b9f5d74bd852588c17b4e534868ef8a290 100644 (file)
@@ -4,6 +4,6 @@
 #include <ft2build.h>
 #include FT_FREETYPE_H
 
-void GlftRenderGlyph(FT_Face face, unsigned int dlist);
+void GlftRenderGlyph(FT_Face face, unsigned int tnum);
 
 #endif
index 1665c6b7423916beb4ebf504608b4fa41408f55b..1000dfd09f2464cbfd0baa01e0c94186f9c9109f 100644 (file)
@@ -85,6 +85,7 @@ int main(int argc, char **argv)
     glLoadIdentity();
     glOrtho(0, W, -100, H-100, 0, 10);
     glMatrixMode(GL_MODELVIEW);
+    glEnable(GL_TEXTURE_2D);
     quit = 0;
     while (!quit) {
         XNextEvent(display, &report);