/** * Copyright 2013 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions 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. * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. * * --------------------- * * Based on Brian Paul's tile rendering library, found * at http://www.mesa3d.org/brianp/TR.html. * * Copyright (C) 1997-2005 Brian Paul. * Licensed under BSD-compatible terms with permission of the author. * See LICENSE.txt for license information. */ package com.jogamp.opengl.util; import javax.media.nativewindow.util.Dimension; import javax.media.nativewindow.util.DimensionImmutable; import javax.media.opengl.GL; import javax.media.opengl.GL2ES3; import javax.media.opengl.GLException; import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes; /** * A fairly direct port of Brian Paul's tile rendering library, found * at * http://www.mesa3d.org/brianp/TR.html . I've java-fied it, but * the functionality is the same. *
* Original code Copyright (C) 1997-2005 Brian Paul. Licensed under * BSD-compatible terms with permission of the author. See LICENSE.txt * for license information. *
** Enhanced for {@link GL2ES3}. *
** See {@link TileRendererBase} for details. *
* * @author ryanm, sgothel */ public class TileRenderer extends TileRendererBase { /** * The width of the final clipped image. See {@link #getParam(int)}. */ public static final int TR_IMAGE_CLIPPING_WIDTH = 7; /** * The height of the final clipped image. See {@link #getParam(int)}. */ public static final int TR_IMAGE_CLIPPING_HEIGHT = 8; /** * The width of the tiles. See {@link #getParam(int)}. */ public static final int TR_TILE_WIDTH = 9; /** * The height of the tiles. See {@link #getParam(int)}. */ public static final int TR_TILE_HEIGHT = 10; /** * The width of the border around the tiles. See {@link #getParam(int)}. */ public static final int TR_TILE_BORDER = 11; /** * The tiles x-offset. See {@link #getParam(int)}. */ public static final int TR_TILE_X_OFFSET = 12; /** * The tiles y-offset. See {@link #getParam(int)}. */ public static final int TR_TILE_Y_OFFSET = 13; /** * The number of rows of tiles. See {@link #getParam(int)}. */ public static final int TR_ROWS = 14; /** * The number of columns of tiles. See {@link #getParam(int)}. */ public static final int TR_COLUMNS = 15; /** * The current tile number. Has value -1 if {@link #eot()}. See {@link #getParam(int)}. */ public static final int TR_CURRENT_TILE_NUM = 16; /** * The current row number. See {@link #getParam(int)}. */ public static final int TR_CURRENT_ROW = 17; /** * The current column number. See {@link #getParam(int)}. */ public static final int TR_CURRENT_COLUMN = 18; /** * The order that the rows are traversed. See {@link #getParam(int)}. */ public static final int TR_ROW_ORDER = 19; /** * Indicates we are traversing rows from the top to the bottom. See {@link #getParam(int)}. */ public static final int TR_TOP_TO_BOTTOM = 20; /** * Indicates we are traversing rows from the bottom to the top (default). See {@link #getParam(int)}. */ public static final int TR_BOTTOM_TO_TOP = 21; private static final int DEFAULT_TILE_WIDTH = 256; private static final int DEFAULT_TILE_HEIGHT = 256; private static final int DEFAULT_TILE_BORDER = 0; private final Dimension tileSize = new Dimension(DEFAULT_TILE_WIDTH, DEFAULT_TILE_HEIGHT); private final Dimension tileSizeNB = new Dimension(DEFAULT_TILE_WIDTH - 2 * DEFAULT_TILE_BORDER, DEFAULT_TILE_HEIGHT - 2 * DEFAULT_TILE_BORDER); private boolean isInit = false; private Dimension imageClippingDim = null; // not set - default private int tileBorder = DEFAULT_TILE_BORDER; private int rowOrder = TR_BOTTOM_TO_TOP; private int rows; private int columns; private int currentTile = 0; private int currentRow; private int currentColumn; private int offsetX; private int offsetY; @Override protected StringBuilder tileDetails(final StringBuilder sb) { sb.append("# "+currentTile+": ["+currentColumn+"]["+currentRow+"] / "+columns+"x"+rows+", ") .append("rowOrder "+rowOrder+", offset/size "+offsetX+"/"+offsetY+" "+tileSize.getWidth()+"x"+tileSize.getHeight()+" brd "+tileBorder+", "); return super.tileDetails(sb); } /** * Creates a new TileRenderer object */ public TileRenderer() { super(); } /** * {@inheritDoc} ** Implementation {@link #reset()} internal states. *
*/ @Override public final void setImageSize(final int width, final int height) { super.setImageSize(width, height); reset(); } /** * Clips the image-size this tile-renderer iterates through, * which can be retrieved via {@link #getClippedImageSize()}. ** Original image-size stored in this tile-renderer is unmodified. *
** Implementation {@link #reset()} internal states. *
* * @param width The image-clipping.width * @param height The image-clipping.height * @see #getClippedImageSize() */ public final void clipImageSize(final int width, final int height) { if( null == imageClippingDim ) { imageClippingDim = new Dimension(width, height); } else { imageClippingDim.set(width, height); } reset(); } /** * Returns the clipped image-size. ** If a image-size is clipped via {@link #clipImageSize(int, int)}, * method returns: *
min( image-clipping, image-size )
, otherwise image-size
* The clipping width and height can be retrieved via {@link #TR_IMAGE_CLIPPING_WIDTH} * {@link #TR_IMAGE_CLIPPING_HEIGHT}. *
*/ public final DimensionImmutable getClippedImageSize() { if( null != imageClippingDim ) { return new Dimension(Math.min(imageClippingDim.getWidth(), imageSize.getWidth()), Math.min(imageClippingDim.getHeight(), imageSize.getHeight()) ); } else { return imageSize; } } /** * 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 ) ** Implementation {@link #reset()} internal states. *
* * @param width * The width of the tiles. Must not be larger than the GL * context * @param height * The height of the tiles. Must not be larger than the * GL context * @param border * The width of the borders on each tile. This is needed * to avoid artifacts when rendering lines or points with * thickness > 1. */ public final void setTileSize(final int width, final int height, final int border) { if( 0 > border ) { throw new IllegalArgumentException("Tile border must be >= 0"); } if( 2 * border >= width || 2 * border >= height ) { throw new IllegalArgumentException("Tile size must be > 0x0 minus 2*border"); } tileBorder = border; tileSize.set( width, height ); tileSizeNB.set( width - 2 * border, height - 2 * border ); reset(); } /** * Sets an xy offset for the resulting tiles * {@link TileRendererBase#TR_CURRENT_TILE_X_POS x-pos} and {@link TileRendererBase#TR_CURRENT_TILE_Y_POS y-pos}. * @see #TR_TILE_X_OFFSET * @see #TR_TILE_Y_OFFSET **/ public void setTileOffset(final int xoff, final int yoff) { offsetX = xoff; offsetY = yoff; } /** * {@inheritDoc} * * Reset internal states of {@link TileRenderer} are: ** end of tiling is reached w/ {@link TileRenderer}, if at least one of the following is true: *