JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLJPanelTextureStateAWT.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 */
28
29package com.jogamp.opengl.test.junit.jogl.awt;
30
31
32import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
33import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
34import com.jogamp.opengl.test.junit.jogl.demos.es2.TextureDraw02ES2ListenerFBO;
35import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
36import com.jogamp.opengl.test.junit.util.MiscUtils;
37import com.jogamp.opengl.test.junit.util.QuitAdapter;
38import com.jogamp.opengl.test.junit.util.UITestCase;
39
40import com.jogamp.opengl.GL;
41import com.jogamp.opengl.GL2ES2;
42import com.jogamp.opengl.GLAutoDrawable;
43import com.jogamp.opengl.GLEventListener;
44import com.jogamp.opengl.GLProfile;
45import com.jogamp.opengl.GLCapabilities;
46import com.jogamp.opengl.awt.GLJPanel;
47import javax.swing.JFrame;
48
49import com.jogamp.opengl.util.texture.TextureIO;
50import com.jogamp.opengl.util.texture.TextureState;
51import com.jogamp.opengl.util.GLReadBufferUtil;
52
53import java.awt.Dimension;
54import java.io.IOException;
55
56import org.junit.Assert;
57import org.junit.Assume;
58import org.junit.BeforeClass;
59import org.junit.Test;
60import org.junit.FixMethodOrder;
61import org.junit.runners.MethodSorters;
62
63/**
64 * Unit test for bug 826, test {@link GLJPanel}'s {@link TextureState} save and restore.
65 */
66@FixMethodOrder(MethodSorters.NAME_ASCENDING)
68 static boolean showFPS = false;
69 static long duration = 250; // ms
70
71 @BeforeClass
72 public static void initClass() {
73 }
74
75 static void setFrameSize(final JFrame frame, final boolean frameLayout, final java.awt.Dimension new_sz) {
76 try {
77 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
78 public void run() {
79 frame.setSize(new_sz);
80 if( frameLayout ) {
81 frame.validate();
82 }
83 } } );
84 } catch( final Throwable throwable ) {
85 throwable.printStackTrace();
86 Assume.assumeNoException( throwable );
87 }
88 }
89
90 public void testImpl(final boolean keepTextureBound, final int texUnit)
91 throws InterruptedException, IOException
92 {
93 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
94 GLProfile glp;
96 glp = GLProfile.getGL2ES2();
97 } else {
98 System.err.println(getSimpleTestName(".")+": GLProfile n/a");
99 return;
100 }
101 final GLCapabilities caps = new GLCapabilities(glp);
102
103 final GLJPanel glc = new GLJPanel(caps);
104 // final GLCanvas glc = new GLCanvas(caps);
105 final Dimension glc_sz = new Dimension(640, 480);
106 final Dimension glc_sz2 = new Dimension(800, 400);
107 glc.setMinimumSize(glc_sz);
108 glc.setPreferredSize(glc_sz);
109 final JFrame frame = new JFrame("TestGLJPanelTextureStateAWT");
110 Assert.assertNotNull(frame);
111 frame.getContentPane().add(glc);
112
113 final GLEventListener gle0;
114 {
115 final GearsES2 gle0sub = new GearsES2( 0 );
116 // gle1sub.setClearBuffers(false);
117 final TextureDraw02ES2ListenerFBO demo = new TextureDraw02ES2ListenerFBO(gle0sub, 1, texUnit ) ;
118 demo.setKeepTextureBound(keepTextureBound);
119 demo.setClearBuffers(false);
120 gle0 = demo;
121 }
122
123 final GLEventListener gle1;
124 {
125 final RedSquareES2 demo = new RedSquareES2( 1 ) ;
126 demo.setClearBuffers(false);
127 gle1 = demo;
128 }
129
130 final boolean[] glelError = { false };
131
133 int gle0X, gle0Y, gle0W, gle0H;
134 int gle1X, gle1Y, gle1W, gle1H;
135 int tX, tY, tW, tH;
136 int shot = 0;
137
138 void setupTex(final GL gl) {
139 // Note: FBObject uses diff defaults, i.e.: GL_NEAREST and GL_CLAMP_TO_EDGE
140 if( keepTextureBound ) {
145 }
146 }
147
148 @Override
149 public void init(final GLAutoDrawable drawable) {
150 // Initialize w/ arbitrary values !
151 final GL2ES2 gl = drawable.getGL().getGL2ES2();
164
165 gle0.init(drawable);
166 gle1.init(drawable);
167 setupTex(gl);
168 }
169
170 @Override
171 public void dispose(final GLAutoDrawable drawable) {
172 gle0.dispose(drawable);
173 gle1.dispose(drawable);
174 }
175 @Override
176 public void display(final GLAutoDrawable drawable) {
177 final GL2ES2 gl = drawable.getGL().getGL2ES2();
178
179 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
181
182 // test viewport
183 {
184 final int[] viewport = new int[] { 0, 0, 0, 0 };
185 gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
186 if( gle1X != viewport[0] || gle1Y != viewport[1] || gle1W != viewport[2] || gle1H != viewport[3] ) {
187 final String msg = "Expected "+gle1X+"/"+gle1Y+" "+gle1W+"x"+gle1H+
188 ", actual "+viewport[0]+"/"+viewport[1]+" "+viewport[2]+"x"+viewport[3];
189 Assert.assertTrue("Viewport not restored: "+msg, false);
190 glelError[0] = true;
191 }
192 }
193
194 gl.glViewport(gle0X, gle0Y, gle0W, gle0H);
195 gle0.display(drawable);
196
197 gl.glViewport(gle1X, gle1Y, gle1W, gle1H);
198 gle1.display(drawable);
199
200 shot++;
201 if( 4 == shot ) {
202 gl.glViewport(tX, tY, tW, tH);
203 snapshot(0, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
204 gl.glViewport(gle1X, gle1Y, gle1W, gle1H); // restore viewport test
205 }
206
207 final TextureState ts = new TextureState(drawable.getGL(), GL.GL_TEXTURE_2D); // as set via gle0!
208 // System.err.println("XXX: "+ts);
209 Assert.assertEquals("Texture unit changed", GL.GL_TEXTURE0+texUnit, ts.getUnit());
210 if( keepTextureBound ) {
211 Assert.assertEquals("Texture mag-filter changed: "+ts, GL.GL_LINEAR, ts.getMagFilter());
212 Assert.assertEquals("Texture mag-filter changed: "+ts, GL.GL_LINEAR, ts.getMinFilter());
213 Assert.assertEquals("Texture wrap-s changed: "+ts, GL.GL_REPEAT, ts.getWrapS());
214 Assert.assertEquals("Texture wrap-t changed: "+ts, GL.GL_REPEAT, ts.getWrapT());
215 glelError[0] = GL.GL_LINEAR != ts.getMagFilter() || GL.GL_LINEAR != ts.getMinFilter() ||
216 GL.GL_REPEAT != ts.getWrapS() || GL.GL_REPEAT != ts.getWrapT();
217 }
218 }
219 final int border = 5;
220 @Override
221 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
222 gle0X = x + border;
223 gle0Y = y;
224 gle0W = width/2 - 2*border;
225 gle0H = height;
226 // System.err.println("GLEL0 "+gle0X+"/"+gle0Y+" "+gle0W+"x"+gle0H);
227
228 gle1X = gle0X + gle0W + 2*border;
229 gle1Y = y;
230 gle1W = width/2 - 2*border;
231 gle1H = height;
232 // System.err.println("GLEL1 "+gle1X+"/"+gle1Y+" "+gle1W+"x"+gle1H);
233
234 tX = x;
235 tY = y;
236 tW = width;
237 tH = height;
238 // System.err.println("Total "+tX+"/"+tY+" "+tW+"x"+tH);
239
240 final GL2ES2 gl = drawable.getGL().getGL2ES2();
241 gl.glViewport(gle0X, gle0Y, gle0W, gle0H);
242 gle0.reshape(drawable, 0, 0, gle0W, gle0H); // don't 'skip' about gle0X/gle0Y
243
244 gl.glViewport(gle1X, gle1Y, gle1W, gle1H);
245 gle1.reshape(drawable, 0, 0, gle1W, gle1H); // don't 'skip' about gle0X/gle0Y
246
247 if( keepTextureBound ) {
248 setupTex(gl);
249 }
250 }
251 });
252
253 final QuitAdapter quitAdapter = new QuitAdapter();
254 new com.jogamp.newt.event.awt.AWTKeyAdapter(quitAdapter, glc).addTo(glc);
255 new com.jogamp.newt.event.awt.AWTWindowAdapter(quitAdapter, glc).addTo(glc);
256 try {
257 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
258 public void run() {
259 frame.pack();
260 frame.setVisible(true);
261 }});
262 } catch( final Throwable throwable ) {
263 throwable.printStackTrace();
264 Assume.assumeNoException( throwable );
265 }
266 Assert.assertTrue("Component didn't become visible", AWTRobotUtil.waitForVisible(glc, true, null));
267 Assert.assertTrue("Component didn't become realized", AWTRobotUtil.waitForRealized(glc, true, null));
268 Thread.sleep(100);
269 setFrameSize(frame, true, glc_sz2);
270 System.err.println("window resize pos/siz: "+glc.getX()+"/"+glc.getY()+" "+glc.getSurfaceWidth()+"x"+glc.getSurfaceHeight());
271 Thread.sleep(100);
272
273 final long t0 = System.currentTimeMillis();
274 while(!quitAdapter.shouldQuit() && System.currentTimeMillis()-t0<duration) {
275 glc.display();
276 Thread.sleep(100);
277 }
278
279 try {
280 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
281 public void run() {
282 frame.setVisible(false);
283 frame.remove(glc);
284 frame.dispose();
285 }});
286 } catch( final Throwable throwable ) {
287 throwable.printStackTrace();
288 Assume.assumeNoException( throwable );
289 }
290 Assume.assumeFalse("Error occured in GLEL .. see log file above", glelError[0]);
291 }
292
293 @Test
294 public void test01_texUnit0_keepTex0_ES2() throws InterruptedException, IOException {
295 testImpl(false /* keepTextureBound */, 0 /* texUnit */);
296 }
297 @Test
298 public void test02_texUnit0_keepTex1_ES2() throws InterruptedException, IOException {
299 testImpl(true /* keepTextureBound */, 0 /* texUnit */);
300 }
301 @Test
302 public void test03_texUnit1_keepTex1_ES2() throws InterruptedException, IOException {
303 testImpl(true /* keepTextureBound */, 1 /* texUnit */);
304 }
305
306 public static void main(final String args[]) throws IOException {
307 for(int i=0; i<args.length; i++) {
308 if(args[i].equals("-time")) {
309 i++;
310 duration = MiscUtils.atol(args[i], duration);
311 }
312 }
313 org.junit.runner.JUnitCore.main(TestGLJPanelTextureStateAWT.class.getName());
314 }
315}
Specifies a set of OpenGL capabilities.
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 final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
Definition: GLProfile.java:594
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLJPanel.java:1217
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLJPanel.java:1222
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
Unit test for bug 826, test GLJPanel's TextureState save and restore.
void testImpl(final boolean keepTextureBound, final int texUnit)
static boolean waitForRealized(final java.awt.Component comp, final boolean realized, final Runnable waitAction)
static boolean waitForVisible(final java.awt.Component comp, final boolean visible, final Runnable waitAction)
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
Definition: TextureIO.java:171
Preserves a [ texture-unit, texture-target ] state.
final int getMinFilter()
Returns the state's min-filter param.
final int getWrapT()
Returns the state's wrap-t param.
final int getWrapS()
Returns the state's wrap-s param.
final int getUnit()
Returns the texture-unit of this state, key value.
final int getMagFilter()
Returns the state's mag-filter param.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void dispose(GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void display(GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
Called by the drawable during the first repaint after the component has been resized.
static final int GL_TEXTURE_MAG_FILTER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_MAG_FILTER" w...
Definition: GL.java:197
void glGetIntegerv(int pname, IntBuffer data)
Entry point to C language function: void {@native glGetIntegerv}(GLenum pname, GLint * data) Part ...
static final int GL_REPEAT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_REPEAT" with expressi...
Definition: GL.java:660
static final int GL_TEXTURE_2D
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_2D" with expr...
Definition: GL.java:491
static final int GL_TEXTURE_WRAP_S
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_WRAP_S" with ...
Definition: GL.java:485
void glBindTexture(int target, int texture)
Entry point to C language function: void {@native glBindTexture}(GLenum target, GLuint texture) Pa...
static final int GL_LINEAR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LINEAR" with expressi...
Definition: GL.java:323
static final int GL_TEXTURE_MIN_FILTER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_MIN_FILTER" w...
Definition: GL.java:372
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
static final int GL_TEXTURE_WRAP_T
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_WRAP_T" with ...
Definition: GL.java:489
void glActiveTexture(int texture)
Entry point to C language function: void {@native glActiveTexture}(GLenum texture) Part of GL_ES_V...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
void glTexParameteri(int target, int pname, int param)
Entry point to C language function: void {@native glTexParameteri}(GLenum target,...
static final int GL_NEAREST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_NEAREST" with express...
Definition: GL.java:715
void glViewport(int x, int y, int width, int height)
Entry point to C language function: void {@native glViewport}(GLint x, GLint y, GLsizei width,...
static final int GL_TEXTURE0
GL_ES_VERSION_2_0, GL_VERSION_1_3, GL_VERSION_ES_1_0, GL_ARB_multitexture Alias for: GL_TEXTURE0_ARB ...
Definition: GL.java:71
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
static final int GL_VIEWPORT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VIEWPORT" with expres...
Definition: GL.java:725