Hello Guest

Displaying Text in OpenGL

  • 33 Replies
  • 63406 Views
Re: Displaying Text in OpenGL
« Reply #15 on: March 22, 2008, 09:42:05 »
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 :(

*

Offline Matzon

  • *****
  • 2242
Re: Displaying Text in OpenGL
« Reply #16 on: March 22, 2008, 10:06:46 »
the TestUtils (org.newdawn.slick.tests.TestUtils) does the following:

Code: [Select]
        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 ...

Re: Displaying Text in OpenGL
« Reply #17 on: March 22, 2008, 12:04:41 »
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!

Re: Displaying Text in OpenGL
« Reply #18 on: March 22, 2008, 16:40:32 »
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?

Re: Displaying Text in OpenGL
« Reply #19 on: March 22, 2008, 17:04:48 »
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 :(

Re: Displaying Text in OpenGL
« Reply #20 on: March 22, 2008, 17:12:36 »
If you would setup a real projection matrix - eg using glOrtho it would be much easier :)

Re: Displaying Text in OpenGL
« Reply #21 on: March 22, 2008, 17:38:16 »
Can anybody tell me how to set up a projection matrix correctly? :S

*

Offline bobjob

  • ****
  • 394
  • LWJGL: WOW SO GOOD
Re: Displaying Text in OpenGL
« Reply #22 on: March 23, 2008, 01:21:39 »
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

Re: Displaying Text in OpenGL
« Reply #23 on: March 23, 2008, 02:21:33 »
Using the code from the nehe-tutorial
Code: [Select]
        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 :/

*

Offline bobjob

  • ****
  • 394
  • LWJGL: WOW SO GOOD
Re: Displaying Text in OpenGL
« Reply #24 on: March 23, 2008, 13:30:53 »
a function to setup 2d mode   

Code: [Select]
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);
}

Re: Displaying Text in OpenGL
« Reply #25 on: March 23, 2008, 13:52:35 »
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

*

Offline bobjob

  • ****
  • 394
  • LWJGL: WOW SO GOOD
Re: Displaying Text in OpenGL
« Reply #26 on: March 23, 2008, 15:34:50 »
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

Re: Displaying Text in OpenGL
« Reply #27 on: March 23, 2008, 16:01:18 »
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 :(

*

Offline bobjob

  • ****
  • 394
  • LWJGL: WOW SO GOOD
Re: Displaying Text in OpenGL
« Reply #28 on: March 23, 2008, 17:36:11 »
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

Re: Displaying Text in OpenGL
« Reply #29 on: March 23, 2008, 17:55:16 »
yes, I know. But doing it like this, everyting else is also thw wrong way round :(