JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestTiledRendering1GL2NEWT.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.test.junit.jogl.tile;
29
30import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
31import com.jogamp.opengl.test.junit.util.UITestCase;
32import com.jogamp.opengl.util.GLPixelBuffer;
33import com.jogamp.opengl.util.TileRenderer;
34import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
35import com.jogamp.opengl.util.TileRendererBase;
36import com.jogamp.opengl.util.texture.TextureData;
37import com.jogamp.opengl.util.texture.TextureIO;
38
39import java.io.File;
40import java.io.IOException;
41
42import com.jogamp.nativewindow.util.PixelFormat;
43import com.jogamp.opengl.GL2;
44import com.jogamp.opengl.GLAutoDrawable;
45import com.jogamp.opengl.GLCapabilities;
46import com.jogamp.opengl.GLContext;
47import com.jogamp.opengl.GLDrawable;
48import com.jogamp.opengl.GLDrawableFactory;
49import com.jogamp.opengl.GLEventListener;
50import com.jogamp.opengl.GLProfile;
51
52import org.junit.Assert;
53import org.junit.FixMethodOrder;
54import org.junit.Test;
55import org.junit.runners.MethodSorters;
56
57/**
58 * Demos offscreen {@link GLDrawable} being used for
59 * {@link TileRenderer} rendering to produce a PNG file.
60 * <p>
61 * All {@link TileRenderer} operations are
62 * being performed from the main thread sequentially
63 * without {@link GLAutoDrawable} or {@link GLEventListener}.
64 * </p>
65*/
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
68 static long duration = 500; // ms
69
70 static class DrawableContext {
71 DrawableContext(final GLDrawable d, final GLContext glc) {
72 this.d = d;
73 this.glc = glc;
74 }
75 GLDrawable d;
76 GLContext glc;
77 }
78
79 private static DrawableContext createDrawableAndCurrentCtx(final GLCapabilities glCaps, final int width, final int height) {
81 final GLDrawable d = factory.createOffscreenDrawable(null, glCaps, null, width, height);
82 d.setRealized(true);
83 GLContext glc = null;
84 glc = d.createContext(null);
85 Assert.assertTrue("Context could not be made current", GLContext.CONTEXT_NOT_CURRENT < glc.makeCurrent());
86 return new DrawableContext(d, glc);
87 }
88
89 private static void destroyDrawableContext(final DrawableContext dc) {
90 if(null != dc.glc) {
91 dc.glc.destroy();
92 dc.glc = null;
93 }
94 if(null != dc.d) {
95 dc.d.setRealized(false);
96 dc.d = null;
97 }
98 }
99
100 @Test
101 public void test01() throws IOException {
102 final GLProfile glp = GLProfile.getMaxFixedFunc(true);
103 final GLCapabilities caps = new GLCapabilities(glp);
104 caps.setOnscreen(false);
105
106 final int maxTileSize = 256;
107 final DrawableContext dc = createDrawableAndCurrentCtx(caps, maxTileSize, maxTileSize);
108 final GL2 gl = dc.glc.getGL().getGL2();
109
110 // Fix the image size for now
111 final int imageWidth = dc.d.getSurfaceWidth() * 6;
112 final int imageHeight = dc.d.getSurfaceHeight() * 4;
113
114 final String filename = this.getSnapshotFilename(0, "-tile", dc.d.getChosenGLCapabilities(), imageWidth, imageHeight, false, TextureIO.PNG, null);
115 final File file = new File(filename);
116
117 // Initialize the tile rendering library
118 final TileRenderer renderer = new com.jogamp.opengl.util.TileRenderer();
119 renderer.setTileSize(dc.d.getSurfaceWidth(), dc.d.getSurfaceHeight(), 0);
120 renderer.setImageSize(imageWidth, imageHeight);
121
122 final GLPixelBuffer.GLPixelBufferProvider pixelBufferProvider = GLPixelBuffer.defaultProviderWithRowStride;
123 final boolean[] flipVertically = { false };
124
125 final PixelFormat.Composition hostPixelComp = pixelBufferProvider.getHostPixelComp(gl.getGLProfile(), 3);
126 final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, 3, true);
127 final GLPixelBuffer pixelBuffer = pixelBufferProvider.allocate(gl, hostPixelComp, pixelAttribs, true, imageWidth, imageHeight, 1, 0);
128 renderer.setImageBuffer(pixelBuffer);
129 flipVertically[0] = false;
130
131 final Gears gears = new Gears();
132 gears.setVerbose(false);
133 gears.init(gl);
134
135 gears.addTileRendererNotify(renderer);
136 while( !renderer.eot() ) {
137 renderer.beginTile(gl);
138 gears.reshape(gl,
142 gears.display(gl);
143 renderer.endTile(gl);
144 }
145 gears.removeTileRendererNotify(renderer);
146
147 destroyDrawableContext(dc);
148
149 final GLPixelBuffer imageBuffer = renderer.getImageBuffer();
150 final TextureData textureData = new TextureData(
151 caps.getGLProfile(),
152 0 /* internalFormat */,
153 imageWidth, imageHeight,
154 0,
155 imageBuffer.pixelAttributes,
156 false, false,
157 flipVertically[0],
158 imageBuffer.buffer,
159 null /* Flusher */);
160
161 TextureIO.write(textureData, file);
162 }
163
164 @Test
165 public void test02_EOT_01() throws IOException {
166 final GLProfile glp = GLProfile.getMaxFixedFunc(true);
167 final GLCapabilities caps = new GLCapabilities(glp);
168 caps.setOnscreen(false);
169
170 final int maxTileSize = 256;
171 final DrawableContext dc = createDrawableAndCurrentCtx(caps, maxTileSize, maxTileSize);
172 final GL2 gl = dc.glc.getGL().getGL2();
173
174 // Fix the image size for now
175 final int imageWidth = dc.d.getSurfaceWidth() * 6;
176 final int imageHeight = dc.d.getSurfaceHeight() * 4;
177
178 // Initialize the tile rendering library
179 final TileRenderer renderer = new com.jogamp.opengl.util.TileRenderer();
180 renderer.setTileSize(dc.d.getSurfaceWidth(), dc.d.getSurfaceHeight(), 0);
181
182 IllegalStateException ise = null;
183 try {
184 renderer.beginTile(gl); // Image size has not been set
185 } catch (final IllegalStateException _ise) {
186 ise = _ise;
187 System.err.println("Expected "+ise.getClass().getSimpleName()+": "+ise.getMessage());
188 }
189 Assert.assertNotNull("TileRenderer.beginTile: Image-size exception missing", ise);
190
191 renderer.setImageSize(imageWidth, imageHeight);
192
193 renderer.clipImageSize(0, 0);
194 try {
195 renderer.beginTile(gl); // EOT reached (1)
196 } catch (final IllegalStateException _ise) {
197 ise = _ise;
198 System.err.println("Expected "+ise.getClass().getSimpleName()+": "+ise.getMessage());
199 }
200 Assert.assertNotNull("TileRenderer.beginTile: EOT (1) exception missing", ise);
201
202 renderer.clipImageSize(imageWidth, imageHeight); // back to full size
203
204 final Gears gears = new Gears();
205 gears.setVerbose(false);
206 gears.init(gl);
207
208 gears.addTileRendererNotify(renderer);
209 int numTiles = 0;
210 while( !renderer.eot() ) {
211 renderer.beginTile(gl);
212 gears.reshape(gl,
216 gears.display(gl);
217 renderer.endTile(gl);
218 numTiles++;
219 }
220 try {
221 renderer.beginTile(gl); // EOT reached (2)
222 } catch (final IllegalStateException _ise) {
223 ise = _ise;
224 System.err.println("Expected "+ise.getClass().getSimpleName()+": "+ise.getMessage());
225 }
226 Assert.assertNotNull("TileRenderer.beginTile: EOT (2) exception missing", ise);
227 gears.removeTileRendererNotify(renderer);
228
229 Assert.assertTrue("TileRenderer not rendered more than one tile but "+numTiles, numTiles > 1);
230
231 destroyDrawableContext(dc);
232 }
233
234 public static void main(final String args[]) {
235 for(int i=0; i<args.length; i++) {
236 if(args[i].equals("-time")) {
237 i++;
238 try {
239 duration = Integer.parseInt(args[i]);
240 } catch (final Exception ex) { ex.printStackTrace(); }
241 }
242 }
243 org.junit.runner.JUnitCore.main(TestTiledRendering1GL2NEWT.class.getName());
244 }
245}
void setOnscreen(final boolean onscreen)
Sets whether the surface shall be on- or offscreen.
Specifies a set of OpenGL capabilities.
final GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
static final int CONTEXT_NOT_CURRENT
Indicates that the context was not made current during the last call to makeCurrent,...
Definition: GLContext.java:112
abstract int makeCurrent()
Makes this GLContext current on the calling thread.
abstract GLDrawable createOffscreenDrawable(AbstractGraphicsDevice device, GLCapabilitiesImmutable caps, GLCapabilitiesChooser chooser, int width, int height)
Creates an unrealized offscreen GLDrawable incl it's offscreen NativeSurface with the given capabilit...
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getMaxFixedFunc(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the fixed function pipeline.
Definition: GLProfile.java:808
Gears.java author: Brian Paul (converted to Java by Ron Cemer and Sven Gothel)
Definition: Gears.java:34
void removeTileRendererNotify(final TileRendererBase tr)
The owning GLAutoDrawable is detached from the given TileRendererBase instance.
Definition: Gears.java:69
void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
Definition: Gears.java:245
void addTileRendererNotify(final TileRendererBase tr)
The owning GLAutoDrawable is attached to the given TileRendererBase instance.
Definition: Gears.java:63
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
Definition: Gears.java:106
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
Definition: Gears.java:333
Demos offscreen GLDrawable being used for TileRenderer rendering to produce a PNG file.
OpenGL pixel data buffer, allowing user to provide buffers via their GLPixelBufferProvider implementa...
static final GLPixelBufferProvider defaultProviderWithRowStride
Default GLPixelBufferProvider with GLPixelBufferProvider#getAllowRowStride() == true,...
final GLPixelAttributes pixelAttributes
The GLPixelAttributes.
final Buffer buffer
Buffer holding the pixel data.
A fairly direct port of Brian Paul's tile rendering library, found at http://www.mesa3d....
final void setImageBuffer(final GLPixelBuffer buffer)
Sets the buffer in which to store the final image.
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.
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.
A fairly direct port of Brian Paul's tile rendering library, found at http://www.mesa3d....
final void clipImageSize(final int width, final int height)
Clips the image-size this tile-renderer iterates through, which can be retrieved via getClippedImageS...
final int getParam(final int pname)
Gets the parameters of this TileRenderer object.
final boolean eot()
Returns true if end of tiling has been reached, otherwise false.end of tiling criteria is implementat...
TileRenderer()
Creates a new 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 endTile(final GL gl)
Must be called after rendering the scene, see beginTile(GL).
final void setImageSize(final int width, final int height)
Sets the desired size of the final image.
final void setTileSize(final int width, final int height, final int border)
Sets the size of the tiles to use in rendering.
Represents the data for an OpenGL texture.
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
Definition: TextureIO.java:171
static void write(final Texture texture, final File file)
Writes the given texture to a file.
Definition: TextureIO.java:607
GL getGL()
Casts this object to the GL interface.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
GL2 getGL2()
Casts this object to the GL2 interface.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
void setRealized(boolean realized)
Indicates to GLDrawable implementations whether the underlying surface has been created and can be dr...
GLContext createContext(GLContext shareWith)
Creates a new context for drawing to this drawable that will optionally share buffer objects,...
PixelFormat.Composition getHostPixelComp(final GLProfile glp, final int componentCount)
Returns the host PixelFormat.Composition matching GL and componentCount if required by implementation...