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/GLNewtRun.java | 31 +++-- src/demos/es1/RedSquare.java | 48 ++++--- src/demos/es1/cube/Cube.java | 28 +++- src/demos/es2/RedSquare.java | 5 - 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 +++++++++++ 10 files changed, 814 insertions(+), 38 deletions(-) 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') diff --git a/src/demos/GLNewtRun.java b/src/demos/GLNewtRun.java index dca68b4..0df07fc 100755 --- a/src/demos/GLNewtRun.java +++ b/src/demos/GLNewtRun.java @@ -93,6 +93,23 @@ public class GLNewtRun implements WindowListener, KeyListener, MouseListener { return def; } + public static boolean setField(Object instance, String fieldName, Object value) { + try { + Field f = instance.getClass().getField(fieldName); + if(f.getType().isInstance(value)) { + f.set(instance, value); + return true; + } else { + System.out.println(instance.getClass()+" '"+fieldName+"' field not assignable with "+value.getClass()+", it's a: "+f.getType()); + } + } catch (NoSuchFieldException nsfe) { + System.out.println(instance.getClass()+" has no '"+fieldName+"' field"); + } catch (Throwable t) { + t.printStackTrace(); + } + return false; + } + public static void main(String[] args) { boolean parented = false; boolean useAWTTestFrame = false; @@ -208,18 +225,8 @@ public class GLNewtRun implements WindowListener, KeyListener, MouseListener { } window = GLWindow.create(nWindow); - try { - Field f = demo.getClass().getField("window"); - if(f.getType().isInstance(window)) { - f.set(demo, window); - } else { - System.out.println("Demo's 'window' field not a Window, but: "+f.getType()); - - } - } catch (NoSuchFieldException nsfe) { - System.out.println("Demo has no 'window' field"); - } catch (Throwable t) { - t.printStackTrace(); + if(!setField(demo, "window", window)) { + setField(demo, "glWindow", window); } window.addWindowListener(listener); diff --git a/src/demos/es1/RedSquare.java b/src/demos/es1/RedSquare.java index 02e8454..53485b0 100755 --- a/src/demos/es1/RedSquare.java +++ b/src/demos/es1/RedSquare.java @@ -14,6 +14,9 @@ import com.sun.javafx.newt.opengl.*; public class RedSquare extends Thread implements WindowListener, KeyListener, MouseListener, GLEventListener { + public static boolean glDebugEmu = false; + public static boolean glDebug = false ; + public static boolean glTrace = false ; public GLWindow window; private GLProfile glp; private GLU glu; @@ -171,15 +174,18 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo public void init(GLAutoDrawable drawable) { GL _gl = drawable.getGL(); - if(debugffemu) { - debuggl = false; + if(glDebugEmu) { try { // Debug .. _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Debug", GL2ES2.class, _gl, null) ); - // Trace .. - _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", GL2ES2.class, _gl, new Object[] { System.err } ) ); + if(glTrace) { + // Trace .. + _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", GL2ES2.class, _gl, new Object[] { System.err } ) ); + } } catch (Exception e) {e.printStackTrace();} + glDebug = false; + glTrace = false; } GL2ES1 gl = FixedFuncUtil.getFixedFuncImpl(_gl); @@ -187,7 +193,21 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo gl.setSwapInterval(swapInterval); } - glu = GLU.createGLU(); + if(glDebug) { + try { + // Debug .. + gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Debug", GL2ES1.class, gl, null) ); + } catch (Exception e) {e.printStackTrace();} + } + + if(glTrace) { + try { + // Trace .. + gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", GL2ES1.class, gl, new Object[] { System.err } ) ); + } catch (Exception e) {e.printStackTrace();} + } + + glu = GLU.createGLU(gl); System.err.println(glp+" Entering initialization"); System.err.println(glp+" GL Profile: "+gl.getGLProfile()); @@ -198,16 +218,6 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo System.err.println(glp+" swapInterval: " + swapInterval + " (GL: "+gl.getSwapInterval()+")"); System.err.println(glp+" GLU: " + glu); - if(debuggl) { - try { - // Debug .. - gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Debug", GL2ES1.class, gl, null) ); - - // Trace .. - gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", GL2ES1.class, gl, new Object[] { System.err } ) ); - } catch (Exception e) {e.printStackTrace();} - } - // Allocate vertex arrays colors = BufferUtil.newFloatBuffer(16); vertices = BufferUtil.newFloatBuffer(12); @@ -281,8 +291,6 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo public static boolean oneThread = false; public static boolean pumpOnce = true; public static int swapInterval = -1; - public static boolean debuggl = false; - public static boolean debugffemu = false; public static void main(String[] args) { int type = USE_NEWT ; @@ -293,10 +301,12 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo try { swapInterval = Integer.parseInt(args[i]); } catch (Exception ex) { ex.printStackTrace(); } + } else if(args[i].equals("-trace")) { + glTrace=true; } else if(args[i].equals("-debug")) { - debuggl=true; + glDebug=true; } else if(args[i].equals("-debugff")) { - debugffemu=true; + glDebugEmu=true; } else if(args[i].equals("-pumponce")) { pumpOnce=true; } else if(args[i].equals("-1thread")) { diff --git a/src/demos/es1/cube/Cube.java b/src/demos/es1/cube/Cube.java index d9c00b2..d46f41b 100644 --- a/src/demos/es1/cube/Cube.java +++ b/src/demos/es1/cube/Cube.java @@ -43,6 +43,8 @@ import com.sun.javafx.newt.*; import com.sun.javafx.newt.opengl.*; public class Cube implements GLEventListener { + public boolean glDebug = false ; + public boolean glTrace = false ; boolean quit = false; public Cube () { @@ -77,7 +79,23 @@ public class Cube implements GLEventListener { } public void init(GLAutoDrawable drawable) { - GL2ES1 gl = FixedFuncUtil.getFixedFuncImpl(drawable.getGL()); + GL _gl = drawable.getGL(); + + _gl.glGetError(); // flush error .. + + if(glDebug) { + try { + _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Debug", null, _gl, null) ); + } catch (Exception e) {e.printStackTrace();} + } + + if(glTrace) { + try { + _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", null, _gl, new Object[] { System.err } ) ); + } catch (Exception e) {e.printStackTrace();} + } + + GL2ES1 gl = FixedFuncUtil.getFixedFuncImpl(_gl); glu = GLU.createGLU(); @@ -179,9 +197,15 @@ public class Cube implements GLEventListener { } public void display(GLAutoDrawable drawable) { + display(drawable, true); + } + + public void display(GLAutoDrawable drawable, boolean clear) { GL2ES1 gl = drawable.getGL().getGL2ES1(); - gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); + if(clear) { + gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); + } gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity(); diff --git a/src/demos/es2/RedSquare.java b/src/demos/es2/RedSquare.java index 4d518a2..2acb321 100755 --- a/src/demos/es2/RedSquare.java +++ b/src/demos/es2/RedSquare.java @@ -3,7 +3,6 @@ package demos.es2; import java.nio.*; import java.util.*; import javax.media.opengl.*; -import javax.media.opengl.glu.*; import javax.media.nativewindow.*; import com.sun.opengl.util.*; @@ -16,7 +15,6 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo public GLWindow window; private GLProfile glp; - private GLU glu; private boolean quit = false; private String glprofile; private int type; @@ -189,8 +187,6 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo gl.setSwapInterval(swapInterval); } - glu = GLU.createGLU(); - System.err.println(glp+" Entering initialization"); System.err.println(glp+" GL Profile: "+gl.getGLProfile()); System.err.println(glp+" GL:" + gl); @@ -198,7 +194,6 @@ public class RedSquare extends Thread implements WindowListener, KeyListener, Mo System.err.println(glp+" GL_EXTENSIONS:"); System.err.println(glp+" " + gl.glGetString(gl.GL_EXTENSIONS)); System.err.println(glp+" swapInterval: " + swapInterval + " (GL: "+gl.getSwapInterval()+")"); - System.err.println(glp+" GLU: " + glu); if(debuggl) { try { 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