Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you render 2D text in lwjgl?

Before you go off on me for "not searching", I've searched around a LOT and found a lot of answers, but none of them worked for me.

Hi, I'm making a 3D java lwjgl game, and I'm trying to render some simple 2D text on my screen in the middle, but no matter what I try, I never see any text rendered onto my screen anywhere. Here's my Text2D.java class:

package com.gmail.br45entei.base.engine.rendering;

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;

import org.newdawn.slick.Color;

import org.newdawn.slick.UnicodeFont;

import com.gmail.br45entei.base.engine.core.Vector2f;
import com.gmail.br45entei.base.game.Game;
import com.gmail.br45entei.base.game.MainComponent;
import com.gmail.br45entei.file.FileMgmt;

/**
 * @author Brian_Entei
 *
 */
public class Text3D {

    private Camera camera;

    private UnicodeFont font;
    private Color color;
    private Vector2f position;
    private String text = "New string";
    private int size = 12;

    public int getSize() {
        return this.size;
    }

    public void setSize(int size) {
        this.size = size;
        updateFont();
    }

    private void updateFont() {
        try {
            this.font = new UnicodeFont(Font.createFont(Font.TRUETYPE_FONT, FileMgmt.getFile(MainComponent.resourceFolder, "/fonts/BrianOwenReidBold.ttf", false)), size, false, false);
        } catch (FontFormatException e) {
            FileMgmt.LogException(e, "updateFont()", "Failed to create font from file!", false);
        } catch (IOException e) {
            FileMgmt.LogException(e, "Text3D()", "Failed to load file to create font!", false);
        }
    }

    public Text3D(String text, Camera camera) {
        this.camera = camera;
        this.color = Color.black;
        this.position = new Vector2f(0, 0);
        this.text = text;
        updateFont();
    }

    public void drawHudString() {
        //org.lwjgl.opengl.GL11.glPushMatrix();// This gets called right before here.
        org.lwjgl.opengl.GL11.glRotatef(camera.getForward().getX(), -1.0f, 0.0f, 0.0f);
        org.lwjgl.opengl.GL11.glRotatef(camera.getForward().getY(), 0.0f, -1.0f, 0.0f);
        org.lwjgl.opengl.GL11.glRotatef(camera.getForward().getZ(), 0.0f, 0.0f, -1.0f);
        org.lwjgl.opengl.GL11.glTranslatef(-camera.getPos().getX(), -camera.getPos().getY(), -camera.getPos().getZ() + 20);

        this.font.drawString(this.position.getX(), this.position.getY(), this.text, this.color);
        //org.lwjgl.opengl.GL11.glPopMatrix();// This gets called right after here.
    }

    public void render() {
        Game.print("Text render!");
        org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_PROJECTION);
        org.lwjgl.opengl.GL11.glPushMatrix();
        org.lwjgl.opengl.GL11.glLoadIdentity();
        org.lwjgl.opengl.GL11.glOrtho(0, 800, 600, 0, 1, -1);
        org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_MODELVIEW);
        org.lwjgl.opengl.GL11.glDisable(org.lwjgl.opengl.GL11.GL_CULL_FACE);
        org.lwjgl.opengl.GL11.glDisable(org.lwjgl.opengl.GL11.GL_DEPTH_TEST); 
        org.lwjgl.opengl.GL11.glClear(org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT);
        org.lwjgl.opengl.GL11.glLoadIdentity();

        // render font
        this.drawHudString();

        org.lwjgl.opengl.GL11.glEnable(org.lwjgl.opengl.GL11.GL_DEPTH_TEST);
        org.lwjgl.opengl.GL11.glEnable(org.lwjgl.opengl.GL11.GL_CULL_FACE);
        org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_PROJECTION);
        org.lwjgl.opengl.GL11.glPopMatrix();
        org.lwjgl.opengl.GL11.glMatrixMode(org.lwjgl.opengl.GL11.GL_MODELVIEW);
    }
}

The render function in this class is called after my 3D objects are rendered. I hope someone can help me out with this!

like image 865
Brian_Entei Avatar asked Feb 22 '14 21:02

Brian_Entei


1 Answers

You have already selected the answer above as solution but you said it doesn't work. The solution is scaling it by 0.01f or something like that depending on your setup. To scale use simply glScale(x,y,z). Also you habe to enable Blending like this:

glEnable(GL11.GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
like image 110
HenrikD Avatar answered Oct 01 '22 12:10

HenrikD