JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGearsGLJPanelAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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 */
28
29package com.jogamp.opengl.test.junit.jogl.demos.gl2.awt;
30
31import com.jogamp.opengl.*;
32
33import com.jogamp.newt.event.KeyEvent;
34import com.jogamp.newt.event.TraceKeyAdapter;
35import com.jogamp.newt.event.TraceWindowAdapter;
36import com.jogamp.newt.event.awt.AWTKeyAdapter;
37import com.jogamp.newt.event.awt.AWTWindowAdapter;
38import com.jogamp.opengl.util.FPSAnimator;
39
40import com.jogamp.opengl.awt.GLJPanel;
41
42import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
43import com.jogamp.opengl.test.junit.util.MiscUtils;
44import com.jogamp.opengl.test.junit.util.QuitAdapter;
45import com.jogamp.opengl.test.junit.util.UITestCase;
46
47import java.awt.AWTException;
48import java.awt.BorderLayout;
49import java.awt.Dimension;
50import java.lang.reflect.InvocationTargetException;
51
52import javax.swing.JFrame;
53import javax.swing.SwingUtilities;
54
55import org.junit.Assert;
56import org.junit.BeforeClass;
57import org.junit.AfterClass;
58import org.junit.Test;
59import org.junit.FixMethodOrder;
60import org.junit.runners.MethodSorters;
61
62@FixMethodOrder(MethodSorters.NAME_ASCENDING)
63public class TestGearsGLJPanelAWT extends UITestCase {
64 static GLProfile glp;
65 static int width, height;
66 static boolean shallUsePBuffer = false;
67 static boolean shallUseBitmap = false;
68 static boolean useMSAA = false;
69 static int swapInterval = 0;
70 static boolean useAnimator = true;
71 static boolean manualTest = false;
72
73 @BeforeClass
74 public static void initClass() {
77 Assert.assertNotNull(glp);
78 width = 640;
79 height = 480;
80 } else {
81 setTestSupported(false);
82 }
83 }
84
85 @AfterClass
86 public static void releaseClass() {
87 }
88
89 protected void runTestGL(final GLCapabilities caps)
90 throws AWTException, InterruptedException, InvocationTargetException
91 {
92 final JFrame frame = new JFrame("Swing GLJPanel");
93 Assert.assertNotNull(frame);
94
95 final GLJPanel glJPanel = new GLJPanel(caps);
96 Assert.assertNotNull(glJPanel);
97 final Dimension glc_sz = new Dimension(width, height);
98 glJPanel.setMinimumSize(glc_sz);
99 glJPanel.setPreferredSize(glc_sz);
100 glJPanel.setSize(glc_sz);
101 glJPanel.addGLEventListener(new Gears(swapInterval));
103 glJPanel.addGLEventListener(snap);
104
105 final FPSAnimator animator = useAnimator ? new FPSAnimator(glJPanel, 60) : null;
106
107 SwingUtilities.invokeAndWait(new Runnable() {
108 public void run() {
109 frame.getContentPane().add(glJPanel, BorderLayout.CENTER);
110 frame.getContentPane().validate();
111 frame.pack();
112 frame.setVisible(true);
113 } } ) ;
114
115 if( useAnimator ) {
116 animator.setUpdateFPSFrames(60, System.err);
117 animator.start();
118 Assert.assertEquals(true, animator.isAnimating());
119 }
120
121 final QuitAdapter quitAdapter = new QuitAdapter();
122 new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glJPanel).addTo(glJPanel);
123 new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glJPanel).addTo(frame);
124
125 final com.jogamp.newt.event.KeyListener kl = new com.jogamp.newt.event.KeyAdapter() {
126 @Override
127 public void keyPressed(final KeyEvent e) {
128 if( e.isAutoRepeat() ) {
129 return;
130 }
131 if(e.getKeyChar()=='m') {
132 final GLCapabilitiesImmutable capsPre = glJPanel.getChosenGLCapabilities();
133 final GLCapabilities capsNew = new GLCapabilities(capsPre.getGLProfile());
134 capsNew.copyFrom(capsPre);
135 final boolean msaa;
136 if( capsPre.getSampleBuffers() ) {
137 capsNew.setSampleBuffers(false);
138 capsNew.setDoubleBuffered(false);
139 msaa = false;
140 } else {
141 capsNew.setSampleBuffers(true);
142 capsNew.setNumSamples(4);
143 msaa = true;
144 }
145 System.err.println("[set MSAA "+msaa+" Caps had]: "+capsPre);
146 System.err.println("[set MSAA "+msaa+" Caps new]: "+capsNew);
147 System.err.println("XXX-A1: "+animator.toString());
148 glJPanel.setRequestedGLCapabilities(capsNew);
149 System.err.println("XXX-A2: "+animator.toString());
150 System.err.println("XXX: "+glJPanel.toString());
151 } else if(e.getKeyChar()=='b') {
152 final GLCapabilitiesImmutable capsPre = glJPanel.getChosenGLCapabilities();
153 final GLCapabilities capsNew = new GLCapabilities(capsPre.getGLProfile());
154 capsNew.copyFrom(capsPre);
155 final boolean bmp;
156 if( capsPre.isBitmap() ) {
157 capsNew.setBitmap(false); // auto-choose
158 bmp = false;
159 } else {
160 capsNew.setBitmap(true);
161 capsNew.setFBO(false);
162 capsNew.setPBuffer(false);
163 bmp = true;
164 }
165 System.err.println("[set Bitmap "+bmp+" Caps had]: "+capsPre);
166 System.err.println("[set Bitmap "+bmp+" Caps new]: "+capsNew);
167 System.err.println("XXX-A1: "+animator.toString());
168 glJPanel.setRequestedGLCapabilities(capsNew);
169 System.err.println("XXX-A2: "+animator.toString());
170 System.err.println("XXX: "+glJPanel.toString());
171 }
172 } };
173 new AWTKeyAdapter(kl, glJPanel).addTo(glJPanel);
174
175 final long t0 = System.currentTimeMillis();
176 long t1 = t0;
177 boolean triggerSnap = false;
178 while(!quitAdapter.shouldQuit() && t1 - t0 < duration) {
179 Thread.sleep(100);
180 t1 = System.currentTimeMillis();
181 snap.getDisplayCount();
182 if( !triggerSnap && snap.getDisplayCount() > 1 ) {
183 // Snapshot only after one frame has been rendered to suite FBO MSAA!
184 snap.setMakeSnapshot();
185 triggerSnap = true;
186 }
187 }
188
189 Assert.assertNotNull(frame);
190 Assert.assertNotNull(glJPanel);
191 Assert.assertNotNull(animator);
192
193 if( useAnimator ) {
194 animator.stop();
195 Assert.assertEquals(false, animator.isAnimating());
196 }
197 SwingUtilities.invokeAndWait(new Runnable() {
198 public void run() {
199 frame.setVisible(false);
200 frame.getContentPane().remove(glJPanel);
201 frame.remove(glJPanel);
202 glJPanel.destroy();
203 frame.dispose();
204 } } );
205 }
206
207 @Test
208 public void test01_DefaultNorm()
209 throws AWTException, InterruptedException, InvocationTargetException
210 {
212 if(useMSAA) {
213 caps.setNumSamples(4);
214 caps.setSampleBuffers(true);
215 }
216 if(shallUsePBuffer) {
217 caps.setPBuffer(true);
218 }
219 if(shallUseBitmap) {
220 caps.setBitmap(true);
221 }
222 runTestGL(caps);
223 }
224
225 @Test
226 public void test02_DefaultMsaa()
227 throws AWTException, InterruptedException, InvocationTargetException
228 {
229 if( manualTest ) {
230 return;
231 }
233 caps.setNumSamples(4);
234 caps.setSampleBuffers(true);
235 runTestGL(caps);
236 }
237
238 @Test
239 public void test03_PbufferNorm()
240 throws AWTException, InterruptedException, InvocationTargetException
241 {
242 if( manualTest ) {
243 return;
244 }
246 caps.setPBuffer(true);
247 runTestGL(caps);
248 }
249
250 @Test
251 public void test04_PbufferMsaa()
252 throws AWTException, InterruptedException, InvocationTargetException
253 {
254 if( manualTest ) {
255 return;
256 }
258 caps.setNumSamples(4);
259 caps.setSampleBuffers(true);
260 caps.setPBuffer(true);
261 runTestGL(caps);
262 }
263
264 @Test
265 public void test05_BitmapNorm()
266 throws AWTException, InterruptedException, InvocationTargetException
267 {
268 if( manualTest ) {
269 return;
270 }
272 caps.setBitmap(true);
273 runTestGL(caps);
274 }
275
276 @Test
277 public void test06_BitmapMsaa()
278 throws AWTException, InterruptedException, InvocationTargetException
279 {
280 if( manualTest ) {
281 return;
282 }
284 caps.setNumSamples(4);
285 caps.setSampleBuffers(true);
286 caps.setBitmap(true);
287 runTestGL(caps);
288 }
289
290 static long duration = 500; // ms
291
292 public static void main(final String args[]) {
293 for(int i=0; i<args.length; i++) {
294 if(args[i].equals("-time")) {
295 i++;
296 duration = MiscUtils.atol(args[i], duration);
297 } else if(args[i].equals("-vsync")) {
298 i++;
299 swapInterval = MiscUtils.atoi(args[i], swapInterval);
300 } else if(args[i].equals("-msaa")) {
301 useMSAA = true;
302 } else if(args[i].equals("-noanim")) {
303 useAnimator = false;
304 } else if(args[i].equals("-pbuffer")) {
305 shallUsePBuffer = true;
306 } else if(args[i].equals("-bitmap")) {
307 shallUseBitmap = true;
308 } else if(args[i].equals("-manual")) {
309 manualTest = true;
310 }
311 }
312 System.err.println("swapInterval "+swapInterval);
313 System.err.println("useMSAA "+useMSAA);
314 System.err.println("useAnimator "+useAnimator);
315 System.err.println("shallUsePBuffer "+shallUsePBuffer);
316 System.err.println("shallUseBitmap "+shallUseBitmap);
317 System.err.println("manualTest "+manualTest);
318
319 org.junit.runner.JUnitCore.main(TestGearsGLJPanelAWT.class.getName());
320 }
321}
void setBitmap(final boolean enable)
Requesting offscreen bitmap mode.
final boolean isAutoRepeat()
getModifiers() contains AUTOREPEAT_MASK.
final char getKeyChar()
Returns the UTF-16 character reflecting the key symbol incl.
Definition: KeyEvent.java:161
AWT: printable: PRESSED (t0), TYPED (t0), RELEASED (t1) non-printable: PRESSED (t0),...
synchronized AWTAdapter addTo(final java.awt.Component awtComponent)
Due to the fact that some NEWT com.jogamp.newt.event.NEWTEventListener are mapped to more than one ja...
synchronized AWTAdapter addTo(final java.awt.Component awtComponent)
Due to the fact that some NEWT com.jogamp.newt.event.NEWTEventListener are mapped to more than one ja...
Specifies a set of OpenGL capabilities.
void setPBuffer(final boolean enable)
Requesting offscreen pbuffer mode.
void setDoubleBuffered(final boolean enable)
Enables or disables double buffering.
void setFBO(final boolean enable)
Requesting offscreen FBO mode.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
GLCapabilities copyFrom(final CapabilitiesImmutable source)
Copies all CapabilitiesImmutable values from source into this instance.
void setSampleBuffers(final boolean enable)
Defaults to false.
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 getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
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
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
Definition: GLJPanel.java:1275
void destroy()
Just an alias for removeNotify.
Definition: GLJPanel.java:531
final void setRequestedGLCapabilities(final GLCapabilitiesImmutable caps)
Set a new requested GLCapabilitiesImmutable for this GLJPanel allowing reconfiguration.
Definition: GLJPanel.java:1300
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
Gears.java author: Brian Paul (converted to Java by Ron Cemer and Sven Gothel)
Definition: Gears.java:34
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
final void setUpdateFPSFrames(final int frames, final PrintStream out)
An Animator subclass which attempts to achieve a target frames-per-second rate to avoid using all CPU...
final synchronized boolean start()
Starts this animator, if not running.
final synchronized boolean stop()
Stops this FPSAnimator.
boolean isBitmap()
Returns whether bitmap offscreen mode is requested, available or chosen.
Listener for KeyEvents.
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.
void setSize(int width, int height)
Requests a new width and height for this AWTGLAutoDrawable.