JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestTexture01AWT.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.util.texture;
30
31
32import com.jogamp.common.util.awt.AWTEDTExecutor;
33import com.jogamp.opengl.test.junit.jogl.demos.gl2.TextureDraw01GL2Listener;
34import com.jogamp.opengl.test.junit.util.MiscUtils;
35import com.jogamp.opengl.test.junit.util.UITestCase;
36
37import com.jogamp.opengl.GLAutoDrawable;
38import com.jogamp.opengl.GLEventListener;
39import com.jogamp.opengl.GLProfile;
40import com.jogamp.opengl.GLCapabilities;
41import com.jogamp.opengl.awt.GLCanvas;
42import javax.swing.ImageIcon;
43import javax.swing.JLabel;
44
45import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
46import com.jogamp.opengl.util.texture.TextureData;
47import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
48
49import java.awt.AlphaComposite;
50import java.awt.Canvas;
51import java.awt.Color;
52import java.awt.Frame;
53import java.awt.GradientPaint;
54import java.awt.Graphics2D;
55import java.awt.image.BufferedImage;
56
57import java.io.IOException;
58import org.junit.Assert;
59import org.junit.After;
60import org.junit.Assume;
61import org.junit.Before;
62import org.junit.BeforeClass;
63import org.junit.Test;
64import org.junit.FixMethodOrder;
65import org.junit.runners.MethodSorters;
66
67/**
68 * Demonstrates TextureData w/ AWT usage in both directions,
69 * i.e. generating a texture based on an AWT BufferedImage data
70 * as well as reading out GL framebuffer and displaying it
71 * as an BufferedImage.
72 */
73@FixMethodOrder(MethodSorters.NAME_ASCENDING)
74public class TestTexture01AWT extends UITestCase {
75 static long durationPerTest = 500;
76 static GLProfile glp;
77 static GLCapabilities caps;
78 BufferedImage textureImage;
79
80 @BeforeClass
81 public static void initClass() {
83 UITestCase.setTestSupported(false);
84 return;
85 }
86 glp = GLProfile.getMaxFixedFunc(true);
87 Assert.assertNotNull(glp);
88 caps = new GLCapabilities(glp);
89 Assert.assertNotNull(caps);
90 }
91
92 @Before
93 public void initTest() {
94 // create base image
95 BufferedImage baseImage = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);
96 Assert.assertNotNull(baseImage);
97 Graphics2D g = baseImage.createGraphics();
98 Assert.assertNotNull(g);
99 g.setPaint(new GradientPaint(0, 0, Color.CYAN,
100 baseImage.getWidth(), baseImage.getHeight(), Color.BLUE));
101 g.fillRect(0, 0, baseImage.getWidth(), baseImage.getHeight());
102 g.dispose();
103
104 // create texture image
105 final int imageType = BufferedImage.TYPE_3BYTE_BGR;
106 textureImage = new BufferedImage(baseImage.getWidth(),
107 baseImage.getHeight(),
108 imageType);
109 Assert.assertNotNull(textureImage);
110 g = textureImage.createGraphics();
111 g.setComposite(AlphaComposite.Src);
112 g.drawImage(baseImage, 0, 0, null);
113 g.dispose();
114
115 baseImage.flush();
116 baseImage=null;
117 }
118
119 @After
120 public void cleanupTest() {
121 Assert.assertNotNull(textureImage);
122 textureImage.flush();
123 textureImage=null;
124 }
125
126 @Test
127 public void test1() throws InterruptedException {
128 final AWTGLReadBufferUtil awtGLReadBufferUtil = new AWTGLReadBufferUtil(caps.getGLProfile(), false);
129 final Frame frame0 = new Frame("GL -> AWT");
130 final Canvas canvas = new Canvas();
131 frame0.add(canvas);
132
133 final GLCanvas glCanvas = new GLCanvas(caps);
134 final Frame frame1 = new Frame("AWT -> Texture");
135 Assert.assertNotNull(frame1);
136 frame1.add(glCanvas);
137
138 // create texture
139 final TextureData textureData = AWTTextureIO.newTextureData(caps.getGLProfile(), textureImage, false);
140 glCanvas.addGLEventListener(new TextureDraw01GL2Listener(textureData));
141 glCanvas.addGLEventListener(new GLEventListener() {
142
143 @Override
144 public void init(final GLAutoDrawable drawable) { }
145 @Override
146 public void dispose(final GLAutoDrawable drawable) { }
147 @Override
148 public void display(final GLAutoDrawable drawable) {
149 final BufferedImage outputImage = awtGLReadBufferUtil.readPixelsToBufferedImage(drawable.getGL(), true /* awtOrientation */);
150 final ImageIcon imageIcon = new ImageIcon(outputImage);
151 final JLabel imageLabel = new JLabel(imageIcon);
152 try {
153 AWTEDTExecutor.singleton.invoke(true, new Runnable() {
154 public void run() {
155 frame0.removeAll();
156 frame0.add(imageLabel);
157 frame0.validate();
158 }});
159 } catch (final Exception e) {
160 e.printStackTrace();
161 }
162 }
163
164 @Override
165 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
166 frame0.setSize(frame1.getWidth(), frame1.getHeight());
167 frame0.setLocation(frame1.getX()+frame1.getWidth()+32, frame0.getY());
168 frame0.validate();
169 }
170 });
171
172 try {
173 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
174 public void run() {
175 frame1.setSize(256, 256);
176 frame1.setLocation(0, 0);
177 frame1.setVisible(true);
178 frame0.setSize(frame1.getWidth(), frame1.getHeight());
179 frame0.setLocation(frame1.getX()+frame1.getWidth()+32, frame0.getY());
180 frame0.setVisible(true);
181 }});
182 } catch( final Throwable throwable ) {
183 throwable.printStackTrace();
184 Assume.assumeNoException( throwable );
185 }
186
187 Thread.sleep(durationPerTest);
188
189 try {
190 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
191 public void run() {
192 frame0.setVisible(false);
193 frame0.dispose();
194 frame1.setVisible(false);
195 frame1.dispose();
196 }});
197 } catch( final Throwable throwable ) {
198 throwable.printStackTrace();
199 Assume.assumeNoException( throwable );
200 }
201 }
202
203 public static void main(final String args[]) throws IOException {
204 for(int i=0; i<args.length; i++) {
205 if(args[i].equals("-time")) {
206 durationPerTest = MiscUtils.atol(args[++i], durationPerTest);
207 }
208 }
209 final String tstname = TestTexture01AWT.class.getName();
210 org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(new String[] {
211 tstname,
212 "filtertrace=true",
213 "haltOnError=false",
214 "haltOnFailure=false",
215 "showoutput=true",
216 "outputtoformatters=true",
217 "logfailedtests=true",
218 "logtestlistenerevents=true",
219 "formatter=org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter",
220 "formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,TEST-"+tstname+".xml" } );
221 }
222}
Specifies a set of OpenGL capabilities.
final GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
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 getMaxFixedFunc(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the fixed function pipeline.
Definition: GLProfile.java:808
static final String GL2GL3
The intersection of the desktop GL3 and GL2 profile.
Definition: GLProfile.java:597
A heavyweight AWT component which provides OpenGL rendering support.
Definition: GLCanvas.java:170
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLCanvas.java:1065
Demonstrates TextureData w/ AWT usage in both directions, i.e.
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,...
Represents the data for an OpenGL texture.
static TextureData newTextureData(final GLProfile glp, final BufferedImage image, final boolean mipmap)
Creates a TextureData from the given BufferedImage.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.