Displaying Text in OpenGL

Started by chrizo, February 06, 2008, 15:59:46

Previous topic - Next topic

Schnitter

Question:
I'm using the Slick-Code like this:
TrueTypeFont ttf = new TrueTypeFont(new Font("Arial", Font.BOLD, 50), true);
[...]
And in the render-method:
ttf.drawString(500, 500, "Hello World :)");

But the only thing I get is this:
http://planschkuh.pl.ohost.de/errorScreen1.jpg

Where's the  problem? I couldn't find any tutorial for the slick-code :(

Matzon

the TestUtils (org.newdawn.slick.tests.TestUtils) does the following:

        java.awt.Font awtFont = new Font("Times New Roman", 1, 16);
        org.newdawn.slick.Font font = new TrueTypeFont(awtFont, false);
        ...
        font.drawString(150F, 300F, "HELLO LWJGL WORLD", Color.yellow);


mind you, it does have some init code that may or may not be relevant.

I am not entirely sure where the source code to the slick-util package is ...

Schnitter

Mh, I looked into the code and it works now!

I think it was the "Arial"-Font. I don't know why it didn't work, but with "Times New Roman" it works ;)

Thanks!

Se7enDays

Hmm, i also tried the Slick demo. It works. But when i try to create a font and draw it in my lwjgl program it only shows a black screen. Can someone help me? How does you code look like, Schnitter?

Schnitter

I get it to work like this: http://nopaste.info/24ab6d6807.html

But I got another - huge - problem! :/
I want my 0/0 point in the bottom left corner. but because of
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();

It is in the upper left corner!(I think it's because of this).
And when I don't write this code - the text is the wrong way round!
I think I could solve the problem by compiling the slick-code myself - but that would be soooo much work :(

Matthias

If you would setup a real projection matrix - eg using glOrtho it would be much easier :)

Schnitter

Can anybody tell me how to set up a projection matrix correctly? :S

bobjob

Here is some source code that displays bitmaps fonts, it will also have the code for setting up ortho(2D) view
http://nehe.gamedev.net/data/lessons/lwjgl/lesson13.jar

Schnitter

Using the code from the nehe-tutorial
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        // Select The Projection Matrix
        GL11.glLoadIdentity(); // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(45.0f,
                (float) PresentParameters.getWidth() / (float) PresentParameters.getHeight(),
                0.1f, 100f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        // Select The Modelview Matrix

I can see nothing^^ There is just a black window :(
I render my sprites to (x, y, 0f). Is that right? And if not - the text is also wrong :/

bobjob

a function to setup 2d mode   

public static void set2DMode() {
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glMatrixMode(GL11.GL_PROJECTION);                        // Select The Projection Matrix
    GL11.glPushMatrix();                                     // Store The Projection Matrix
    GL11.glLoadIdentity();                                   // Reset The Projection Matrix
    GL11.glOrtho(0, Settings.width, 0, Settings.height, -1, 1);                          // Set Up An Ortho Screen
    GL11.glMatrixMode(GL11.GL_MODELVIEW);                         // Select The Modelview Matrix
    GL11.glPushMatrix();                                     // Store The Modelview Matrix
     //GL11.glLoadIdentity();                                   // Reset The Modelview Matrix
}


a function to set it back to 3d mode

Quote
public static void set3DMode() {
    GL11.glMatrixMode(GL11.GL_PROJECTION);                        // Select The Projection Matrix
    GL11.glPopMatrix();                                      // Restore The Old Projection Matrix
    GL11.glMatrixMode(GL11.GL_MODELVIEW);                         // Select The Modelview Matrix
    GL11.glPopMatrix();                                      // Restore The Old Projection Matrix
    GL11.glEnable(GL11.GL_DEPTH_TEST);
}

Schnitter

It seems that I can not let an TrueTypeTexture-variable null. I got 2 of them and one was null. By setting both to new TrueTypeFont(f, true), it worked.
Using you method to set the 2d-mode, it works. but the text is still the wrong way round.
Here is the complete Code:
http://nopaste.info/e0a2c896da_nl.html

bobjob

not sure how 2 fix the problem as i use my own custom Font class.

if my text is going the wrong way round i can just change the test co-ordinates myself.

but ill take a stab at what might work:
1. depending on the direction try change the, ortho bounds. so there going in an oposite direction.
2. Is there a way to setup ortho/2d view in the class you are using

Schnitter

I always wanted to do it myself - so, do you know any tutorial to use ttf-fonts in lwjgl-applications?
I think it'll be not easy but I want to try it.
Oh, I already looked into the slick-code but it didn't help :(

bobjob

dunno about ttf fonts, I just use bitmap fonts.

is it only that the fonts are displaying the wrong direction?

if so: then change the line
GL11.glOrtho(0, Settings.width, 0, Settings.height, -1, 1);                          // Set Up An Ortho Screen
to
GL11.glOrtho(Settings.width, 0,  0, Settings.height, -1, 1);                          // Set Up An Ortho Screen

or something like that

Schnitter

yes, I know. But doing it like this, everyting else is also thw wrong way round :(