package cg; //----------------------------------------------------------------------------- import com.jogamp.common.nio.Buffers; import java.nio.ShortBuffer; import java.nio.FloatBuffer; import javax.swing.JFrame; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.util.FPSAnimator; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; //----------------------------------------------------------------------------- public class Main implements GLEventListener, KeyListener { private int[] bufID = new int[3]; private double rotateX = 15; private double rotateY = -15; private double rotateZ = 0; //----------------------------------------------------------------------------- public static void main(String[] args) { JFrame window = new JFrame("V(B+I)O Würfel"); window.setLocation(100, 100); window.setSize(800, 800); GLCanvas canvas = new GLCanvas(); window.add(canvas); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); Main mn = new Main(); canvas.addGLEventListener(mn); canvas.addKeyListener(mn); canvas.setFocusable(true); FPSAnimator animator = new FPSAnimator(canvas, 60); animator.start(); } //----------------------------------------------------------------------------- // v4----- v7 // /| /| // v0------v3| // | | | | // | v5----|-v6 // |/ |/ // v1------v2 private float[] cubeCoords = { -1.0f, 1.0f, 1.0f, //v0 -1.0f, -1.0f, 1.0f, //v1 1.0f, -1.0f, 1.0f, //v2 1.0f, 1.0f, 1.0f, //v3 -1.0f, 1.0f, -1.0f, //v4 -1.0f, -1.0f, -1.0f, //v5 1.0f, -1.0f, -1.0f, //v6 1.0f, 1.0f, -1.0f, //v7 }; private float[] cubeFaceCols = { 0.0f, 0.0f, 0.0f, //v0 - schwarz 0.0f, 0.0f, 1.0f, //v1 - blau 0.0f, 1.0f, 0.0f, //v2 - grün 0.0f, 1.0f, 1.0f, //v3 - türkis 1.0f, 0.0f, 0.0f, //v4 - rot 1.0f, 0.0f, 1.0f, //v5 - pink 1.0f, 1.0f, 0.0f, //v6 - gelb 1.0f, 1.0f, 1.0f, //v7 - weiß }; private short[] cubeFaceInds = { //äußere Seiten der Flächen sind im Gegenuhrzeigersinn 0, 1, 2, 3, //vordere Fläche 0, 4, 5, 1, //linke Fläche 4, 7, 6, 5, //hintere Fläche 2, 6, 7, 3, //rechte Fläche 1, 5, 6, 2, //untere Fläche 3, 7, 4, 0, //obere Fläche }; //----------------------------------------------------------------------------- private FloatBuffer cubeCBuf = Buffers.newDirectFloatBuffer(cubeCoords); private FloatBuffer cubeFCBuf = Buffers.newDirectFloatBuffer(cubeFaceCols); private ShortBuffer cubeFIBuf = Buffers.newDirectShortBuffer(cubeFaceInds); //----------------------------------------------------------------------------- public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glOrtho(-7, 7, -7, 7, -10, 10); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glClearColor(0.5f, 0.5f, 0.5f, 1); gl.glEnable(GL2.GL_DEPTH_TEST); //----------------------------------------------------------------------------- gl.glGenBuffers(3, bufID, 0); gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufID[0]); gl.glBufferData(GL2.GL_ARRAY_BUFFER, cubeCBuf.capacity()*Buffers.SIZEOF_FLOAT, cubeCBuf, GL2.GL_STATIC_DRAW); gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, bufID[1]); gl.glBufferData(GL2.GL_ARRAY_BUFFER, cubeFCBuf.capacity()*Buffers.SIZEOF_FLOAT, cubeFCBuf, GL2.GL_STATIC_DRAW); gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, bufID[2]); gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, cubeFIBuf.capacity()*Buffers.SIZEOF_INT, cubeFIBuf, GL2.GL_STATIC_DRAW); } //----------------------------------------------------------------------------- public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glRotated(rotateZ,0,0,1); gl.glRotated(rotateY,0,1,0); gl.glRotated(rotateX,1,0,0); gl.glScalef(4f, 4f, 4f); //----------------------------------------------------------------------------------------------- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,bufID[0]); gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0); gl.glEnableClientState(GL2.GL_VERTEX_ARRAY); gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,bufID[1]); gl.glColorPointer(3, GL2.GL_FLOAT, 0, 0); gl.glEnableClientState(GL2.GL_COLOR_ARRAY); gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, bufID[2]); gl.glDrawElements(GL2.GL_QUADS, cubeFIBuf.capacity(), GL2.GL_UNSIGNED_SHORT, 0); gl.glDisableClientState(GL2.GL_VERTEX_ARRAY); gl.glDisableClientState(GL2.GL_COLOR_ARRAY); gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0); gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0); } //----------------------------------------------------------------------------- public void dispose(GLAutoDrawable drawable) {} public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {} //----------------------------------------------------------------------------- public void keyPressed(KeyEvent e) { int key = e.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_SPACE) {rotateX = 15; rotateY = -15; rotateZ = 0;} } public void keyReleased(KeyEvent evt) {} public void keyTyped(KeyEvent evt) {} }