Tutorial 18 : 2D Font - Part I

Tutorial Advanced Download Section

This GLUT library have some utilities to draw text. In this tutorial, we will use this library to render 2D text.
Note: Jogl-JSR231 have now support for system font through Java 2D using TextRenderer.

In next tutorial, we will see 3D text using GLF. In Tutorial 20, we will see a second method to draw 2D text.
 

In addition of 3D object rendering (cone, spherre torus ...), the GLUT library can render text.

To access GLUT methods, we create a GLUT object (like for GL and GLU methods) :

Creating the GLUT object

    //Gl4java
    GLUTFunc glut = new GLUTFuncLightImplWithFonts(gl, glu);
    //Jogl / Jogl JSR-231
    GLUT glut = new GLUT();

Note: In Gl4java, several implementation of GLUT are available. We use here the implementation with font support.

GLUT does not have support for TTF font (True Type Font). It has a support for few font style like time roman and helvetia fonts. Fonts supported by GLUT are reference by the constants :
    GLUTEnum.GLUT_BITMAP_***       //in Gl4java
    GLUT.BITMAP_***                //in Jogl / Jogl JSR-231
*** is the font 'name'.

To render the text in 2D mode, we use the small utility created in the previous tutorial.
We first go to the text position with glRasterPos, then we render it with the GLUT method glutBitmapString.
 

Drawing a GLUT text

    //Creates a glut object
    GLUTFunc glut = new GLUT();
    //Glut font
    int font = GLUT.BITMAP_HELVETICA_10;
   
    ViewingTools.beginOrtho();    //Start 2D mode, see tutorial 17
        //Go to the bottom left position of the text
        gl.glRasterPos3f(xPos, yPos, 0);
        //Draw the text
        GLUT.glutBitmapString(gl, font, "This text was drawn by GLUT");
    ViewingTools.endOrtho();      //End of 2D mode, see tutorial 17

 

glutBitmapString is a glBitmap based operation. To position of bitmap writting operations are not controlled by glVertex, but by glRasterPos. The raster position is used by pixels and bitmap operations like glDrawPixels, glCopyPixels and glBitmap.
 

The dimension in pixel of the text can be known in advanced. The width in pixels of a text can be obtained, for the specified font, with:
    glut.glutBitmapLength(font, text)         //Gl4java / Jogl JSR-231
    glut.glutBitmapLength(gl, font, text)     //Jogl
font is a GLUT constant that refers to a specific font
text is the text to get its width in pixels using the GLUT font
To have the width of a character, we use similarly the method glutBitmapWidth.


I didn't find a method that returns the height of a text/character, but this can be known in the font name. Effectively, the height (and sometimes the width) are included in the font constant, ie
    GLUT.BITMAP_HELVETICA_10
means that each character have 10 pixel height.
 

F : switch to a different GLUT font
 

Remember to download the GraphicEngine-1.1.2 to run this tutorial !


If you've got any remarks on this tutorial, please let me know to improve it.
Thanks for your feedback.
 

Previous Tutorial

Back

Next turorial

Copyright © 2004-2012 Jérôme Jouvie - All rights reserved. http://jerome.jouvie.free.fr/