diff options
author | JOGAMP DEV TEAM <[email protected]> | 2010-04-22 21:12:36 -0400 |
---|---|---|
committer | JOGAMP DEV TEAM <[email protected]> | 2010-04-22 21:12:36 -0400 |
commit | 1d889ddc4728d1534e310b44fad74a2932d22d39 (patch) | |
tree | 4ad0c3eeb3ef728522ac9459eb12c667301a0c32 /src/junit/com/jogamp/test | |
parent | 8790075f074013aa3c71b96993838cf1117275f1 (diff) | |
parent | a588326d206ff32e5ff5db97560851cb9b826022 (diff) |
Merge branch 'master' of github.com:sgothel/jogl
Diffstat (limited to 'src/junit/com/jogamp/test')
16 files changed, 2367 insertions, 0 deletions
diff --git a/src/junit/com/jogamp/test/junit/jogl/acore/TestGLProfile01CORE.java b/src/junit/com/jogamp/test/junit/jogl/acore/TestGLProfile01CORE.java new file mode 100755 index 000000000..a1ff0d860 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/acore/TestGLProfile01CORE.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.acore; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.Test; + +import javax.media.opengl.*; + +import com.jogamp.newt.*; +import java.io.IOException; + +public class TestGLProfile01CORE { + static GLProfile glp; + static GLDrawableFactory factory; + + @BeforeClass + public static void initClass() { + glp = GLProfile.getDefault(); + Assert.assertNotNull(glp); + factory = GLDrawableFactory.getFactory(glp); + Assert.assertNotNull(factory); + } + + @AfterClass + public static void releaseClass() { + factory.shutdown(); + factory=null; + } + + @Test + public void test01GLProfileDefault() { + System.out.println("GLProfile <static> "+GLProfile.glAvailabilityToString()); + } + + @Test + public void test02GLProfileMaxFixedFunc() { + System.out.println("GLProfile <static> getMaxFixedFunc(): "+GLProfile.getMaxFixedFunc()); + } + + @Test + public void test02GLProfileMaxProgrammable() { + System.out.println("GLProfile <static> getMaxProgrammable(): "+GLProfile.getMaxProgrammable()); + } + + public static void main(String args[]) throws IOException { + String tstname = TestGLProfile01CORE.class.getName(); + org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] { + tstname, + "filtertrace=true", + "haltOnError=false", + "haltOnFailure=false", + "showoutput=true", + "outputtoformatters=true", + "logfailedtests=true", + "logtestlistenerevents=true", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); + } + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/awt/TestAWT01GLn.java b/src/junit/com/jogamp/test/junit/jogl/awt/TestAWT01GLn.java new file mode 100755 index 000000000..ac34b46b5 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/awt/TestAWT01GLn.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.awt; + +import javax.media.opengl.GLProfile; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.awt.GLCanvas; +import com.jogamp.opengl.util.Animator; + +import com.jogamp.test.junit.jogl.demos.gl2.gears.Gears; +import java.awt.Frame; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.After; +import org.junit.Test; + +public class TestAWT01GLn { + Frame frame=null; + GLCanvas glCanvas=null; + + @Before + public void init() { + frame = new Frame("Texture Test"); + Assert.assertNotNull(frame); + } + + @After + public void release() { + Assert.assertNotNull(frame); + Assert.assertNotNull(glCanvas); + frame.setVisible(false); + frame.remove(glCanvas); + frame.dispose(); + frame=null; + glCanvas=null; + } + + protected void runTestGL(GLCapabilities caps) throws InterruptedException { + glCanvas = new GLCanvas(caps); + Assert.assertNotNull(glCanvas); + frame.add(glCanvas); + frame.setSize(512, 512); + + glCanvas.addGLEventListener(new Gears()); + + Animator animator = new Animator(glCanvas); + frame.setVisible(true); + animator.start(); + + Thread.sleep(500); // 500 ms + + animator.stop(); + } + + @Test + public void test01GLDefault() throws InterruptedException { + GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); + runTestGL(caps); + } + + /** Both fail on ATI .. if GLn n>2 + public void test02GL3bc() throws InterruptedException { + GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL3bc)); + runTestGL(caps); + } + + public void test03GLMaxFixed() throws InterruptedException { + GLCapabilities caps = new GLCapabilities(GLProfile.getMaxFixedFunc()); + runTestGL(caps); + } */ + + public static void main(String args[]) { + org.junit.runner.JUnitCore.main(TestAWT01GLn.class.getName()); + } +} diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/es1/RedSquare.java b/src/junit/com/jogamp/test/junit/jogl/demos/es1/RedSquare.java new file mode 100755 index 000000000..9b8982ed4 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/demos/es1/RedSquare.java @@ -0,0 +1,170 @@ +package com.jogamp.test.junit.jogl.demos.es1; + +import com.jogamp.common.nio.Buffers; +import java.nio.*; +import java.util.*; +import javax.media.opengl.*; +import javax.media.opengl.glu.*; +import javax.media.nativewindow.*; + +import com.jogamp.opengl.util.*; +import com.jogamp.opengl.util.glsl.fixedfunc.*; + +public class RedSquare implements GLEventListener { + + public static boolean glDebugEmu = false; + public static boolean glDebug = false ; + public static boolean glTrace = false ; + public static boolean oneThread = false; + public static boolean useAnimator = false; + public static int swapInterval = -1; + + boolean debug = false; + long startTime = 0; + long curTime = 0; + + GLU glu = null; + + public RedSquare() { + this(false); + } + + public RedSquare(boolean debug) { + this.debug = debug; + } + + // FIXME: we must add storage of the pointers in the GL state to + // the GLImpl classes. The need for this can be seen by making + // these variables method local instead of instance members. The + // square will disappear after a second or so due to garbage + // collection. On desktop OpenGL this implies a stack of + // references due to the existence of glPush/PopClientAttrib. On + // OpenGL ES 1/2 it can simply be one set of references. + private FloatBuffer colors; + private FloatBuffer vertices; + + public void init(GLAutoDrawable drawable) { + GL _gl = drawable.getGL(); + + if(glDebugEmu) { + try { + // Debug .. + _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Debug", GL2ES2.class, _gl, null) ); + + 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); + if(swapInterval>=0) { + gl.setSwapInterval(swapInterval); + } + + 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); + + if(debug) { + System.err.println(Thread.currentThread()+" Entering initialization"); + System.err.println(Thread.currentThread()+" GL Profile: "+gl.getGLProfile()); + System.err.println(Thread.currentThread()+" GL:" + gl); + System.err.println(Thread.currentThread()+" GL_VERSION=" + gl.glGetString(gl.GL_VERSION)); + System.err.println(Thread.currentThread()+" GL_EXTENSIONS:"); + System.err.println(Thread.currentThread()+" " + gl.glGetString(gl.GL_EXTENSIONS)); + System.err.println(Thread.currentThread()+" swapInterval: " + swapInterval + " (GL: "+gl.getSwapInterval()+")"); + System.err.println(Thread.currentThread()+" GLU: " + glu); + } + + // Allocate vertex arrays + colors = Buffers.newDirectFloatBuffer(16); + vertices = Buffers.newDirectFloatBuffer(12); + // Fill them up + colors.put( 0, 1); colors.put( 1, 0); colors.put( 2, 0); colors.put( 3, 1); + colors.put( 4, 0); colors.put( 5, 0); colors.put( 6, 1); colors.put( 7, 1); + colors.put( 8, 1); colors.put( 9, 0); colors.put(10, 0); colors.put(11, 1); + colors.put(12, 1); colors.put(13, 0); colors.put(14, 0); colors.put(15, 1); + vertices.put(0, -2); vertices.put( 1, 2); vertices.put( 2, 0); + vertices.put(3, 2); vertices.put( 4, 2); vertices.put( 5, 0); + vertices.put(6, -2); vertices.put( 7, -2); vertices.put( 8, 0); + vertices.put(9, 2); vertices.put(10, -2); vertices.put(11, 0); + + gl.glEnableClientState(gl.GL_VERTEX_ARRAY); + gl.glEnableClientState(gl.GL_COLOR_ARRAY); + gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices); + gl.glColorPointer(4, GL.GL_FLOAT, 0, colors); + + // OpenGL Render Settings + gl.glClearColor(0, 0, 0, 1); + gl.glEnable(GL.GL_DEPTH_TEST); + + startTime = System.currentTimeMillis(); + curTime = startTime; + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + GL2ES1 gl = drawable.getGL().getGL2ES1(); + // Set location in front of camera + gl.glMatrixMode(gl.GL_PROJECTION); + gl.glLoadIdentity(); + glu.gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100.0f); + //gl.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f); + //glu.gluLookAt(0, 0, -20, 0, 0, 0, 0, 1, 0); + } + + public void display(GLAutoDrawable drawable) { + curTime = System.currentTimeMillis(); + GL2ES1 gl = drawable.getGL().getGL2ES1(); + gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + + // One rotation every four seconds + gl.glMatrixMode(gl.GL_MODELVIEW); + gl.glLoadIdentity(); + gl.glTranslatef(0, 0, -10); + float ang = ((float) (curTime - startTime) * 360.0f) / 4000.0f; + gl.glRotatef(ang, 0, 0, 1); + gl.glRotatef(ang, 0, 1, 0); + + + // Draw a square + gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4); + } + + public void dispose(GLAutoDrawable drawable) { + GL2ES1 gl = drawable.getGL().getGL2ES1(); + if(debug) { + System.out.println(Thread.currentThread()+" RedSquare.dispose: "+gl.getContext()); + } + gl.glDisableClientState(gl.GL_VERTEX_ARRAY); + gl.glDisableClientState(gl.GL_COLOR_ARRAY); + glu.destroy(); + glu = null; + colors.clear(); + colors = null; + vertices.clear(); + vertices = null; + if(debug) { + System.out.println(Thread.currentThread()+" RedSquare.dispose: FIN"); + } + } + + public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { + } + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java b/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java new file mode 100644 index 000000000..135efd341 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/demos/gl2/gears/Gears.java @@ -0,0 +1,306 @@ + +package com.jogamp.test.junit.jogl.demos.gl2.gears; + +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES1; +import javax.media.opengl.GL2; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; + +/** + * Gears.java <BR> + * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P> + * + * This version is equal to Brian Paul's version 1.2 1999/10/21 + */ + +public class Gears implements GLEventListener /* , MouseListener, MouseMotionListener */ { + private float view_rotx = 20.0f, view_roty = 30.0f, view_rotz = 0.0f; + private int gear1, gear2, gear3; + private float angle = 0.0f; + + private int prevMouseX, prevMouseY; + private boolean mouseRButtonDown = false; + + public void init(GLAutoDrawable drawable) { + // Use debug pipeline + // drawable.setGL(new DebugGL(drawable.getGL())); + + GL2 gl = drawable.getGL().getGL2(); + + System.err.println("INIT GL IS: " + gl.getClass().getName()); + + System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities()); + + gl.setSwapInterval(1); + + float pos[] = { 5.0f, 5.0f, 10.0f, 0.0f }; + float red[] = { 0.8f, 0.1f, 0.0f, 1.0f }; + float green[] = { 0.0f, 0.8f, 0.2f, 1.0f }; + float blue[] = { 0.2f, 0.2f, 1.0f, 1.0f }; + + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, pos, 0); + gl.glEnable(GL2.GL_CULL_FACE); + gl.glEnable(GL2.GL_LIGHTING); + gl.glEnable(GL2.GL_LIGHT0); + gl.glEnable(GL2.GL_DEPTH_TEST); + + /* make the gears */ + gear1 = gl.glGenLists(1); + gl.glNewList(gear1, GL2.GL_COMPILE); + gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, red, 0); + gear(gl, 1.0f, 4.0f, 1.0f, 20, 0.7f); + gl.glEndList(); + + gear2 = gl.glGenLists(1); + gl.glNewList(gear2, GL2.GL_COMPILE); + gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, green, 0); + gear(gl, 0.5f, 2.0f, 2.0f, 10, 0.7f); + gl.glEndList(); + + gear3 = gl.glGenLists(1); + gl.glNewList(gear3, GL2.GL_COMPILE); + gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, blue, 0); + gear(gl, 1.3f, 2.0f, 0.5f, 10, 0.7f); + gl.glEndList(); + + gl.glEnable(GL2.GL_NORMALIZE); + + /** + if (drawable instanceof AWTGLAutoDrawable) { + AWTGLAutoDrawable awtDrawable = (AWTGLAutoDrawable) drawable; + awtDrawable.addMouseListener(this); + awtDrawable.addMouseMotionListener(this); + } */ + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + GL2 gl = drawable.getGL().getGL2(); + + float h = (float)height / (float)width; + + gl.glMatrixMode(GL2.GL_PROJECTION); + + System.err.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR)); + System.err.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER)); + System.err.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION)); + gl.glLoadIdentity(); + gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f); + gl.glMatrixMode(GL2.GL_MODELVIEW); + gl.glLoadIdentity(); + gl.glTranslatef(0.0f, 0.0f, -40.0f); + } + + public void dispose(GLAutoDrawable drawable) { + System.out.println("Gears.dispose: "+drawable); + } + + public void display(GLAutoDrawable drawable) { + // Turn the gears' teeth + angle += 2.0f; + + // Get the GL corresponding to the drawable we are animating + GL2 gl = drawable.getGL().getGL2(); + + /** + // Special handling for the case where the GLJPanel is translucent + // and wants to be composited with other Java 2D content + if ((drawable instanceof GLJPanel) && + !((GLJPanel) drawable).isOpaque() && + ((GLJPanel) drawable).shouldPreserveColorBufferIfTranslucent()) { + gl.glClear(GL2.GL_DEPTH_BUFFER_BIT); + } else */ { + gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); + } + + // Rotate the entire assembly of gears based on how the user + // dragged the mouse around + gl.glPushMatrix(); + gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f); + gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f); + gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f); + + // Place the first gear and call its display list + gl.glPushMatrix(); + gl.glTranslatef(-3.0f, -2.0f, 0.0f); + gl.glRotatef(angle, 0.0f, 0.0f, 1.0f); + gl.glCallList(gear1); + gl.glPopMatrix(); + + // Place the second gear and call its display list + gl.glPushMatrix(); + gl.glTranslatef(3.1f, -2.0f, 0.0f); + gl.glRotatef(-2.0f * angle - 9.0f, 0.0f, 0.0f, 1.0f); + gl.glCallList(gear2); + gl.glPopMatrix(); + + // Place the third gear and call its display list + gl.glPushMatrix(); + gl.glTranslatef(-3.1f, 4.2f, 0.0f); + gl.glRotatef(-2.0f * angle - 25.0f, 0.0f, 0.0f, 1.0f); + gl.glCallList(gear3); + gl.glPopMatrix(); + + // Remember that every push needs a pop; this one is paired with + // rotating the entire gear assembly + gl.glPopMatrix(); + } + + public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {} + + public static void gear(GL2 gl, + float inner_radius, + float outer_radius, + float width, + int teeth, + float tooth_depth) + { + int i; + float r0, r1, r2; + float angle, da; + float u, v, len; + + r0 = inner_radius; + r1 = outer_radius - tooth_depth / 2.0f; + r2 = outer_radius + tooth_depth / 2.0f; + + da = 2.0f * (float) Math.PI / teeth / 4.0f; + + gl.glShadeModel(GL2.GL_FLAT); + + gl.glNormal3f(0.0f, 0.0f, 1.0f); + + /* draw front face */ + gl.glBegin(GL2.GL_QUAD_STRIP); + for (i = 0; i <= teeth; i++) + { + angle = i * 2.0f * (float) Math.PI / teeth; + gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), width * 0.5f); + if(i < teeth) + { + gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(angle + 3.0f * da), r1 * (float)Math.sin(angle + 3.0f * da), width * 0.5f); + } + } + gl.glEnd(); + + /* draw front sides of teeth */ + gl.glBegin(GL2.GL_QUADS); + for (i = 0; i < teeth; i++) + { + angle = i * 2.0f * (float) Math.PI / teeth; + gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), width * 0.5f); + gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), width * 0.5f); + gl.glVertex3f(r2 * (float)Math.cos(angle + 2.0f * da), r2 * (float)Math.sin(angle + 2.0f * da), width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(angle + 3.0f * da), r1 * (float)Math.sin(angle + 3.0f * da), width * 0.5f); + } + gl.glEnd(); + + /* draw back face */ + gl.glBegin(GL2.GL_QUAD_STRIP); + for (i = 0; i <= teeth; i++) + { + angle = i * 2.0f * (float) Math.PI / teeth; + gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), -width * 0.5f); + gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), -width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), -width * 0.5f); + gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), -width * 0.5f); + } + gl.glEnd(); + + /* draw back sides of teeth */ + gl.glBegin(GL2.GL_QUADS); + for (i = 0; i < teeth; i++) + { + angle = i * 2.0f * (float) Math.PI / teeth; + gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), -width * 0.5f); + gl.glVertex3f(r2 * (float)Math.cos(angle + 2 * da), r2 * (float)Math.sin(angle + 2 * da), -width * 0.5f); + gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), -width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), -width * 0.5f); + } + gl.glEnd(); + + /* draw outward faces of teeth */ + gl.glBegin(GL2.GL_QUAD_STRIP); + for (i = 0; i < teeth; i++) + { + angle = i * 2.0f * (float) Math.PI / teeth; + gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), -width * 0.5f); + u = r2 * (float)Math.cos(angle + da) - r1 * (float)Math.cos(angle); + v = r2 * (float)Math.sin(angle + da) - r1 * (float)Math.sin(angle); + len = (float)Math.sqrt(u * u + v * v); + u /= len; + v /= len; + gl.glNormal3f(v, -u, 0.0f); + gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), width * 0.5f); + gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), -width * 0.5f); + gl.glNormal3f((float)Math.cos(angle), (float)Math.sin(angle), 0.0f); + gl.glVertex3f(r2 * (float)Math.cos(angle + 2 * da), r2 * (float)Math.sin(angle + 2 * da), width * 0.5f); + gl.glVertex3f(r2 * (float)Math.cos(angle + 2 * da), r2 * (float)Math.sin(angle + 2 * da), -width * 0.5f); + u = r1 * (float)Math.cos(angle + 3 * da) - r2 * (float)Math.cos(angle + 2 * da); + v = r1 * (float)Math.sin(angle + 3 * da) - r2 * (float)Math.sin(angle + 2 * da); + gl.glNormal3f(v, -u, 0.0f); + gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), -width * 0.5f); + gl.glNormal3f((float)Math.cos(angle), (float)Math.sin(angle), 0.0f); + } + gl.glVertex3f(r1 * (float)Math.cos(0), r1 * (float)Math.sin(0), width * 0.5f); + gl.glVertex3f(r1 * (float)Math.cos(0), r1 * (float)Math.sin(0), -width * 0.5f); + gl.glEnd(); + + gl.glShadeModel(GL2.GL_SMOOTH); + + /* draw inside radius cylinder */ + gl.glBegin(GL2.GL_QUAD_STRIP); + for (i = 0; i <= teeth; i++) + { + angle = i * 2.0f * (float) Math.PI / teeth; + gl.glNormal3f(-(float)Math.cos(angle), -(float)Math.sin(angle), 0.0f); + gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), -width * 0.5f); + gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), width * 0.5f); + } + gl.glEnd(); + } + + /*** + // Methods required for the implementation of MouseListener + public void mouseEntered(MouseEvent e) {} + public void mouseExited(MouseEvent e) {} + + public void mousePressed(MouseEvent e) { + prevMouseX = e.getX(); + prevMouseY = e.getY(); + if ((e.getModifiers() & e.BUTTON3_MASK) != 0) { + mouseRButtonDown = true; + } + } + + public void mouseReleased(MouseEvent e) { + if ((e.getModifiers() & e.BUTTON3_MASK) != 0) { + mouseRButtonDown = false; + } + } + + public void mouseClicked(MouseEvent e) {} + + // Methods required for the implementation of MouseMotionListener + public void mouseDragged(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + Dimension size = e.getComponent().getSize(); + + float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)size.width); + float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)size.height); + + prevMouseX = x; + prevMouseY = y; + + view_rotx += thetaX; + view_roty += thetaY; + } + + public void mouseMoved(MouseEvent e) {} + */ +} diff --git a/src/junit/com/jogamp/test/junit/jogl/drawable/TestDrawable01NEWT.java b/src/junit/com/jogamp/test/junit/jogl/drawable/TestDrawable01NEWT.java new file mode 100755 index 000000000..0320c50ae --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/drawable/TestDrawable01NEWT.java @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.drawable; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.Test; + +import javax.media.opengl.*; + +import com.jogamp.newt.*; +import java.io.IOException; + +public class TestDrawable01NEWT { + static GLProfile glp; + static GLDrawableFactory factory; + static int width, height; + GLCapabilities caps; + Window window; + GLDrawable drawable; + GLContext context; + + @BeforeClass + public static void initClass() { + glp = GLProfile.getDefault(); + Assert.assertNotNull(glp); + factory = GLDrawableFactory.getFactory(glp); + Assert.assertNotNull(factory); + width = 640; + height = 480; + } + + @AfterClass + public static void releaseClass() { + factory.shutdown(); + factory=null; + } + + @Before + public void initTest() { + caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + } + + void createWindow(boolean onscreen, boolean pbuffer, boolean undecorated) { + caps.setOnscreen(onscreen); + caps.setPBuffer(!onscreen && pbuffer); + caps.setDoubleBuffered(!onscreen); + // System.out.println("Requested: "+caps); + + // + // Create native windowing resources .. X11/Win/OSX + // + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + + window = NewtFactory.createWindow(screen, caps, onscreen && undecorated); + Assert.assertNotNull(window); + window.setSize(width, height); + window.setVisible(true); + // System.out.println("Created: "+window); + + // + // Create native OpenGL resources .. XGL/WGL/CGL .. + // equivalent to GLAutoDrawable methods: setVisible(true) + // + GLCapabilities glCaps = (GLCapabilities) window.getGraphicsConfiguration().getNativeGraphicsConfiguration().getChosenCapabilities(); + Assert.assertNotNull(glCaps); + Assert.assertTrue(glCaps.getGreenBits()>5); + Assert.assertTrue(glCaps.getBlueBits()>5); + Assert.assertTrue(glCaps.getRedBits()>5); + Assert.assertTrue(glCaps.isOnscreen()==onscreen); + Assert.assertTrue(onscreen || !pbuffer || glCaps.isPBuffer()); // pass if onscreen, or !pbuffer req. or have pbuffer + Assert.assertTrue(glCaps.getDoubleBuffered()==!onscreen); + Assert.assertTrue(glCaps.getDepthBits()>4); + + drawable = factory.createGLDrawable(window); + Assert.assertNotNull(drawable); + // System.out.println("Pre: "+drawable); + // + drawable.setRealized(true); + Assert.assertTrue(width==drawable.getWidth()); + Assert.assertTrue(height==drawable.getHeight()); + // Assert.assertTrue(glCaps==drawable.getChosenGLCapabilities()); + Assert.assertTrue(window==drawable.getNativeWindow()); + // System.out.println("Post: "+drawable); + + context = drawable.createContext(null); + Assert.assertNotNull(context); + // System.out.println(context); + + int res = context.makeCurrent(); + Assert.assertTrue(GLContext.CONTEXT_CURRENT_NEW==res || GLContext.CONTEXT_CURRENT==res); + + // draw something .. + + drawable.swapBuffers(); + context.release(); + + // System.out.println("Final: "+window); + } + + void destroyWindow() { + // GLWindow.dispose(..) sequence + Assert.assertNotNull(context); + context.destroy(); + + Assert.assertNotNull(drawable); + drawable.setRealized(false); + + // GLWindow.destroy(..) sequence cont.. + Assert.assertNotNull(window); + window.destroy(true); // incl screen + display + + drawable = null; + context = null; + window = null; + } + + @Test + public void testOnScreenDecorated() throws InterruptedException { + createWindow(true, false, false); + Thread.sleep(1000); // 1000 ms + destroyWindow(); + } + + @Test + public void testOnScreenUndecorated() throws InterruptedException { + createWindow(true, false, true); + Thread.sleep(1000); // 1000 ms + destroyWindow(); + } + + public static void main(String args[]) throws IOException { + String tstname = TestDrawable01NEWT.class.getName(); + org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] { + tstname, + "filtertrace=true", + "haltOnError=false", + "haltOnFailure=false", + "showoutput=true", + "outputtoformatters=true", + "logfailedtests=true", + "logtestlistenerevents=true", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); + } + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBuffer2File.java b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBuffer2File.java new file mode 100755 index 000000000..de7c8d43a --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBuffer2File.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ +package com.jogamp.test.junit.jogl.offscreen; + +import java.io.IOException; +import javax.media.opengl.*; + +import com.jogamp.opengl.util.texture.TextureIO; +import java.io.File; + +public class ReadBuffer2File extends ReadBufferBase { + + public ReadBuffer2File(GLDrawable externalRead) { + super(externalRead); + } + + @Override + public void dispose(GLAutoDrawable drawable) { + super.dispose(drawable); + } + int shotNum = 0; + + void copyTextureData2File() throws IOException { + if (!readBufferUtil.isValid()) { + return; + } + + File file = File.createTempFile("shot" + shotNum + "-", ".ppm"); + TextureIO.write(readBufferUtil.getTextureData(), file); + System.out.println("Wrote: " + file.getAbsolutePath() + ", ..."); + shotNum++; + readBufferUtil.rewindPixelBuffer(); + } + + @Override + public void display(GLAutoDrawable drawable) { + super.display(drawable); + try { + copyTextureData2File(); + } catch (IOException ex) { + throw new RuntimeException("can not read buffer to file", ex); + } + } +} diff --git a/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBuffer2Screen.java b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBuffer2Screen.java new file mode 100755 index 000000000..030a7e173 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBuffer2Screen.java @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.offscreen; + +import java.nio.*; +import javax.media.opengl.*; +import javax.media.opengl.fixedfunc.*; + +import com.jogamp.opengl.util.*; + +import javax.media.opengl.fixedfunc.GLPointerFunc; +import com.jogamp.opengl.util.texture.TextureCoords; +import com.jogamp.opengl.util.GLArrayDataClient; +import com.jogamp.opengl.util.GLArrayDataServer; + +public class ReadBuffer2Screen extends ReadBufferBase { + PMVMatrix pmvMatrix; + GLArrayDataClient readTextureVertices = null; + GLArrayDataClient readTextureCoords = null; + boolean enableBufferAlways = false; // FIXME + boolean enableBufferVBO = true; // FIXME + + public ReadBuffer2Screen (GLDrawable externalRead) { + super(externalRead); + } + + @Override + public void init(GLAutoDrawable drawable) { + super.init(drawable); + + GL gl = drawable.getGL(); + + pmvMatrix = new PMVMatrix(); + + float f_edge = 1f; + if(null==readTextureVertices) { + //readTextureVertices = GLArrayDataClient.createFixed(gl, GLPointerFunc.GL_VERTEX_ARRAY, "mgl_Vertex", + // 2, GL.GL_FLOAT, true, 4); + readTextureVertices = GLArrayDataServer.createFixed(gl, GLPointerFunc.GL_VERTEX_ARRAY, "mgl_Vertex", + 2, GL.GL_FLOAT, true, 4, GL.GL_STATIC_DRAW); + readTextureVertices.setEnableAlways(enableBufferAlways); + readTextureVertices.setVBOUsage(enableBufferVBO); + { + 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); + System.out.println(readTextureVertices); + } + + // Clear background to gray + gl.glClearColor(0.5f, 0.5f, 0.5f, 0.4f); + } + + @Override + 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()); + } + } + + @Override + public void dispose(GLAutoDrawable drawable) { + super.dispose(drawable); + } + + void renderOffscreenTexture(GL gl) { + if(!readBufferUtil.isValid()) return; + + // 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); + } + + updateTextureCoords(gl, false); + + readTextureVertices.enableBuffer(gl, true); + if(null!=readTextureCoords) { + readTextureCoords.enableBuffer(gl, true); + } + gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, readTextureVertices.getElementNumber()); + /** + if(null!=readTextureCoords) { + readTextureCoords.enableBuffer(gl, false); + } + readTextureVertices.enableBuffer(gl, false); */ + + 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); + readTextureCoords.setEnableAlways(enableBufferAlways); + readTextureCoords.setVBOUsage(enableBufferVBO); + { + 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); + System.out.println(readTextureCoords); + } + } + + @Override + 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/junit/com/jogamp/test/junit/jogl/offscreen/ReadBufferBase.java b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBufferBase.java new file mode 100755 index 000000000..4841d33e9 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBufferBase.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.offscreen; + +import javax.media.opengl.*; + +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) { + throw new RuntimeException("can not set debug pipeline", e); + } + } + + if(glTrace) { + try { + _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", null, _gl, new Object[] { System.err } ) ); + } catch (Exception e) { + throw new RuntimeException("can not set trace pipeline", e); + } + } + + System.out.println(_gl); + + _gl.getContext().setGLDrawableRead(externalRead); + if(_gl.isGL2GL3()) { + _gl.getGL2GL3().glReadBuffer(GL2GL3.GL_FRONT); + } + System.out.println("---------------------------"); + System.out.println(_gl.getContext()); + System.out.println("---------------------------"); + } + + 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/junit/com/jogamp/test/junit/jogl/offscreen/ReadBufferUtil.java b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBufferUtil.java new file mode 100755 index 000000000..ff3b9b58a --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/offscreen/ReadBufferUtil.java @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.offscreen; + +import com.jogamp.opengl.util.GLBuffers; +import java.nio.*; +import javax.media.opengl.*; + +import com.jogamp.opengl.util.texture.Texture; +import com.jogamp.opengl.util.texture.TextureData; + +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 = GLBuffers.newDirectGLBuffer(GL.GL_UNSIGNED_BYTE, readPixelSize); + readPixelSizeLast = readPixelSize ; + try { + readTextureData = new TextureData( + gl.getGLProfile(), + // gl.isGL2GL3()?gl.GL_RGBA:gl.GL_RGB, + gl.GL_RGB, + drawable.getWidth(), drawable.getHeight(), + 0, + gl.GL_RGB, + gl.GL_UNSIGNED_BYTE, + false, false, + false /* flip */, + readPixelBuffer, + null /* Flusher */); + newData = true; + } catch (Exception e) { + readTextureData = null; + readPixelBuffer = null; + readPixelSizeLast = 0; + throw new RuntimeException("can not fetch offscreen texture", e); + } + } + 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(); + } + } + + @SuppressWarnings("deprecation") + public void dispose() { + readTexture.dispose(); + readTextureData = null; + readPixelBuffer.clear(); + readPixelBuffer = null; + readPixelSizeLast = 0; + } + +} + diff --git a/src/junit/com/jogamp/test/junit/jogl/offscreen/Surface2File.java b/src/junit/com/jogamp/test/junit/jogl/offscreen/Surface2File.java new file mode 100755 index 000000000..2799d72c4 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/offscreen/Surface2File.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ +package com.jogamp.test.junit.jogl.offscreen; + +import javax.media.opengl.*; + +import com.jogamp.opengl.util.texture.TextureIO; + +import java.io.File; +import java.io.IOException; + +import javax.media.nativewindow.*; + +public class Surface2File implements SurfaceUpdatedListener { + + ReadBufferUtil readBufferUtil = new ReadBufferUtil(); + 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) { + GL gl = ctx.getGL(); + // FIXME glFinish() is an expensive paranoia sync, should not be necessary due to spec + gl.glFinish(); + readBufferUtil.fetchOffscreenTexture(drawable, gl); + gl.glFinish(); + try { + surface2File("shot"); + } catch (IOException ex) { + throw new RuntimeException("can not write survace to file", ex); + } + } + } + } + + public void surface2File(String basename) throws IOException { + if (!readBufferUtil.isValid()) { + return; + } + + File file = File.createTempFile(basename + shotNum + "-", ".ppm"); + TextureIO.write(readBufferUtil.getTextureData(), file); + System.out.println("Wrote: " + file.getAbsolutePath() + ", ..."); + shotNum++; + readBufferUtil.rewindPixelBuffer(); + } +} diff --git a/src/junit/com/jogamp/test/junit/jogl/offscreen/TestOffscreen01NEWT.java b/src/junit/com/jogamp/test/junit/jogl/offscreen/TestOffscreen01NEWT.java new file mode 100755 index 000000000..459b41f16 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/offscreen/TestOffscreen01NEWT.java @@ -0,0 +1,389 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.offscreen; + +import java.lang.reflect.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.After; +import org.junit.Test; + +import javax.media.opengl.*; +import javax.media.nativewindow.*; + +import com.jogamp.newt.*; +import com.jogamp.newt.opengl.*; + +import com.jogamp.test.junit.jogl.demos.gl2.gears.Gears; +import com.jogamp.test.junit.jogl.demos.es1.RedSquare; +import java.io.IOException; + +public class TestOffscreen01NEWT { + static GLProfile glpDefault; + static GLDrawableFactory factory; + static int width, height; + GLCapabilities capsDefault; + + @BeforeClass + public static void initClass() { + glpDefault = GLProfile.getDefault(); + Assert.assertNotNull(glpDefault); + factory = GLDrawableFactory.getFactory(glpDefault); + Assert.assertNotNull(factory); + width = 640; + height = 480; + } + + @AfterClass + public static void releaseClass() { + factory.shutdown(); + factory=null; + } + + @Before + public void init() { + capsDefault = new GLCapabilities(glpDefault); + Assert.assertNotNull(capsDefault); + } + + private void do01OffscreenWindowPBuffer(GLCapabilities caps) { + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + Window window = NewtFactory.createWindow(screen, caps, false /* undecorated */); + Assert.assertNotNull(window); + window.setSize(width, height); + GLWindow glWindow = GLWindow.create(window); + Assert.assertNotNull(glWindow); + glWindow.setVisible(true); + GLEventListener demo = new RedSquare(); + WindowUtilNEWT.setDemoFields(demo, window, glWindow, false); + + while ( glWindow.getTotalFrames() < 2) { + glWindow.display(); + } + + if(null!=glWindow) { + glWindow.destroy(); + } + if(null!=window) { + window.destroy(); + } + if(null!=screen) { + screen.destroy(); + } + if(null!=display) { + display.destroy(); + } + } + + @Test + public void test01aOffscreenWindowPBuffer() { + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(capsDefault, false, true, false); + do01OffscreenWindowPBuffer(caps2); + } + + @Test + public void test01bOffscreenWindowPBufferStencil() { + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(capsDefault, false, true, false); + caps2.setStencilBits(8); + do01OffscreenWindowPBuffer(caps2); + } + + @Test + public void test01cOffscreenWindowPBufferStencilAlpha() { + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(capsDefault, false, true, false); + caps2.setStencilBits(8); + caps2.setAlphaBits(8); + do01OffscreenWindowPBuffer(caps2); + } + + @Test + public void test01cOffscreenWindowPBuffer555() { + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(capsDefault, false, true, false); + caps2.setRedBits(5); + caps2.setGreenBits(5); + caps2.setBlueBits(5); + do01OffscreenWindowPBuffer(caps2); + } + + @Test + public void test02Offscreen3Windows1DisplayPBuffer() { + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(capsDefault, false, true, false); + int winnum = 3, i; + Window windows[] = new Window[winnum]; + GLWindow glWindows[] = new GLWindow[winnum]; + GLEventListener demos[] = new GLEventListener[winnum]; + + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + + for(i=0; i<winnum; i++) { + System.out.println("Create Window "+i); + windows[i] = NewtFactory.createWindow(screen, caps2, false /* undecorated */); + Assert.assertNotNull(windows[i]); + windows[i].setSize(width, height); + glWindows[i] = GLWindow.create(windows[i]); + Assert.assertNotNull(glWindows[i]); + glWindows[i].setVisible(true); + demos[i] = new RedSquare(); + WindowUtilNEWT.setDemoFields(demos[i], windows[i], glWindows[i], false); + } + + while ( glWindows[0].getTotalFrames() < 2) { + for(i=0; i<winnum; i++) { + glWindows[i].display(); + } + } + + for(i=0; i<winnum; i++) { + if(null!=glWindows[i]) { + glWindows[i].destroy(); + } + if(null!=windows[i]) { + windows[i].destroy(); + } + } + if(null!=screen) { + screen.destroy(); + } + if(null!=display) { + display.destroy(); + } + } + + @Test + public void test03Offscreen3Windows3DisplaysPBuffer() { + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(capsDefault, false, true, false); + int winnum = 3, i; + Display displays[] = new Display[winnum]; + Screen screens[] = new Screen[winnum]; + Window windows[] = new Window[winnum]; + GLWindow glWindows[] = new GLWindow[winnum]; + GLEventListener demos[] = new GLEventListener[winnum]; + + for(i=0; i<winnum; i++) { + System.out.println("Create Window "+i); + displays[i] = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(displays[i]); + screens[i] = NewtFactory.createScreen(displays[i], 0); // screen 0 + Assert.assertNotNull(screens[i]); + windows[i] = NewtFactory.createWindow(screens[i], caps2, false /* undecorated */); + Assert.assertNotNull(windows[i]); + windows[i].setSize(width, height); + glWindows[i] = GLWindow.create(windows[i]); + Assert.assertNotNull(glWindows[i]); + glWindows[i].setVisible(true); + demos[i] = new RedSquare(); + WindowUtilNEWT.setDemoFields(demos[i], windows[i], glWindows[i], false); + } + + while ( glWindows[0].getTotalFrames() < 2) { + for(i=0; i<winnum; i++) { + glWindows[i].display(); + } + } + + for(i=0; i<winnum; i++) { + if(null!=glWindows[i]) { + glWindows[i].destroy(); + } + if(null!=windows[i]) { + windows[i].destroy(); + } + if(null!=screens[i]) { + screens[i].destroy(); + } + if(null!=displays[i]) { + displays[i].destroy(); + } + } + } + + @Test + public void test04OffscreenSnapshotWithDemoPBuffer() { + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(capsDefault, false, true, false); + + System.out.println("Create Window 1"); + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + Window window = NewtFactory.createWindow(screen, caps2, false /* undecorated */); + Assert.assertNotNull(window); + window.setSize(width, height); + GLWindow glWindow = GLWindow.create(window); + Assert.assertNotNull(glWindow); + glWindow.setVisible(true); + + GLWindow windowOnScreen = null; + WindowListener wl=null; + MouseListener ml=null; + SurfaceUpdatedListener ul=null; + + GLEventListener demo = new RedSquare(); + Assert.assertNotNull(demo); + + WindowUtilNEWT.run(glWindow, demo, windowOnScreen, wl, ml, ul, 2, true /*snapshot*/, false /*debug*/); + + if(null!=windowOnScreen) { + windowOnScreen.destroy(); + } + if(null!=glWindow) { + glWindow.destroy(); + } + if(null!=window) { + window.destroy(); + } + if(null!=screen) { + screen.destroy(); + } + if(null!=display) { + display.destroy(); + } + } + + @Test + public void test11OffscreenWindowPixmap() { + // Offscreen doesn't work on >= GL3 (ATI) + GLProfile glp = GLProfile.get(GLProfile.GL2); + Assert.assertNotNull(glp); + GLCapabilities caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(caps, false, false, false); + + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + Window window = NewtFactory.createWindow(screen, caps2, false /* undecorated */); + Assert.assertNotNull(window); + window.setSize(width, height); + GLWindow glWindow = GLWindow.create(window); + Assert.assertNotNull(glWindow); + glWindow.setVisible(true); + GLEventListener demo = new RedSquare(); + WindowUtilNEWT.setDemoFields(demo, window, glWindow, false); + + while ( glWindow.getTotalFrames() < 2) { + glWindow.display(); + } + + if(null!=glWindow) { + glWindow.destroy(); + } + if(null!=window) { + window.destroy(); + } + if(null!=screen) { + screen.destroy(); + } + if(null!=display) { + display.destroy(); + } + } + + @Test + public void test14OffscreenSnapshotWithDemoPixmap() { + // Offscreen doesn't work on >= GL3 (ATI) + GLProfile glp = GLProfile.get(GLProfile.GL2); + Assert.assertNotNull(glp); + GLCapabilities caps = new GLCapabilities(glp); + Assert.assertNotNull(caps); + + GLCapabilities caps2 = WindowUtilNEWT.fixCaps(caps, false, false, false); + + System.out.println("Create Window 1"); + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + Window window = NewtFactory.createWindow(screen, caps2, false /* undecorated */); + Assert.assertNotNull(window); + window.setSize(width, height); + GLWindow glWindow = GLWindow.create(window); + Assert.assertNotNull(glWindow); + glWindow.setVisible(true); + + GLWindow windowOnScreen = null; + WindowListener wl=null; + MouseListener ml=null; + SurfaceUpdatedListener ul=null; + + GLEventListener demo = new RedSquare(); + Assert.assertNotNull(demo); + + WindowUtilNEWT.run(glWindow, demo, windowOnScreen, wl, ml, ul, 2, true /*snapshot*/, false /*debug*/); + + if(null!=windowOnScreen) { + windowOnScreen.destroy(); + } + if(null!=glWindow) { + glWindow.destroy(); + } + if(null!=window) { + window.destroy(); + } + if(null!=screen) { + screen.destroy(); + } + if(null!=display) { + display.destroy(); + } + } + public static void main(String args[]) throws IOException { + String tstname = TestOffscreen01NEWT.class.getName(); + org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] { + tstname, + "filtertrace=true", + "haltOnError=false", + "haltOnFailure=false", + "showoutput=true", + "outputtoformatters=true", + "logfailedtests=true", + "logtestlistenerevents=true", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); + } + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/offscreen/WindowUtilNEWT.java b/src/junit/com/jogamp/test/junit/jogl/offscreen/WindowUtilNEWT.java new file mode 100755 index 000000000..55ad83d25 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/offscreen/WindowUtilNEWT.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.offscreen; + +import com.jogamp.test.junit.util.*; + +import org.junit.Assert; + +import javax.media.opengl.*; +import javax.media.nativewindow.*; +import com.jogamp.newt.*; +import com.jogamp.newt.opengl.*; + +public class WindowUtilNEWT { + + public static GLCapabilities fixCaps(GLCapabilities caps, boolean onscreen, boolean pbuffer, boolean undecorated) { + GLCapabilities caps2 = (GLCapabilities) caps.clone(); + caps2.setOnscreen(onscreen); + caps2.setPBuffer(!onscreen && pbuffer); + caps2.setDoubleBuffered(!onscreen); + return caps2; + } + + public static void setDemoFields(GLEventListener demo, Window window, GLWindow glWindow, boolean debug) { + Assert.assertNotNull(demo); + Assert.assertNotNull(window); + if(debug) { + MiscUtils.setFieldIfExists(demo, "glDebug", true); + MiscUtils.setFieldIfExists(demo, "glTrace", true); + } + if(!MiscUtils.setFieldIfExists(demo, "window", window)) { + MiscUtils.setFieldIfExists(demo, "glWindow", glWindow); + } + } + + public static void run(GLWindow windowOffScreen, GLEventListener demo, + GLWindow windowOnScreen, WindowListener wl, MouseListener ml, + SurfaceUpdatedListener ul, int frames, boolean snapshot, boolean debug) { + Assert.assertNotNull(windowOffScreen); + Assert.assertNotNull(demo); + + setDemoFields(demo, windowOffScreen, windowOffScreen, debug); + windowOffScreen.addGLEventListener(demo); + + if ( null != windowOnScreen ) { + if(null!=wl) { + windowOnScreen.addWindowListener(wl); + } + if(null!=ml) { + windowOnScreen.addMouseListener(ml); + } + windowOnScreen.setVisible(true); + } + + GLDrawable readDrawable = windowOffScreen.getContext().getGLDrawable() ; + + if ( null == windowOnScreen ) { + if(snapshot) { + Surface2File s2f = new Surface2File(); + windowOffScreen.addSurfaceUpdatedListener(s2f); + } + } else { + ReadBuffer2Screen readDemo = new ReadBuffer2Screen( readDrawable ) ; + windowOnScreen.addGLEventListener(readDemo); + } + if(null!=ul) { + windowOffScreen.addSurfaceUpdatedListener(ul); + } + + if(debug) { + System.out.println("+++++++++++++++++++++++++++"); + System.out.println(windowOffScreen); + System.out.println("+++++++++++++++++++++++++++"); + } + + while ( windowOffScreen.getTotalFrames() < frames) { + windowOffScreen.display(); + } + windowOffScreen.removeAllSurfaceUpdatedListener(); + + } + +} diff --git a/src/junit/com/jogamp/test/junit/jogl/texture/TestTexture01AWT.java b/src/junit/com/jogamp/test/junit/jogl/texture/TestTexture01AWT.java new file mode 100755 index 000000000..4bbbaa271 --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/texture/TestTexture01AWT.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.texture; + +import com.jogamp.test.junit.jogl.util.texture.gl2.TextureGL2ListenerDraw1; + +import javax.media.opengl.GLProfile; +import javax.media.opengl.GLCapabilities; +import javax.media.opengl.awt.GLCanvas; +import com.jogamp.opengl.util.texture.TextureData; +import com.jogamp.opengl.util.texture.awt.AWTTextureIO; +import com.jogamp.opengl.util.Animator; + +import java.awt.AlphaComposite; +import java.awt.Color; +import java.awt.Frame; +import java.awt.GradientPaint; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import org.junit.Before; +import org.junit.Test; + +public class TestTexture01AWT { + Frame frame; + BufferedImage textureImage; + + @Before + public void init() { + // create base image + BufferedImage baseImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB); + Graphics2D g = baseImage.createGraphics(); + g.setPaint(new GradientPaint(0, 0, Color.CYAN, + baseImage.getWidth(), baseImage.getHeight(), Color.BLUE)); + g.fillRect(0, 0, baseImage.getWidth(), baseImage.getHeight()); + g.dispose(); + + // create texture image + int imageType = BufferedImage.TYPE_INT_RGB; + textureImage = new BufferedImage(baseImage.getWidth(), + baseImage.getHeight(), + imageType); + g = textureImage.createGraphics(); + g.setComposite(AlphaComposite.Src); + g.drawImage(baseImage, 0, 0, null); + g.dispose(); + + frame = new Frame("Texture Test"); + } + + @Test + public void test1() throws InterruptedException { + GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL2GL3)); + GLCanvas glCanvas = new GLCanvas(caps); + frame.add(glCanvas); + frame.setSize(512, 512); + + // create texture + TextureData textureData = AWTTextureIO.newTextureData(caps.getGLProfile(), textureImage, false); + glCanvas.addGLEventListener(new TextureGL2ListenerDraw1(textureData)); + + Animator animator = new Animator(glCanvas); + frame.setVisible(true); + animator.start(); + + Thread.sleep(500); // 500 ms + + animator.stop(); + frame.setVisible(false); + + frame.remove(glCanvas); + frame.dispose(); + frame=null; + } + + public static void main(String args[]) { + org.junit.runner.JUnitCore.main(TestTexture01AWT.class.getName()); + } +} diff --git a/src/junit/com/jogamp/test/junit/jogl/util/texture/gl2/TextureGL2ListenerDraw1.java b/src/junit/com/jogamp/test/junit/jogl/util/texture/gl2/TextureGL2ListenerDraw1.java new file mode 100755 index 000000000..eabd4b79d --- /dev/null +++ b/src/junit/com/jogamp/test/junit/jogl/util/texture/gl2/TextureGL2ListenerDraw1.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.jogl.util.texture.gl2; + +import com.jogamp.opengl.util.texture.Texture; +import com.jogamp.opengl.util.texture.TextureCoords; +import com.jogamp.opengl.util.texture.TextureData; +import com.jogamp.opengl.util.texture.TextureIO; +import javax.media.opengl.GL; +import javax.media.opengl.GL2ES1; +import javax.media.opengl.GL2; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.glu.GLU; + +public class TextureGL2ListenerDraw1 implements GLEventListener { + private GLU glu = new GLU(); + private TextureData textureData; + private Texture texture; + + public TextureGL2ListenerDraw1(TextureData td) { + this.textureData = td; + } + + public void init(GLAutoDrawable drawable) { + this.texture = TextureIO.newTexture(textureData); + } + + public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { + GL2 gl = drawable.getGL().getGL2(); + gl.glMatrixMode(GL2ES1.GL_PROJECTION); + gl.glLoadIdentity(); + glu.gluOrtho2D(0, 1, 0, 1); + gl.glMatrixMode(GL2ES1.GL_MODELVIEW); + gl.glLoadIdentity(); + } + + public void dispose(GLAutoDrawable drawable) { + GL2 gl = drawable.getGL().getGL2(); + if(null!=texture) { + texture.disable(); + texture.destroy(gl); + } + if(null!=textureData) { + textureData.destroy(); + } + } + + public void display(GLAutoDrawable drawable) { + GL2 gl = drawable.getGL().getGL2(); + + // need a valid GL context for this .. + + /** OpenGL .. + texture.updateSubImage(textureData, 0, + 20, 20, + 20, 20, + 100, 100); */ + + + // Now draw one quad with the texture + texture.enable(); + texture.bind(); + gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE); + TextureCoords coords = texture.getImageTexCoords(); + gl.glBegin(GL2.GL_QUADS); + gl.glTexCoord2f(coords.left(), coords.bottom()); + gl.glVertex3f(0, 0, 0); + gl.glTexCoord2f(coords.right(), coords.bottom()); + gl.glVertex3f(1, 0, 0); + gl.glTexCoord2f(coords.right(), coords.top()); + gl.glVertex3f(1, 1, 0); + gl.glTexCoord2f(coords.left(), coords.top()); + gl.glVertex3f(0, 1, 0); + gl.glEnd(); + texture.disable(); + } +} + diff --git a/src/junit/com/jogamp/test/junit/newt/TestWindows01NEWT.java b/src/junit/com/jogamp/test/junit/newt/TestWindows01NEWT.java new file mode 100755 index 000000000..4969a685f --- /dev/null +++ b/src/junit/com/jogamp/test/junit/newt/TestWindows01NEWT.java @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.newt; + +import java.lang.reflect.*; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Test; + +import javax.media.nativewindow.*; + +import com.jogamp.newt.*; +import java.io.IOException; + +public class TestWindows01NEWT { + static int width, height; + + @BeforeClass + public static void initClass() { + width = 640; + height = 480; + } + + static Window createWindow(Screen screen, Capabilities caps, int width, int height, boolean onscreen, boolean undecorated) { + Assert.assertNotNull(caps); + caps.setOnscreen(onscreen); + // System.out.println("Requested: "+caps); + + // + // Create native windowing resources .. X11/Win/OSX + // + Window window = NewtFactory.createWindow(screen, caps, onscreen && undecorated); + Assert.assertNotNull(window); + window.setSize(width, height); + Assert.assertTrue(false==window.isVisible()); + window.setVisible(true); + Assert.assertTrue(true==window.isVisible()); + Assert.assertTrue(width==window.getWidth()); + Assert.assertTrue(height==window.getHeight()); + // System.out.println("Created: "+window); + + // + // Create native OpenGL resources .. XGL/WGL/CGL .. + // equivalent to GLAutoDrawable methods: setVisible(true) + // + caps = window.getGraphicsConfiguration().getNativeGraphicsConfiguration().getChosenCapabilities(); + Assert.assertNotNull(caps); + Assert.assertTrue(caps.getGreenBits()>5); + Assert.assertTrue(caps.getBlueBits()>5); + Assert.assertTrue(caps.getRedBits()>5); + Assert.assertTrue(caps.isOnscreen()==onscreen); + + return window; + } + + static void destroyWindow(Display display, Screen screen, Window window) { + if(null!=window) { + window.destroy(); + } + if(null!=screen) { + screen.destroy(); + } + if(null!=display) { + display.destroy(); + } + } + + @Test + public void testWindowDecor01Simple() throws InterruptedException { + Capabilities caps = new Capabilities(); + Assert.assertNotNull(caps); + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + + Window window = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */); + Thread.sleep(1000); // 1000 ms + destroyWindow(display, screen, window); + } + + @Test + public void testWindowDecor02DestroyWinTwiceA() throws InterruptedException { + Capabilities caps = new Capabilities(); + Assert.assertNotNull(caps); + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + + Window window = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */); + Thread.sleep(1000); // 1000 ms + destroyWindow(null, null, window); + destroyWindow(display, screen, window); + } + + @Test + public void testWindowDecor03TwoWin() throws InterruptedException { + Capabilities caps = new Capabilities(); + Assert.assertNotNull(caps); + Display display = NewtFactory.createDisplay(null); // local display + Assert.assertNotNull(display); + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + Assert.assertNotNull(screen); + + Window window1 = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */); + Window window2 = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */); + Thread.sleep(1000); // 1000 ms + destroyWindow(null, null, window2); + destroyWindow(display, screen, window1); + } + + public static void main(String args[]) throws IOException { + String tstname = TestWindows01NEWT.class.getName(); + org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] { + tstname, + "filtertrace=true", + "haltOnError=false", + "haltOnFailure=false", + "showoutput=true", + "outputtoformatters=true", + "logfailedtests=true", + "logtestlistenerevents=true", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter", + "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } ); + } + +} diff --git a/src/junit/com/jogamp/test/junit/util/MiscUtils.java b/src/junit/com/jogamp/test/junit/util/MiscUtils.java new file mode 100644 index 000000000..d28d0e7cb --- /dev/null +++ b/src/junit/com/jogamp/test/junit/util/MiscUtils.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.test.junit.util; + +import java.lang.reflect.*; + +public class MiscUtils { + @Deprecated + public static int str2int(String str, int def) { + try { + return Integer.parseInt(str); + } catch (Exception ex) { + ex.printStackTrace(); + } + return def; + } + + public static boolean setFieldIfExists(Object instance, String fieldName, Object value) { + try { + Field f = instance.getClass().getField(fieldName); + if(value instanceof Boolean || 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 (IllegalAccessException ex) { + throw new RuntimeException(ex); + } catch (NoSuchFieldException nsfe) { + // OK - throw new RuntimeException(instance.getClass()+" has no '"+fieldName+"' field", nsfe); + } + return false; + } +} + + + |