How to do OpenGL live text-rendering for a GUI?

后端 未结 7 1942
面向向阳花
面向向阳花 2020-12-04 08:12

I\'m implementing a GUI built on top of OpenGL. I came to the problem that each GUI will have -- text rendering. I know of several methods of rendering text in OpenGL, howev

相关标签:
7条回答
  • 2020-12-04 08:20

    It is tricky to do, especially if you want to use subpixel font rendering techniques. Have a look at the ClanLib SDK. It uses a batch renderer to render an entire screen of text in a single OpenGL call. Since it has a zlib based license, you can extract its code if you don't wish to use the SDK itself

    0 讨论(0)
  • 2020-12-04 08:21

    You can render the text as triangulated faces, and avoid using a texture. This way you avoid unsharp edges when the text is zoomed. I authored a low poly ASCII font that is open sourced and available at my github.

    Another way to have sharp edges on your text is SDF font rendering, but this suffers from some artefacts.

    0 讨论(0)
  • 2020-12-04 08:30

    Try reading this: http://dmedia.dprogramming.com/?n=Tutorials.TextRendering1.

    The approach described is the typical method for text rendering with graphics APIs. One character per quad, and all of the image data for text in a single texture. If you can fit your entire character set into one texture (actually, depending on the size of the texture, you might be able to fit several) the rendering is extremely fast.

    The key is that texture binding is the operation you need to minimize. If you can render all of your text with a single texture, it won't matter how much text you need to put on the screen. Even if you have to switch textures a handful of times (different fonts, different font attributes [bold, underline, etc]) performance should still be good. Text performance might be more critical for a HUD, but it less important in the GUI.

    0 讨论(0)
  • 2020-12-04 08:38

    freetype-gl

    https://github.com/rougier/freetype-gl is a library that integrates freetype and OpenGL.

    imagine console

    See console: https://github.com/rougier/freetype-gl/blob/a4cfb9abac19a0ab62b625a9b6f856e032fe3732/demos/console.c

    How to get it to run on Ubuntu 15.10: https://github.com/rougier/freetype-gl/issues/82#issuecomment-216025527

    More details at: How to draw text using only OpenGL methods?

    Urho3D ConsoleInput.cpp

    https://github.com/urho3d/Urho3D/blob/6b63f20065558cff1842bc8e1e3c6ee11f4bf577/Source/Samples/26_ConsoleInput/ConsoleInput.cpp

    0 讨论(0)
  • 2020-12-04 08:38

    Per quad character handled thought a display list (updated only when text changes) is quite enough for most cases.

    I used it with X11 fonts using the XLoadQueryFont, glGenLists, glXUseXFont, glCallLists you have an array of display lists describing each character.

    the glCallLists function accepts a char * argument for your text and it can be embedded inside a display list.

    So you end up with a single call to display blocks text.

    The font texture suggested by G. Pakosz is similar, but you compute your own "by character" display lists.

    Your first options will be quite slower as it will require processor based rendering at each text change.

    0 讨论(0)
  • 2020-12-04 08:40

    EDIT2: Sean Barrett just released Bitmap fonts for C/C++ 3D programmers.

    EDIT: another code gem that's worth a look is Font Stash which leverages Sean Barrett's stb_truetype.h.


    You can create a texture in which you render all the characters of your font. Then you just draw textured quads with orthographic projection and proper texture coordinates: this approach works when your text is in a language that doesn't contain much symbols: like english. The font texture is created once at the beginning of the application and then rendering quads is really fast.

    That's what I'm using and I believe it's the fastest way to render text in OpenGL. At first, I used Angelcode's Bitmap Font Generator tool and then I integrated FreeType directly and built a big texture containing all the glyphs at application launch. As for tips to improve the quads rendering speed, I just used VBO as for the rest of the geometry in my application.

    I'm surprised you have doubts about quad rendering while you don't seem to worry about the performance of generating a texture on the cpu, then uploading it, then binding it to finally render a rectangle with it, that for each frame. Changing OpenGL states is the bottleneck, not the 500 - 1000 quads you'll use for text.

    Also, have a look at libraries like the FTGL library who converts the whole font into polygons (geometric fonts).

    0 讨论(0)
提交回复
热议问题