[cairo] trouble with Cairo text in OpenGL

Victor henri nadaeck at hotmail.com
Wed Jan 4 06:37:31 PST 2012





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/fcfa2b53/attachment.htm>


More information about the cairo mailing list