Skip to content

Commit

Permalink
TestTiledPrintingGearsSwingAWT: Add a second demo GLEventListener sho…
Browse files Browse the repository at this point in the history
…wing it's working/scaling well; Adding TileRendererBase.TileRendererNotify to RedAquareES2 having a 3rd demo case.
  • Loading branch information
sgothel committed Sep 10, 2013
1 parent d2be196 commit 569c538
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 27 deletions.
4 changes: 2 additions & 2 deletions make/scripts/tests.sh
Expand Up @@ -330,8 +330,8 @@ function testawtswt() {
#testnoawt com.jogamp.opengl.test.junit.jogl.tile.TestRandomTiledRendering2GL2NEWT $*
#testawt com.jogamp.opengl.test.junit.jogl.tile.TestRandomTiledRendering3GL2AWT $*
#testawt com.jogamp.opengl.test.junit.jogl.tile.TestTiledPrintingGearsAWT $*
#testawt com.jogamp.opengl.test.junit.jogl.tile.TestTiledPrintingGearsSwingAWT $*
testawt com.jogamp.opengl.test.junit.jogl.tile.TestTiledPrintingGearsNewtAWT $*
testawt com.jogamp.opengl.test.junit.jogl.tile.TestTiledPrintingGearsSwingAWT $*
#testawt com.jogamp.opengl.test.junit.jogl.tile.TestTiledPrintingGearsNewtAWT $*

#
# core/newt (testnoawt and testawt)
Expand Down
Expand Up @@ -269,7 +269,8 @@ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height
imageWidth = tileRendererInUse.getParam(TileRendererBase.TR_IMAGE_WIDTH);
imageHeight = tileRendererInUse.getParam(TileRendererBase.TR_IMAGE_HEIGHT);
}
/* compute projection parameters */

// compute projection parameters 'normal'
float left, right, bottom, top;
if( imageHeight > imageWidth ) {
float a = (float)imageHeight / (float)imageWidth;
Expand All @@ -286,14 +287,19 @@ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height
}
final float w = right - left;
final float h = top - bottom;

// compute projection parameters 'tiled'
final float l = left + tileX * w / imageWidth;
final float r = l + tileWidth * w / imageWidth;
final float b = bottom + tileY * h / imageHeight;
final float t = b + tileHeight * h / imageHeight;

final float _w = r - l;
final float _h = t - b;
System.err.println(">> angle "+angle+", [l "+left+", r "+right+", b "+bottom+", t "+top+"] "+w+"x"+h+" -> [l "+l+", r "+r+", b "+b+", t "+t+"] "+_w+"x"+_h);
if(verbose) {
System.err.println(">> angle "+angle+", [l "+left+", r "+right+", b "+bottom+", t "+top+"] "+w+"x"+h+" -> [l "+l+", r "+r+", b "+b+", t "+t+"] "+_w+"x"+_h);
}

pmvMatrix.glFrustumf(l, r, b, t, 5.0f, 200.0f);

pmvMatrix.glMatrixMode(PMVMatrix.GL_MODELVIEW);
Expand Down
Expand Up @@ -30,6 +30,7 @@
import com.jogamp.opengl.JoglVersion;
import com.jogamp.opengl.util.GLArrayDataServer;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.util.TileRendererBase;
import com.jogamp.opengl.util.glsl.ShaderCode;
import com.jogamp.opengl.util.glsl.ShaderProgram;
import com.jogamp.opengl.util.glsl.ShaderState;
Expand All @@ -39,7 +40,7 @@
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLUniformData;

