package cg; //-------------------------------------------------------------------------------- import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.jogamp.opengl.*; import com.jogamp.opengl.awt.*; import com.jogamp.common.nio.Buffers; import java.nio.FloatBuffer; //-------------------------------------------------------------------------------- public class Main extends GLJPanel implements GLEventListener, KeyListener { private static final long serialVersionUID = 1L; //-------------------------------------------------------------------------------- public static void main(String[] args) { JFrame window = new JFrame("Vertex Arrays - Würfel"); Main panel = new Main(); window.setContentPane(panel); window.pack(); window.setLocation(50,50); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); panel.requestFocusInWindow(); } //-------------------------------------------------------------------------------- public Main() { super(new GLCapabilities(null)); setPreferredSize(new Dimension(600,300)); addGLEventListener(this); addKeyListener(this); } private double rotateX = 15; private double rotateY = -15; private double rotateZ = 0; //-------------------------------------------------------------------------------- // v6----- v5 // /| /| // v1------v0| // | | | | // | v7----|-v4 // |/ |/ // v2------v3 private float[] cubeCoords = { // äußere Seiten im Gegenuhrzeigersinn beschrieben 1,1,1, -1,1,1, -1,-1,1, 1,-1,1, // v00[v0]-v01[v1]-v02[v2]-v03[v3] - vordere Fläche 1,1,1, 1,-1,1, 1,-1,-1, 1,1,-1, // v04[v0]-v05[v3]-v06[v4]-v07[v5] - rechte Fläche 1,1,1, 1,1,-1, -1,1,-1, -1,1,1, // v08[v0]-v09[v5]-v10[v6]-v11[v1] - obere Fläche -1,-1,-1, -1,1,-1, 1,1,-1, 1,-1,-1, // v12[v7]-v13[v6]-v14[v5]-v15[v4] - hintere Fläche -1,-1,-1, -1,-1,1, -1,1,1, -1,1,-1, // v16[v7]-v17[v2]-v18[v1]-v19[v6] - linke Fläche -1,-1,-1, 1,-1,-1, 1,-1,1, -1,-1,1 // v20[v7]-v21[v4]-v22[v3]-v23[v2] - untere Fläche }; private float[] cubeFaceColors = { 1,0,0, 1,0,0, 1,0,0, 1,0,0, // rot 0,1,0, 0,1,0, 0,1,0, 0,1,0, // grün 0,0,1, 0,0,1, 0,0,1, 0,0,1, // blau 1,1,0, 1,1,0, 1,1,0, 1,1,0, // gelb 0,1,1, 0,1,1, 0,1,1, 0,1,1, // türkis 1,0,1, 1,0,1, 1,0,1, 1,0,1 // pink }; //-------------------------------------------------------------------------------- private FloatBuffer cubeCoordBuffer = Buffers.newDirectFloatBuffer(cubeCoords); private FloatBuffer cubeFaceColorBuffer = Buffers.newDirectFloatBuffer(cubeFaceColors); //-------------------------------------------------------------------------------- public void display(GLAutoDrawable drawable) { GL2 gl2 = drawable.getGL().getGL2(); gl2.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT ); gl2.glLoadIdentity(); gl2.glRotated(rotateZ,0,0,1); gl2.glRotated(rotateY,0,1,0); gl2.glRotated(rotateX,1,0,0); //-------------------------------------------------------------------------------- gl2.glVertexPointer(3, GL2.GL_FLOAT, 0, cubeCoordBuffer); gl2.glColorPointer(3,GL2. GL_FLOAT, 0, cubeFaceColorBuffer); gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY); gl2.glEnableClientState(GL2.GL_COLOR_ARRAY); gl2.glDrawArrays(GL2.GL_QUADS, 0, 24); gl2.glDisableClientState(GL2.GL_VERTEX_ARRAY); gl2.glDisableClientState(GL2.GL_COLOR_ARRAY); } //-------------------------------------------------------------------------------- public void init(GLAutoDrawable drawable) { GL2 gl2 = drawable.getGL().getGL2(); gl2.glMatrixMode(GL2.GL_PROJECTION); gl2.glOrtho(-4, 4, -2, 2, -2, 2); gl2.glMatrixMode(GL2.GL_MODELVIEW); gl2.glClearColor( 0.5F, 0.5F, 0.5F, 1 ); gl2.glEnable(GL2.GL_DEPTH_TEST); } public void dispose(GLAutoDrawable drawable) {} public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {} //-------------------------------------------------------------------------------- public void keyPressed(KeyEvent evt) { int key = evt.getKeyCode(); if ( key == KeyEvent.VK_LEFT ) rotateY -= 15; else if ( key == KeyEvent.VK_RIGHT ) rotateY += 15; else if ( key == KeyEvent.VK_DOWN) rotateX += 15; else if ( key == KeyEvent.VK_UP ) rotateX -= 15; else if ( key == KeyEvent.VK_PAGE_UP ) rotateZ += 15; else if ( key == KeyEvent.VK_PAGE_DOWN ) rotateZ -= 15; else if ( key == KeyEvent.VK_HOME ) {rotateX = 15; rotateY = -15; rotateZ = 0;} repaint(); } public void keyReleased(KeyEvent evt) {} public void keyTyped(KeyEvent evt) {} }