JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestRandomTiledRendering2GL2NEWT.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.RandomTileRenderer;
34import com.jogamp.opengl.util.TileRendererBase;
35import com.jogamp.opengl.util.GLPixelBuffer.GLPixelAttributes;
36import com.jogamp.opengl.util.texture.TextureData;
37import com.jogamp.opengl.util.texture.TextureIO;
38
39import java.io.File;
40import java.io.IOException;
41import java.lang.reflect.InvocationTargetException;
42
43import com.jogamp.nativewindow.util.PixelFormat;
44import com.jogamp.opengl.GL;
45import com.jogamp.opengl.GLAutoDrawable;
46import com.jogamp.opengl.GLCapabilities;
47import com.jogamp.opengl.GLDrawableFactory;
48import com.jogamp.opengl.GLEventListener;
49import com.jogamp.opengl.GLOffscreenAutoDrawable;
50import com.jogamp.opengl.GLRunnable;
51
52import org.junit.FixMethodOrder;
53import org.junit.Test;
54import org.junit.runners.MethodSorters;
55
56/**
57 * Demos offscreen {@link GLAutoDrawable} being used for
58 * {@link RandomTileRenderer} rendering to produce a PNG file.
59 * <p>
60 * {@link RandomTileRenderer} is being kicked off from the main thread.
61 * </p>
62 * <p>
63 * {@link RandomTileRenderer} buffer allocation is performed
64 * within the pre {@link GLEventListener}
65 * set via {@link TileRendererBase#setGLEventListener(GLEventListener, GLEventListener)}
66 * on the main thread.
67 * </p>
68 * <p>
69 * At tile rendering finish, the viewport and
70 * and the original {@link GLEventListener}'s PMV matrix as well.
71 * The latter is done by calling it's {@link GLEventListener#reshape(GLAutoDrawable, int, int, int, int) reshape} method.
72 * </p>
73*/
74@FixMethodOrder(MethodSorters.NAME_ASCENDING)
76 static long duration = 500; // ms
77
78 @Test
79 public void test01_aa0() throws IOException, InterruptedException, InvocationTargetException {
80 doTest(0);
81 }
82 @Test
83 public void test02_aa8() throws IOException, InterruptedException, InvocationTargetException {
84 doTest(8);
85 }
86
87 void doTest(final int msaaCount) throws IOException, InterruptedException, InvocationTargetException {
88 final GLCapabilities caps = new GLCapabilities(null);
89 caps.setDoubleBuffered(true);
90 if( msaaCount > 0 ) {
91 caps.setSampleBuffers(true);
92 caps.setNumSamples(msaaCount);
93 }
94
95 final int maxTileSize = 64;
97 final GLOffscreenAutoDrawable glad = factory.createOffscreenAutoDrawable(null, caps, null, maxTileSize, maxTileSize);
98
99 final Gears gears = new Gears();
100 glad.addGLEventListener( gears );
101
102 // Fix the image size for now
103 final int imageWidth = 256 * 6;
104 final int imageHeight = 256 * 4;
105
106 final String filename = this.getSnapshotFilename(0, "-tile", glad.getChosenGLCapabilities(), imageWidth, imageHeight, false, TextureIO.PNG, null);
107 final File file = new File(filename);
108
109 // Initialize the tile rendering library
110 final RandomTileRenderer renderer = new RandomTileRenderer();
111 renderer.attachAutoDrawable(glad);
112 renderer.setImageSize(imageWidth, imageHeight);
113
114 final GLPixelBuffer.GLPixelBufferProvider pixelBufferProvider = GLPixelBuffer.defaultProviderWithRowStride;
115 final boolean[] flipVertically = { false };
116
117 final GLEventListener preTileGLEL = new GLEventListener() {
118 @Override
119 public void init(final GLAutoDrawable drawable) {
120 final GL gl = drawable.getGL();
121 final PixelFormat.Composition hostPixelComp = pixelBufferProvider.getHostPixelComp(gl.getGLProfile(), 3);
122 final GLPixelAttributes pixelAttribs = pixelBufferProvider.getAttributes(gl, 3, true);
123 final GLPixelBuffer pixelBuffer = pixelBufferProvider.allocate(gl, hostPixelComp, pixelAttribs, true, imageWidth, imageHeight, 1, 0);
124 renderer.setImageBuffer(pixelBuffer);
125 if( drawable.isGLOriented() ) {
126 flipVertically[0] = false;
127 } else {
128 flipVertically[0] = true;
129 }
130 }
131 @Override
132 public void dispose(final GLAutoDrawable drawable) {}
133 @Override
134 public void display(final GLAutoDrawable drawable) {}
135 @Override
136 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {}
137 };
138 renderer.setGLEventListener(preTileGLEL, null);
139
140 final int w = maxTileSize, h = maxTileSize;
141 int dx = 0, dy = 0;
142 while( dx+w <= imageWidth && dy+h <= imageHeight ) {
143 renderer.display(dx, dy, w, h);
144 dx+=w+w/2;
145 if( dx + w > imageWidth ) {
146 dx = 0;
147 dy+=h+h/2;
148 }
149 }
150
151 renderer.detachAutoDrawable();
152
153 // Restore viewport and Gear's PMV matrix
154 // .. even though we close the demo, this is for documentation!
155 glad.invoke(true, new GLRunnable() {
156 @Override
157 public boolean run(final GLAutoDrawable drawable) {
158 drawable.getGL().glViewport(0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
159 gears.reshape(drawable, 0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
160 return false;
161 }
162 });
163
164 glad.destroy();
165
166 final GLPixelBuffer imageBuffer = renderer.getImageBuffer();
167 imageBuffer.clear(); // full size available
168 System.err.println("XXX2: "+imageBuffer);
169 final TextureData textureData = new TextureData(
170 caps.getGLProfile(),
171 0 /* internalFormat */,
172 imageWidth, imageHeight,
173 0,
174 imageBuffer.pixelAttributes,
175 false, false,
176 flipVertically[0],
177 imageBuffer.buffer,
178 null /* Flusher */);
179 System.err.println("XXX3: "+textureData.getPixelFormat()+", "+textureData.getPixelAttributes());
180
181 TextureIO.write(textureData, file);
182 }
183
184 public static void main(final String args[]) {
185 for(int i=0; i<args.length; i++) {
186 if(args[i].equals("-time")) {
187 i++;
188 try {
189 duration = Integer.parseInt(args[i]);
190 } catch (final Exception ex) { ex.printStackTrace(); }
191 }
192 }
193 org.junit.runner.JUnitCore.main(TestRandomTiledRendering2GL2NEWT.class.getName());
194 }
195}
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.
Demos offscreen GLAutoDrawable being used for RandomTileRenderer 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...
boolean isGLOriented()
Returns true if the drawable is rendered in OpenGL's coordinate system, origin at bottom left.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
Platform-independent GLAutoDrawable specialization, exposing offscreen functionality.