*How to use glCallLists in jogl<转>

package glredbook10;

/**
 * This program demonstrates some characters of a stroke (vector) font. The
 * characters are represented by display lists, which are given numbers which
 * correspond to the ASCII values of the characters. Use of glCallLists() is
 * demonstrated.
 * 
 * 
@author Kiet Le (Java port)
 
*/

import java.awt.event.*;
import javax.swing.*;

import java.nio.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;

public class stroke
  
extends JFrame
    
implements GLEventListener, KeyListener
{
  
private GLU glu;
  
private GLCapabilities caps;
  
private GLCanvas canvas;
  
//
  private static final int PT = 1;
  
private static final int STROKE = 2;
  
private static final int END = 3;

  
// * C struct. Compare this to charpoint class.
  
// typedef struct charpoint
  
// {
  
// GLfloat x, y;
  
// int type;
  
// } CP;
  private class charpoint
  
{
    
public float x;
    
public float y;
    
public int type;

    
public charpoint(float x, float y, int type)
    
{
      
this.x = x;
      
this.y = y;
      
this.type = type;
    }

  }


  
// * Saved here from original for you to see the difference
  
// CP Adata[] =
  
// {
  
// { 0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT},
  
// {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}
  
// };
  charpoint Adata[] =
  
new charpoint(00, PT), new charpoint(09, PT), //
    new charpoint(110, PT), new charpoint(410, PT),//
    new charpoint(59, PT), new charpoint(50, STROKE),//
    new charpoint(05, PT), new charpoint(55, END) }
;

  charpoint Edata[] 
=
  
new charpoint(50, PT), new charpoint(00, PT), //
    new charpoint(010, PT), new charpoint(510, STROKE),//
    new charpoint(05, PT), new charpoint(45, END) }
;

  charpoint Pdata[] 
=
  
new charpoint(00, PT), new charpoint(010, PT),//
    new charpoint(410, PT), new charpoint(59, PT),//
    new charpoint(56, PT), new charpoint(45, PT), //
    new charpoint(05, END) }
;

  charpoint Rdata[] 
=
  
new charpoint(00, PT), new charpoint(010, PT),//
    new charpoint(410, PT), new charpoint(59, PT),//
    new charpoint(56, PT), new charpoint(45, PT),//
    new charpoint(05, STROKE), new charpoint(35, PT),//
    new charpoint(50, END) }
;

  charpoint Sdata[] 
=
  
new charpoint(01, PT), new charpoint(10, PT), //
    new charpoint(40, PT), new charpoint(51, PT),//
    new charpoint(54, PT), new charpoint(45, PT), //
    new charpoint(15, PT), new charpoint(06, PT), //
    new charpoint(09, PT), new charpoint(110, PT),//
    new charpoint(410, PT), new charpoint(59, END) }
;

  
// char *test1 = "A SPARE SERAPE APPEARS AS"; C char ptr to str 
  
// char *test2 = "APES PREPARE RARE PEPPERS";
  private String test1 = "A SPARE SERAPE APPEARS AS";// String object
  private String test2 = "APES PREPARE RARE PEPPERS";

  
public stroke()
  
{
    
super("stroke");

    caps 
= new GLCapabilities();
    canvas 
= new GLCanvas(caps);
    canvas.addGLEventListener(
this);
    canvas.addKeyListener(
this);
 
    getContentPane().add(canvas);
  }


  
public void run()
  
{
    setSize(
440120);
    setLocationRelativeTo(
null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(
true);
    canvas.requestFocusInWindow();
  }


  
public static void main(String[] args)
  
{
    
new stroke().run();
  }


  
/* Create a display list for each of 6 characters */
  
public void init(GLAutoDrawable drawable)
  
{
    GL gl 
= drawable.getGL();
    glu 
= new GLU();

    
int base;

    gl.glShadeModel(GL.GL_FLAT);

    base 
= gl.glGenLists(128);
    gl.glListBase(base);
    gl.glNewList(base 
+ 'A', GL.GL_COMPILE);
    drawLetter(gl, Adata);
    gl.glEndList();
    gl.glNewList(base 
+ 'E', GL.GL_COMPILE);
    drawLetter(gl, Edata);
    gl.glEndList();
    gl.glNewList(base 
+ 'P', GL.GL_COMPILE);
    drawLetter(gl, Pdata);
    gl.glEndList();
    gl.glNewList(base 
+ 'R', GL.GL_COMPILE);
    drawLetter(gl, Rdata);
    gl.glEndList();
    gl.glNewList(base 
+ 'S', GL.GL_COMPILE);
    drawLetter(gl, Sdata);
    gl.glEndList();
    gl.glNewList(base 
+ ' ', GL.GL_COMPILE);
    gl.glTranslatef(
8.0f0.0f0.0f);
    gl.glEndList();
  }


  
public void display(GLAutoDrawable drawable)
  
{
    GL gl 
= drawable.getGL();
    
//
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glColor3f(
1.0f1.0f1.0f);

    gl.glPushMatrix();
    gl.glScalef(
2.0f2.0f2.0f);
    gl.glTranslatef(
10.0f30.0f0.0f);
    
this.printStrokedString(gl, test1);
    gl.glPopMatrix();

    gl.glPushMatrix();
    gl.glScalef(
2.0f2.0f2.0f);
    gl.glTranslatef(
10.0f13.0f0.0f);
    
this.printStrokedString(gl, test2);
    gl.glPopMatrix();

    gl.glFlush();
  }


  
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  
{
    GL gl 
= drawable.getGL();

    gl.glViewport(
00, w, h);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(
0, w, 0, h);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }


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


  
/*
   * interprets the instructions from the array for that letter and renders the
   * letter with line segments.
   
*/

  
private void drawLetter(GL gl, charpoint[] l)
  
{
    
int i = 0;
    gl.glBegin(GL.GL_LINE_STRIP);
    
while (i < l.length)
    
{
      
switch (l[i].type) {
        
case PT:
          gl.glVertex2f(l[i].x, l[i].y);
          
break;
        
case STROKE:
          gl.glVertex2f(l[i].x, l[i].y);
          gl.glEnd();
          gl.glBegin(GL.GL_LINE_STRIP);
          
break;
        
case END:
          gl.glVertex2f(l[i].x, l[i].y);
          gl.glEnd();
          gl.glTranslatef(
8.0f0.0f0.0f);
          
return;
      }

      i
++;// System.out.println(i+" ");
    }

  }


  
// private void printStrokedString(char *s)
  private void printStrokedString(GL gl, String s)
  
{
    
int len = s.length();
    ByteBuffer str 
= BufferUtil.newByteBuffer(len);
    str.put(s.getBytes());
    str.rewind();
    gl.glCallLists(len, GL.GL_BYTE, str);
  }


  
public void keyTyped(KeyEvent key)
  
{
  }


  
public void keyPressed(KeyEvent key)
  
{
    
switch (key.getKeyCode()) {
      
case KeyEvent.VK_ESCAPE:
        System.exit(
0);
        
break;

      
default:
        
break;
    }

  }


  
public void keyReleased(KeyEvent key)
  
{
  }

}
 
原文地址:https://www.cnblogs.com/dwjaissk/p/705474.html