// This gets called by the constructor, hopefully only // once but it can be called at any time. Called prior to any // Window creation so it can only set variables, not draw things public void init() { /* initialize the widget */ int width = gl.getWidth(); int height = gl.getHeight(); // Initialize the rendering viewport size to OpenGL gl.viewport(0, 0, width, height); gl.matrixMode(GL.PROJECTION); // Set up the camera mode gl.loadIdentity(); // Reset the transformation matrix if (width <= height) gl.ortho( -50.0, 50.0, -50.0 * (double) height / (double) width, 50.0 * (double) height / (double) width, -1.0, 1.0); else gl.ortho( -50.0 * (double) width / (double) height, 50.0 * (double) width / (double) height, -50.0, 50.0, -1.0, 1.0); gl.matrixMode(GL.MODELVIEW); // Reset to model transforms }
// Simple redirector to our display function public void paint(Graphics g) { // First call the base class paint method to do a one time // Initialization - specific to the JoglCanvas class super.paint(g); // System.out.println("Call to paint"); display(); }
// Thread thingy to run at start thread public void run() { try { while (true) { gl.use(); spinDisplay(); Thread.sleep(10); } } catch (InterruptedException e) { // the user sent an interupt, // So lets exit... } }
// Sort of a legacy type function call, actually inside Java // This stuff should be in the paint call public synchronized void display() { // clear the screen and draw a yellow square gl.clear(GL.COLOR_BUFFER_BIT); // First rectangle gl.pushMatrix(); gl.translate(-0.5, 0.0, 0.0); gl.rotate(spin, 0.0, 0.0, 1.0); gl.rect(-0.5, -0.5, 0.5, 0.5); gl.popMatrix(); // Second rectangle gl.pushMatrix(); gl.translate(0.5, 0.0, 0.0); gl.rotate(-spin, 0.0, 0.0, 1.0); gl.rect(-0.5, -0.5, 0.5, 0.5); gl.popMatrix(); gl.flush(); // Make sure all commands have completed. gl.swap(); // Swap the render buffer with the screen buffer }
// Handle the keystrokes public void keyTyped(KeyEvent e) { switch (e.getKeyChar()) { case 'h': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(15.0, 0.0, 1.0, 0.0); break; case 'j': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(15.0, 1.0, 0.0, 0.0); break; case 'k': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(-15.0, 1.0, 0.0, 0.0); break; case 'l': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(-15.0, 0.0, 1.0, 0.0); break; case '+': gl.matrixMode(GL.PROJECTION); /* manipulate Projection matrix */ gl.translate(0.0, 0.0, 0.5); e.consume(); break; case '-': gl.matrixMode(GL.PROJECTION); /* manipulate Projection matrix */ gl.translate(0.0, 0.0, -0.5); e.consume(); break; case 's': if (r_thread == null) { r_thread = new Thread(this); r_thread.start(); } break; case 'p': if (r_thread != null) { r_thread.interrupt(); r_thread = null; } break; case 27: /* Esc will quit */ System.exit(1); break; default: break; } e.consume(); display(); }