views:

21

answers:

1

I'm trying to display text in my OpenGL / JOGL app. Here is some of the code:

private void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    GLUgl2 glu = new GLUgl2();

    float[] rgba = new float[4];
    backgroundColor.getRGBComponents(rgba);
    gl.glClearColor(rgba[0], rgba[1], rgba[2], 1);
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();


    // Position the camera
    glu.gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, cameraPos.x + cameraForward.x, cameraPos.y + cameraForward.y,
            cameraPos.z + cameraForward.z, cameraUp.x, cameraUp.y, cameraUp.z);

    gl.glPushMatrix();
    GLUT glut = new GLUT();
    gl.glTranslatef(0, 0, 0);
    gl.glColor3f(1, 0, 0);
    glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, "We're going to the moon!");
    gl.glPopMatrix();

    // ...

The text appears as soon as rendering begins, then runs off the right hand side of the screen. Why is this happening?

I'd like to display text in front of the user, not as a model in the world.

+2  A: 

You need to set the current raster position before invoking glutBitmapString. The raster position is the pixel where the string will start being drawn. You can set it using the glRasterPos... family of functions. The reason your text seems to "run off" the right side of the screen is that calling glutBitmapString implicitly moves the raster position to the end of the string (see here).

Mike Daniels
Ok, that seems to work, and the text is drawn at some spot in the world. Now how can I make it be drawn at the same point on the user's screen, regardless of camera orientation?
Rosarch
Ah, I forgot that the raster position gets transformed like a vertex would if you specify it that way. You might need `glWindowPos` (http://www.opengl.org/sdk/docs/man/xhtml/glWindowPos.xml)
Mike Daniels