I am working on a Minecraft mod where I am rendering custom text using OpenGL. However, the text is not rendered fully vertically and is cut to 2/3rd of its height. I suspect it might be a clipping issue or something related to the font metrics, but I am not sure how to diagnose and fix this problem. Here is the relevant code for the FontCharacter
class and the rendering method:
package keystrokesmod.utility.font.impl;
import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.opengl.GL11;
public class FontCharacter {
private final int texture;
private final float width;
private final float height;
public FontCharacter(int texture, float width, float height) {
this.texture = texture;
this.width = width;
this.height = height;
}
public void render(final float x, final float y) {
GlStateManager.bindTexture(texture);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x, y + height);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x + width, y + height);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x + width, y);
GL11.glEnd();
}
public int getTexture() {
return texture;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}
My usage in the other file
public int drawString(String text, double x, double y, final int color, final boolean shadow) {
if (!this.international && this.requiresInternationalFont(text)) {
return FontManager.getInternational(this.font.getSize() - 1).drawString(text, x, y, color);
}
final FontCharacter[] characterSet = this.international ? internationalCharacters : defaultCharacters;
double givenX = x;
GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glScalef(SCALE, SCALE, SCALE);
x -= MARGIN_WIDTH / SCALE_INVERSE;
y -= MARGIN_WIDTH / SCALE_INVERSE;
x *= SCALE_INVERSE;
y *= SCALE_INVERSE;
y -= fontHeight / 5;
final double startX = x;
final int length = text.length();
glColor(shadow ? 50 : color);
for (int i = 0; i < length; ++i) {
final char character = text.charAt(i);
try {
if (character == 'n') {
x = startX;
y += height() * 2;
continue;
}
final FontCharacter fontCharacter = characterSet[character];
fontCharacter.render((float) x, (float) y);
x += fontCharacter.getWidth() - MARGIN_WIDTH * 2;
} catch (Exception exception) {
System.out.println("Character "" + character + "" was out of bounds " +
"(" + ((int) character) + " out of bounds for " + characterSet.length + ")");
exception.printStackTrace();
}
}
GL11.glDisable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GlStateManager.bindTexture(0);
GL11.glPopAttrib();
GL11.glPopMatrix();
return (int) (x - givenX);
}
I have tried checking the font metrics and ensuring that the height of the character is correctly set and used in rendering. I expected the text to be rendered fully vertically without being cut off.
1