JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
RandomTileRenderer.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.opengl.util;
29
30import com.jogamp.opengl.GL;
31import com.jogamp.opengl.GL2ES3;
32import com.jogamp.opengl.GLAutoDrawable;
33import com.jogamp.opengl.GLException;
34
35import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
36
37/**
38 * Variation of {@link TileRenderer} w/o using fixed tiles but arbitrary rectangular regions.
39 * <p>
40 * See {@link TileRendererBase} for details.
41 * </p>
42 */
44 private boolean tileRectSet = false;
45
46 /**
47 * Creates a new TileRenderer object
48 */
50 super();
51 }
52
53 @Override
54 public final int getParam(final int pname) {
55 switch (pname) {
56 case TR_IMAGE_WIDTH:
57 return imageSize.getWidth();
58 case TR_IMAGE_HEIGHT:
59 return imageSize.getHeight();
61 return currentTileXPos;
63 return currentTileYPos;
65 return currentTileWidth;
67 return currentTileHeight;
68 default:
69 throw new IllegalArgumentException("Invalid pname: "+pname);
70 }
71 }
72
73 /**
74 * Set the tile rectangle for the subsequent rendering calls.
75 *
76 * @throws IllegalArgumentException is tile x/y are < 0 or tile size is <= 0x0
77 */
78 public void setTileRect(final int tX, final int tY, final int tWidth, final int tHeight) throws IllegalStateException, IllegalArgumentException {
79 if( 0 > tX || 0 > tY ) {
80 throw new IllegalArgumentException("Tile pos must be >= 0/0");
81 }
82 if( 0 >= tWidth || 0 >= tHeight ) {
83 throw new IllegalArgumentException("Tile size must be > 0x0");
84 }
85 this.currentTileXPos = tX;
86 this.currentTileYPos = tY;
87 this.currentTileWidth = tWidth;
88 this.currentTileHeight = tHeight;
89 tileRectSet = true;
90 }
91
92 @Override
93 public final boolean isSetup() {
94 return 0 < imageSize.getWidth() && 0 < imageSize.getHeight() && tileRectSet;
95 }
96
97 /**
98 * {@inheritDoc}
99 *
100 * <p>
101 * <i>end of tiling</i> is never reached w/ {@link RandomRileRenderer},
102 * i.e. method always returns false.
103 * </p>
104 */
105 @Override
106 public final boolean eot() { return false; }
107
108 /**
109 * {@inheritDoc}
110 *
111 * Reset internal states of {@link RandomTileRenderer} are: <i>none</i>.
112 */
113 @Override
114 public final void reset() { }
115
116 /**
117 * {@inheritDoc}
118 *
119 * @throws IllegalStateException if {@link #setImageSize(int, int) image-size} has not been set or
120 * {@link #setTileRect(int, int, int, int) tile-rect} has not been set.
121 */
122 @Override
123 public final void beginTile(final GL gl) throws IllegalStateException, GLException {
124 if( 0 >= imageSize.getWidth() || 0 >= imageSize.getHeight() ) {
125 throw new IllegalStateException("Image size has not been set");
126 }
127 if( !tileRectSet ) {
128 throw new IllegalStateException("tileRect has not been set");
129 }
130 validateGL(gl);
131
132 gl.glViewport( 0, 0, currentTileWidth, currentTileHeight );
133
134 if( DEBUG ) {
135 System.err.println("TileRenderer.begin.X: "+this.toString());
136 }
137
138 // Do not forget to issue:
139 // reshape( 0, 0, tW, tH );
140 // which shall reflect tile renderer fileds: currentTileXPos, currentTileYPos and imageSize
141
142 beginCalled = true;
143 }
144
145 @Override
146 public void endTile( final GL gl ) throws IllegalStateException, GLException {
147 if( !beginCalled ) {
148 throw new IllegalStateException("beginTile(..) has not been called");
149 }
150 validateGL(gl);
151
152 // be sure OpenGL rendering is finished
153 gl.glFlush();
154
155 // implicitly save current glPixelStore values
156 psm.setPackAlignment(gl, 1);
157 final GL2ES3 gl2es3;
158 final int readBuffer;
159 if( gl.isGL2ES3() ) {
160 gl2es3 = gl.getGL2ES3();
161 readBuffer = gl2es3.getDefaultReadBuffer();
162 gl2es3.glReadBuffer(readBuffer);
163 } else {
164 gl2es3 = null;
165 readBuffer = 0; // undef. probably default: GL_FRONT (single buffering) GL_BACK (double buffering)
166 }
167 if( DEBUG ) {
168 System.err.println("TileRenderer.end.0: readBuffer 0x"+Integer.toHexString(readBuffer)+", "+this.toString());
169 }
170
171 final int tmp[] = new int[1];
172
173 if( tileBuffer != null ) {
174 final GLPixelAttributes pixelAttribs = tileBuffer.pixelAttributes;
175 final int srcX = 0;
176 final int srcY = 0;
177 final int srcWidth = currentTileWidth;
178 final int srcHeight = currentTileHeight;
179 final int readPixelSize = GLBuffers.sizeof(gl, tmp, pixelAttribs.pfmt.comp.bytesPerPixel(), srcWidth, srcHeight, 1, true);
181 if( tileBuffer.requiresNewBuffer(gl, srcWidth, srcHeight, readPixelSize) ) {
182 throw new IndexOutOfBoundsException("Required " + readPixelSize + " bytes of buffer, only had " + tileBuffer);
183 }
184 gl.glReadPixels( srcX, srcY, srcWidth, srcHeight, pixelAttribs.format, pixelAttribs.type, tileBuffer.buffer);
185 // be sure OpenGL rendering is finished
186 gl.glFlush();
187 tileBuffer.position( readPixelSize );
189 }
190
191 if( imageBuffer != null ) {
192 final GLPixelAttributes pixelAttribs = imageBuffer.pixelAttributes;
193 final int srcX = 0;
194 final int srcY = 0;
195 final int srcWidth = currentTileWidth;
196 final int srcHeight = currentTileHeight;
197
198 /* setup pixel store for glReadPixels */
199 final int rowLength = imageSize.getWidth();
200 psm.setPackRowLength(gl2es3, rowLength);
201
202 /* read the tile into the final image */
203 final int readPixelSize = GLBuffers.sizeof(gl, tmp, pixelAttribs.pfmt.comp.bytesPerPixel(), srcWidth, srcHeight, 1, true);
204
205 final int ibPos = ( currentTileXPos + ( currentTileYPos * rowLength ) ) * pixelAttribs.pfmt.comp.bytesPerPixel(); // skipPixels + skipRows
206 final int ibLim = ibPos + readPixelSize;
208 if( imageBuffer.requiresNewBuffer(gl, srcWidth, srcHeight, readPixelSize) ) {
209 throw new IndexOutOfBoundsException("Required " + ibLim + " bytes of buffer, only had " + imageBuffer);
210 }
211 imageBuffer.position(ibPos);
212
213 gl.glReadPixels( srcX, srcY, srcWidth, srcHeight, pixelAttribs.format, pixelAttribs.type, imageBuffer.buffer);
214 // be sure OpenGL rendering is finished
215 gl.glFlush();
216 imageBuffer.position( ibLim );
218 }
219
220 /* restore previous glPixelStore values */
221 psm.restore(gl);
222
223 beginCalled = false;
224 }
225
226 /**
227 * Rendering one tile, by simply calling {@link GLAutoDrawable#display()}.
228 *
229 * @throws IllegalStateException if no {@link GLAutoDrawable} is {@link #attachAutoDrawable(GLAutoDrawable) attached}
230 * or imageSize is not set
231 */
232 public void display(final int tX, final int tY, final int tWidth, final int tHeight) throws IllegalStateException {
233 setTileRect(tX, tY, tWidth, tHeight);
234 display();
235 }
236}
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Utility routines for dealing with direct buffers.
Definition: GLBuffers.java:60
static final int sizeof(final GL gl, final int tmp[], final int bytesPerPixel, int width, int height, int depth, final boolean pack)
Returns the number of bytes required to read/write a memory buffer via OpenGL using the current GL pi...
Definition: GLBuffers.java:364
final PixelFormat pfmt
PixelFormat describing the component layout
final int format
The OpenGL pixel data format.
int position()
Returns the byte position of the buffer.
Buffer flip()
See Buffer#flip().
final GLPixelAttributes pixelAttributes
The GLPixelAttributes.
Buffer clear()
See Buffer#clear().
boolean requiresNewBuffer(final GL gl, final int newWidth, final int newHeight, int newByteSize)
Returns true, if invalid or implementation requires a new buffer based on the new size due to pixel a...
final Buffer buffer
Buffer holding the pixel data.
final void setPackRowLength(final GL2ES3 gl, final int packRowLength)
Sets the GL2ES3#GL_PACK_ROW_LENGTH.
final void restore(final GL gl)
Restores PACK and UNPACK pixel storage mode previously saved w/ saveAll(GL) or savePack(GL) and saveU...
final void setPackAlignment(final GL gl, final int packAlignment)
Sets the GL#GL_PACK_ALIGNMENT.
Variation of TileRenderer w/o using fixed tiles but arbitrary rectangular regions.
void display(final int tX, final int tY, final int tWidth, final int tHeight)
Rendering one tile, by simply calling GLAutoDrawable#display().
final void reset()
Method resets implementation's internal state to start of tiling as required for beginTile(GL) if end...
final boolean isSetup()
Returns true if this instance is setup properly, i.e.
final boolean eot()
Returns true if end of tiling has been reached, otherwise false.end of tiling criteria is implementat...
void endTile(final GL gl)
Must be called after rendering the scene, see beginTile(GL).
final int getParam(final int pname)
Gets the parameters of this TileRenderer object.
final void beginTile(final GL gl)
Begins rendering a tile.This method modifies the viewport, see below. User shall reset the viewport w...
void setTileRect(final int tX, final int tY, final int tWidth, final int tHeight)
Set the tile rectangle for the subsequent rendering calls.
RandomTileRenderer()
Creates a new TileRenderer object.
A fairly direct port of Brian Paul's tile rendering library, found at http://www.mesa3d....
static final int TR_CURRENT_TILE_WIDTH
The width of the current tile.
static final int TR_IMAGE_WIDTH
The width of the final image.
static final int TR_CURRENT_TILE_HEIGHT
The height of the current tile.
static final int TR_CURRENT_TILE_X_POS
The x-pos of the current tile.
final void display()
Rendering one tile, by simply calling GLAutoDrawable#display().
static final int TR_CURRENT_TILE_Y_POS
The y-pos of the current tile.
static final int TR_IMAGE_HEIGHT
The height of the final image.
final Composition comp
Unique Pixel Composition, i.e.
int bytesPerPixel()
Number of bytes per pixel, i.e.
void glReadBuffer(int mode)
Entry point to C language function: void {@native glReadBuffer}(GLenum mode) Part of GL_ES_VERSION...
GL2ES3 getGL2ES3()
Casts this object to the GL2ES3 interface.
int getDefaultReadBuffer()
Returns the default color buffer within the current bound getDefaultReadFramebuffer(),...