package cg; //------------------------------------------------------------------------------------------ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; //------------------------------------------------------------------------------------------ public class Main implements GLEventListener, KeyListener { final GLU glu = new GLU(); float delta = 0.1f; static float camera_position[] = {0.0f, 0.0f, -10.0f}; static float camera_orientation[] = {0.0f, 0.0f, 0.0f}; float speed = 0.2f; float angle_incr = 1.0f; //------------------------------------------------------------------------------------------ public static void main(String[] args) { GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(new Main()); canvas.addKeyListener(new Main()); FPSAnimator animator = new FPSAnimator(canvas, 60); animator.start(); final int width = 800; final int height = 600; JFrame frame = new JFrame("OpenGL Fenster"); frame.setSize(width, height); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(canvas); frame.setVisible(true); canvas.setFocusable(true); } //------------------------------------------------------------------------------------------ public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {} @Override public void dispose(GLAutoDrawable drawable) {} //------------------------------------------------------------------------------------------ @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { final GL2 gl = drawable.getGL().getGL2(); final float h = (float) width / (float) height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, h, 1.0f, 100.0f); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } //------------------------------------------------------------------------------------------ @Override public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClearColor(0.1f, 0.3f, 0.8f, 1.0f); gl.glEnable(GL2.GL_CULL_FACE); gl.glEnable(GL2.GL_DEPTH_TEST); } //------------------------------------------------------------------------------------------ @Override 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.glRotatef(camera_orientation[2], 0.0f, 0.0f, 1.0f); gl.glRotatef(camera_orientation[0], 1.0f, 0.0f, 0.0f); gl.glRotatef(camera_orientation[1], 0.0f, 1.0f, 0.0f); gl.glTranslatef(camera_position[0], camera_position[1], camera_position[2]); gl.glPushMatrix(); gl.glTranslatef(0.0f, -1.0f, 0.0f); drawGround(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(0.0f, 0.0f, 0.0f); drawCube(gl); gl.glPopMatrix(); } //------------------------------------------------------------------------------------------ void drawGround(GL2 gl) { gl.glBegin(GL2.GL_QUADS); gl.glVertex3f(-5.0f, 0.0f-delta, 5.0f); gl.glVertex3f( 5.0f, 0.0f-delta, 5.0f); gl.glVertex3f( 5.0f, 0.0f-delta,-5.0f); gl.glVertex3f(-5.0f, 0.0f-delta,-5.0f); gl.glEnd(); } //------------------------------------------------------------------------------------------ void drawCube(GL2 gl) { gl.glBegin( GL2.GL_QUADS ); gl.glColor3f( 1f,0f,0f ); //red color gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Top) gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top) gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom Right Of The Quad (Top) gl.glColor3f( 0f,1f,0f ); //green color gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Top Right Of The Quad (Bottom) gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Top Left Of The Quad (Bottom) gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad (Bottom) gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad (Bottom) gl.glColor3f( 0f,0f,1f ); //blue color gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Front) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front) gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad (Front) gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad (Front) gl.glColor3f( 1f,1f,0f ); //yellow (red + green) gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad (Back) gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad (Back) gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back) gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Back) gl.glColor3f( 1f,0f,1f ); //purple (red + green) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Left) gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Left) gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad (Left) gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad (Left) gl.glColor3f( 0f,1f, 1f ); //sky blue (blue + green) gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right) gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Right) gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad (Right) gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad (Right) gl.glEnd(); } //----------------------------------------------------------------------------------------------------// @Override public void keyTyped(KeyEvent e) { int keyCo = e.getKeyCode(); if (keyCo == KeyEvent.VK_A) { System.out.println("typed " + e.getKeyChar() + " button"); } } //------------------------------------------------------------------------------------------ @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_UP) { camera_position[2]+=speed; } if (keyCode == KeyEvent.VK_DOWN) { camera_position[2]-=speed; } else if (keyCode == KeyEvent.VK_LEFT) { camera_position[0]+=speed; } else if (keyCode == KeyEvent.VK_RIGHT) { camera_position[0]-=speed; } else if (keyCode == KeyEvent.VK_PAGE_UP) { camera_position[1]-=speed; } else if (keyCode == KeyEvent.VK_PAGE_DOWN) { camera_position[1]+=speed; } else if (keyCode == KeyEvent.VK_A) { camera_orientation[1] -= angle_incr; if (camera_orientation[1] < -360.0f) camera_orientation[1] += 360.0f; } else if (keyCode == KeyEvent.VK_D) { camera_orientation[1] += angle_incr; if (camera_orientation[1] > 360.0f) camera_orientation[1] -= 360.0f; } else if (keyCode == KeyEvent.VK_W) { camera_orientation[0] -= angle_incr; if (camera_orientation[0] < -360.0f) camera_orientation[0] += 360.0f; } else if (keyCode == KeyEvent.VK_S) { camera_orientation[0] += angle_incr; if (camera_orientation[0] > 360.0f) camera_orientation[0] -= 360.0f; } else if (keyCode == KeyEvent.VK_C) { camera_position[0] = 0.0f; camera_position[1] = 0.0f; camera_position[2] = -10.0f; camera_orientation[0] = 0.0f; camera_orientation[1] = 0.0f; camera_orientation[2] = 0.0f; } } //------------------------------------------------------------------------------------------ @Override public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_DOWN) { System.out.println("released " + e.getExtendedKeyCode() + " button"); } } }