Drawing Text to a canvas

Hi, I am writing a jogl application and would like to display worlds and letters on the glcanvas. Can anybody tell me how I’d go about writing “hello world” (a really original example ;D )onto a glcanvas at specified co-ordinates ?

Tks

Sally

Drawing text isn’t as easy as you might suspect. But the GLUT function glutBitmapString() in JOGL helps for simple text.

Here is a Hello world chunk you could safely inject into your display() method to get you started:


gl.glPushMatrix();
gl.glLoadIdentity();

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glDisable(GL.GL_LIGHTING);

gl.glColor3f(1.0f, 1.0f, 0f);
gl.glRasterPos2f(0f, .5f); // <-- position of text

glut.glutBitmapString(gl, GLUT.BITMAP_HELVETICA_12, "Hello World");

gl.glEnable(GL.GL_LIGHTING);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();

Another related question :

Once I have a window (or a fullscreen) with a GLCanvas, is it possible to alternate phases where I use the GL display, and phases where I would use the canvas with standard AWT or swing display operations?
For example a game using 3D display, and when the match is finished I display a score table (but no more 3D at all).
Is that possible?

Ive wondered if we could render text to offscreen BufferedImage using the Java2D methods.

Then somehow use inmemory BufferedImage and render to a screen.

  • dynamically bind to a model
  • render to 2D overlay canvas (if this is possible)

Is it possible to have 2D overlay layer on top of the glcanvas. It could display the textual information.

Following on from that you can draw to a BufferedImage using Java2D and from that to a texture and then draw the texture.

This whole discussion leads me to a question on performance with writing text.
What is faster (and why), bitmaps or textures?
In particular in regards to non-scaling text. i.e. text that ignores projection/distance/depth etc and always draws to the same number of pixels.

Textures would be faster because the image is stored on card, and so can the vertices be if the text is static.

Thank you for your help. However, with your original code the graphics failed to render after displaying the text. I stripped out some of the lines to leave it like this:

 
    gl.glColor3f(1.0f, 1.0f, 0f); 
    gl.glRasterPos2f(0f, .5f); // <-- position of text 
    glut.glutBitmapString(gl, GLUT.BITMAP_HELVETICA_12, "Hello World"); 
 

and then it seemed to work perfectly.

Just wondered what those other lines were about as it works without it. What am I missing here :stuck_out_tongue:

Txs

Sally :slight_smile: