Rendering text

Started by
8 comments, last by Zotoaster 10 years, 5 months ago

I've been working on a GUI system because why not. I couldn't get Qt to compile for me despite all my efforts, and I happened to have a decent GUI system that worked for Windows, and it had the graphics abstracted out, so I thought I'd replace it with OpenGL so I can work it from my Mac. I've never used OpenGL before but I've got the form rendering working fine, but I was a little surprised at how complicated it can be to get any text up. I followed the NeHe tutorial on text rendering using FreeType (http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/ ). The first problem was that for some reason text positioned at 0,0 was rendering at the bottom left. I'm sure I've made some mistake with some matrix transformation somewhere but my understanding of them is still lacking so I fixed it by subtracting the Y coord from the screen height. More importantly, the text is really blurry. Attached is a screenshot for reference.

I am willing to show my code for the text and general rendering if anyone wants to look at it. Ideally I'd like a plug-and-play library with a text(x, y, string) method so I can get on with my engine and finish with this story.

Thanks

Advertisement

http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01

I'm using one of the techniques described in 3TATUK2's link - I basically make a quad for each letter. I support multiple fonts, where fonts are stored as distance field map, and the images are described in AngelCode's format http://www.angelcode.com/products/bmfont/

For more distance field and fonts usage - see http://code.google.com/p/libgdx/wiki/DistanceFieldFonts

My current blog on programming, linux and stuff - http://gameprogrammerdiary.blogspot.com


The first problem was that for some reason text positioned at 0,0 was rendering at the bottom left

In the OpenGL convention, 0,0 is the bottom left. The Y coordinate subtraction from the height is the typical practice to place the coordinate into window space.

I'm looking at 3TATUK2's link. Is it possible to do it without using shaders? My current system doesn't need them and I'd rather have fewer dependencies.

I've based my font rendering on this: http://code.google.com/p/freetype-gl/

--Z

Simply + Unicode support: https://github.com/SC0x1/XFontSys smile.png

I've got my text rendering properly. I have a small request. My code is based on this: http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/

Does anyone know how to get the width of a string using this?

FreeType is explained in 3 tutorials. The first one shows how to render bitmaps, how to determine the size of the bitmaps, and how to determine the advance when rendering the text (see especially section "7. Simple text rendering" in that tutorial). You can find the corresponding part in line 40 of one of the NeHe listing, namely

glTranslatef(face->glyph->advance.x >> 6 ,0,0);

This line extracts the horizontal advance, which is a 26.6 fixed point number (again, see the FreeType documentation), throws the decimal places away, and builds a translation from that. Obviously it assume a horizontal scripting system.

However, for your use case you want to sum up the advances for all characters of your string (for the last perhaps just the width).

The problem is that after the 'init' method, the 'face' is discarded so I don't have access to it later on. It seems the data is stored somewhere in these lists, but I'm new to OpenGL and I don't have a clue what they're about.

This topic is closed to new replies.

Advertisement