Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jogltest;
- import java.awt.geom.AffineTransform;
- import java.awt.image.AffineTransformOp;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferByte;
- import java.io.File;
- import java.io.IOException;
- import java.nio.Buffer;
- import java.nio.ByteBuffer;
- import javax.imageio.ImageIO;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLCapabilities;
- import javax.media.opengl.GLContext;
- import javax.media.opengl.GLDrawableFactory;
- import javax.media.opengl.GLOffscreenAutoDrawable;
- import javax.media.opengl.GLProfile;
- import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
- public class TestOffScreen {
- public static void main(String[] args) {
- GL2 gl2;
- GLDrawableFactory fac = GLDrawableFactory.getFactory(GLProfile.getDefault());
- GLCapabilities glCap = new GLCapabilities(GLProfile.getDefault());
- // Without line below, there is an error on Windows.
- glCap.setDoubleBuffered(false);
- //makes a new buffer 100x100
- GLOffscreenAutoDrawable buf = fac.createOffscreenAutoDrawable(null, glCap, null, 100, 100);
- GLContext context = buf.createContext(null);
- context.makeCurrent();
- gl2 = context.getGL().getGL2();
- System.out.println(gl2.isFunctionAvailable( "glBindBuffer" )? "true":"false");
- gl2.glViewport(0, 0, 100, 100);
- gl2.glShadeModel(GL2.GL_SMOOTH);
- gl2.glClearColor(1.0f, 0.80f, 0.80f, 1); // This Will Clear The Background Color
- gl2.glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
- gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
- gl2.glLoadIdentity(); // Reset The Projection Matrix
- AWTGLReadBufferUtil agb = new AWTGLReadBufferUtil(buf.getGLProfile(), true);
- BufferedImage image = agb.readPixelsToBufferedImage(context.getGL(), true);
- try {
- ImageIO.write(image, "PNG", new File("wrong.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- // gets and write down same image but WITHOUT alpha channel
- image = new BufferedImage(100,100, BufferedImage.TYPE_3BYTE_BGR);
- DataBufferByte buffer = (DataBufferByte) image.getRaster().getDataBuffer();
- Buffer b = ByteBuffer.wrap(buffer.getData());
- gl2.glPixelStorei(GL2.GL_PACK_ALIGNMENT, 1);
- gl2.glReadPixels(0, 0, 100, 100, GL2.GL_BGR, GL2.GL_UNSIGNED_BYTE, b);
- context.release();
- AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
- tx.translate(0,-image.getHeight());
- AffineTransformOp op = new AffineTransformOp(tx,
- AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
- image = op.filter(image, null);
- try {
- ImageIO.write(image, "PNG", new File("right.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- context.destroy();
- buf.destroy();
- System.out.println("Done!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement