aboutsummaryrefslogtreecommitdiffstats
path: root/src/jogl/classes/com/jogamp/opengl/util/RandomTileRenderer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/util/RandomTileRenderer.java')
-rw-r--r--src/jogl/classes/com/jogamp/opengl/util/RandomTileRenderer.java233
1 files changed, 59 insertions, 174 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/RandomTileRenderer.java b/src/jogl/classes/com/jogamp/opengl/util/RandomTileRenderer.java
index ac420da3d..0fba1170d 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/RandomTileRenderer.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/RandomTileRenderer.java
@@ -27,8 +27,6 @@
*/
package com.jogamp.opengl.util;
-import javax.media.nativewindow.util.Dimension;
-import javax.media.opengl.GL;
import javax.media.opengl.GL2ES3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
@@ -38,128 +36,80 @@ import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
/**
* Variation of {@link TileRenderer} w/o using fixed tiles but arbitrary rectangular regions.
*/
-public class RandomTileRenderer {
- private final Dimension imageSize = new Dimension(0, 0);
- private final GLPixelStorageModes psm = new GLPixelStorageModes();
-
- private GLPixelBuffer imageBuffer;
- private GLPixelBuffer tileBuffer;
- private PMVMatrixCallback pmvMatrixCB = null;
- private int tX = 0;
- private int tY = 0;
- private int tWidth = 0;
- private int tHeight = 0;
-
- private GLAutoDrawable glad;
- private GLEventListener[] listeners;
- private boolean[] listenersInit;
- private GLEventListener glEventListenerPre = null;
- private GLEventListener glEventListenerPost = null;
+public class RandomTileRenderer extends TileRendererBase {
+ private boolean tileRectSet = false;
- public static interface PMVMatrixCallback {
- void reshapePMVMatrix(GL gl, int tileX, int tileY, int tileWidth, int tileHeight, int imageWidth, int imageHeight);
- }
-
/**
* Creates a new TileRenderer object
*/
public RandomTileRenderer() {
+ super();
}
- public final void setPMVMatrixCallback(PMVMatrixCallback pmvMatrixCB) {
- assert ( null != pmvMatrixCB );
- this.pmvMatrixCB = pmvMatrixCB;
- }
-
- /**
- * Specify a buffer the tiles to be copied to. This is not
- * necessary for the creation of the final image, but useful if you
- * want to inspect each tile in turn.
- *
- * @param buffer The buffer itself. Must be large enough to contain a random tile
- */
- public final void setTileBuffer(GLPixelBuffer buffer) {
- tileBuffer = buffer;
- }
-
- /** @see #setTileBuffer(GLPixelBuffer) */
- public final GLPixelBuffer getTileBuffer() { return tileBuffer; }
-
- /**
- * Sets the desired size of the final image
- *
- * @param width
- * The width of the final image
- * @param height
- * The height of the final image
- */
- public final void setImageSize(int width, int height) {
- imageSize.setWidth(width);
- imageSize.setHeight(height);
+ @Override
+ public final int getParam(int param) {
+ switch (param) {
+ case TR_IMAGE_WIDTH:
+ return imageSize.getWidth();
+ case TR_IMAGE_HEIGHT:
+ return imageSize.getHeight();
+ case TR_CURRENT_TILE_X_POS:
+ return currentTileXPos;
+ case TR_CURRENT_TILE_Y_POS:
+ return currentTileYPos;
+ case TR_CURRENT_TILE_WIDTH:
+ return currentTileWidth;
+ case TR_CURRENT_TILE_HEIGHT:
+ return currentTileHeight;
+ default:
+ throw new IllegalArgumentException("Invalid enumerant as argument");
+ }
}
-
- /** @see #setImageSize(int, int) */
- public final Dimension getImageSize() { return imageSize; }
/**
- * Sets the buffer in which to store the final image
+ * Set the tile rectangle for the subsequent rendering calls.
*
- * @param buffer the buffer itself, must be large enough to hold the final image
+ * @throws IllegalArgumentException is tile x/y are < 0 or tile size is <= 0x0
*/
- public final void setImageBuffer(GLPixelBuffer buffer) {
- imageBuffer = buffer;
+ public void setTileRect(int tX, int tY, int tWidth, int tHeight) throws IllegalStateException, IllegalArgumentException {
+ if( 0 > tX || 0 > tX ) {
+ throw new IllegalArgumentException("Tile pos must be >= 0/0");
+ }
+ if( 0 >= tWidth || 0 >= tHeight ) {
+ throw new IllegalArgumentException("Tile size must be > 0x0");
+ }
+ this.currentTileXPos = tX;
+ this.currentTileYPos = tY;
+ this.currentTileWidth = tWidth;
+ this.currentTileHeight = tHeight;
+ tileRectSet = true;
}
-
- /** @see #setImageBuffer(GLPixelBuffer) */
- public final GLPixelBuffer getImageBuffer() { return imageBuffer; }
/**
- * Begins rendering a tile.
- * <p>
- * Methods modifies the viewport, hence user shall reset the viewport when finishing tile rendering.
- * </p>
- * <p>
- * The projection matrix stack should be
- * left alone after calling this method!
- * </p>
- *
- * @param gl The gl context
- * @throws IllegalStateException
- * @throws IllegalArgumentException
+ * {@inheritDoc}
+ * @throws IllegalStateException if image-size, pmvMatrixCB or tileRect has not been set
*/
- public final void beginTile(GL2ES3 gl, int tX, int tY, int tWidth, int tHeight) throws IllegalStateException, IllegalArgumentException {
+ @Override
+ public final void beginTile(GL2ES3 gl) throws IllegalStateException {
if( 0 >= imageSize.getWidth() || 0 >= imageSize.getHeight() ) {
throw new IllegalStateException("Image size has not been set");
}
if( null == this.pmvMatrixCB ) {
throw new IllegalStateException("pmvMatrixCB has not been set");
}
- if( 0 > tX || 0 > tX ) {
- throw new IllegalArgumentException("Tile pos must be >= 0/0");
- }
- if( 0 >= tWidth || 0 >= tHeight ) {
- throw new IllegalArgumentException("Tile size must be > 0x0");
+ if( !tileRectSet ) {
+ throw new IllegalStateException("tileRect has not been set");
}
- gl.glViewport( 0, 0, tWidth, tHeight );
- pmvMatrixCB.reshapePMVMatrix(gl, tX, tY, tWidth, tHeight, imageSize.getWidth(), imageSize.getHeight());
+ gl.glViewport( 0, 0, currentTileWidth, currentTileHeight );
+ pmvMatrixCB.reshapePMVMatrix(gl, currentTileXPos, currentTileYPos, currentTileWidth, currentTileHeight, imageSize.getWidth(), imageSize.getHeight());
- this.tX = tX;
- this.tY = tY;
- this.tWidth = tWidth;
- this.tHeight = tHeight;
+ beginCalled = true;
}
-
- /**
- * Must be called after rendering the scene
- *
- * @param gl
- * the gl context
- * @return true if there are more tiles to be rendered, false if
- * the final image is complete
- */
- public void endTile( GL2ES3 gl ) {
- if( 0 >= tWidth || 0 >= tHeight ) {
+
+ @Override
+ public void endTile( GL2ES3 gl ) throws IllegalStateException {
+ if( !beginCalled ) {
throw new IllegalStateException("beginTile(..) has not been called");
}
@@ -175,8 +125,8 @@ public class RandomTileRenderer {
final GLPixelAttributes pixelAttribs = tileBuffer.pixelAttributes;
final int srcX = 0;
final int srcY = 0;
- final int srcWidth = tWidth;
- final int srcHeight = tHeight;
+ final int srcWidth = currentTileWidth;
+ final int srcHeight = currentTileHeight;
final int readPixelSize = GLBuffers.sizeof(gl, tmp, pixelAttribs.bytesPerPixel, srcWidth, srcHeight, 1, true);
tileBuffer.clear();
if( tileBuffer.requiresNewBuffer(gl, srcWidth, srcHeight, readPixelSize) ) {
@@ -193,8 +143,8 @@ public class RandomTileRenderer {
final GLPixelAttributes pixelAttribs = imageBuffer.pixelAttributes;
final int srcX = 0;
final int srcY = 0;
- final int srcWidth = tWidth;
- final int srcHeight = tHeight;
+ final int srcWidth = currentTileWidth;
+ final int srcHeight = currentTileHeight;
/* setup pixel store for glReadPixels */
final int rowLength = imageSize.getWidth();
@@ -204,7 +154,7 @@ public class RandomTileRenderer {
/* read the tile into the final image */
final int readPixelSize = GLBuffers.sizeof(gl, tmp, pixelAttribs.bytesPerPixel, srcWidth, srcHeight, 1, true);
- final int ibPos = ( tX + ( tY * rowLength ) ) * pixelAttribs.bytesPerPixel; // skipPixels + skipRows
+ final int ibPos = ( currentTileXPos + ( currentTileYPos * rowLength ) ) * pixelAttribs.bytesPerPixel; // skipPixels + skipRows
final int ibLim = ibPos + readPixelSize;
imageBuffer.clear();
if( imageBuffer.requiresNewBuffer(gl, srcWidth, srcHeight, readPixelSize) ) {
@@ -221,87 +171,22 @@ public class RandomTileRenderer {
/* restore previous glPixelStore values */
psm.restore(gl);
-
- this.tX = 0;
- this.tY = 0;
- this.tWidth = 0;
- this.tHeight = 0;
- }
-
- /**
- *
- * <p>
- * Sets the size of the tiles to use in rendering. The actual
- * effective size of the tile depends on the border size, ie (
- * width - 2*border ) * ( height - 2 * border )
- * </p>
- * @param glad
- * @throws IllegalStateException if an {@link GLAutoDrawable} is already attached
- */
- public void attachAutoDrawable(GLAutoDrawable glad, PMVMatrixCallback pmvMatrixCB) throws IllegalStateException {
- if( null != this.glad ) {
- throw new IllegalStateException("GLAutoDrawable already attached");
- }
- this.glad = glad;
- setPMVMatrixCallback(pmvMatrixCB);
- final int aSz = glad.getGLEventListenerCount();
- listeners = new GLEventListener[aSz];
- listenersInit = new boolean[aSz];
- for(int i=0; i<aSz; i++) {
- final GLEventListener l = glad.getGLEventListener(0);
- listenersInit[i] = glad.getGLEventListenerInitState(l);
- listeners[i] = glad.removeGLEventListener( l );
- }
- glad.addGLEventListener(tiledGLEL);
- }
-
- public void detachAutoDrawable() {
- if( null != glad ) {
- glad.removeGLEventListener(tiledGLEL);
- final int aSz = listenersInit.length;
- for(int i=0; i<aSz; i++) {
- final GLEventListener l = listeners[i];
- glad.addGLEventListener(l);
- glad.setGLEventListenerInitState(l, listenersInit[i]);
- }
- listeners = null;
- listenersInit = null;
- glad = null;
- pmvMatrixCB = null;
- }
- }
-
- /**
- * Set {@link GLEventListener} for pre- and post operations when used w/
- * {@link #attachAutoDrawable(GLAutoDrawable, int, PMVMatrixCallback)}
- * for each {@link GLEventListener} callback.
- * @param preTile the pre operations
- * @param postTile the post operations
- */
- public void setGLEventListener(GLEventListener preTile, GLEventListener postTile) {
- glEventListenerPre = preTile;
- glEventListenerPost = postTile;
+ beginCalled = false;
}
/**
* Rendering one tile, by simply calling {@link GLAutoDrawable#display()}.
*
- * @return true if there are more tiles to be rendered, false if the final image is complete
- * @throws IllegalStateException if no {@link GLAutoDrawable} is {@link #attachAutoDrawable(GLAutoDrawable, int) attached}
+ * @throws IllegalStateException if no {@link GLAutoDrawable} is {@link #attachToAutoDrawable(GLAutoDrawable, int) attached}
* or imageSize is not set
*/
public void display(int tX, int tY, int tWidth, int tHeight) throws IllegalStateException {
- if( null == glad ) {
- throw new IllegalStateException("No GLAutoDrawable attached");
- }
- this.tX = tX;
- this.tY = tY;
- this.tWidth = tWidth;
- this.tHeight = tHeight;
- glad.display();
+ setTileRect(tX, tY, tWidth, tHeight);
+ display();
}
+ protected final GLEventListener getTiledGLEL() { return tiledGLEL; }
private final GLEventListener tiledGLEL = new GLEventListener() {
@Override
public void init(GLAutoDrawable drawable) {
@@ -338,7 +223,7 @@ public class RandomTileRenderer {
}
final GL2ES3 gl = drawable.getGL().getGL2ES3();
- beginTile(gl, tX, tY, tWidth, tHeight);
+ beginTile(gl);
final int aSz = listenersInit.length;
for(int i=0; i<aSz; i++) {