Advertisement
Guest User

JOGL test for OffScreen buffer

a guest
Aug 11th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1.  
  2.  
  3. package jogltest;
  4.  
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8.  
  9. import javax.imageio.ImageIO;
  10. import javax.media.opengl.GL2;
  11. import javax.media.opengl.GLCapabilities;
  12. import javax.media.opengl.GLContext;
  13. import javax.media.opengl.GLDrawableFactory;
  14. import javax.media.opengl.GLOffscreenAutoDrawable;
  15. import javax.media.opengl.GLProfile;
  16.  
  17. import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
  18.  
  19. public class TestOffScreen {
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         GL2 gl2;
  24.        
  25.         GLDrawableFactory fac = GLDrawableFactory.getFactory(GLProfile.getDefault());
  26.         GLCapabilities glCap = new GLCapabilities(GLProfile.getDefault());
  27.         // Without line below, there is an error on Windows.
  28.         glCap.setDoubleBuffered(false);
  29.         //makes a new buffer 100x100
  30.         GLOffscreenAutoDrawable buf = fac.createOffscreenAutoDrawable(null, glCap, null, 100, 100);
  31.         GLContext context =  buf.createContext(null);
  32.         context.makeCurrent();
  33.         gl2 = context.getGL().getGL2();
  34.         gl2.glViewport(0, 0, 100, 100);
  35.         gl2.glShadeModel(GL2.GL_SMOOTH);
  36.         gl2.glClearColor(1.0f, 0.80f, 0.80f, 1);    // This Will Clear The Background Color
  37.         gl2.glClearDepth(1.0);                    // Enables Clearing Of The Depth Buffer
  38.         gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
  39.         gl2.glLoadIdentity();                    // Reset The Projection Matrix
  40.         AWTGLReadBufferUtil agb = new AWTGLReadBufferUtil(buf.getGLProfile(), true);
  41.         BufferedImage image = agb.readPixelsToBufferedImage(context.getGL(), true);
  42.         try {
  43.             ImageIO.write(image, "PNG", new File("test.png"));
  44.         } catch (IOException e) {
  45.             e.printStackTrace();
  46.         }
  47.         context.destroy();
  48.         buf.destroy();
  49.         System.out.println("Done!");
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement