drmgc
drmgc

Reputation: 171

LWJGL: Slick: Drawing fonts in 3D world

How to draw Slick's fonts (UnicodeFont), which can be drawn only via drawString(), which provides only x and y, in a 3D world?

I have tried this:

public void drawHudString(float _posX, float _posY, String _text, UnicodeFont _font, Color _color) {
    glPushMatrix();
        glRotatef(cam.rotX, -1.0f, 0.0f, 0.0f);
        glRotatef(cam.rotY, 0.0f, -1.0f, 0.0f);
        glRotatef(cam.rotZ, 0.0f, 0.0f, -1.0f);
        glTranslatef(-cam.posX, -cam.posY, -cam.posZ + 20);

        _font.drawString(_posX, _posY, _text, _color);
    glPopMatrix();
}

but the text was not displayex. If used without glRotatefs and glTranslatef, then the text will be rendered by 3D-world coords

Upvotes: 1

Views: 942

Answers (2)

bluenote10
bluenote10

Reputation: 26640

The rendering of fonts with Slick is based on OpenGL's immediate mode. This means that the behavior of drawString is determined by the current state, or in other words, the current GL_PROJECTION and GL_MODELVIEW matrices.

Your question does not really make it clear whether you want to draw text as a 2D overlay (probably in screen coordinates) or truly perspective embedded in 3d space. Nevertheless, you can achieve both with drawString. drawString renders the font texture at the specified x/y coordinates in the z=0 plane. Iirc the convention in drawString is to assume a left-handed coordinate system and the texture is visible from the negative z-side. The reason for this convention is probably that OpenGL uses a left-handed coordinate system for window space, while using a right-handed for world/object space (see this answer for a good explanation). As a result, rendering in 2D is straightforward with Slick: Set up a typical orthogonal projection matrix (as suggested by ryuyah2000) and you are good to go.

If you want to render in 3D space, you instead keep your regular perspective projection matrix (i.e., you are using the same projection you use for rendering your world). In order to control the position of the text in 3D space you have to set up the modelview matrix accordingly (i.e., aligning the z=0 plane in the plane where you want to render your text). Due to the left-hand and z-visibility conventions you may have to rotate your z-axis by 180° and have to invert the handedness (just scale your modelview by -1). In case you get one of these steps wrong, your text is either not visible (= looking to wrong z-side) or is written right-to-left (= wrong handedness). Slick's drawString method uses a scaling of 1 unit = 1 pixel of the font texture, so you have to apply an appropriate scaling to match that to your world units.

Upvotes: 2

ryuyah2000
ryuyah2000

Reputation: 31

use this

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 800, 600, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST); 
glClear(GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// render font

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

Upvotes: 0

Related Questions