Text output in GtkGLArea


I am pretty new to GTK and OpenGL programming. I am looking
for a simple example in C to output text in a GtkGLArea window
using Pango. I found the example from GTK3-demo to output a triangle,
but could not find one for simple text output.



There is no “text output” in GL, and there’s no “simple” when it comes to text rendering either.

As a general approach, you can show pre-rendered text from assets—this is how games typically work. Alternatively, you can render text on a pixel buffer and then upload it as a GL texture.

To implement the latter approach, you will need to call gtk_widget_create_pango_layout() and let Pango handle the text shaping and layout; in order to draw the text, you will need to create a Cairo image surface, then render the PangoLayout into it by creating a Cairo context from the surface, then calling pango_cairo_show_layout(), and then destroying the Cairo context; you can also use Cairo API to render the text using different colors, or backgrounds, or add effects to it. After that, you will need to take the contents of the image surface and upload them as a GL 2D texture. Cairo uses an ARGB format for its data, so you will need to convert it to a format that GL handles, like RGBA; to do that you can either tweak the pixel buffer directly, or use a GLSL fragment shader program to swizzle the color channels on the GPU.

1 Like

@ebassi Thanks, I will try your second suggestion.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.