Text in JOGL

could some help me in creating text in JOGL.

any sample code will be highly appreciated.

thanks in advance,
-jan

Hi,

You can use GLUT class to draw your string but it’s awfully slow…

The other way is to create an array of bitmaps, compile them in display lists and call gl.glCallLists to draw your string.

Here are some pieces of code :

    fontOffset = gl.glGenLists ( 255 );

    for( int i = first; i < first + num_chars; i++ )
    {
        bmpChar = myChars[i];
        if( bmpChar == null )
        {
            continue;
        }
        gl.glNewList ( i, GL.GL_COMPILE );
        {
            gl.glBitmap ( bmpChar.width, bmpChar.height, 
                        bmpChar.xorig, bmpChar.yorig,
                        bmpChar.advance, 0,bmpChar.bitmap);
        }
        gl.glEndList ();
    }   

This create the lists, then to draw a string :

public void drawString( GL gl, String s )
{
    gl.glPushAttrib ( GL.GL_LIST_BIT );
    gl.glCallLists ( s.length (), GL.GL_UNSIGNED_SHORT, s.toCharArray () );
    gl.glPopAttrib ();
}       

Hope it helps

thanks turquoise3232.

Could some one tell me what is bmChar ??
where can i get this class, or how to read
char bitmap in java (new to java).

thanks in advance,
-jan

I you have JOGL sources you can look at the glut package to find an kind of class that handle chars.

If you want to use custom fonts you can draw it yourself or use a program that convert TT fonts to bitmap (i don’t have any name to give :-/ but there are some programs…)

You can have a look a the nehe tutorial too, it have a lot of good stuff about fonts…

i just put together a class which can generate an image from a TrueTypeFont.
to get the image to Jogl as a texture shouldn t be that hard.
no need to mention that the class is not complete and has several flaws :slight_smile:
hope this helps anyway!


import java.io.*;

import java.awt.*;
import java.awt.font.*;
import java.awt.image.*;


public class DynamicFont
{

      private Font basefont;

      private Font font;

      private FontRenderContext context;

      private float fontsize;

      private static final boolean DEBUG = true;

      /**
       * create a font from an input stream
       * @param is InputStream
       */
      public DynamicFont(InputStream is) {
      basefont = loadBaseFont(is);
            context = getContext();
            fontsize = 48f;
            setSize(fontsize);
      }



      /**
       * derive a font with a new size from basefont
       * @param size float
       * @return Font
       */
      private Font getFontBySize(float s) {
            return basefont.deriveFont(s);
      }



      /**
       * set size of font
       * @param size float
       */
      public void setSize(float size) {
            fontsize = size;
            font = getFontBySize(size);
      }



      /**
       * load the base font
       * @param name String
       * @return Font
       */
      private Font loadBaseFont(InputStream is) {
            Font font = null;
            try {
                  font = Font.createFont(Font.TRUETYPE_FONT, is);
            } catch (IOException ex) {
                  System.err.println("### ERROR @DynamicFont IOException");
            } catch (FontFormatException ex) {
                  System.err.println("### ERROR @DynamicFont FontFormatException");
            }
            return font;
      }



      /**
       * get a font rendering context
       * @return FontRenderContext
       */
      private FontRenderContext getContext() {
            BufferedImage b = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D g = b.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            FontRenderContext frc = g.getFontRenderContext();
            return frc;
      }



      /**
       * get the pixel data from the specified text
       * @param text String
       * @return byte[]
       */
      /*public byte[] getData(String text) {
            BufferedImage myImage = getImage(text);
            Raster myRaster = myImage.getData();
            DataBufferByte myDataBufferByte = (DataBufferByte)myRaster.getDataBuffer();
            return myDataBufferByte.getData();
      }*/



      /**
       * check if characters are supproted
       * @param text String
       * @return char
       */
      private void checkString(String text) {
            int illegalCharacter = font.canDisplayUpTo(text);
            if (illegalCharacter > -1) {
                  System.err.println("### INFO cann t display character #" + illegalCharacter);
            }
      }



      /**
       * get a buffered image from the specified text 
       * @param text String
       * @return BufferedImage
       */
      public BufferedImage getImage(String text) {
            if (DEBUG) {
                  checkString(text);
            }
            int width = (int) font.getStringBounds(text, context).getWidth();
            int height = (int) font.getMaxCharBounds(context).getHeight();
            //int x = (int)font.getStringBounds(text, context).getX();
            int y = (int) font.getStringBounds(text, context).getY();
            BufferedImage bitmap = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); //BufferedImage.TYPE_4BYTE_ABGR
            Graphics2D g = bitmap.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setFont(font);
            g.drawString(text, 0, -y);
            return bitmap;
      }
}

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=LWJGL;action=display;num=1078868758

Hi, in case anyone is interested, I think there is a bug in the DynamicFont class. If the width or the hieght of the BufferedImage is odd it causes problems. As a hack I added this to the getImage method.


if (width % 2 != 0)
{
   width += 1;
}
if (height % 2 != 0)
{
   height += 1;
}

Also, I need to set the font size to a very high value to get sharp results.

Also, if anyone has any better/newer/more standard way of creating text I would love to see it. This thread seems to be a couple of years old.

-spiraljetty

four beginning font examples (drawf, font, stroke, jfont) are http://ak.kiet.le.googlepages.com/theredbookinjava.html .