diff options
-rw-r--r-- | make/build.xml | 12 | ||||
-rwxr-xr-x | src/demos/GLNewtRun.java | 31 | ||||
-rwxr-xr-x | src/demos/es1/RedSquare.java | 48 | ||||
-rw-r--r-- | src/demos/es1/cube/Cube.java | 28 | ||||
-rwxr-xr-x | src/demos/es2/RedSquare.java | 5 | ||||
-rwxr-xr-x | src/demos/readbuffer/Main.java | 207 | ||||
-rwxr-xr-x | src/demos/readbuffer/ReadBuffer2File.java | 74 | ||||
-rwxr-xr-x | src/demos/readbuffer/ReadBuffer2Screen.java | 176 | ||||
-rwxr-xr-x | src/demos/readbuffer/ReadBufferBase.java | 95 | ||||
-rwxr-xr-x | src/demos/readbuffer/ReadBufferUtil.java | 109 | ||||
-rwxr-xr-x | src/demos/readbuffer/Surface2File.java | 79 |
11 files changed, 822 insertions, 42 deletions
diff --git a/make/build.xml b/make/build.xml index a59134e..dd14d05 100644 --- a/make/build.xml +++ b/make/build.xml @@ -197,12 +197,16 @@ <include name="demos/data/**" /> </fileset> </jar> - <!-- Build a source archive as well --> + <!-- Build a source archive as well --> + <antcall target="all-src-zip" inheritrefs="true" /> + </target> + + <target name="all-src-zip" unless="skipSourceZIP"> <delete file="${jogl.demos.src.zip}" /> <zip destfile="${jogl.demos.src.zip}" - basedir="../.." - includes="jogl-demos/doc/**,jogl-demos/make/**,jogl-demos/src/**,jogl-demos/lib/**" - excludes="**/*.class,**/*~" + basedir="../.." + includes="jogl-demos/doc/**,jogl-demos/make/**,jogl-demos/src/**,jogl-demos/lib/**" + excludes="**/*.class,**/*~" /> </target> 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(i<args.length-1) { + if(args[i].equals("-awt")) { + typeNewt |= USE_AWT; + } else if(args[i].equals("-fs")) { + fullscreen = true; + } else if(args[i].equals("-test")) { + i++; + typeTest = str2int(args[i], typeTest); + } + i++; + } + String demoClassName = args[i]; + Object demoObject = null; + + try { + Class demoClazz = Class.forName(demoClassName); + demoObject = demoClazz.newInstance(); + } catch (Throwable t) { + t.printStackTrace(); + throw new RuntimeException("Error while instantiating demo: "+demoClassName); + } + if( !(demoObject instanceof GLEventListener) ) { + throw new RuntimeException("Not a GLEventListener: "+demoClassName); + } + GLEventListener demo = (GLEventListener) demoObject; + + new Main().run(typeNewt, fullscreen, typeTest, demo); + System.exit(0); + } + + public static int str2int(String str, int def) { + try { + return Integer.parseInt(str); + } catch (Exception ex) { ex.printStackTrace(); } + 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; + } + +} diff --git a/src/demos/readbuffer/ReadBuffer2File.java b/src/demos/readbuffer/ReadBuffer2File.java new file mode 100755 index 0000000..9b96d9b --- /dev/null +++ b/src/demos/readbuffer/ReadBuffer2File.java @@ -0,0 +1,74 @@ +/* + * 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.texture.spi.awt.IIOTextureWriter; +import java.io.File; +import java.io.IOException; + +import javax.media.nativewindow.*; + +public class ReadBuffer2File extends ReadBufferBase { + + public ReadBuffer2File (GLDrawable externalRead) { + super(externalRead); + } + + public void dispose(GLAutoDrawable drawable) { + super.dispose(drawable); + } + + IIOTextureWriter textureWriter = new IIOTextureWriter(); + int shotNum=0; + + void copyTextureData2File() { + if(!readBufferUtil.isValid()) return; + + try { + textureWriter.write(new File("/tmp/shot/shot-"+shotNum+".jpg"), readBufferUtil.getTextureData()); + shotNum++; + } catch (IOException ioe) { ioe.printStackTrace(); } + readBufferUtil.rewindPixelBuffer(); + } + + public void display(GLAutoDrawable drawable) { + super.display(drawable); + copyTextureData2File(); + } +} + diff --git a/src/demos/readbuffer/ReadBuffer2Screen.java b/src/demos/readbuffer/ReadBuffer2Screen.java new file mode 100755 index 0000000..48db635 --- /dev/null +++ b/src/demos/readbuffer/ReadBuffer2Screen.java @@ -0,0 +1,176 @@ +/* + * 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 javax.media.opengl.fixedfunc.*; + +import com.sun.opengl.util.*; +import com.sun.opengl.util.glsl.fixedfunc.*; + +import javax.media.opengl.fixedfunc.GLPointerFunc; +import com.sun.opengl.util.texture.Texture; +import com.sun.opengl.util.texture.TextureData; +import com.sun.opengl.util.texture.TextureCoords; +import com.sun.opengl.util.GLArrayDataServer; +import com.sun.opengl.util.BufferUtil; + +public class ReadBuffer2Screen extends ReadBufferBase { + PMVMatrix pmvMatrix; + GLArrayDataServer readTextureVertices = null; + GLArrayDataServer readTextureCoords = null; + + public ReadBuffer2Screen (GLDrawable externalRead) { + super(externalRead); + } + + public void init(GLAutoDrawable drawable) { + super.init(drawable); + + GL gl = drawable.getGL(); + + pmvMatrix = new PMVMatrix(); + + float f_edge = 1f; + if(null==readTextureVertices) { + readTextureVertices = GLArrayDataServer.createFixed(gl, GLPointerFunc.GL_VERTEX_ARRAY, "mgl_Vertex", + 2, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW); + { + FloatBuffer vb = (FloatBuffer)readTextureVertices.getBuffer(); + vb.put(-f_edge); vb.put(-f_edge); + vb.put( f_edge); vb.put(-f_edge); + vb.put(-f_edge); vb.put( f_edge); + vb.put( f_edge); vb.put( f_edge); + } + readTextureVertices.seal(gl, true); + } + + // Clear background to gray + gl.glClearColor(0.5f, 0.5f, 0.5f, 0.4f); + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + super.reshape(drawable, x, y, width, height); + + GL gl = drawable.getGL(); + + gl.glViewport(0, 0, width, height); + + if(gl instanceof GLLightingFunc) { + ((GLLightingFunc)gl).glShadeModel(GLLightingFunc.GL_SMOOTH); + } + + GLMatrixFunc glM; + if(gl instanceof GLMatrixFunc) { + glM = (GLMatrixFunc)gl; + } else { + throw new GLException("ES2 currently unhandled .. "); + } + + // Identity .. + pmvMatrix.glMatrixMode(pmvMatrix.GL_MODELVIEW); + pmvMatrix.glLoadIdentity(); + pmvMatrix.glTranslatef(0, 0, -2.5f); + if(null!=glM) { + glM.glMatrixMode(pmvMatrix.GL_MODELVIEW); + glM.glLoadMatrixf(pmvMatrix.glGetMvMatrixf()); + } + + // Set location in front of camera + pmvMatrix.glMatrixMode(pmvMatrix.GL_PROJECTION); + pmvMatrix.glLoadIdentity(); + pmvMatrix.gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100.0f); + if(null!=glM) { + glM.glMatrixMode(pmvMatrix.GL_PROJECTION); + glM.glLoadMatrixf(pmvMatrix.glGetPMatrixf()); + } + } + + public void dispose(GLAutoDrawable drawable) { + super.dispose(drawable); + } + + void renderOffscreenTexture(GL gl) { + if(!readBufferUtil.isValid()) return; + + updateTextureCoords(gl, false); + + // Now draw one quad with the texture + readBufferUtil.getTexture().enable(); + readBufferUtil.getTexture().bind(); + if(gl.isGL2ES1()) { + // gl.getGL2ES1().glTexEnvi(GL2ES1.GL_TEXTURE_ENV, GL2ES1.GL_TEXTURE_ENV_MODE, GL2ES1.GL_REPLACE); + gl.getGL2ES1().glTexEnvi(GL2ES1.GL_TEXTURE_ENV, GL2ES1.GL_TEXTURE_ENV_MODE, GL2ES1.GL_MODULATE); + } + + readTextureVertices.enableBuffer(gl, true); + if(null!=readTextureCoords) { + readTextureCoords.enableBuffer(gl, true); + } + gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, readTextureVertices.getElementNumber()); + + readBufferUtil.getTexture().disable(); + } + + void updateTextureCoords(GL gl, boolean force) { + if(force || null==readTextureCoords) { + readTextureCoords = GLArrayDataServer.createFixed(gl, GLPointerFunc.GL_TEXTURE_COORD_ARRAY, "mgl_MultiTexCoord0", + 2, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW); + { + TextureCoords coords = readBufferUtil.getTexture().getImageTexCoords(); + FloatBuffer cb = (FloatBuffer)readTextureCoords.getBuffer(); + cb.put(coords.left()); cb.put(coords.bottom()); + cb.put(coords.right()); cb.put(coords.bottom()); + cb.put(coords.left()); cb.put(coords.top()); + cb.put(coords.right()); cb.put(coords.top()); + } + readTextureCoords.seal(gl, true); + } + } + + public void display(GLAutoDrawable drawable) { + super.display(drawable); + + GL gl = drawable.getGL(); + + gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT); + if(gl instanceof GLLightingFunc) { + ((GLLightingFunc)gl).glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + } + + renderOffscreenTexture(gl); + } +} + diff --git a/src/demos/readbuffer/ReadBufferBase.java b/src/demos/readbuffer/ReadBufferBase.java new file mode 100755 index 0000000..77ffd96 --- /dev/null +++ b/src/demos/readbuffer/ReadBufferBase.java @@ -0,0 +1,95 @@ +/* + * 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.Texture; +import com.sun.opengl.util.texture.TextureData; +import com.sun.opengl.util.BufferUtil; + + +public class ReadBufferBase implements GLEventListener { + public boolean glDebug = false ; + public boolean glTrace = false ; + + protected GLDrawable externalRead; + + ReadBufferUtil readBufferUtil = new ReadBufferUtil(); + + public ReadBufferBase (GLDrawable externalRead) { + this.externalRead = externalRead ; + } + + public void init(GLAutoDrawable drawable) { + 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();} + } + + System.out.println(_gl); + + _gl.getContext().setGLDrawableRead(externalRead); + if(_gl.isGL2GL3()) { + _gl.getGL2GL3().glReadBuffer(GL2GL3.GL_FRONT); + } + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + } + + public void dispose(GLAutoDrawable drawable) { + readBufferUtil.dispose(); + } + + public void display(GLAutoDrawable drawable) { + GL gl = drawable.getGL(); + + readBufferUtil.fetchOffscreenTexture(drawable, gl); + } + +} + diff --git a/src/demos/readbuffer/ReadBufferUtil.java b/src/demos/readbuffer/ReadBufferUtil.java new file mode 100755 index 0000000..96f4b06 --- /dev/null +++ b/src/demos/readbuffer/ReadBufferUtil.java @@ -0,0 +1,109 @@ +/* + * 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.Texture; +import com.sun.opengl.util.texture.TextureData; +import com.sun.opengl.util.BufferUtil; + +public class ReadBufferUtil { + protected int readPixelSizeLast = 0; + protected Buffer readPixelBuffer = null; + protected TextureData readTextureData = null; + protected Texture readTexture = new Texture(GL.GL_TEXTURE_2D); + + public Buffer getPixelBuffer() { return readPixelBuffer; } + public void rewindPixelBuffer() { readPixelBuffer.rewind(); } + + public TextureData getTextureData() { return readTextureData; } + public Texture getTexture() { return readTexture; } + + public boolean isValid() { + return null!=readTexture && null!=readTextureData && null!=readPixelBuffer ; + } + + public void fetchOffscreenTexture(GLDrawable drawable, GL gl) { + int readPixelSize = drawable.getWidth() * drawable.getHeight() * 3 ; // RGB + boolean newData = false; + if(readPixelSize>readPixelSizeLast) { + 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(); + } +} + |