opengl – LWJGL 2 GUI / Text

LWJGL 3 has stb_true_type. You can generate a texture atlas and the mesh for each character.
I'm using it with my engine, it requires a projection matrix with the top left origin.

The change to LWJGL 3 will require you to download the jars lwjgl.org files or if you are using Maven or other construction tools to generate a project from there.

To generate the font atlas.

this.fontSize = fontSize;
this.bitmapWidth = bitmapWidth;
this.bitmapHeight = bitmapHeight;
try {
cdata = STBTTBakedChar.malloc (CHAR_AMT);

ByteBuffer ttf = FileLoader.loadResurce (fontFile);

STBTTFontinfo info = STBTTFontinfo.create ();

if (! stbtt_InitFont (info, ttf)) {
launch a new IllegalStateException ("Error initializing the source information");
}
ByteBuffer bitmap = BufferUtils.createByteBuffer (bitmapWidth * bitmapHeight);
stbtt_BakeFontBitmap (ttf, fontSize, bitmap, bitmapWidth, bitmapHeight, 0, cdata);

glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
textureId = glGenTextures ();
glBindTexture (GL_TEXTURE_2D, textureId);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D (GL_TEXTURE_2D, 0, GL11.GL_RED, bitmapWidth, bitmapHeight, 0, GL11.GL_RED, GL_UNSIGNED_BYTE, bitmap);
glPixelStorei (GL_UNPACK_ALIGNMENT, 4);

} capture (IOException e) {
e.printStackTrace ();
}

To generate the mesh of the chain.

    int count = text.length () * 4;
int verticesId = glGenBuffers ();
int textureCoordinates = glGenBuffers ();
try (MemoryStack stack = MemoryStack.stackPush ()) {

FloatBuffer vert = stack.mallocFloat (text.length () * 8);
FloatBuffer tex = stack.mallocFloat (text.length () * 8);

flotation length = 0;
for (char c: text.toCharArray ()) {
STBTTAlignedQuad stbQuad = STBTTAlignedQuad.create ();
float[] x = new float[1];
float[] y = new float[1];
stbtt_GetBakedQuad (cdata, bitmapWidth, bitmapHeight, c, x, y, stbQuad, true);

float[] vertices = new float[]{
length + stbQuad.x0 (), stbQuad.y0 (),
length + stbQuad.x1 (), stbQuad.y0 (),
length + stbQuad.x1 (), stbQuad.y1 (),
length + stbQuad.x0 (), stbQuad.y1 ()
};

float[] textures = new float[]{
stbQuad.s0 (), stbQuad.t0 (),
stbQuad.s1 (), stbQuad.t0 (),
stbQuad.s1 (), stbQuad.t1 (),
stbQuad.s0 (), stbQuad.t1 ()
};
tex.put (textures);
vert.put (vertices);
length + = x[0];
}

vert.flip ();
tex.flip ();

glBindBuffer (GL_ARRAY_BUFFER, verticesId);
glBufferData (GL_ARRAY_BUFFER, vert, GL_STATIC_DRAW);

glBindBuffer (GL_ARRAY_BUFFER, textureCoordinates);
glBufferData (GL_ARRAY_BUFFER, tex, GL_STATIC_DRAW);
glBindBuffer (GL_ARRAY_BUFFER, 0);