Search code examples
javaopengllwjgl

Lwjgl Font Upside down


My rendered text appears upside down. I understand that the bottom left corner is position (0,0) and the entirety of my code is based around this. I have a feeling that the reason my text is rendered upside down is because I am using java's awt font class instead of newdawn.slick.Font. Is there a simple way to accomplish flipping is over? I tried using

glScalef(1, -1, 1);

to flip it however that cause everything to stop rendering.

Code below

import static org.lwjgl.opengl.GL11.*;
import java.awt.Font;

import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;

public class FontRenderer {
    private TrueTypeFont font;

    public FontRenderer(){
        Font awtFont = new Font("Times New Roman", java.awt.Font.PLAIN, 24);
        font = new TrueTypeFont(awtFont, false);
    }

    public void render(float x, float y, String text){
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        font.drawString(x, y, text);
    }

    public void render(float x, float y, String text, float r, float g, float b)
    {
        Color color = new Color(r, g, b);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        font.drawString(x, y, text, color);
    }
}

Solution

  • Your fonts are drawing upside down because Slick uses a origin in the top left corner, whereas by default LWJGL uses a origin in the bottom left. You stated that you know your origin is in the bottom left, so why would you expect your rendering code to work?