public class RedSquareES2 implements GLEventListener {
public class RedSquareES2 implements GLEventListener, TileRendererBase.TileRendererNotify {
private ShaderState st;
private PMVMatrix pmvMatrix;
private GLUniformData pmvMatrixUniform;
Expand All @@ -50,6 +51,8 @@ public class RedSquareES2 implements GLEventListener {
private float aspect = 1.0f;
private boolean doRotate = true;
private boolean clearBuffers = true;
private TileRendererBase tileRendererInUse = null;
private boolean doRotateBeforePrinting;

public RedSquareES2(int swapInterval) {
this.swapInterval = swapInterval;
Expand All @@ -59,6 +62,15 @@ public RedSquareES2() {
this.swapInterval = 1;
}

public void addTileRendererNotify(TileRendererBase tr) {
tileRendererInUse = tr;
doRotateBeforePrinting = doRotate;
setDoRotation(false);
}
public void removeTileRendererNotify(TileRendererBase tr) {
tileRendererInUse = null;
setDoRotation(doRotateBeforePrinting);
}
public void setAspect(float aspect) { this.aspect = aspect; }
public void setDoRotation(boolean rotate) { this.doRotate = rotate; }
public void setClearBuffers(boolean v) { clearBuffers = v; }
Expand Down Expand Up @@ -131,7 +143,11 @@ public void display(GLAutoDrawable glad) {

final GL2ES2 gl = glad.getGL().getGL2ES2();
if( clearBuffers ) {
gl.glClearColor(0, 0, 0, 0);
if( null != tileRendererInUse ) {
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
} else {
gl.glClearColor(0, 0, 0, 0);
}
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
}
if( !gl.hasGLSL() ) {
Expand Down Expand Up @@ -166,15 +182,50 @@ public void reshape(GLAutoDrawable glad, int x, int y, int width, int height) {
return;
}

if(-1 != swapInterval) {
gl.setSwapInterval(swapInterval); // in case switching the drawable (impl. may bound attribute there)
}

st.useProgram(gl, true);
// Set location in front of camera
pmvMatrix.glMatrixMode(PMVMatrix.GL_PROJECTION);
pmvMatrix.glLoadIdentity();
pmvMatrix.gluPerspective(45.0F, ( (float) width / (float) height ) / aspect, 1.0F, 100.0F);

final int tileWidth = width;
final int tileHeight = height;
final int tileX, tileY, imageWidth, imageHeight;
if( null == tileRendererInUse ) {
if(-1 != swapInterval) {
gl.setSwapInterval(swapInterval);
}
tileX = 0;
tileY = 0;
imageWidth = width;
imageHeight = height;
} else {
gl.setSwapInterval(0);
tileX = tileRendererInUse.getParam(TileRendererBase.TR_CURRENT_TILE_X_POS);
tileY = tileRendererInUse.getParam(TileRendererBase.TR_CURRENT_TILE_Y_POS);
imageWidth = tileRendererInUse.getParam(TileRendererBase.TR_IMAGE_WIDTH);
imageHeight = tileRendererInUse.getParam(TileRendererBase.TR_IMAGE_HEIGHT);
}
// compute projection parameters 'normal' perspective
final float fovy=45f;
final float aspect2 = ( (float) width / (float) height ) / aspect;
final float zNear=1f;
final float zFar=100f;

// compute projection parameters 'normal' frustum
final float top=(float)Math.tan(fovy*((float)Math.PI)/360.0f)*zNear;
final float bottom=-1.0f*top;
final float left=aspect2*bottom;
final float right=aspect2*top;
final float w = right - left;
final float h = top - bottom;

// compute projection parameters 'tiled'
final float l = left + tileX * w / imageWidth;
final float r = l + tileWidth * w / imageWidth;
final float b = bottom + tileY * h / imageHeight;
final float t = b + tileHeight * h / imageHeight;

pmvMatrix.glFrustumf(l, r, b, t, zNear, zFar);
//pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f);
st.uniform(gl, pmvMatrixUniform);
st.useProgram(gl, false);
Expand Down
11 changes: 9 additions & 2 deletions src/test/com/jogamp/opengl/test/junit/jogl/demos/gl2/Gears.java
Expand Up @@ -36,6 +36,7 @@ public class Gears implements GLEventListener, TileRendererBase.TileRendererNoti
private KeyListener gearsKeys = new GearsKeyAdapter();
private TileRendererBase tileRendererInUse = null;
private boolean doRotateBeforePrinting;
private boolean verbose = true;

// private boolean mouseRButtonDown = false;
private int prevMouseX, prevMouseY;
Expand All @@ -59,6 +60,7 @@ public void removeTileRendererNotify(TileRendererBase tr) {
}

public void setDoRotation(boolean rotate) { doRotate = rotate; }
public void setVerbose(boolean v) { verbose = v; }

public void setGears(int g1, int g2, int g3) {
gear1 = g1;
Expand Down Expand Up @@ -185,7 +187,8 @@ public void reshape(GL2 gl, int x, int y, int width, int height) {
imageWidth = tileRendererInUse.getParam(TileRendererBase.TR_IMAGE_WIDTH);
imageHeight = tileRendererInUse.getParam(TileRendererBase.TR_IMAGE_HEIGHT);
}
/* compute projection parameters */

// compute projection parameters 'normal'
float left, right, bottom, top;
if( imageHeight > imageWidth ) {
float a = (float)imageHeight / (float)imageWidth;
Expand All @@ -202,14 +205,18 @@ public void reshape(GL2 gl, int x, int y, int width, int height) {
}
final float w = right - left;
final float h = top - bottom;

// compute projection parameters 'tiled'
final float l = left + tileX * w / imageWidth;
final float r = l + tileWidth * w / imageWidth;
final float b = bottom + tileY * h / imageHeight;
final float t = b + tileHeight * h / imageHeight;

final float _w = r - l;
final float _h = t - b;
System.err.println(">> angle "+angle+", [l "+left+", r "+right+", b "+bottom+", t "+top+"] "+w+"x"+h+" -> [l "+l+", r "+r+", b "+b+", t "+t+"] "+_w+"x"+_h);
if(verbose) {
System.err.println(">> angle "+angle+", [l "+left+", r "+right+", b "+bottom+", t "+top+"] "+w+"x"+h+" -> [l "+l+", r "+r+", b "+b+", t "+t+"] "+_w+"x"+_h);
}
gl.glFrustum(l, r, b, t, 5.0f, 60.0f);

gl.glMatrixMode(GL2.GL_MODELVIEW);
Expand Down
Expand Up @@ -60,6 +60,7 @@
import com.jogamp.newt.event.TraceWindowAdapter;
import com.jogamp.newt.event.awt.AWTKeyAdapter;
import com.jogamp.newt.event.awt.AWTWindowAdapter;
import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
import com.jogamp.opengl.test.junit.util.QuitAdapter;
Expand Down Expand Up @@ -93,15 +94,24 @@ public static void releaseClass() {
}

protected void runTestGL(GLCapabilities caps) throws InterruptedException, InvocationTargetException {
final GLJPanel glJPanel = new GLJPanel(caps);
Assert.assertNotNull(glJPanel);
Dimension glc_sz = new Dimension(width, height);
glJPanel.setMinimumSize(glc_sz);
glJPanel.setPreferredSize(glc_sz);
glJPanel.setSize(glc_sz);
final Dimension glc_sz = new Dimension(width/2, height);
final GLJPanel glJPanel1 = new GLJPanel(caps);
Assert.assertNotNull(glJPanel1);
glJPanel1.setMinimumSize(glc_sz);
glJPanel1.setPreferredSize(glc_sz);
glJPanel1.setSize(glc_sz);
glJPanel1.addGLEventListener(new Gears());

final Gears gears = new Gears();
glJPanel.addGLEventListener(gears);
final GLJPanel glJPanel2 = new GLJPanel(caps);
Assert.assertNotNull(glJPanel2);
glJPanel2.setMinimumSize(glc_sz);
glJPanel2.setPreferredSize(glc_sz);
glJPanel2.setSize(glc_sz);
glJPanel2.addGLEventListener(new RedSquareES2());

final JPanel demoPanel = new JPanel();
demoPanel.add(glJPanel1);
demoPanel.add(glJPanel2);

final JFrame frame = new JFrame("Swing Print");
Assert.assertNotNull(frame);
Expand Down Expand Up @@ -136,18 +146,21 @@ public void actionPerformed(ActionEvent e) {
final JPanel westPanel = new JPanel();
westPanel.add(new Label("West"));

Animator animator = new Animator(glJPanel);
Animator animator = new Animator();
animator.add(glJPanel1);
animator.add(glJPanel2);
QuitAdapter quitAdapter = new QuitAdapter();

new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter)).addTo(glJPanel);
new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter)).addTo(glJPanel1);
new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter)).addTo(glJPanel2);
new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter)).addTo(frame);

SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
final Container fcont = frame.getContentPane();
fcont.setLayout(new BorderLayout());
fcont.add(printPanel, BorderLayout.NORTH);
fcont.add(glJPanel, BorderLayout.CENTER);
fcont.add(demoPanel, BorderLayout.CENTER);
fcont.add(southPanel, BorderLayout.SOUTH);
fcont.add(eastPanel, BorderLayout.EAST);
fcont.add(westPanel, BorderLayout.WEST);
Expand All @@ -157,7 +170,8 @@ public void run() {
} } ) ;

Assert.assertEquals(true, AWTRobotUtil.waitForVisible(frame, true));
Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glJPanel, true));
Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glJPanel1, true));
Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glJPanel2, true));

animator.setUpdateFPSFrames(60, System.err);
animator.start();
Expand All @@ -184,7 +198,8 @@ public void run() {
// try { Thread.sleep(4000); } catch (InterruptedException e) { } // time to finish print jobs .. FIXME ??

Assert.assertNotNull(frame);
Assert.assertNotNull(glJPanel);
Assert.assertNotNull(glJPanel1);
Assert.assertNotNull(glJPanel2);
Assert.assertNotNull(animator);

animator.stop();
Expand All @@ -197,7 +212,7 @@ public void run() {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
final Frame _frame = frame;
_frame.remove(glJPanel);
_frame.remove(demoPanel);
_frame.dispose();
}});
}
Expand Down

0 comments on commit 569c538

Please sign in to comment.