Advertisement
Guest User

JOGL workaround for OSX Mavericks

a guest
Sep 23rd, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1.  
  2.  
  3. package jogltest;
  4.  
  5. import java.awt.geom.AffineTransform;
  6. import java.awt.image.AffineTransformOp;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.DataBufferByte;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.nio.Buffer;
  12. import java.nio.ByteBuffer;
  13.  
  14. import javax.imageio.ImageIO;
  15. import javax.media.opengl.GL2;
  16. import javax.media.opengl.GLCapabilities;
  17. import javax.media.opengl.GLContext;
  18. import javax.media.opengl.GLDrawableFactory;
  19. import javax.media.opengl.GLOffscreenAutoDrawable;
  20. import javax.media.opengl.GLProfile;
  21.  
  22. import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
  23.  
  24. public class TestOffScreen {
  25.  
  26.     public static void main(String[] args) {
  27.  
  28.         GL2 gl2;
  29.        
  30.         GLDrawableFactory fac = GLDrawableFactory.getFactory(GLProfile.getDefault());
  31.         GLCapabilities glCap = new GLCapabilities(GLProfile.getDefault());
  32.         // Without line below, there is an error on Windows.
  33.         glCap.setDoubleBuffered(false);
  34.         //makes a new buffer 100x100
  35.         GLOffscreenAutoDrawable buf = fac.createOffscreenAutoDrawable(null, glCap, null, 100, 100);
  36.         GLContext context =  buf.createContext(null);
  37.         context.makeCurrent();
  38.         gl2 = context.getGL().getGL2();
  39.         System.out.println(gl2.isFunctionAvailable( "glBindBuffer" )? "true":"false");
  40.         gl2.glViewport(0, 0, 100, 100);
  41.         gl2.glShadeModel(GL2.GL_SMOOTH);
  42.         gl2.glClearColor(1.0f, 0.80f, 0.80f, 1);    // This Will Clear The Background Color
  43.         gl2.glClearDepth(1.0);                    // Enables Clearing Of The Depth Buffer
  44.         gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
  45.         gl2.glLoadIdentity();                    // Reset The Projection Matrix
  46.         AWTGLReadBufferUtil agb = new AWTGLReadBufferUtil(buf.getGLProfile(), true);
  47.         BufferedImage image = agb.readPixelsToBufferedImage(context.getGL(), true);
  48.         try {
  49.             ImageIO.write(image, "PNG", new File("wrong.png"));
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.         // gets and write down same image but WITHOUT alpha channel
  54.         image = new BufferedImage(100,100, BufferedImage.TYPE_3BYTE_BGR);
  55.         DataBufferByte buffer = (DataBufferByte) image.getRaster().getDataBuffer();
  56.         Buffer b = ByteBuffer.wrap(buffer.getData());
  57.         gl2.glPixelStorei(GL2.GL_PACK_ALIGNMENT, 1);
  58.         gl2.glReadPixels(0, 0, 100, 100, GL2.GL_BGR, GL2.GL_UNSIGNED_BYTE, b);
  59.         context.release();
  60.         AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
  61.         tx.translate(0,-image.getHeight());
  62.         AffineTransformOp op = new AffineTransformOp(tx,
  63.             AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
  64.         image = op.filter(image, null);
  65.         try {
  66.             ImageIO.write(image, "PNG", new File("right.png"));
  67.         } catch (IOException e) {
  68.             e.printStackTrace();
  69.         }
  70.         context.destroy();
  71.         buf.destroy();
  72.         System.out.println("Done!");
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement