JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestTGATextureFromFileNEWT.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.util.texture;
30
31import java.io.IOException;
32import java.io.InputStream;
33import java.net.URLConnection;
34
35import com.jogamp.opengl.GLAutoDrawable;
36import com.jogamp.opengl.GLCapabilities;
37import com.jogamp.opengl.GLEventListener;
38import com.jogamp.opengl.GLProfile;
39
40import org.junit.After;
41import org.junit.Assert;
42import org.junit.Before;
43import org.junit.Test;
44import org.junit.FixMethodOrder;
45import org.junit.runners.MethodSorters;
46
47import com.jogamp.common.util.IOUtil;
48import com.jogamp.newt.opengl.GLWindow;
49import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
50import com.jogamp.opengl.test.junit.jogl.demos.es2.TextureDraw01ES2Listener;
51import com.jogamp.opengl.test.junit.jogl.demos.gl2.TextureDraw01GL2Listener;
52import com.jogamp.opengl.test.junit.util.MiscUtils;
53import com.jogamp.opengl.test.junit.util.QuitAdapter;
54import com.jogamp.opengl.test.junit.util.UITestCase;
55import com.jogamp.opengl.util.Animator;
56import com.jogamp.opengl.util.GLReadBufferUtil;
57import com.jogamp.opengl.util.texture.TextureData;
58import com.jogamp.opengl.util.texture.TextureIO;
59
60@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62 static boolean showFPS = false;
63 static long duration = 100; // ms
64
65 InputStream testTextureStream01U32;
66 InputStream testTextureStream02RLE32;
67 InputStream testTextureStream03RLE32;
68
69 @Before
70 public void initTest() throws IOException {
71 {
72 final URLConnection testTextureUrlConn = IOUtil.getResource("test-u32.tga", this.getClass().getClassLoader(), this.getClass());
73 Assert.assertNotNull(testTextureUrlConn);
74 testTextureStream01U32 = testTextureUrlConn.getInputStream();
75 Assert.assertNotNull(testTextureStream01U32);
76 }
77 {
78 final URLConnection testTextureUrlConn = IOUtil.getResource("bug744-rle32.tga", this.getClass().getClassLoader(), this.getClass());
79 Assert.assertNotNull(testTextureUrlConn);
80 testTextureStream02RLE32 = testTextureUrlConn.getInputStream();
81 Assert.assertNotNull(testTextureStream02RLE32);
82 }
83 {
84 final URLConnection testTextureUrlConn = IOUtil.getResource("bug982.rle32.256x256.tga", this.getClass().getClassLoader(), this.getClass());
85 Assert.assertNotNull(testTextureUrlConn);
86 testTextureStream03RLE32 = testTextureUrlConn.getInputStream();
87 Assert.assertNotNull(testTextureStream03RLE32);
88 }
89 }
90
91 public void testImpl(final boolean useFFP, final InputStream istream) throws InterruptedException, IOException {
92 final GLReadBufferUtil screenshot = new GLReadBufferUtil(true, false);
93 GLProfile glp;
94 if(useFFP && GLProfile.isAvailable(GLProfile.GL2)) {
95 glp = GLProfile.getMaxFixedFunc(true);
96 } else if(!useFFP && GLProfile.isAvailable(GLProfile.GL2ES2)) {
97 glp = GLProfile.getGL2ES2();
98 } else {
99 System.err.println(getSimpleTestName(".")+": GLProfile n/a, useFFP: "+useFFP);
100 return;
101 }
102 final GLCapabilities caps = new GLCapabilities(glp);
103 caps.setAlphaBits(1);
104
105 final TextureData texData = TextureIO.newTextureData(glp, istream, false /* mipmap */, TextureIO.TGA);
106 System.err.println("TextureData: "+texData);
107
108 final GLWindow glad = GLWindow.create(caps);
109 glad.setTitle("TestTGATextureGL2FromFileNEWT");
110 // Size OpenGL to Video Surface
111 glad.setSize(texData.getWidth(), texData.getHeight());
112
113 // load texture from file inside current GL context to match the way
114 // the bug submitter was doing it
115 final GLEventListener gle = useFFP ? new TextureDraw01GL2Listener( texData ) : new TextureDraw01ES2Listener( texData, 0 ) ;
116 glad.addGLEventListener(gle);
118 boolean shot = false;
119
120 @Override public void init(final GLAutoDrawable drawable) {}
121
122 public void display(final GLAutoDrawable drawable) {
123 // 1 snapshot
124 if(null!=((TextureDraw01Accessor)gle).getTexture() && !shot) {
125 shot = true;
126 snapshot(0, null, drawable.getGL(), screenshot, TextureIO.PNG, null);
127 }
128 }
129
130 @Override public void dispose(final GLAutoDrawable drawable) { }
131 @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
132 });
133
134 final Animator animator = new Animator(glad);
135 animator.setUpdateFPSFrames(60, showFPS ? System.err : null);
136 final QuitAdapter quitAdapter = new QuitAdapter();
137 glad.addKeyListener(quitAdapter);
138 glad.addWindowListener(quitAdapter);
139 glad.setVisible(true);
140 animator.start();
141
142 while(!quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration) {
143 Thread.sleep(100);
144 }
145
146 animator.stop();
147 glad.destroy();
148 }
149
150 @Test
151 public void test01U32__GL2() throws InterruptedException, IOException {
152 testImpl(true, testTextureStream01U32);
153 }
154
155 @Test
156 public void test02RLE32__GL2() throws InterruptedException, IOException {
157 testImpl(true, testTextureStream02RLE32);
158 }
159
160 @Test
161 public void test03RLE32__GL2() throws InterruptedException, IOException {
162 testImpl(true, testTextureStream03RLE32);
163 }
164
165 @After
166 public void cleanupTest() {
167 testTextureStream01U32 = null;
168 testTextureStream02RLE32 = null;
169 testTextureStream03RLE32 = null;
170 }
171
172 public static void main(final String args[]) throws IOException {
173 for(int i=0; i<args.length; i++) {
174 if(args[i].equals("-time")) {
175 i++;
176 duration = MiscUtils.atol(args[i], duration);
177 }
178 }
179 org.junit.runner.JUnitCore.main(TestTGATextureFromFileNEWT.class.getName());
180 }
181}
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void setTitle(final String title)
Definition: GLWindow.java:297
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
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 GLProfile getMaxFixedFunc(final AbstractGraphicsDevice device, final boolean favorHardwareRasterizer)
Returns the highest profile, implementing the fixed function pipeline.
Definition: GLProfile.java:808
static final String GL2ES2
The intersection of the desktop GL3, GL2 and embedded ES2 profile.
Definition: GLProfile.java:594
static final String GL2
The desktop OpenGL profile 1.x up to 3.0.
Definition: GLProfile.java:579
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
static long atol(final String str, final long def)
Definition: MiscUtils.java:66
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
Represents the data for an OpenGL texture.
int getHeight()
Returns the height in pixels of the texture data.
int getWidth()
Returns the width in pixels of the texture data.
static final String TGA
Constant which can be used as a file suffix to indicate a Targa file, value {@value}.
Definition: TextureIO.java:176
static final String PNG
Constant which can be used as a file suffix to indicate a PNG file, value {@value}.
Definition: TextureIO.java:171
static TextureData newTextureData(final GLProfile glp, final File file, final boolean mipmap, String fileSuffix)
Creates a TextureData from the given file.
Definition: TextureIO.java:233
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.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.