[SOLVED]Rendering font with LWJGL

I am having some trouble rendering fonts with LWJGL, i have a 128x128 png with all the letters drawn out, my current code is

package com.bomberBox.game.screen;

import org.lwjgl.opengl.GL11;

import com.bomberBox.game.TextureHandler;

public class Font {
	private static final String chars = "" + //
			"ABCDEFGHIJ" +//
			"KLMNOPQRST" +//
			"UVWXYZabcd" +//
			"efghijklmn" +//
			"opqrstuvwx" +//
			"yz/#:     " +//
			"0123456789";


	
	public static void draw(String string, int x, int y, float r, float g, float b, float a) {
		for (int i = 0; i < string.length(); i++) {
			int ch = chars.indexOf(string.charAt(i));
			if (ch < 0) continue;

			int xx = (ch % 10) * 12;
			int yy = (ch / 10) * 16;
			int xd = x + i * 12;
	        GL11.glPushMatrix();
			GL11.glColor4f(r, g, b, a);
			TextureHandler.font.bind();
	        GL11.glBegin(GL11.GL_QUADS);
	        GL11.glTexCoord2f(xx, yy); GL11.glVertex2f(xd, y);
	        GL11.glTexCoord2f(xx + 10, yy); GL11.glVertex2f(xd+20, y);
	        GL11.glTexCoord2f(xx + 10, yy + 16); GL11.glVertex2f(xd+20, y+32);
	        GL11.glTexCoord2f(xx, yy + 16); GL11.glVertex2f(xd, y+32); 
	        GL11.glEnd();
	        GL11.glPopMatrix();
		}
	}
}

It renders some white bars but not the font or words… I am struggling to see where i’ve gone wrong…

Texcoords are in the range 0…1 :point:

You forgot


glEnable(GL_TEXTURE_2D); 
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

Cas :slight_smile:

Thanks, completely forgot that, fixed it now

	public static void draw(String string, int x, int y, float r, float g, float b, float a) {
		for (int i = 0; i < string.length(); i++) {
			int ch = chars.indexOf(string.charAt(i));
			if (ch < 0) continue;

			int xx = (ch % 10) * 12;
			int yy = (ch / 10) * 16;
			int xd = x + i * 12;
	        GL11.glPushMatrix();
			GL11.glColor4f(r, g, b, a);
			TextureHandler.font.bind();
	        GL11.glBegin(GL11.GL_QUADS);
	        float left = (float) (1.0*(xx/128.0));
	        float right = (float) (1.0*((xx+10)/128.0));
	        float top = (float) (1.0*(yy/128.0));
	        float bottom = (float) (1.0*((yy+16)/128.0));
	        GL11.glTexCoord2f(left, top); GL11.glVertex2f(xd, y);
	        GL11.glTexCoord2f(right, top); GL11.glVertex2f(xd+10, y);
	        GL11.glTexCoord2f(right, bottom); GL11.glVertex2f(xd+10, y+16);
	        GL11.glTexCoord2f(left, bottom); GL11.glVertex2f(xd, y+16); 
	        GL11.glEnd();
	        GL11.glPopMatrix();
		}
	}

Performance tips:
[]Reduce the calls to glBegin/glEnd by only calling it once outside your loop. If you use it for every character in a string, you will very quickly run into performance problems.
[
]Same for binding textures; no point in doing them every iteration. Instead; call it once at the beginning of your method and (if you don’t already) cache the last bound texture to minimize redundant calls to glBindTexture.
[]Ideally use vertex arrays or VBOs to further improve performance.
[
]Don’t push/pop matrices when it’s not necessary.