JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestGLJPanelAWTBug450.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 package com.jogamp.opengl.test.junit.jogl.demos.gl2.awt;
29
30import com.jogamp.opengl.*;
31
32import com.jogamp.opengl.util.FPSAnimator;
33import com.jogamp.opengl.util.GLReadBufferUtil;
34import com.jogamp.opengl.util.texture.TextureIO;
35
36import com.jogamp.opengl.awt.GLJPanel;
37import com.jogamp.opengl.glu.gl2.GLUgl2;
38
39import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
40import com.jogamp.opengl.test.junit.util.UITestCase;
41
42import java.awt.AWTException;
43import java.awt.BorderLayout;
44import java.lang.reflect.InvocationTargetException;
45import java.nio.ByteBuffer;
46
47import javax.swing.JFrame;
48import javax.swing.SwingUtilities;
49
50import org.junit.Assert;
51import org.junit.BeforeClass;
52import org.junit.AfterClass;
53import org.junit.Test;
54import org.junit.FixMethodOrder;
55import org.junit.runners.MethodSorters;
56
57/**
58 * Test for bug 450, which causes the right part of the frame to be black
59 * for all x >= height.
60 *
61 * Draws the Gears demo in a window that's twice as wide than it is tall,
62 * and checks to see if a particular pixel in the right half of the frame
63 * is colored.
64 *
65 * @author Wade Walker (adapted from TestGearsGLJPanelAWT)
66 */
67@FixMethodOrder(MethodSorters.NAME_ASCENDING)
68public class TestGLJPanelAWTBug450 extends UITestCase {
69 static GLProfile glp;
70 static int width, height;
71 static int r_x, r_y;
72 /** Set this if test fails. Needed because we can't throw an exception
73 * all the way up the stack from where we test the pixel. */
74 static boolean failed;
75
76 @BeforeClass
77 public static void initClass() {
78 glp = GLProfile.getGL2ES2();
79 Assert.assertNotNull(glp);
80 height = 256;
81 width = 2*height;
82 r_x = 5*height/4; // 5/8 * width
83 r_y = height/2;
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 GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
94 final JFrame frame = new JFrame("Swing GLJPanel");
95 Assert.assertNotNull(frame);
96
97 final GLJPanel glJPanel = new GLJPanel(caps);
98 Assert.assertNotNull(glJPanel);
99 final RedSquareES2 demo = new RedSquareES2();
100 demo.setAspect((float)width/(float)height);
101 demo.setDoRotation(false);
102 glJPanel.addGLEventListener(demo);
103 glJPanel.addGLEventListener(new GLEventListener() {
104 int f = 0;
105 @Override
106 public void init(final GLAutoDrawable drawable) {
107 // drawable.getGL().glClearColor(0, 0, 1, 1);
108 }
109 @Override
110 public void display(final GLAutoDrawable drawable) {
111 // look at one pixel at the bottom of the frame, just right of
112 // the center line, and make sure it's not black
113 final GL2 gl = GLUgl2.getCurrentGL2();
114 final ByteBuffer bytebuffer = ByteBuffer.allocateDirect( 3 );
115 gl.glReadPixels( r_x, r_y, 1, 1, GL2GL3.GL_BGR, GL.GL_UNSIGNED_BYTE, bytebuffer );
116 final byte byte0 = bytebuffer.get( 0 );
117 final byte byte1 = bytebuffer.get( 1 );
118 final byte byte2 = bytebuffer.get( 2 );
119 if( (byte0 == 0) && (byte1 == 0) && (byte2 == 0) ) {
120 failed = true;
121 }
122 if(0 == f) {
123 System.err.println("BGR ("+r_x+"/"+r_y+"): "+byte0+", "+byte1+", "+byte2+" - OK "+(!failed));
124 snapshot(f, null, gl, screenshot, TextureIO.PNG, null);
125 }
126 f++;
127 }
128 @Override
129 public void dispose(final GLAutoDrawable drawable) {}
130 @Override
131 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
132 });
133
134 final FPSAnimator animator = new FPSAnimator(glJPanel, 60);
135
136 SwingUtilities.invokeAndWait(new Runnable() {
137 public void run() {
138 frame.getContentPane().add(glJPanel, BorderLayout.CENTER);
139 frame.setSize(width, height);
140 frame.setVisible(true);
141 } } ) ;
142
143 animator.setUpdateFPSFrames(1, null);
144 animator.start();
145 Assert.assertEquals(true, animator.isAnimating());
146
147 while(animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
148 Thread.sleep(100);
149 }
150
151 Assert.assertNotNull(frame);
152 Assert.assertNotNull(glJPanel);
153 Assert.assertNotNull(animator);
154
155 animator.stop();
156 Assert.assertEquals(false, animator.isAnimating());
157 SwingUtilities.invokeAndWait(new Runnable() {
158 public void run() {
159 frame.setVisible(false);
160 frame.getContentPane().remove(glJPanel);
161 frame.remove(glJPanel);
162 glJPanel.destroy();
163 frame.dispose();
164 } } );
165
166 Assert.assertFalse( failed );
167 }
168
169 @Test
170 public void test01()
171 throws AWTException, InterruptedException, InvocationTargetException
172 {
174 runTestGL(caps);
175 }
176
177 static long duration = 500; // ms
178
179 public static void main(final String args[]) {
180 for(int i=0; i<args.length; i++) {
181 if(args[i].equals("-time")) {
182 i++;
183 try {
184 duration = Integer.parseInt(args[i]);
185 } catch (final Exception ex) { ex.printStackTrace(); }
186 }
187 }
188 org.junit.runner.JUnitCore.main(TestGLJPanelAWTBug450.class.getName());
189 }
190}
Specifies a set of OpenGL capabilities.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
Definition: GLProfile.java:739
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
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 final GL2 getCurrentGL2()
Definition: GLUgl2.java:195
Test for bug 450, which causes the right part of the frame to be black for all x >= height.
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.
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
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void glReadPixels(int x, int y, int width, int height, int format, int type, Buffer pixels)
Entry point to C language function: void {@native glReadPixels}(GLint x, GLint y,...
static final int GL_BGR
GL_VERSION_1_2, GL_EXT_bgra Alias for: GL_BGR_EXT Define "GL_BGR" with expression '0x80E0',...
Definition: GL.java:399
static final int GL_UNSIGNED_BYTE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_UNSIGNED_BYTE" with e...
Definition: GL.java:284