java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JOGL arrow Texture mapped outline fonts - NeHe Tutorial JOGL Port
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4085
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Texture mapped outline fonts - NeHe Tutorial JOGL Port E-mail
User Rating: / 1
PoorBest 

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.


Image

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<< |
                (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[] bthrows IOException {
        readFully(b, 0, b.length);
    }

    public void readFully(byte[] b, int off, int lenthrows 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 nthrows 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 (bytereadCheckEOF();
    }

    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 gluthrows 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.0f0.0f0.0f0.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.0f0.0f0.0f);        // Rotate On The X Axis
        gl.glRotatef(rot * 1.2f0.0f1.0f0.0f)// Rotate On The Y Axis
        gl.glRotatef(rot * 1.4f0.0f0.0f1.0f)// Rotate On The Z Axis
        gl.glTranslatef(-0.35f, -0.35f0.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: height;

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

        glu.gluPerspective(45(floatwidth / height, 11000);
        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.


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.