JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestBug605FlippedImageAWT.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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 */
28package com.jogamp.opengl.test.junit.jogl.caps;
29
30import java.io.File;
31
32import com.jogamp.opengl.GL;
33import com.jogamp.opengl.GL2;
34import com.jogamp.opengl.GLAutoDrawable;
35import com.jogamp.opengl.GLCapabilities;
36import com.jogamp.opengl.GLCapabilitiesImmutable;
37import com.jogamp.opengl.GLDrawableFactory;
38import com.jogamp.opengl.GLEventListener;
39import com.jogamp.opengl.GLException;
40import com.jogamp.opengl.GLProfile;
41import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
42
43import com.jogamp.opengl.util.awt.AWTGLReadBufferUtil;
44import com.jogamp.opengl.util.texture.TextureIO;
45
46import java.awt.image.BufferedImage;
47
48import org.junit.Assert;
49import org.junit.Test;
50import org.junit.FixMethodOrder;
51import org.junit.runners.MethodSorters;
52
53import com.jogamp.opengl.test.junit.util.UITestCase;
54
55@FixMethodOrder(MethodSorters.NAME_ASCENDING)
57 class FlippedImageTest implements GLEventListener {
58 AWTGLReadBufferUtil screenshot;
59
60 public void display(final GLAutoDrawable drawable) {
61 final GL2 gl = drawable.getGL().getGL2();
62
63 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
65
67 gl.glLoadIdentity();
69 gl.glLoadIdentity();
70
71 // red below
72 gl.glColor3f(1, 0, 0);
73 gl.glRectf(-1, -1, 1, 0);
74
75 // green above
76 gl.glColor3f(0, 1, 0);
77 gl.glRectf(-1, 0, 1, 1);
78 gl.glFinish();
79
81 if(caps.getAccumGreenBits() > 0) {
82 gl.glAccum(GL2.GL_ACCUM, 1.0f);
83 gl.glAccum(GL2.GL_RETURN, 1.0f);
84 }
85 gl.glFinish();
86
87 final int width = drawable.getSurfaceWidth();
88 final int height = drawable.getSurfaceHeight();
89
90 final String fname = getSnapshotFilename(0, null, caps, width, height, false, TextureIO.PNG, null);
91 final BufferedImage image;
92 try {
93 image = screenshot.readPixelsToBufferedImage(gl, 0, 0, width, height, true /* awtOrientation */);
94 screenshot.write(new File(fname));
95 } catch (final GLException e) {
96 throw e;
97 }
98 testFlipped(image, width, height);
99 }
100
101 public void init(final GLAutoDrawable drawable) {
102 final GL gl = drawable.getGL();
103 System.err.println("GL_RENDERER: "+gl.glGetString(GL.GL_RENDERER));
104 System.err.println("GL_VERSION: "+gl.glGetString(GL.GL_VERSION));
105 screenshot = new AWTGLReadBufferUtil(drawable.getGLProfile(), false);
106 }
107 public void reshape(final GLAutoDrawable glDrawable, final int x, final int y, final int w, final int h) {}
108 public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {}
109 public void dispose(final GLAutoDrawable drawable) {
110 screenshot.dispose(drawable.getGL());
111 }
112 }
113
114 static final int green = 0x0000ff00; // above
115 static final int red = 0x00ff0000; // below
116
117 private void testFlipped(final BufferedImage image, final int width, final int height) {
118 // Default origin 0/0 is lower left corner, so is the memory layout
119 // However AWT origin 0/0 is upper left corner
120 final int below = image.getRGB(0, height-1) & 0x00ffffff;
121 System.err.println("below: 0x"+Integer.toHexString(below));
122
123 final int above = image.getRGB(0, 0) & 0x00ffffff;
124 System.err.println("above: 0x"+Integer.toHexString(above));
125
126 if (above == green && below == red) {
127 System.out.println("Image right side up");
128 } else if (above == red && below == green) {
129 Assert.assertTrue("Image is flipped", false);
130 } else {
131 Assert.assertTrue("Error in test", false);
132 }
133 }
134
135 private void test(final GLCapabilitiesImmutable caps) {
136
138 final GLAutoDrawable glad = glFactory.createOffscreenAutoDrawable(null, caps, null, 256, 256);
139 final FlippedImageTest tglel = new FlippedImageTest();
140 glad.addGLEventListener(tglel);
141
142 // 1 frame incl. snapshot to memory & file
143 glad.display();
144 System.err.println("XXX "+glad.getChosenGLCapabilities());
145 System.err.println("XXX "+glad.getContext().getGLVersion());
146
147 glad.destroy();
148 }
149
150 @Test
151 public void test01DefaultFBO() {
152 final GLProfile glp = GLProfile.get(GLProfile.GL2);
153 final GLCapabilities caps = new GLCapabilities(glp);
154 caps.setFBO(true);
155 test(caps);
156 }
157
158 @Test
159 public void test01StencilFBO() {
160 final GLProfile glp = GLProfile.get(GLProfile.GL2);
161 final GLCapabilities caps = new GLCapabilities(glp);
162 caps.setStencilBits(8);
163 caps.setFBO(true);
164 test(caps);
165 }
166
167 @Test
168 public void test01DefaultPBuffer() {
169 final GLProfile glp = GLProfile.get(GLProfile.GL2);
170 final GLCapabilities caps = new GLCapabilities(glp);
171 caps.setPBuffer(true);
172 test(caps);
173 }
174
175 @Test
177 final GLProfile glp = GLProfile.get(GLProfile.GL2);
178 final GLCapabilities caps = new GLCapabilities(glp);
179 caps.setAccumRedBits(16);
180 caps.setAccumGreenBits(16);
181 caps.setAccumBlueBits(16);
182 caps.setStencilBits(8);
183 caps.setPBuffer(true);
184 test(caps);
185 }
186
187 public static void main(final String[] args) {
188 org.junit.runner.JUnitCore.main(TestBug605FlippedImageAWT.class.getName());
189 }
190}
Specifies a set of OpenGL capabilities.
void setPBuffer(final boolean enable)
Requesting offscreen pbuffer mode.
void setAccumRedBits(final int accumRedBits)
Sets the number of bits requested for the accumulation buffer's red component.
void setStencilBits(final int stencilBits)
Sets the number of bits requested for the stencil buffer.
void setAccumGreenBits(final int accumGreenBits)
Sets the number of bits requested for the accumulation buffer's green component.
void setFBO(final boolean enable)
Requesting offscreen FBO mode.
void setAccumBlueBits(final int accumBlueBits)
Sets the number of bits requested for the accumulation buffer's blue component.
final String getGLVersion()
Returns a valid OpenGL version string, ie
Definition: GLContext.java:769
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.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
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
void write(final File dest)
Write the TextureData filled by readPixels(GLAutoDrawable, boolean) to file.
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,...
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 final int GL_ACCUM_BUFFER_BIT
GL_VERSION_1_0 Define "GL_ACCUM_BUFFER_BIT" with expression '0x00000200', CType: int
Definition: GL2.java:1547
static final int GL_ACCUM
GL_VERSION_1_0 Define "GL_ACCUM" with expression '0x0100', CType: int
Definition: GL2.java:2164
static final int GL_RETURN
GL_VERSION_1_0 Define "GL_RETURN" with expression '0x0102', CType: int
Definition: GL2.java:1413
void glAccum(int op, float value)
Entry point to C language function: void {@native glAccum}(GLenum op, GLfloat value) Part of GL_VE...
void glColor3f(float red, float green, float blue)
Entry point to C language function: void {@native glColor3f}(GLfloat red, GLfloat green,...
void glRectf(float x1, float y1, float x2, float y2)
Entry point to C language function: void {@native glRectf}(GLfloat x1, GLfloat y1,...
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.
GLContext getContext()
Returns the context associated with this drawable.
GL2 getGL2()
Casts this object to the GL2 interface.
Specifies an immutable set of OpenGL capabilities.
int getAccumGreenBits()
Returns the number of bits for the accumulation buffer's green component.
GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
GLProfile getGLProfile()
Fetches the GLProfile for this drawable.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_VERSION
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VERSION" with express...
Definition: GL.java:190
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,...
String glGetString(int name)
Entry point to C language function: const GLubyte * {@native glGetString}(GLenum name) Part of GL_...
static final int GL_RENDERER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RENDERER" with expres...
Definition: GL.java:662
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
void glFinish()
Entry point to C language function: void {@native glFinish}() Part of GL_ES_VERSION_2_0,...
static final int GL_STENCIL_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_STENCIL_BUFFER_BIT" w...
Definition: GL.java:60
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
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.
void glLoadIdentity()
Load the current matrix with the identity matrix.
void glMatrixMode(int mode)
Sets the current matrix mode.