Search code examples
pythonopenglutf-8pyopengl

OpenGL utf-8 string rendering


Is there a python module that can display UTF-8 string in OpenGL?

I found pyFTGL but when I run the following code:

    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, [1, 1, 1, 1])
    font = FTGL.PolygonFont("NeoTechStd-Medium.ttf")
    font.FaceSize(8)
    font.line_height
    font.Render("Angle = ? \u03C4")

I get this error:

    font.Render(u"Weld Head Angle = ? \u03C4")
Boost.Python.ArgumentError: Python argument types in
    PolygonFont.Render(PolygonFont, unicode)
did not match C++ signature:
    Render(FontWrapper<FTPolygonFont> {lvalue}, char const*, double)
    Render(FontWrapper<FTPolygonFont> {lvalue}, char const*)
DEBUG:Helpers.opengl_pipe:Redrawing contents of GLArea.

Solution

  • You are not passing the Render method UTF-8; you are passing it a unicode literal.

    You'll need to encode it to UTF-8:

    font.Render("Angle = ? \u03C4".encode('utf8'))
    

    I can heartily recommend you read this article on the subject of Unicode and encodings to help understand the difference.