package cg; import static javax.swing.JFrame.EXIT_ON_CLOSE; import java.awt.BorderLayout; import java.awt.Label; import java.awt.TextField; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; 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; /* key control: c - reset camera position and orientation arrow-up - move forward arrow-down - move backward arrow-left - move left arrow-right - move right page-up - move up page-down - move down l - rotate left r - rotate right u - rotate up d - rotate down */ public class Main implements GLEventListener, KeyListener, MouseListener, MouseMotionListener { // create an instance of GL Utility library and keep it final GLU glu = new GLU(); // count the frames that were rendered int frames; static float camera_position[] = {0.0f, 0.0f, -10.0f}; static float camera_orientation[] = {0.0f, 100.0f, 0.0f}; // speed for camera movement float speed = 0.2f; // camera angle increment in degrees float angle_incr = 1.0f; private static TextField tf; public static void main(String[] args) { // create OpenGL window GLCanvas canvas = new GLCanvas(); canvas.addGLEventListener(new Main()); canvas.addKeyListener(new Main()); canvas.addMouseListener(new Main()); //canvas.addMouseMotionListener(new Main()); // animate the canvas with 60 frames per second FPSAnimator animator = new FPSAnimator(canvas, 60); animator.start(); final int width = 800; final int height = 600; tf = new TextField(30); Label label = new Label("Click and drag the mouse"); label.addMouseMotionListener(new Main()); // create main window and insert the canvas into it JFrame frame = new JFrame("OpenGL Fenster"); frame.setSize(width, height); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.getContentPane().add(canvas); frame.add(tf, BorderLayout.SOUTH); frame.add(label, BorderLayout.NORTH); // show the main window frame.setVisible(true); // set input focus to the canvas canvas.setFocusable(true); canvas.requestFocus(); } public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); // background color gl.glClearColor(0.1f, 0.5f, 0.5f, 1.0f); // enable backface culling (default is off) gl.glEnable(GL2.GL_CULL_FACE); // enable z-buffer (default is off) gl.glEnable(GL2.GL_DEPTH_TEST); } public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); frames++; // clear the framebuffer gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); // place object into the scene 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], 1.5f*camera_position[2]); // ground // save matrix state gl.glPushMatrix(); gl.glTranslatef(0.0f, -1.0f, 0.0f); drawGround(gl); // restore matrix state gl.glPopMatrix(); } /** * When the window changes its position or shape the projection transformation * and viewport dimensions have to be adjusted accordingly. */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); // set viewport to window dimensions gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); // set perspective projection glu.gluPerspective(45.0f, (float) width / (float) height, 1.0f, 20.0f); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) { // TODO Auto-generated method stub } // draw a ground void drawGround(GL2 gl) { gl.glBegin(GL2.GL_QUADS); gl.glColor3f(0.0f, 0.0f, 0.0f); // black gl.glVertex3f(-5.0f, 0.0f, 5.0f); gl.glVertex3f( 5.0f, 0.0f, 5.0f); gl.glVertex3f( 5.0f, 0.0f,-5.0f); gl.glVertex3f(-5.0f, 0.0f,-5.0f); gl.glEnd(); } @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); // move backward if (keyCode == KeyEvent.VK_DOWN) { System.out.println("pressed " + e.getID() + " button"); } // move left else if (keyCode == KeyEvent.VK_LEFT) { System.out.println("pressed " + e.getKeyChar() + " button"); } // move right else if (keyCode == KeyEvent.VK_RIGHT) { System.out.println("pressed " + e.getKeyChar() + " button"); } // move up else if (keyCode == KeyEvent.VK_PAGE_UP) { System.out.println("pressed " + e.getKeyChar() + " button"); } // move down else if (keyCode == KeyEvent.VK_PAGE_DOWN) { System.out.println("pressed " + e.getKeyChar() + " button"); } // turn left else if (keyCode == KeyEvent.VK_L) { camera_orientation[1] -= angle_incr; if (camera_orientation[1] < -360.0f) camera_orientation[1] += 360.0f; } // turn right else if (keyCode == KeyEvent.VK_R) { camera_orientation[1] += angle_incr; if (camera_orientation[1] > 360.0f) camera_orientation[1] -= 360.0f; } // turn up else if (keyCode == KeyEvent.VK_U) { camera_orientation[0] -= angle_incr; if (camera_orientation[0] < -360.0f) camera_orientation[0] += 360.0f; } // turn down else if (keyCode == KeyEvent.VK_D) { camera_orientation[0] += angle_incr; if (camera_orientation[0] > 360.0f) camera_orientation[0] -= 360.0f; } // reset the camera position and orientation 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 arg0) { // TODO Auto-generated method stub int keyCode = arg0.getKeyCode(); // move backward if (keyCode == KeyEvent.VK_DOWN) { System.out.println("released " + arg0.getExtendedKeyCode() + " button"); } } @Override public void keyTyped(KeyEvent arg1) { // TODO Auto-generated method stub int keyCo = arg1.getKeyCode(); if (keyCo == KeyEvent.VK_A) { System.out.println("typed " + arg1.getKeyChar() + " button"); } } @Override public void dispose(GLAutoDrawable drawable) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub String s = "Mouse clicked: X = " + e.getX() + " Y = " + e.getY(); tf.setText(s); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub System.out.println("Mouse pressed;" + "\n" + "# of clicks: " + e.getClickCount()+ "\n"); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub System.out.println("Mouse released;" + "\n" + "# of clicks: " + e.getClickCount()+ "\n"); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub System.out.println("Mouse entered: "+ "\n" + "X on screen: " + e.getXOnScreen()+ "\n" + "X: " + e.getX()); } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub System.out.println("Mouse exited: " + e.getPoint()); } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub System.out.println("Mouse dragged: " + e.getWhen()); } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub System.out.println("Mouse moved: " + e.isShiftDown()); } }