From 4b9a72c24bb97ed81c79c3931d4ffb5fa53d9978 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sun, 4 Oct 2009 00:19:32 -0700 Subject: Add ReadBuffer Demos/ModuleTests; Incl. SurfaceUpdated Listener, ReadPixel to file, texture - direct or via attaching the drawable to another context as it's readbuffer --- src/demos/readbuffer/Main.java | 207 ++++++++++++++++++++++++++++ src/demos/readbuffer/ReadBuffer2File.java | 74 ++++++++++ src/demos/readbuffer/ReadBuffer2Screen.java | 176 +++++++++++++++++++++++ src/demos/readbuffer/ReadBufferBase.java | 95 +++++++++++++ src/demos/readbuffer/ReadBufferUtil.java | 109 +++++++++++++++ src/demos/readbuffer/Surface2File.java | 79 +++++++++++ 6 files changed, 740 insertions(+) create mode 100755 src/demos/readbuffer/Main.java create mode 100755 src/demos/readbuffer/ReadBuffer2File.java create mode 100755 src/demos/readbuffer/ReadBuffer2Screen.java create mode 100755 src/demos/readbuffer/ReadBufferBase.java create mode 100755 src/demos/readbuffer/ReadBufferUtil.java create mode 100755 src/demos/readbuffer/Surface2File.java (limited to 'src/demos/readbuffer') diff --git a/src/demos/readbuffer/Main.java b/src/demos/readbuffer/Main.java new file mode 100755 index 0000000..ccc3988 --- /dev/null +++ b/src/demos/readbuffer/Main.java @@ -0,0 +1,207 @@ +package demos.readbuffer; + +import java.lang.reflect.*; +import java.nio.*; +import javax.media.opengl.*; +import javax.media.nativewindow.*; +import com.sun.javafx.newt.*; +import com.sun.javafx.newt.opengl.*; + +public class Main implements WindowListener, MouseListener, SurfaceUpdatedListener { + + public boolean quit = false; + public GLWindow window = null; + + public void surfaceUpdated(Object updater, NativeWindow window, long when) { + if(null!=window) { + this.window.display(); + } + } + + public void windowResized(WindowEvent e) { } + public void windowMoved(WindowEvent e) { } + public void windowGainedFocus(WindowEvent e) { } + public void windowLostFocus(WindowEvent e) { } + public void windowDestroyNotify(WindowEvent e) { + quit = true; + } + + public void mouseClicked(MouseEvent e) { + System.out.println("mouseevent: "+e); + switch(e.getClickCount()) { + case 1: + if(null!=window) { + window.setFullscreen(!window.isFullscreen()); + } + break; + default: + quit=true; + break; + } + } + public void mouseEntered(MouseEvent e) { + } + public void mouseExited(MouseEvent e) { + } + public void mousePressed(MouseEvent e) { + } + public void mouseReleased(MouseEvent e) { + } + public void mouseMoved(MouseEvent e) { + } + public void mouseDragged(MouseEvent e) { + } + public void mouseWheelMoved(MouseEvent e) { + } + + public GLWindow createOffscreen(GLCapabilities caps, int w, int h) { + GLCapabilities capsOffscreen = (GLCapabilities) caps.clone(); + capsOffscreen.setOnscreen(false); + capsOffscreen.setPBuffer(true); + GLWindow windowOffscreen = GLWindow.create(capsOffscreen); + windowOffscreen.enablePerfLog(true); + windowOffscreen.setSize(w, h); + windowOffscreen.setVisible(true); + return windowOffscreen; + } + + private void run(int typeNewt, boolean fullscreen, int typeTest, GLEventListener demo) { + int width = 800; + int height = 480; + System.out.println("readbuffer.Main.run() Test: "+typeTest); + try { + GLCapabilities caps = new GLCapabilities(null); + + // Full init pbuffer window .. + GLWindow windowOffscreen = createOffscreen(caps, width, height); + // setField(demo, "glDebug", new Boolean(true)); + // setField(demo, "glTrace", new Boolean(true)); + if(!setField(demo, "window", windowOffscreen)) { + setField(demo, "glWindow", windowOffscreen); + } + windowOffscreen.addGLEventListener(demo); + + if ( TEST_SURFACE2FILE < typeTest ) { + System.out.println("readbuffer.Main.run() Using a target onscreen window with read drawable attachment"); + // Setup init onscreen window .. + Window nWindow = null; + if(0!=(typeNewt&USE_AWT)) { + Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display + Screen nScreen = NewtFactory.createScreen(NativeWindowFactory.TYPE_AWT, nDisplay, 0); // screen 0 + nWindow = NewtFactory.createWindow(NativeWindowFactory.TYPE_AWT, nScreen, caps); + window = GLWindow.create(nWindow); + } else { + window = GLWindow.create(caps); + } + + window.addWindowListener(this); + window.addMouseListener(this); + + window.setSize(width, height); + window.setFullscreen(fullscreen); + window.setVisible(true); + } + + GLDrawable readDrawable = windowOffscreen.getContext().getGLDrawable() ; + + if ( TEST_SURFACE2FILE == typeTest ) { + Surface2File s2f = new Surface2File(); + windowOffscreen.addSurfaceUpdatedListener(s2f); + } else if ( TEST_READBUFFER2FILE == typeTest ) { + ReadBuffer2File readDemo = new ReadBuffer2File( readDrawable ) ; + window.addGLEventListener(readDemo); + } else if ( TEST_READBUFFER2SCREEN == typeTest ) { + ReadBuffer2Screen readDemo = new ReadBuffer2Screen( readDrawable ) ; + window.addGLEventListener(readDemo); + } + + if(null!=window) { + windowOffscreen.addSurfaceUpdatedListener(this); + } + + System.out.println("+++++++++++++++++++++++++++"); + System.out.println(windowOffscreen); + System.out.println("+++++++++++++++++++++++++++"); + + while ( !quit && windowOffscreen.getDuration() < 31000) { + // System.out.println("..............................."); + windowOffscreen.display(); + } + + // Shut things down cooperatively + window.destroy(); + windowOffscreen.destroy(); + window.getFactory().shutdown(); + System.out.println("readbuffer.Main shut down cleanly."); + } catch (GLException e) { + e.printStackTrace(); + } + } + + public static int USE_NEWT = 0; + public static int USE_AWT = 1 << 0; + + public static int TEST_SURFACE2FILE = 0; + public static int TEST_READBUFFER2FILE = 1; + public static int TEST_READBUFFER2SCREEN = 2; + + public static void main(String[] args) { + boolean fullscreen = false; + int typeNewt = USE_NEWT ; + int typeTest = TEST_SURFACE2FILE; + int i=0; + while(ireadPixelSizeLast) { + readPixelBuffer = BufferUtil.newGLBuffer(gl.GL_UNSIGNED_BYTE, readPixelSize); + readPixelSizeLast = readPixelSize ; + try { + readTextureData = new TextureData( + gl.GL_RGBA, + drawable.getWidth(), drawable.getHeight(), + 0, + gl.GL_RGB, + gl.GL_UNSIGNED_BYTE, + false, false, + false /* flip */, + readPixelBuffer, + null /* Flusher */); + newData = true; + } catch (Exception e) { + e.printStackTrace(); + readTextureData = null; + readPixelBuffer = null; + readPixelSizeLast = 0; + } + } + if(null!=readPixelBuffer) { + readPixelBuffer.clear(); + gl.glReadPixels(0, 0, drawable.getWidth(), drawable.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, readPixelBuffer); + readPixelBuffer.rewind(); + if(newData) { + readTexture.updateImage(readTextureData); + } else { + readTexture.updateSubImage(readTextureData, 0, + 0, 0, // src offset + 0, 0, // dst offset + drawable.getWidth(), drawable.getHeight()); + } + readPixelBuffer.rewind(); + } + } + + public void dispose() { + readTexture.dispose(); + readTextureData = null; + readPixelBuffer.clear(); + readPixelBuffer = null; + readPixelSizeLast = 0; + } + +} + diff --git a/src/demos/readbuffer/Surface2File.java b/src/demos/readbuffer/Surface2File.java new file mode 100755 index 0000000..12a5256 --- /dev/null +++ b/src/demos/readbuffer/Surface2File.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + */ + +package demos.readbuffer; + +import java.nio.*; +import javax.media.opengl.*; + +import com.sun.opengl.util.texture.TextureData; +import com.sun.opengl.util.BufferUtil; + +import com.sun.opengl.util.texture.spi.awt.IIOTextureWriter; +import java.io.File; +import java.io.IOException; + +import javax.media.nativewindow.*; + +public class Surface2File implements SurfaceUpdatedListener { + + ReadBufferUtil readBufferUtil = new ReadBufferUtil(); + IIOTextureWriter textureWriter = new IIOTextureWriter(); + int shotNum=0; + + public void dispose() { + readBufferUtil.dispose(); + } + + public void surfaceUpdated(Object updater, NativeWindow window, long when) { + if(updater instanceof GLDrawable) { + GLDrawable drawable = (GLDrawable) updater; + GLContext ctx = GLContext.getCurrent(); + if(null!=ctx && ctx.getGLDrawable()==drawable) { + readBufferUtil.fetchOffscreenTexture(drawable, ctx.getGL()); + surface2File("/tmp/shot/shot"); + } + } + } + + public void surface2File(String basename) { + if(!readBufferUtil.isValid()) return; + + try { + textureWriter.write(new File(basename+"-"+shotNum+".jpg"), readBufferUtil.getTextureData()); + shotNum++; + } catch (IOException ioe) { ioe.printStackTrace(); } + readBufferUtil.rewindPixelBuffer(); + } +} + -- cgit v1.2.3