# LWJGL Text Rendering

Currently in my project I am using LWJGL and the Slick2D library to render text onto the screen. Here is a more specific example:

    Font f = new Font("Times New Roman",Font.BOLD,18);
font = new UnicodeFont(f);
font.getEffects().add(new ColorEffect(Color.white));
font.addAsciiGlyphs();
try {
font.loadGlyphs();
} catch (SlickException e) {
e.printStackTrace();
}


then i use font.drawString to write onto the screen. This is a quick easy way but it has a lot of disadvantages. for example font.loadGlyphs take a very long time 1-3 seconds. so when i want to change a color or font type then i have to wait 1-3 seconds which means I cannot do it while rendering (ie. cant have different color text on the same screen).

My question is what is a better way of drawing multicolored text onto the screen? I use slick2d only for the text rendering so maybe i can fully get rid of the library and draw text some other way...

If you have an answer please leave a quick short example.

Thanks!

## 2 Answers

I can't give you a short example using LWJGL alone, but what I can do is give you an way to solve your problem.

As I understand, you want to load one font at rendering time, and display it right away with multiple colours. My suggestion is to use Bitmap Fonts for this. Just use a tool (Example) to generate the bitmap font texture file and the textual file with the glyphs information, and load it to your engine.

The change of colours than becomes very easy, because all you have to do is generate white bitmap fonts (with transparent background), and draw the font texture using OpenGL to change its colours (like glColor3f) :)

*Edit: No need for OpenGL shaders for this. Also, transparent background for the font is needed.

• One example of this using libGDX can be found in this link: link. libGDX uses LWJGL for its desktop version, you can check it out if this solutions interests you. Nov 1, 2012 at 23:06
• I implemented the above solution (AranHase) Can you just give me a link to a shader example of shading the texture using opengl? Nov 2, 2012 at 7:01
• @Trixmix if you use the fixed rendering pipeline, you may read this link. All you have to do is set your vertices color with glColor3f() and draw your glyphs as usual. Nov 2, 2012 at 13:36
• @Trixmix I edited my answer, add two main things. Shaders are not actually needed, sorry for that. And, a small touch: font texture background must be transparent. Nov 2, 2012 at 14:10

Remember to switch depth buffer off when drawing your font. Had same problem and this helped.

GL11.glDisable(GL11.GL_DEPTH_TEST);
// font drawing code here
GL11.glEnable(GL11.GL_DEPTH_TEST);