Drawing text using ttf files

Started by Nazca Visitor, March 28, 2006, 05:21:48

Previous topic - Next topic

Nazca Visitor

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

Matzon

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.

Nazca Visitor

igz r 4 n00bz

Nazca Visitor

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:

     // 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:

       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:

       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

elias4444

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

Schnitter

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)

bobjob

try open the *.jar file with winRar

elias4444

Or, just rename the file from *.jar to *.zip, and then open it that way.  ;D
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com