1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| import com.sun.opengl.util.Animator; import com.sun.opengl.util.BufferUtil; import java.awt.BorderLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener;
import java.nio.IntBuffer; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU; import javax.swing.JFrame;
public class VBO extends JFrame implements GLEventListener, KeyListener { private GL gl; private GLU glu; private GLCanvas canvas; private Animator anim; private GLCapabilities caps; private static final int SW = 1024; private static final int SH = 768; private boolean VBOsupported = false; private int vertexCount; private IntBuffer vertices; private int[] VBOVertices = new int[1]; public VBO() { setUndecorated( true ); caps = new GLCapabilities(); canvas = new GLCanvas( caps ); canvas.addGLEventListener(this); canvas.addKeyListener( this ); getContentPane().add(canvas, BorderLayout.CENTER); setSize( SW, SH ); setTitle("AStar"); setVisible(true); anim = new Animator(canvas); anim.start(); } public static void main(String[] args) { new VBO(); }
public void init(GLAutoDrawable arg0) { gl = arg0.getGL(); glu = new GLU(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glColor4f( 1f, 1f, 1f, 1f ); glu.gluOrtho2D( 0.0, SW, 0.0, SH ); gl.glDisable( GL.GL_TEXTURE_2D ); gl.glLineWidth( 1.0f ); gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL ); gl.glShadeModel( GL.GL_SMOOTH ); gl.glHint( GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST ); VBOsupported = gl.isFunctionAvailable("glGenBuffersARB") && gl.isFunctionAvailable("glBindBufferARB") && gl.isFunctionAvailable("glBufferDataARB") && gl.isFunctionAvailable("glDeleteBuffersARB"); if ( VBOsupported ){ debug( "VBO SUPPORTED!" ); } else { debug( "VBO NOT SUPPORTED!" ); System.exit( 0 ); } vertexCount = 4; vertices = BufferUtil.newIntBuffer( vertexCount * 2 ); vertices.put( SW/2 ); vertices.put( SH/2 ); vertices.put( SW/2 ); vertices.put( SH/2+20 ); vertices.put( SW/2+20 ); vertices.put( SH/2+20 ); vertices.put( SW/2+20 ); vertices.put( SH/2 ); gl.glGenBuffersARB( 1, VBOVertices, 0 ); gl.glBindBufferARB( GL.GL_ARRAY_BUFFER_ARB, VBOVertices[0] ); gl.glBufferDataARB( GL.GL_ARRAY_BUFFER_ARB, vertexCount * 2 * BufferUtil.SIZEOF_INT, vertices, GL.GL_STATIC_DRAW_ARB );
vertices = null; } private void debug( String str ){ System.out.println( "DEBUG: " + str ); } public void display(GLAutoDrawable arg0) { final GL gl = arg0.getGL(); gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); gl.glLoadIdentity(); gl.glEnableClientState( GL.GL_VERTEX_ARRAY ); gl.glBindBufferARB( GL.GL_ARRAY_BUFFER_ARB, VBOVertices[0] ); gl.glVertexPointer( 2, GL.GL_INT, 0, 0 ); gl.glDrawArrays( GL.GL_QUADS, 0, vertexCount ); gl.glDisableClientState( GL.GL_VERTEX_ARRAY ); }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}
public void keyTyped( KeyEvent e ){ }
public void keyPressed( KeyEvent e ){ if ( e.getKeyCode() == KeyEvent.VK_ESCAPE ){ System.exit( 0 ); } }
public void keyReleased( KeyEvent e ){ } } |