JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLReadBuffer01GLWindowNEWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 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.acore;
29
30import java.io.File;
31import java.util.Locale;
32
33import com.jogamp.nativewindow.util.Dimension;
34import com.jogamp.nativewindow.util.DimensionImmutable;
35import com.jogamp.opengl.GL;
36import com.jogamp.opengl.GLAutoDrawable;
37import com.jogamp.opengl.GLCapabilitiesImmutable;
38import com.jogamp.opengl.GLEventListener;
39import com.jogamp.opengl.GLProfile;
40
41import org.junit.Assume;
42import org.junit.BeforeClass;
43import org.junit.FixMethodOrder;
44import org.junit.runners.MethodSorters;
45
46import com.jogamp.common.util.VersionUtil;
47import com.jogamp.newt.opengl.GLWindow;
48import com.jogamp.opengl.JoglVersion;
49import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
50import com.jogamp.opengl.test.junit.jogl.demos.es2.MultisampleDemoES2;
51import com.jogamp.opengl.test.junit.util.MiscUtils;
52import com.jogamp.opengl.util.Animator;
53import com.jogamp.opengl.util.GLDrawableUtil;
54import com.jogamp.opengl.util.GLReadBufferUtil;
55import com.jogamp.opengl.util.texture.TextureIO;
56
57/**
58 * Test synchronous GLAutoDrawable display, swap-buffer and read-pixels with NEWT
59 * including non-MSAA and MSAA framebuffer.
60 * <p>
61 * See {@link GLReadBuffer00Base} for related bugs and further details.
62 * </p>
63 */
64@FixMethodOrder(MethodSorters.NAME_ASCENDING)
66
67 @BeforeClass
68 public static void initClass() {
70 }
71
72 @Override
73 public void test(final GLCapabilitiesImmutable caps, final boolean useSwingDoubleBuffer, final boolean skipGLOrientationVerticalFlip) {
74 if( skipGLOrientationVerticalFlip || useSwingDoubleBuffer ) {
75 return; // NOP
76 }
77 final GLReadBufferUtil glReadBufferUtil = new GLReadBufferUtil(false, false);
78 final GLWindow glad= GLWindow.create(caps);
79 final TextRendererGLEL textRendererGLEL = new TextRendererGLEL(caps.getGLProfile());
80 final SnapshotGLEL snapshotGLEL = doSnapshot ? new SnapshotGLEL(textRendererGLEL, glReadBufferUtil) : null;
81 try {
82 glad.setPosition(64, 64);
83 glad.setSize(320, 240);
85 @Override
86 public void init(final GLAutoDrawable drawable) {
87 final GL gl = drawable.getGL();
88 System.err.println(VersionUtil.getPlatformInfo());
89 System.err.println("GLEventListener init on "+Thread.currentThread());
90 System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
91 System.err.println("INIT GL IS: " + gl.getClass().getName());
92 System.err.println(JoglVersion.getGLStrings(gl, null, false).toString());
93 }
94 @Override
95 public void dispose(final GLAutoDrawable drawable) {}
96 @Override
97 public void display(final GLAutoDrawable drawable) {}
98 @Override
99 public void reshape(final GLAutoDrawable drawable, final int x,final int y, final int width, final int height) {}
100 });
101 {
102 final GearsES2 gears = new GearsES2(1);
103 gears.setVerbose(false);
104 glad.addGLEventListener(gears);
105 }
106 {
108 demo.setClearBuffers(false);;
109 glad.addGLEventListener(demo);
110 }
111 textRendererGLEL.setFlipVerticalInGLOrientation(skipGLOrientationVerticalFlip);
112 glad.addGLEventListener(textRendererGLEL);
113 if( doSnapshot ) {
114 glad.addGLEventListener(snapshotGLEL);
115 }
116 glad.setVisible(true);
117 } catch( final Throwable throwable ) {
118 throwable.printStackTrace();
119 Assume.assumeNoException( throwable );
120 }
121 final DimensionImmutable size0 = new Dimension(glad.getSurfaceWidth(), glad.getSurfaceHeight());
122 final DimensionImmutable size1 = new Dimension(size0.getWidth()+100, size0.getHeight()+100);
123 final DimensionImmutable size2 = new Dimension(size0.getWidth()-100, size0.getHeight()-100);
124 try {
125 for(int i=0; i<3; i++) {
126 final String str = "Frame# "+textRendererGLEL.frameNo+", user #"+(i+1);
127 System.err.println(str);
128 if( keyFrame ) {
129 waitForKey(str);
130 }
131 textRendererGLEL.userCounter = i + 1;
132 glad.display();
133 }
134 try { Thread.sleep(duration); } catch (final InterruptedException e) { }
135 glad.setSize(size1.getWidth(), size1.getHeight());
136 try { Thread.sleep(duration); } catch (final InterruptedException e) { }
137 glad.setSize(size2.getWidth(), size2.getHeight());
138 try { Thread.sleep(duration); } catch (final InterruptedException e) { }
139 glad.setSize(size0.getWidth(), size0.getHeight());
140 try { Thread.sleep(duration); } catch (final InterruptedException e) { }
141
142 if( doSnapshot ) {
143 glad.disposeGLEventListener(snapshotGLEL, true /* remove */);
144 }
145 final Animator anim = new Animator(glad);
146 anim.start();
147 try { Thread.sleep(2*duration); } catch (final InterruptedException e) { }
148 anim.stop();
149 } catch (final Exception e1) {
150 e1.printStackTrace();
151 }
152 glad.destroy();
153 }
154
155 private class SnapshotGLEL implements GLEventListener {
156 final TextRendererGLEL textRendererGLEL;
157 final GLReadBufferUtil glReadBufferUtil;
158 boolean defAutoSwapMode;
159 boolean swapBuffersBeforeRead;
160 int i;
161
162 SnapshotGLEL(final TextRendererGLEL textRendererGLEL, final GLReadBufferUtil glReadBufferUtil) {
163 this.textRendererGLEL = textRendererGLEL;
164 this.glReadBufferUtil = glReadBufferUtil;
165 this.defAutoSwapMode = true;
166 this.swapBuffersBeforeRead = false;
167 i = 0;
168 }
169
170 @Override
171 public void init(final GLAutoDrawable drawable) {
172 defAutoSwapMode = drawable.getAutoSwapBufferMode();
173 swapBuffersBeforeRead = GLDrawableUtil.swapBuffersBeforeRead(drawable.getChosenGLCapabilities());
174 drawable.setAutoSwapBufferMode( !swapBuffersBeforeRead );
175 }
176 @Override
177 public void dispose(final GLAutoDrawable drawable) {
178 drawable.setAutoSwapBufferMode( defAutoSwapMode );
179 }
180 @Override
181 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
182 @Override
183 public void display(final GLAutoDrawable drawable) {
184 snapshot(i++, drawable, TextureIO.PNG, null);
185 }
186 public void snapshot(final int sn, final GLAutoDrawable drawable, final String fileSuffix, final String destPath) {
187 final GL gl = drawable.getGL();
188 final String postSNDetail = String.format((Locale)null, "jgl-usr%03d", textRendererGLEL.userCounter);
189 final String filenameJGL = getSnapshotFilename(sn, postSNDetail,
190 drawable.getChosenGLCapabilities(), drawable.getSurfaceWidth(), drawable.getSurfaceHeight(),
191 glReadBufferUtil.hasAlpha(), fileSuffix, destPath);
192 if( swapBuffersBeforeRead ) {
193 drawable.swapBuffers();
194 // Just to test whether we use the right buffer,
195 // i.e. back-buffer shall no more be required ..
196 gl.glClear(GL.GL_COLOR_BUFFER_BIT);
197 } else {
198 gl.glFinish(); // just make sure rendering finished ..
199 }
200 final boolean mustFlipVertically = !drawable.isGLOriented();
201 System.err.println(Thread.currentThread().getName()+": ** screenshot: v-flip "+mustFlipVertically+", swapBuffersBeforeRead "+swapBuffersBeforeRead+", "+filenameJGL);
202
203 if(glReadBufferUtil.readPixels(gl, mustFlipVertically)) {
204 glReadBufferUtil.write(new File(filenameJGL));
205 }
206 }
207 };
208
209 static GLCapabilitiesImmutable caps = null;
210 static boolean doSnapshot = true;
211 static boolean keyFrame = false;
212
213 public static void main(final String[] args) {
214 for(int i=0; i<args.length; i++) {
215 if(args[i].equals("-time")) {
216 i++;
217 duration = MiscUtils.atol(args[i], duration);
218 } else if(args[i].equals("-keyFrame")) {
219 keyFrame = true;
220 } else if(args[i].equals("-noSnapshot")) {
221 doSnapshot = false;
222 }
223 }
224 org.junit.runner.JUnitCore.main(TestGLReadBuffer01GLWindowNEWT.class.getName());
225 }
226
227}
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:466
final void setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static void initSingleton()
Static initialization of JOGL.
Definition: GLProfile.java:204
static StringBuilder getGLStrings(final GL gl, final StringBuilder sb)
Test synchronous GLAutoDrawable display, swap-buffer and read-pixels including non-MSAA and MSAA fram...
Test synchronous GLAutoDrawable display, swap-buffer and read-pixels with NEWT including non-MSAA and...
void test(final GLCapabilitiesImmutable caps, final boolean useSwingDoubleBuffer, final boolean skipGLOrientationVerticalFlip)
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Providing utility functions dealing w/ GLDrawables, GLAutoDrawable and their GLEventListener.
static final boolean swapBuffersBeforeRead(final GLCapabilitiesImmutable chosenCaps)
Determines whether the chosen GLCapabilitiesImmutable requires a swap-buffers before reading pixels.
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
void write(final File dest)
Write the TextureData filled by readPixels(GLAutoDrawable, boolean) to file.
boolean readPixels(final GL gl, final boolean mustFlipVertically)
Read the drawable's pixels to TextureData and Texture, if requested at construction.
Immutable Dimension Interface, consisting of it's read only components:
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GLEventListener disposeGLEventListener(GLEventListener listener, boolean remove)
Disposes the given listener via dispose(..) if it has been initialized and added to this queue.
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
boolean getAutoSwapBufferMode()
Indicates whether automatic buffer swapping is enabled for this drawable.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void setAutoSwapBufferMode(boolean enable)
Enables or disables automatic buffer swapping for this drawable.
Specifies an immutable set of OpenGL capabilities.
boolean getSampleBuffers()
Returns whether sample buffers for full-scene antialiasing (FSAA) should be allocated for this drawab...
GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.