Hello Guest

Drawing text using ttf files

  • 7 Replies
  • 19148 Views
Drawing text using ttf files
« on: March 28, 2006, 05:21:48 »
Forgive my undoubtedly extreme n00biness, but can one draw text using the LWJGL from ttf files? If not, what is the best alternative - making a transparent image with all of the characters of a specific font? Thanks in advance for anyone kind enough to assist a n00b like me.
igz r 4 n00bz

*

Offline Matzon

  • *****
  • 2242
Drawing text using ttf files
« Reply #1 on: March 28, 2006, 06:22:39 »
bitmap fonts is pretty common.
An example can be found here:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=13

download the lwjgl source code, the tutorial uses windows specific extensions while the lwjgl code uses a method to build a bitmap list using a generic buildFont method.

Drawing text using ttf files
« Reply #2 on: March 28, 2006, 07:39:01 »
thanks, I'll try it
igz r 4 n00bz

Drawing text using ttf files
« Reply #3 on: April 01, 2006, 21:54:35 »
Ok, this is probably has a really simple solution, so apologies in advance, but I can't get the sample code to work alongside my current OGL initialization. Here's the code my project uses currently:

Code: [Select]
     // Enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// Go into orthographic projection mode
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluOrtho2D(0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
// Set clear color to black
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Sync frame (only works on windows)
Display.setVSyncEnabled(true);


And this is the text tutorial's init code:

Code: [Select]

        GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        // Black Background
        GL11.glClearDepth(1.0); // Depth Buffer Setup
        GL11.glDepthFunc(GL11.GL_LEQUAL);
        GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
        //GL11.glEnable(GL11.GL_CULL_FACE);
        // The Type Of Depth Testing To Do

        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) displayMode.getWidth() / (float) displayMode.getHeight(),
                0.1f,100.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        // Select The Modelview Matrix

        // Really Nice Perspective Calculations
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);


And then the text is drawn with the following lines:

Code: [Select]
       GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  // Clear Screen And Depth Buffer
        GL11.glLoadIdentity();                                              // Reset The Current Modelview Matrix

        // Position The Text On The Screen
        GL11.glTranslatef(-0.9f + 0.05f * ((float)Math.cos(cnt1)), 0.32f * ((float)Math.sin(cnt2)), -2.0f); // Move One Unit Into The Screen

        glPrint("Active OpenGL Text With NeHe - " + numberFormat.format(cnt1));     // Print GL Text To The Screen

...

public void glPrint(String msg) {                                      // Custom GL "Print" Routine
        if(msg != null) {
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
            for(int i=0;i<msg.length();i++) {
                GL11.glCallList(base + msg.charAt(i));
                GL11.glTranslatef(0.05f, 0.0f, 0.0f);
            }
        }
    }


So I've tried using the functions 'GL11.glClear', 'GL11.glLoadIdentity', 'GL11.glTranslatef' and 'glPrint' shown above in various combinations alongside my own drawing code. The result is that either the text doesn't draw or I get a black screen. Now I don't really know what half of what the static GL11 methods do, so I'm kind of lost. Does the order of the initialization code matter? Or do I need to change glPrint? If it helps, the app that I want to use the text in uses a different DisplayMode (with a larger resolution). Thanks.
igz r 4 n00bz

Drawing text using ttf files
« Reply #4 on: April 02, 2006, 18:55:13 »
This is older code now, but if you're interested, PM me with your e-mail address and I'll send the newer stuff to you.

http://www.lwjgl.org/forum/viewtopic.php?t=1258
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

Re: Drawing text using ttf files
« Reply #5 on: December 31, 2007, 12:23:36 »
Sorry. I know, the topic is really old. But I've a question about this tutorial in Java.
In the tutorial, HFONT is used. There isn't HFONT in Java/LWJGL. When I try to download the LWJGL-sourcecode, I can just download a *.jar file.
The jar-file can't be runned - there isn't a manifest-file. But I also cannot read the sourcecode!
Where can I find the sourcecode to this tutorial?(Or another code for creating bitmapfonts in OpenGL+Java)

*

Offline bobjob

  • ****
  • 394
  • LWJGL: WOW SO GOOD
Re: Drawing text using ttf files
« Reply #6 on: January 01, 2008, 01:18:37 »
try open the *.jar file with winRar

Re: Drawing text using ttf files
« Reply #7 on: January 01, 2008, 14:45:46 »
Or, just rename the file from *.jar to *.zip, and then open it that way.  ;D
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com