// ------------------------------------------------------------------ // This code was written by Philipp Seifried, except where // otherwise stated. // You're permitted to do whatever you want with the parts // I wrote, including modification, distribution and // removal of this notice, as long as I will not be held // responsible for any harm that results in the use of // this code. // Remember that for this to work, you will also need the // JOGL classes to be found at // https://jogl.dev.java.net/ // For instructions, refer to // http://www.repeatwhiletrue.com/p5/P5JOGL.html // Have fun! // ------------------------------------------------------------------ Frame oglFrame; // our openGL Window GLCanvas canvas; // the component openGL will render to JOGLRenderer renderer; // our custom event listener that will do the rendering boolean fullscreen; KeyInputHandler keyHandler; MouseInputHandler mouseHandler; int oglMouseX, oglMouseY, oglPMouseX, oglPMouseY; // ----------------- Add your own stuff here! float ourVariable; // We will use this one to do some fancy color warping. float ourZ; // ------------------------------------------ void setup() { // ----------------- Add your own stuff here! size(320,240); ourVariable = 0; ourZ = -3; fullscreen = true; keyHandler = new KeyInputHandler(); mouseHandler = new MouseInputHandler(); // ... // ... // ... // ------------------------------------------ // set up our window oglFrame = new Frame("Our very own OpenGL Window!"); oglFrame.setSize( 800, 600 ); renderer = new JOGLRenderer(); // set the properties of our openGL component... GLCapabilities glCaps = new GLCapabilities(); glCaps.setRedBits(8); // 32 bit color resolution glCaps.setBlueBits(8); glCaps.setGreenBits(8); glCaps.setAlphaBits(8); glCaps.setDoubleBuffered(false); // double buffered canvas = GLDrawableFactory.getFactory().createGLCanvas( glCaps ); // add the renderer to the component and the component to our window. canvas.addGLEventListener(renderer); canvas.addKeyListener(keyHandler); canvas.addMouseListener(mouseHandler); canvas.addMouseMotionListener(mouseHandler); oglFrame.add( canvas ); // let the program exit if our openGL window is shut down. oglFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (fullscreen) { GraphicsEnvironment ge=null; GraphicsDevice gd=null; ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); gd = ge.getDefaultScreenDevice(); if( gd.isFullScreenSupported() ) { gd.setFullScreenWindow(null); } } System.exit(0); } }); if (fullscreen) { oglFrame.setUndecorated(true); // We don't want any title bar for this window GraphicsEnvironment ge=null; GraphicsDevice gd=null; ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); gd = ge.getDefaultScreenDevice(); if( gd.isFullScreenSupported() ) { gd.setFullScreenWindow(oglFrame); } } // show our openGL window! oglFrame.setVisible(true); oglFrame.setFocusable(true); oglFrame.requestFocus(); } void loop() { // ----------------- Add your own stuff here! if (mousePressed) { ourVariable += 0.1; } // ... // ... // ... // ------------------------------------------ canvas.display(); } class JOGLRenderer implements GLEventListener { boolean isFirstFrame; public void init(GLDrawable drawable) { // fetch GL and GLU instances from our GLCanvas GL gl = drawable.getGL(); GLU glu = drawable.getGLU(); isFirstFrame = true; // ----------------- Add your own stuff here! gl.glShadeModel(GL.GL_SMOOTH); // enable smooth shading gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set the background color gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // nice perspective correction gl.glEnable(GL.GL_DEPTH_TEST); // enable the z-Buffer gl.glDepthFunc(GL.GL_LEQUAL); // the z comparison function gl.glClearDepth(1.0f); // set the depth the z-Buffer will be cleared to gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); // clear the screen glu.gluPerspective(45.0f,(float)(800)/(float)(600),0.1f,100.0f); // and set up a perspective. // ------------------------------------------ } public void display(GLDrawable drawable) { // -------- this is our render loop! GL gl = drawable.getGL(); // gl should be fetched like this in every frame... if (isFirstFrame) { isFirstFrame = false; gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); // clear the screen } // ----------------- Add your own stuff here! ourZ += 0.02; if (ourZ > -1) {ourZ = -3;} if (mousePressed) { // If the mouse is pressed... float a = sin(ourVariable)/2+0.5; float b = sin(ourVariable*1.3)/2+0.5; float c = sin(ourVariable*1.9)/2+0.5; // mouse coordinates are pixel distances from the top left corner of the window. // we will need to convert them to floating point values ranging from -1 to 1 float x = ((float) oglMouseX / (float) drawable.getSize().getWidth())*2-1; float y = -(((float) oglMouseY / (float) drawable.getSize().getHeight())*2-1); gl.glBegin( GL.GL_TRIANGLES ); // draw a triangle gl.glColor3f(a, 0.5, c); gl.glVertex3f( x, y+0.1, ourZ ); gl.glColor3f(0.5, 1-a, b); gl.glVertex3f( x-0.1, y-0.1, ourZ ); gl.glColor3f(c, 1-c, 1); gl.glVertex3f( x+0.1, y-0.1, ourZ ); gl.glEnd(); } if (keyPressed && key == ' ') { // If the spacebar is pressed... gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); // clear the screen } // ------------------------------------------ // update the "previous Mouse" positions oglPMouseX = oglMouseX; oglPMouseY = oglMouseY; } public void reshape(GLDrawable drawable, int x, int y, int width, int height) { // -------- this will be called whenever our openGL window is resized! GL gl = drawable.getGL(); // gl should be fetched like this in every frame... gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT ); // clear the screen } public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) { // -------- this is not implemented in JOGL yet. Best to leave it alone. } } class KeyInputHandler extends KeyAdapter { public void keyPressed(KeyEvent e) { key = e.getKeyChar(); keyPressed = true; // ----------------- Add your own stuff here! // ... // ------------------------------------------ } public void keyReleased(KeyEvent e) { key = e.getKeyChar(); keyPressed = false; // ----------------- Add your own stuff here! // ... // ------------------------------------------ } public void keyTyped(KeyEvent e) { key = e.getKeyChar(); if (e.getKeyChar() == 27) { // Escape key closes the application! if (fullscreen) { GraphicsEnvironment ge=null; GraphicsDevice gd=null; ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); gd = ge.getDefaultScreenDevice(); if( gd.isFullScreenSupported() ) { gd.setFullScreenWindow(null); } } System.exit(0); } // ----------------- Add your own stuff here! // ... // ------------------------------------------ } } class MouseInputHandler extends javax.swing.event.MouseInputAdapter { public void mouseDragged(MouseEvent e) { oglMouseX = e.getX(); oglMouseY = e.getY(); // ----------------- Add your own stuff here! // ... // ------------------------------------------ } public void mouseMoved(MouseEvent e) { oglMouseX = e.getX(); oglMouseY = e.getY(); // ----------------- Add your own stuff here! // ... // ------------------------------------------ } public void mousePressed(MouseEvent e) { oglMouseX = e.getX(); oglMouseY = e.getY(); mousePressed = true; // ----------------- Add your own stuff here! // ... // ------------------------------------------ } public void mouseReleased(MouseEvent e) { oglMouseX = e.getX(); oglMouseY = e.getY(); mousePressed = false; // ----------------- Add your own stuff here! // ... // ------------------------------------------ } public void mouseClicked(MouseEvent e) { oglMouseX = e.getX(); oglMouseY = e.getY(); // ----------------- Add your own stuff here! // ... // ------------------------------------------ } }