This example shows texture map fonts and any other 3D object on your screen.

This is the Java port of the one of the NeHe OpenGL tutorials.

You can get complete IntelliJ IDEA project structure (all source, resources, build script, …) by downloading the source distribution from here.

The original post of the programmer who ported the examples can be found here.

 package demos.nehe.lesson15;

import demos.common.GLDisplay;

/**
 * @author Jeff Kirby
 */
public class Lesson15 {
    public static void main(String[] args) {
        GLDisplay neheGLDisplay = GLDisplay.createGLDisplay(
                "Lesson 15: Texture mapped outline fonts");
        
        Renderer renderer = new Renderer();
        neheGLDisplay.addGLEventListener(renderer);
        neheGLDisplay.start();
    }
}


package demos.nehe.lesson15;

import java.io.*;

class LittleEndianInputStream extends FilterInputStream implements DataInput {
    private DataInputStream dataInputStream;
    private int byteCount;

    public LittleEndianInputStream(InputStream inputStream) {
        super(inputStream);
        dataInputStream = new DataInputStream(inputStream);
    }

    public int getByteCount() {
        return byteCount;
    }

    public int read() throws IOException {
        byteCount++;
        return super.read();
    }

    private int readCheckEOF() throws IOException {
        int byteRead = read();
        if (byteRead == -1)
            throw new EOFException();
        return byteRead;
    }

    public int readUnsignedByte() throws IOException {
        return (readCheckEOF() & 0xFF);
    }

    public short readShort() throws IOException {
        int byte1 = (read() & 0xFF);
        int byte2 = (read() & 0xFF) << 8;
        return ((short) (byte1 | byte2));
    }

    public int readUnsignedShort() throws IOException {
        return (readCheckEOF() << 8) + readCheckEOF();
    }

    public int readInt() throws IOException {
        return (readCheckEOF() & 0xFF) |
                (readCheckEOF() & 0xFF) << 8 |
                (readCheckEOF() & 0xFF) << 16 |
                (readCheckEOF() & 0xFF) << 24;
    }

    public float readFloat() throws IOException {
        return Float.intBitsToFloat(readInt());
    }

    public long readLong() throws IOException {
        return (
                ((long) (readCheckEOF() & 0xff)) |
                ((long) (readCheckEOF() & 0xff) << 8) |
                ((long) (readCheckEOF() & 0xff) << 16) |
                ((long) (readCheckEOF() & 0xff) << 24) |
                ((long) (readCheckEOF() & 0xff) << 32) |
                ((long) (readCheckEOF() & 0xff) << 40) |
                ((long) (readCheckEOF() & 0xff) << 48) |
                ((long) (readCheckEOF() & 0xff) << 56)
                );
    }

    public double readDouble() throws IOException {
        return Double.longBitsToDouble(readLong());
    }

    public void readFully(byte[] b) throws IOException {
        readFully(b, 0, b.length);
    }

    public void readFully(byte[] b, int off, int len) throws IOException {
        while (len > 0) {
            int bytesRead = read(b, off, len);
            if (bytesRead == -1) {
                throw new EOFException("Unexpected end of file");
            } else {
                len -= bytesRead;
                off += bytesRead;
            }
        }
    }

    public int skipBytes(int n) throws IOException {
        int bytesRead = 0;
        while (bytesRead < n) {
            int read = read();
            if (read == -1) {
                break;
            } else {
                bytesRead++;
            }
        }
        return bytesRead;
    }

    public boolean readBoolean() throws IOException {
        return readInt() != 0;
    }

    public byte readByte() throws IOException {
        return (byte) readCheckEOF();
    }

    public char readChar() throws IOException {
        return (char) ((readCheckEOF() << 8) | (readCheckEOF() & 0xff));
    }

    public String readLine() throws IOException {
        return dataInputStream.readLine();
    }

    public String readUTF() throws IOException {
        return dataInputStream.readUTF();
    }

    public synchronized void mark(int readlimit) {
    }

    public boolean markSupported() {
        return false;
    }

    public synchronized void reset() throws IOException {
        throw new IOException();
    }
}


package demos.nehe.lesson15;

import demos.common.TextureReader;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

import java.io.IOException;

class Renderer implements GLEventListener {
    private float rot;        // Used To Rotate The Text
    private int[] textures = new int[1];
    private GLF glf = new GLF();
    private GLU glu = new GLU();
    private int timesNew1;

    public void loadGLTextures(GL gl, GLU glu) throws IOException {
        
        TextureReader.Texture texture = TextureReader.readTexture(
                "demos/data/images/Lights.png");
        //Create Texture
        gl.glGenTextures(1, textures, 0);
        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);

        glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D,
                3,
                texture.getWidth(),
                texture.getHeight(),
                GL.GL_RGB,
                GL.GL_UNSIGNED_BYTE,
                texture.getPixels());

        gl.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, GL.GL_OBJECT_LINEAR);
        gl.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, GL.GL_OBJECT_LINEAR);
        gl.glEnable(GL.GL_TEXTURE_GEN_S);
        gl.glEnable(GL.GL_TEXTURE_GEN_T);

        glf = new GLF();
        glf.glfInit();
        timesNew1 = glf.glfLoadFont("demos/data/fonts/times_new1.glf");
        if (timesNew1 == GLF.GLF_ERROR)
            throw new RuntimeException("Error loading font");
        glf.glfStringCentering(true);
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        try {
            loadGLTextures(gl, glu);    // Jump To Texture Loading Routine
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        gl.glShadeModel(GL.GL_SMOOTH);            // Enables Smooth Color Shading
        
        // This Will Clear The Background Color To Black
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
        gl.glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer
        gl.glEnable(GL.GL_DEPTH_TEST);     // Enables Depth Testing
        gl.glDepthFunc(GL.GL_LEQUAL);      // The Type Of Depth Test To Do
        gl.glEnable(GL.GL_LIGHT0);     // Quick And Dirty Lighting (Assumes Light0 Is Set Up)
        gl.glEnable(GL.GL_LIGHTING);       // Enable Lighting
        
        // Really Nice Perspective Calculations
        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  
        
        gl.glEnable(GL.GL_TEXTURE_2D);          // Enable Texture Mapping
        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);    // Select The Texture
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        // Clear Screen And Depth Buffer
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);  
        
        gl.glLoadIdentity();  // Reset The Current Modelview Matrix
        
        gl.glTranslatef(1.1f * (float) (Math.cos(rot / 16.0f)), 0.8f * 
                (float) (Math.sin(rot / 20.0f)), -10.0f);
        
        gl.glRotatef(rot, 1.0f, 0.0f, 0.0f);        // Rotate On The X Axis
        gl.glRotatef(rot * 1.2f, 0.0f, 1.0f, 0.0f); // Rotate On The Y Axis
        gl.glRotatef(rot * 1.4f, 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis
        gl.glTranslatef(-0.35f, -0.35f, 0.1f);      // Center On X, Y, Z Axis

        glf.glfDraw3DSolidStringF(gl, timesNew1, "N");

        rot += 0.1f;  // Increase The Rotation Variable
    }

    public void reshape(GLAutoDrawable drawable,
                        int xstart,
                        int ystart,
                        int width,
                        int height) {
        GL gl = drawable.getGL();

        height = (height == 0) ? 1 : height;

        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();

        glu.gluPerspective(45, (float) width / height, 1, 1000);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void displayChanged(GLAutoDrawable drawable,
                               boolean modeChanged,
                               boolean deviceChanged) {
    }
}

--> GLF.java is available in the source distribution of examples.