JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestInstancedReneringGL4AWT.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.demos.gl4.awt;
29
30import java.awt.AWTException;
31import java.awt.BorderLayout;
32import java.awt.Dimension;
33import java.lang.reflect.InvocationTargetException;
34
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLCapabilitiesImmutable;
37import com.jogamp.opengl.GLProfile;
38import com.jogamp.opengl.awt.GLJPanel;
39import javax.swing.JFrame;
40import javax.swing.SwingUtilities;
41
42import org.junit.AfterClass;
43import org.junit.Assert;
44import org.junit.BeforeClass;
45import org.junit.FixMethodOrder;
46import org.junit.Test;
47import org.junit.runners.MethodSorters;
48
49import com.jogamp.newt.event.KeyEvent;
50import com.jogamp.newt.event.TraceKeyAdapter;
51import com.jogamp.newt.event.TraceWindowAdapter;
52import com.jogamp.newt.event.awt.AWTKeyAdapter;
53import com.jogamp.newt.event.awt.AWTWindowAdapter;
54import com.jogamp.opengl.test.junit.jogl.demos.gl4.TriangleInstancedRendererWithShaderState;
55import com.jogamp.opengl.test.junit.util.MiscUtils;
56import com.jogamp.opengl.test.junit.util.QuitAdapter;
57import com.jogamp.opengl.test.junit.util.UITestCase;
58import com.jogamp.opengl.util.FPSAnimator;
59
60/**
61 * Test Instanced rendering demo TrianglesInstancedRenderer
62 */
63@FixMethodOrder(MethodSorters.NAME_ASCENDING)
65 static GLProfile glp;
66 static int width, height;
67 static boolean shallUsePBuffer = false;
68 static boolean shallUseBitmap = false;
69 static boolean useMSAA = false;
70 static int swapInterval = 0;
71 static boolean useAnimator = true;
72 static boolean manualTest = false;
73
74 @BeforeClass
75 public static void initClass() {
78 Assert.assertNotNull(glp);
79 width = 640;
80 height = 480;
81 } else {
82 setTestSupported(false);
83 }
84 }
85
86 @AfterClass
87 public static void releaseClass() {
88 }
89
90 protected void runTestGL(final GLCapabilities caps)
91 throws AWTException, InterruptedException, InvocationTargetException
92 {
93 final JFrame frame = new JFrame("Swing GLJPanel");
94 Assert.assertNotNull(frame);
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);
103 glJPanel.addGLEventListener(snap);
104 final FPSAnimator animator = useAnimator ? new FPSAnimator(glJPanel, 60) : null;
105 SwingUtilities.invokeAndWait(new Runnable() {
106 @Override
107 public void run() {
108 frame.getContentPane().add(glJPanel, BorderLayout.CENTER);
109 frame.getContentPane().validate();
110 frame.pack();
111 frame.setVisible(true);
112 } } ) ;
113 if( useAnimator ) {
114 animator.setUpdateFPSFrames(60, System.err);
115 animator.start();
116 Assert.assertEquals(true, animator.isAnimating());
117 }
118 final QuitAdapter quitAdapter = new QuitAdapter();
119 new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glJPanel).addTo(glJPanel);
120 new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glJPanel).addTo(frame);
121 final com.jogamp.newt.event.KeyListener kl = new com.jogamp.newt.event.KeyAdapter() {
122 @Override
123 public void keyPressed(final KeyEvent e) {
124 if( e.isAutoRepeat() ) {
125 return;
126 }
127 if(e.getKeyChar()=='m') {
128 final GLCapabilitiesImmutable capsPre = glJPanel.getChosenGLCapabilities();
129 final GLCapabilities capsNew = new GLCapabilities(capsPre.getGLProfile());
130 capsNew.copyFrom(capsPre);
131 final boolean msaa;
132 if( capsPre.getSampleBuffers() ) {
133 capsNew.setSampleBuffers(false);
134 capsNew.setDoubleBuffered(false);
135 msaa = false;
136 } else {
137 capsNew.setSampleBuffers(true);
138 capsNew.setNumSamples(4);
139 msaa = true;
140 }
141 System.err.println("[set MSAA "+msaa+" Caps had]: "+capsPre);
142 System.err.println("[set MSAA "+msaa+" Caps new]: "+capsNew);
143 System.err.println("XXX-A1: "+animator.toString());
144// glJPanel.setRequestedGLCapabilities(capsNew);
145 System.err.println("XXX-A2: "+animator.toString());
146 System.err.println("XXX: "+glJPanel.toString());
147 } else if(e.getKeyChar()=='b') {
148 final GLCapabilitiesImmutable capsPre = glJPanel.getChosenGLCapabilities();
149 final GLCapabilities capsNew = new GLCapabilities(capsPre.getGLProfile());
150 capsNew.copyFrom(capsPre);
151 final boolean bmp;
152 if( capsPre.isBitmap() ) {
153 capsNew.setBitmap(false); // auto-choose
154 bmp = false;
155 } else {
156 capsNew.setBitmap(true);
157 capsNew.setFBO(false);
158 capsNew.setPBuffer(false);
159 bmp = true;
160 }
161 System.err.println("[set Bitmap "+bmp+" Caps had]: "+capsPre);
162 System.err.println("[set Bitmap "+bmp+" Caps new]: "+capsNew);
163 System.err.println("XXX-A1: "+animator.toString());
164// glJPanel.setRequestedGLCapabilities(capsNew);
165 System.err.println("XXX-A2: "+animator.toString());
166 System.err.println("XXX: "+glJPanel.toString());
167 }
168 } };
169 new AWTKeyAdapter(kl, glJPanel).addTo(glJPanel);
170 final long t0 = System.currentTimeMillis();
171 long t1 = t0;
172 boolean triggerSnap = false;
173 while(!quitAdapter.shouldQuit() && t1 - t0 < duration) {
174 Thread.sleep(100);
175 t1 = System.currentTimeMillis();
176 snap.getDisplayCount();
177 if( !triggerSnap && snap.getDisplayCount() > 1 ) {
178 // Snapshot only after one frame has been rendered to suite FBO MSAA!
179 snap.setMakeSnapshot();
180 triggerSnap = true;
181 }
182 }
183 Assert.assertNotNull(frame);
184 Assert.assertNotNull(glJPanel);
185 Assert.assertNotNull(animator);
186 if( useAnimator ) {
187 animator.stop();
188 Assert.assertEquals(false, animator.isAnimating());
189 }
190 SwingUtilities.invokeAndWait(new Runnable() {
191 @Override
192 public void run() {
193 frame.setVisible(false);
194 frame.getContentPane().remove(glJPanel);
195 frame.remove(glJPanel);
196 glJPanel.destroy();
197 frame.dispose();
198 } } );
199 }
200
201 @Test
202 public void test01_DefaultMsaa()
203 throws AWTException, InterruptedException, InvocationTargetException
204 {
205 if( manualTest ) {
206 return;
207 }
209 caps.setNumSamples(4);
210 caps.setSampleBuffers(true);
211 runTestGL(caps);
212 }
213
214 static long duration = 500; // ms
215
216 public static void main(final String args[]) {
217 for(int i=0; i<args.length; i++) {
218 if(args[i].equals("-time")) {
219 i++;
220 duration = MiscUtils.atol(args[i], duration);
221 } else if(args[i].equals("-vsync")) {
222 i++;
223 swapInterval = MiscUtils.atoi(args[i], swapInterval);
224 } else if(args[i].equals("-msaa")) {
225 useMSAA = true;
226 } else if(args[i].equals("-noanim")) {
227 useAnimator = false;
228 } else if(args[i].equals("-pbuffer")) {
229 shallUsePBuffer = true;
230 } else if(args[i].equals("-bitmap")) {
231 shallUseBitmap = true;
232 } else if(args[i].equals("-manual")) {
233 manualTest = true;
234 }
235 }
236 System.err.println("swapInterval "+swapInterval);
237 System.err.println("useMSAA "+useMSAA);
238 System.err.println("useAnimator "+useAnimator);
239 System.err.println("shallUsePBuffer "+shallUsePBuffer);
240 System.err.println("shallUseBitmap "+shallUseBitmap);
241 System.err.println("manualTest "+manualTest);
242 org.junit.runner.JUnitCore.main(TestInstancedReneringGL4AWT.class.getName());
243 }
244}
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 get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static final String GL4
The desktop OpenGL core profile 4.x, with x >= 0.
Definition: GLProfile.java:569
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
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
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.