/* * This is an example class for Vertex Arrays with JOGL. I ported * this code from the Red Book Example * http://www.opengl.org/resources/code/basics/redbook/varray.c * to Java. * * Unfortunately, there are no comments in this class. I know * that it's bad style... * I rather want this class publish * as soon as possible because I had some trouble * getting the whole vertex array thing running and I * guess that there are a few more people having the same * problem. Besides, I could not find an adequate example * class covering this topic yet and someone who's looking for * vertex arrays does not need an explanation about * glClearColor and similar commands. * * Anyway, you're welcome to write me a mail * for any reason :) * * Johannes.schaback@gmail.com * * modified on 2005-3-12 * */ import net.java.games.jogl.*; import java.awt.BorderLayout; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.nio.Buffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import javax.swing.JFrame; import net.java.games.jogl.Animator; import net.java.games.jogl.GL; import net.java.games.jogl.GLCanvas; import net.java.games.jogl.GLCapabilities; import net.java.games.jogl.GLDrawable; import net.java.games.jogl.GLDrawableFactory; import net.java.games.jogl.GLU; import net.java.games.jogl.util.BufferUtils; public class VertexArrayTest extends JFrame implements GLEventListener { private GL gl; private GLU glu; private GLCanvas canvas; private Animator anim; public VertexArrayTest() { canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities()); canvas.addGLEventListener(this); getContentPane().add(canvas, BorderLayout.CENTER); setSize(500, 500); setTitle("Red Book Example "); setVisible(true); anim = new Animator(canvas); // calls display() periodically anim.start(); this.addWindowListener(new WindowListener() { public void windowOpened(WindowEvent arg0) {} public void windowClosing(WindowEvent arg0) { anim.stop(); setVisible(false); System.exit(0); } public void windowClosed(WindowEvent arg0) {} public void windowIconified(WindowEvent arg0) {} public void windowDeiconified(WindowEvent arg0) {} public void windowActivated(WindowEvent arg0) {} public void windowDeactivated(WindowEvent arg0) {} }); } public static void main(String[] args) { VertexArrayTest desk = new VertexArrayTest(); } public void init(GLDrawable arg0) { gl = arg0.getGL(); glu = arg0.getGLU(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); int[] v = new int[] {25, 25, 100, 325, 175, 25, 175, 325, 250, 25, 325, 325}; float[] c = new float[] { 1.0f, 0.2f, 0.2f, 0.2f, 0.2f, 1.0f, 0.8f, 1.0f, 0.2f, 0.75f, 0.75f, 0.75f, 0.35f, 0.35f, 0.35f, 0.5f, 0.5f, 0.5f}; IntBuffer vertices = BufferUtils.newIntBuffer(6*2); FloatBuffer colors = BufferUtils.newFloatBuffer(6*3); vertices.put(v); colors.put(c); gl.glEnableClientState (GL.GL_COLOR_ARRAY); gl.glEnableClientState (GL.GL_VERTEX_ARRAY); gl.glColorPointer (3, GL.GL_FLOAT, 0, colors); gl.glVertexPointer (2, GL.GL_INT, 0, vertices); } public void display(GLDrawable arg0) { gl.glClear(GL.GL_COLOR_BUFFER_BIT ); gl.glDrawArrays (GL.GL_TRIANGLES, 0, 6); gl.glFlush (); } public void reshape(GLDrawable drawable, int x, int y, int width, int height) { gl.glViewport (0, 0, width, height); gl.glMatrixMode (GL.GL_PROJECTION); gl.glLoadIdentity (); glu.gluOrtho2D (0.0, width, 0.0, height); } public void displayChanged(GLDrawable arg0, boolean arg1, boolean arg2) {} }