JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestTiledRendering2NEWT.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.newt.opengl.GLWindow;
31import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
32import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
33import com.jogamp.opengl.test.junit.util.UITestCase;
34import com.jogamp.opengl.util.GLPixelBuffer;
35import com.jogamp.opengl.util.TileRenderer;
36import com.jogamp.opengl.util.TileRendererBase;
37import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
38import com.jogamp.opengl.util.texture.TextureData;
39import com.jogamp.opengl.util.texture.TextureIO;
40
41import java.io.File;
42import java.io.IOException;
43
44import com.jogamp.nativewindow.util.PixelFormat;
45import com.jogamp.opengl.GL;
46import com.jogamp.opengl.GLAutoDrawable;
47import com.jogamp.opengl.GLCapabilities;
48import com.jogamp.opengl.GLDrawableFactory;
49import com.jogamp.opengl.GLEventListener;
50import com.jogamp.opengl.GLProfile;
51import com.jogamp.opengl.GLRunnable;
52
53import org.junit.FixMethodOrder;
54import org.junit.Test;
55import org.junit.runners.MethodSorters;
56
57/**
58 * Demos offscreen {@link GLAutoDrawable} being used for
59 * {@link TileRenderer} rendering to produce a PNG file.
60 * <p>
61 * {@link TileRenderer} is being kicked off from the main thread.
62 * </p>
63 * <p>
64 * {@link TileRenderer} buffer allocation is performed
65 * within the pre {@link GLEventListener}
66 * set via {@link TileRendererBase#setGLEventListener(GLEventListener, GLEventListener)}
67 * on the main thread.
68 * </p>
69 * <p>
70 * At tile rendering finish, the viewport and
71 * and the original {@link GLEventListener}'s PMV matrix as well.
72 * The latter is done by calling it's {@link GLEventListener#reshape(GLAutoDrawable, int, int, int, int) reshape} method.
73 * </p>
74*/
75@FixMethodOrder(MethodSorters.NAME_ASCENDING)
76public class TestTiledRendering2NEWT extends UITestCase {
77 static long duration = 500; // ms
78
79 static GLProfile getGLProfile(final String profile) {
80 if( !GLProfile.isAvailable(profile) ) {
81 System.err.println("Profile "+profile+" n/a");
82 return null;
83 }
84 return GLProfile.get(profile);
85 }
86 static GLProfile getGL2ES3() {
88 if( null == glp || !glp.isGL2ES3() ) {
89 System.err.println("GL2ES3 n/a, has max-core "+glp);
90 return null;
91 }
92 return glp;
93 }
94
95 @Test
96 public void test001_off_gl2___aa0() throws IOException {
97 final GLProfile glp = getGLProfile(GLProfile.GL2);
98 if( null == glp ) {
99 return;
100 }
101 doTest(false, new Gears(), glp, 0);
102 }
103 @Test
104 public void test002_off_gl2___aa8() throws IOException {
105 final GLProfile glp = getGLProfile(GLProfile.GL2);
106 if( null == glp ) {
107 return;
108 }
109 doTest(false, new Gears(), glp, 8);
110 }
111 @Test
112 public void test011_off_gl2es3_aa0() throws IOException {
113 final GLProfile glp = getGL2ES3();
114 if( null == glp ) {
115 return;
116 }
117 doTest(false, new GearsES2(), glp, 0);
118 }
119 @Test
120 public void test012_off_gl2es3_aa8() throws IOException {
121 final GLProfile glp = getGL2ES3();
122 if( null == glp ) {
123 return;
124 }
125 doTest(false, new GearsES2(), glp, 8);
126 }
127 @Test
128 public void test101_on__gl2___aa0() throws IOException {
129 final GLProfile glp = getGLProfile(GLProfile.GL2);
130 if( null == glp ) {
131 return;
132 }
133 doTest(true, new Gears(), glp, 0);
134 }
135 @Test
136 public void test102_on__gl2___aa8() throws IOException {
137 final GLProfile glp = getGLProfile(GLProfile.GL2);
138 if( null == glp ) {
139 return;
140 }
141 doTest(true, new Gears(), glp, 8);
142 }
143 @Test
144 public void test111_on__gl2es3_aa0() throws IOException {
145 final GLProfile glp = getGL2ES3();
146 if( null == glp ) {
147 return;
148 }
149 doTest(true, new GearsES2(), glp, 0);
150 }
151 @Test
152 public void test112_on__gl2es3_aa8() throws IOException {
153 final GLProfile glp = getGL2ES3();
154 if( null == glp ) {
155 return;
156 }
157 doTest(true, new GearsES2(), glp, 8);
158 }
159
160 void doTest(final boolean onscreen, final GLEventListener demo, final GLProfile glp, final int msaaCount) throws IOException {
161 final GLCapabilities caps = new GLCapabilities(glp);
162 caps.setDoubleBuffered(onscreen);
163 if( msaaCount > 0 ) {
164 caps.setSampleBuffers(true);
165 caps.setNumSamples(msaaCount);
166 }
167
168 final int maxTileSize = 256;
169 final GLAutoDrawable glad;
170 if( onscreen ) {
171 final GLWindow glWin = GLWindow.create(caps);
172 glWin.setSize(maxTileSize, maxTileSize);
173 glWin.setVisible(true);
174 glad = glWin;
175 } else {
177 glad = factory.createOffscreenAutoDrawable(null, caps, null, maxTileSize, maxTileSize);
178 }
179
180 glad.addGLEventListener( demo );
181
182 // Fix the image size for now
183 final int imageWidth = glad.getSurfaceWidth() * 6;
184 final int imageHeight = glad.getSurfaceHeight() * 4;
185
186 final String filename = this.getSnapshotFilename(0, "-tile", glad.getChosenGLCapabilities(), imageWidth, imageHeight, false, TextureIO.PNG, null);
187 final File file = new File(filename);
188
189 // Initialize the tile rendering library
190 final TileRenderer renderer = new TileRenderer();
191 renderer.setImageSize(imageWidth, imageHeight);
192 renderer.setTileSize(glad.getSurfaceWidth(), glad.getSurfaceHeight(), 0);
193 renderer.attachAutoDrawable(glad);
194
195 final GLPixelBuffer.GLPixelBufferProvider pixelBufferProvider = GLPixelBuffer.defaultProviderWithRowStride;
196 final boolean[] flipVertically = { false };
197
198 final GLEventListener preTileGLEL = new GLEventListener() {
199 @Override
200 public void init(final GLAutoDrawable drawable) {
201 final GL gl = drawable.getGL();
202 final PixelFormat.Composition hostPixelComp = pixelBufferProvider.getHostPixelComp(gl.getGLProfile(), 3);
203 final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, 3, true);
204 final GLPixelBuffer pixelBuffer = pixelBufferProvider.allocate(gl, hostPixelComp, pixelAttribs, true, imageWidth, imageHeight, 1, 0);
205 renderer.setImageBuffer(pixelBuffer);
206 if( drawable.isGLOriented() ) {
207 flipVertically[0] = false;
208 } else {
209 flipVertically[0] = true;
210 }
211 }
212 @Override
213 public void dispose(final GLAutoDrawable drawable) {}
214 @Override
215 public void display(final GLAutoDrawable drawable) {}
216 @Override
217 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {}
218 };
219 renderer.setGLEventListener(preTileGLEL, null);
220
221 while ( !renderer.eot() ) {
222 renderer.display();
223 }
224
225 renderer.detachAutoDrawable();
226
227 // Restore viewport and Gear's PMV matrix
228 // .. even though we close the demo, this is for documentation!
229 glad.invoke(true, new GLRunnable() {
230 @Override
231 public boolean run(final GLAutoDrawable drawable) {
232 drawable.getGL().glViewport(0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
233 demo.reshape(drawable, 0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
234 return false;
235 }
236 });
237
238 final GLPixelBuffer imageBuffer = renderer.getImageBuffer();
239 final TextureData textureData = new TextureData(
240 caps.getGLProfile(),
241 0 /* internalFormat */,
242 imageWidth, imageHeight,
243 0,
244 imageBuffer.pixelAttributes,
245 false, false,
246 flipVertically[0],
247 imageBuffer.buffer,
248 null /* Flusher */);
249
250 TextureIO.write(textureData, file);
251
252 glad.destroy();
253 }
254
255 public static void main(final String args[]) {
256 for(int i=0; i<args.length; i++) {
257 if(args[i].equals("-time")) {
258 i++;
259 try {
260 duration = Integer.parseInt(args[i]);
261 } catch (final Exception ex) { ex.printStackTrace(); }
262 }
263 }
264 org.junit.runner.JUnitCore.main(TestTiledRendering2NEWT.class.getName());
265 }
266}
Specifies a set of OpenGL capabilities.
void setDoubleBuffered(final boolean enable)
Enables or disables double buffering.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
final GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
void setSampleBuffers(final boolean enable)
Defaults to false.
abstract GLOffscreenAutoDrawable createOffscreenAutoDrawable(AbstractGraphicsDevice device, GLCapabilitiesImmutable caps, GLCapabilitiesChooser chooser, int width, int height)
Creates a realized GLOffscreenAutoDrawable 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 boolean isAvailable(final AbstractGraphicsDevice device, final String profile)
Returns the availability of a profile on a device.
Definition: GLProfile.java:305
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
static GLProfile getMaxProgrammableCore(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the programmable shader core pipeline only.
Definition: GLProfile.java:854
final boolean isGL2ES3()
Indicates whether this profile is capable of GL2ES3.
Gears.java author: Brian Paul (converted to Java by Ron Cemer and Sven Gothel)
Definition: Gears.java:34
Demos offscreen GLAutoDrawable being used for TileRenderer rendering to produce a PNG file.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
boolean invoke(boolean wait, GLRunnable glRunnable)
Enqueues a one-shot GLRunnable, which will be executed within the next display() call after all regis...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
boolean isGLOriented()
Returns true if the drawable is rendered in OpenGL's coordinate system, origin at bottom left.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.