// ------------------------------------------------------------------ // Points and Lines example // by REAS // ported to JOGL by Philipp Seifried // all the example specific code in this sketch is to be found at // the beginning of the sketch and in the display() function. // Please remember that you will also need the JOGL natives for // this to work. For instructions, please refer to // http://www.repeatwhiletrue.com/p5/P5JOGL.html // ------------------------------------------------------------------ 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! // -- this is the original Processing setup code: /* int d = 40; int p1 = d; int p2 = p1+d; int p3 = p2+d; int p4 = p3+d; */ // -- we will have to modify it in order to adapt to openGLs coordinate system: // where our original coordinates go from 0 to 200 (the width of the original window), the coordinate system of our openGL window goes from -1 to 1 float d = 0.4; // 200 (the width of the window) / 40 = 5; 2/5 = 0.4 float p1 = -(1-d); // -0.6 float p2 = p1+d; // -0.2 float p3 = p2+d; // 0.2 float p4 = p3+d; // 0.6 // also, I have modified the drawing code (see display() function) to flip the drawing horizontally to make it look like the original example. // ------------------------------------------ void setup() { // ----------------- Add your own stuff here! size(320,240); fullscreen = false; // ... // ... // ... // ------------------------------------------ // set up our window oglFrame = new Frame("Our very own OpenGL Window!"); oglFrame.setSize( 200, 200 ); // ------------------ size(200,200) 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(true); // double buffered canvas = GLDrawableFactory.getFactory().createGLCanvas( glCaps ); keyHandler = new KeyInputHandler(); mouseHandler = new MouseInputHandler(); // 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! // ... // ... // ... // ------------------------------------------ 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(); // ----------------- Add your own stuff here! // ... // ------------------------------------------ } public void display(GLDrawable drawable) { // -------- this is our render loop! 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 // -------- this is the original Processing drawing code: /* background(0); // Draw gray box stroke(153); line(p3, p3, p2, p3); line(p2, p3, p2, p2); line(p2, p2, p3, p2); line(p3, p2, p3, p3); // Draw white points stroke(255); point(p1, p1); point(p1, p3); point(p2, p4); point(p3, p1); point(p4, p2); point(p4, p4); */ // ------------------------------------------ // ----------------- This is our translation! gl.glClearColor(0,0,0,0); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glColor3f(0.6,0.6,0.6); // 153/255 = 0.6 gl.glBegin(GL.GL_LINES); // line (p3,p3, p2,p3); gl.glVertex2f(-p3, p3); gl.glVertex2f(-p2, p3); // line (p2,p3, p2,p2); gl.glVertex2f(-p2, p3); gl.glVertex2f(-p2, p2); // line (p2,p2, p3,p2); gl.glVertex2f(-p2, p2); gl.glVertex2f(-p3, p2); // line (p3,p2, p3,p3); gl.glVertex2f(-p3, p2); gl.glVertex2f(-p3, p3); gl.glEnd(); gl.glColor3f(1.0,1.0,1.0); // 255/255 = 1.0 gl.glBegin(GL.GL_POINTS); gl.glVertex2f(p1, p1); gl.glVertex2f(p1, p3); gl.glVertex2f(p2, p4); gl.glVertex2f(p3, p1); gl.glVertex2f(p4, p2); gl.glVertex2f(p4, p4); gl.glEnd(); // ------------------------------------------ // 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! // ... // ------------------------------------------ } } void makeTexture(int textureID, BImage img) { makeTexture(textureID, img.pixels, img.width, img.height); } void makeTexture(int textureID, int[] imageData, int imageWidth, int imageHeight) { java.nio.ByteBuffer imgBuffer = java.nio.ByteBuffer.allocateDirect(imageHeight*imageWidth*4); // create a new direct ByteBuffer with the capacity of the BImage imgBuffer.order(java.nio.ByteOrder.nativeOrder()); // Set the byte order of our ByteBuffer java.nio.IntBuffer pixelData = imgBuffer.asIntBuffer(); // get an interpretation of the ByteBuffer as an IntBuffer pixelData.put(imageData, 0, imageData.length); GL gl = canvas.getGL(); gl.glBindTexture(GL.GL_TEXTURE_2D, textureID ); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, imageWidth, imageHeight, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, imgBuffer ); } // ----------------------------------------------------------- // The following two functions have been adapted from a posting by // keving on the JOGL board. The original post can be found here: // http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=jogl;action=display;num=1091095319;start=2#2 // ----------------------------------------------------------- int[] copyFrame() { // copies the Frame to an integer array int framewidth = canvas.getSize().width; // get the canvas' dimensions int frameheight = canvas.getSize().height; java.nio.ByteBuffer pixelsRGB = BufferUtils.newByteBuffer(framewidth * frameheight * 3); // create a ByteBuffer to hold the image data GL gl = canvas.getGL(); // acquire our GL Object // read the Frame back into our ByteBuffer gl.glReadBuffer(GL.GL_BACK); gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1); gl.glReadPixels(0, 0, framewidth, frameheight, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixelsRGB); int[] pixelInts = new int[framewidth * frameheight]; // Convert RGB bytes to ARGB ints with no transparency. Flip image vertically by reading the // rows of pixels in the byte buffer in reverse - (0,0) is at bottom left in OpenGL. int p = framewidth * frameheight * 3; // Points to first byte (red) in each row. int q; // Index into ByteBuffer int i = 0; // Index into target int[] int w3 = framewidth*3; // Number of bytes in each row for (int row = 0; row < frameheight; row++) { p -= w3; q = p; for (int col = 0; col < framewidth; col++) { int iR = pixelsRGB.get(q++); int iG = pixelsRGB.get(q++); int iB = pixelsRGB.get(q++); pixelInts[i++] = 0xFF000000 | ((iR & 0x000000FF) << 16) | ((iG & 0x000000FF) << 8) | (iB & 0x000000FF); } } return pixelInts; } void saveFrameAsPNG(String fileName) { // saves a PNG File outputFile = new File(fileName); // create a File Object int[] pixelInts = copyFrame(); // get the frame's pixel data int framewidth = canvas.getSize().width; // get the canvas' dimensions int frameheight = canvas.getSize().height; // create a new BufferedImage from the pixeldata. BufferedImage bufferedImage = new BufferedImage(framewidth, frameheight, BufferedImage.TYPE_INT_ARGB); bufferedImage.setRGB(0, 0, framewidth, frameheight, pixelInts, 0, framewidth); // write the file. try { javax.imageio.ImageIO.write(bufferedImage, "PNG", outputFile); } catch (IOException e) { e.printStackTrace(); } }