[cairo] trouble with Cairo text in OpenGL

Henry (Yu) Song - SISA hsong at sisa.samsung.com
Wed Jan 4 09:10:34 PST 2012


Hi, Vector,

If you use cairo/gl backend, there is no need to make GL calls directly.

For example

/* create a gl surface */
cairo_surface_t *surface = cairo_gl_surface_create_for_window (....);

cairo_t *cr = cairo_create (surface);

/* draw text on gl surface */
cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL,  CAIRO_FONT_WEIGHT_BOLD);
 cairo_set_font_size (cr, 32.0);
 cairo_set_source_rgb (cr, 1.0, 1.0, 0.0);
 cairo_show_text (cr, "HELLO");

/* stroke a rectangle on gl surface */
cairo_rectangle (cr, 10, 10, 100, 100);
cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 1.0);
cairo_stroke (cr);

/* display gl surface on screen */
cairo_gl_surface_swapbuffers (surface);

/* clean up */
cairo_surface_destroy (surface);
cairo_destroy (cr);


Hope this helps

Henry

From: cairo-bounces+hsong=sisa.samsung.com at cairographics.org [mailto:cairo-bounces+hsong=sisa.samsung.com at cairographics.org] On Behalf Of Victor henri
Sent: Wednesday, January 04, 2012 6:38 AM
To: cairo at cairographics.org
Subject: [cairo] trouble with Cairo text in OpenGL

Hello

I have trouble drawing text in an OpenGL texture with Cairo

It works with SDL and SDL_ttf with :

    GLuint textureId;

        SDL_Color Color = {150, 0, 240};
        SDL_Surface *Message = TTF_RenderText_Blended(font, "Hello!", Color);
        unsigned Texture = 0;

        glGenTextures(1, &Texture);
        glBindTexture(GL_TEXTURE_2D, Texture);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Message->w, Message->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, Message->pixels);

        glBegin(GL_QUADS);
        glColor4f(1, 0, 0, 1);
        glTexCoord2d(0, 0); glVertex3d(0, 0, 0);
        glTexCoord2d(1, 0); glVertex3d(0.2, 0, 0);
        glTexCoord2d(1, 1); glVertex3d(0.2, -0.2, 0);
        glTexCoord2d(0, 1); glVertex3d(0,  -0.2, 0);
        glEnd();

        glDeleteTextures(1, &Texture);

        SDL_FreeSurface(Message);

Clearly, the Message->pixels argument in "glTexImage2D" seems important; if I replace it with NULL, I get random lines and points.

If I do this with Cairo :

        cairo_surface_t *surface;
        cairo_t *cr;
        GLuint textureId;
        unsigned char* surfaceData;

        surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 64, 64);
        cr = cairo_create (surface);
        cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL,  CAIRO_FONT_WEIGHT_BOLD);
        cairo_set_font_size (cr, 32.0);
        cairo_set_source_rgb (cr, 1.0, 1.0, 0.0);
        cairo_show_text (cr, "HELLO");

        surfaceData = cairo_image_surface_get_data (surface);

        unsigned Texture = 0;

        glGenTextures(1, &Texture);
        glBindTexture(GL_TEXTURE_2D, Texture);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, cairo_image_surface_get_width (surface), cairo_image_surface_get_height (surface), 0, GL_BGRA, GL_UNSIGNED_BYTE, surfaceData);

        glBegin(GL_QUADS);
        glColor4f(1, 0, 0, 1);
       glTexCoord2d(0, 0); glVertex3d(0, 0, 0);
        glTexCoord2d(1, 0); glVertex3d(0.2, 0, 0);
        glTexCoord2d(1, 1); glVertex3d(0.2, -0.2, 0);
        glTexCoord2d(0, 1); glVertex3d(0,  -0.2, 0);
        glEnd();

        glDeleteTextures(1, &Texture);
        cairo_destroy (cr);
        cairo_surface_destroy (surface);



If I do that I get a few red points but not the text I want. I use the " cairo_image_surface_get_data (surface)" function to get the appropriate data and to give it to glTextImage2D but I suppose this is where it won't work.

In some examples I have come across the "create_cairo_context" through which surfaceData can be taken. But this function seems completely unknown on my system...
I am using Ubuntu Linux 11.10 (lateste version)

Please could give me some help about this?

Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20120104/7aba0f4e/attachment-0001.htm>


More information about the cairo mailing list