If you draw misaligned text, you should see much more rendering artifacts than in the previous tutorial. Make it so you can pass a pointer to such as struct to render_text(). Make a struct atlas that holds the texture and cache for a given font and font size, so that you do not have any global variables anymore.We add an optimisation to skip drawing glyphs that have zero width and/or height, such as the space character.Īfter the loop has filled in our vertex buffer, we just need to upload it to the graphics card, and tell it to render all the triangles. In the loop, we perform the same calculations as before, but using the cached glyph metrics. We need 6 points for each character we will render, since we have to use GL_TRIANGLES instead of a GL_TRIANGLE_STRIP to be able to render separate quads. Void render_text ( const char * text, float x, float y, float sx, float sy ) įirst we define a struct to hold the vertex and texture coordinates for each point, to make it slightly easier to write the code to fill in all the values. We will put all characters next to each other in a single row.īefore we can create the atlas itself though, we need to know the combined width of all the glyph images, and the height of the tallest glyph.Īssuming we have already initialized FreeType, have loaded a font, and set the font size, it is a simple matter: We will use a very simple method in this tutorial: This is true even when using monospaced fonts!Īlthough there are various methods to efficiently pack a set of arbitrarily sized rectangles, However, we have already seen that the glyph images that FreeType produces have wildly varying sizes. If all the subimages have the same size, it is rather easy to create a tightly packed atlas. However, this means we can only draw a quad at a time, and if we have a lot of text that means a lot of OpenGL calls.Ī slightly more complex way is to have a single, large texture that contains all the characters,Īnd to properly set the texture coordinates for each quad so that the right character will be rendered.Ĭreating a texture atlas Ī texture atlas is basically a big texture which contains many small images that are packed together. It would be much better to have all possible character images stored permanently on the graphics card.Ī simple way to do this is to have many textures, one for each character, so that we just have to switch between textures when drawing a quad. In the first text rendering tutorial, we were uploading a new texture to the graphics card for each character that we drew.
0 Comments
Leave a Reply. |