JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug461FBOSupersamplingSwingAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 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
31import java.awt.Container;
32import java.awt.event.WindowAdapter;
33import java.awt.event.WindowEvent;
34
35import java.awt.image.BufferedImage;
36import java.lang.reflect.InvocationTargetException;
37
38import com.jogamp.opengl.GL;
39import com.jogamp.opengl.GL2;
40import com.jogamp.opengl.GLAutoDrawable;
41import com.jogamp.opengl.GLCapabilities;
42import com.jogamp.opengl.GLDrawableFactory;
43import com.jogamp.opengl.GLEventListener;
44import com.jogamp.opengl.GLOffscreenAutoDrawable;
45import com.jogamp.opengl.GLProfile;
46import javax.swing.ImageIcon;
47import javax.swing.JFrame;
48import javax.swing.JLabel;
49
50import org.junit.Assert;
51import org.junit.Test;
52import org.junit.FixMethodOrder;
53import org.junit.runners.MethodSorters;
54
55import com.jogamp.opengl.test.junit.util.MiscUtils;
56import com.jogamp.opengl.test.junit.util.UITestCase;
57import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
58
59/**
60 * Tests for bug 461, a failure of GLDrawableFactory.createOffscreenAutoDrawable(..) on Windows
61 * when the stencil buffer is turned on.
62 *
63 * @author Wade Walker (from code sample provided by Owen Dimond)
64 */
65@FixMethodOrder(MethodSorters.NAME_ASCENDING)
67 static long durationPerTest = 500;
68 JFrame jframe;
69 GLOffscreenAutoDrawable offScreenBuffer;
70 AWTGLReadBufferUtil awtGLReadBufferUtil;
71
72 private void render(final GLAutoDrawable drawable) {
73 final GL2 gl = drawable.getGL().getGL2();
74 Assert.assertNotNull(gl);
76
77 // draw a triangle filling the window
79 gl.glColor3f(1, 0, 0);
80 gl.glVertex2d(-1, -1);
81 gl.glColor3f(0, 1, 0);
82 gl.glVertex2d(0, 1);
83 gl.glColor3f(0, 0, 1);
84 gl.glVertex2d(1, -1);
85 gl.glEnd();
86 }
87
88 /* @Override */
89 public void init(final GLAutoDrawable drawable) {
90 awtGLReadBufferUtil = new AWTGLReadBufferUtil(drawable.getGLProfile(), false);
91 }
92
93 /* @Override */
94 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
95 }
96
97 /* @Override */
98 public void display(final GLAutoDrawable drawable) {
99 render(offScreenBuffer);
100 final BufferedImage outputImage = awtGLReadBufferUtil.readPixelsToBufferedImage(drawable.getGL(), 0, 0, 200, 200, true /* awtOrientation */);
101 Assert.assertNotNull(outputImage);
102 final ImageIcon imageIcon = new ImageIcon(outputImage);
103 final JLabel imageLabel = new JLabel(imageIcon);
104 try {
105 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
106 public void run() {
107 final Container cont = jframe.getContentPane();
108 cont.removeAll();
109 cont.add(imageLabel);
110 cont.validate();
111 }});
112 } catch (final Exception e) {
113 e.printStackTrace();
114 }
115 }
116
117 /* @Override */
118 public void dispose(final GLAutoDrawable drawable) {
119 try {
120 awtGLReadBufferUtil.dispose(drawable.getGL());
121 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
122 public void run() {
123 jframe.setVisible(false);
124 jframe.dispose();
125 }});
126 } catch (final Exception e) {
127 e.printStackTrace();
128 }
129 }
130
131 @Test
132 public void testOffscreenSupersampling() throws InterruptedException, InvocationTargetException {
133 jframe = new JFrame("Offscreen Supersampling");
134 Assert.assertNotNull(jframe);
135 jframe.addWindowListener(new WindowAdapter() {
136 public void windowClosing(final WindowEvent e) {
137 System.exit(0);
138 }
139 });
140
141 final GLProfile glp = GLProfile.get(GLProfile.GL2);
142 Assert.assertNotNull(glp);
143
145 Assert.assertNotNull(fac);
146
147 final GLCapabilities glCap = new GLCapabilities(glp);
148 Assert.assertNotNull(glCap);
149
150 // COMMENTING OUT THIS LINE FIXES THE ISSUE.
151 // Setting this in JOGL1 works. Thus this is a JOGL2 issue.
152 glCap.setSampleBuffers(true);
153 glCap.setNumSamples(4);
154
155 // Without line below, there is an error on Windows.
156 // glCap.setDoubleBuffered(false); // implicit double buffer -> MSAA + FBO
157
158 // Needed for drop shadows
159 glCap.setStencilBits(1);
160
161 //makes a new buffer
162 offScreenBuffer = fac.createOffscreenAutoDrawable(GLProfile.getDefaultDevice(), glCap, null, 200, 200);
163 Assert.assertNotNull(offScreenBuffer);
164 offScreenBuffer.addGLEventListener(this);
165 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
166 public void run() {
167 jframe.setSize( 300, 300);
168 jframe.setVisible(true);
169 }});
170 offScreenBuffer.display(); // read from front buffer due to FBO+MSAA -> double-buffer
171 offScreenBuffer.display(); // now we have prev. image in front buffer to be read out
172
173 Thread.sleep(durationPerTest);
174
175 offScreenBuffer.destroy();
176 }
177
178 public static void main(final String args[]) {
179 for(int i=0; i<args.length; i++) {
180 if(args[i].equals("-time")) {
181 durationPerTest = MiscUtils.atol(args[++i], durationPerTest);
182 }
183 }
184 org.junit.runner.JUnitCore.main(TestBug461FBOSupersamplingSwingAWT.class.getName());
185 }
186}
187
Specifies a set of OpenGL capabilities.
void setStencilBits(final int stencilBits)
Sets the number of bits requested for the stencil buffer.
void setNumSamples(final int numSamples)
If sample buffers are enabled, indicates the number of buffers to be allocated.
void setSampleBuffers(final boolean enable)
Defaults to false.
abstract GLOffscreenAutoDrawable createOffscreenAutoDrawable(AbstractGraphicsDevice device, GLCapabilitiesImmutable caps, GLCapabilitiesChooser chooser, int width, int height)
Creates a realized GLOffscreenAutoDrawable incl it's offscreen NativeSurface with the given capabilit...
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static AbstractGraphicsDevice getDefaultDevice()
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
Tests for bug 461, a failure of GLDrawableFactory.createOffscreenAutoDrawable(..) on Windows when the...
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
GLReadBufferUtil specialization allowing to read out a frambuffer to an AWT BufferedImage utilizing A...
BufferedImage readPixelsToBufferedImage(final GL gl, final boolean awtOrientation)
Read the drawable's pixels to TextureData and Texture, if requested at construction,...
void glVertex2d(double x, double y)
Entry point to C language function: void {@native glVertex2d}(GLdouble x, GLdouble y) Part of GL_V...
void glBegin(int mode)
Entry point to C language function: void {@native glBegin}(GLenum mode) Part of GL_VERSION_1_0
void glEnd()
Entry point to C language function: void {@native glEnd}() Part of GL_VERSION_1_0
void glColor3f(float red, float green, float blue)
Entry point to C language function: void {@native glColor3f}(GLfloat red, GLfloat green,...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
GL2 getGL2()
Casts this object to the GL2 interface.
GLProfile getGLProfile()
Fetches the GLProfile for this drawable.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
Platform-independent GLAutoDrawable specialization, exposing offscreen functionality.
static final int GL_TRIANGLES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLES" with expre...
Definition: GL.java:145
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 glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...