JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Bug818GLJPanelAndGLCanvasApplet.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 */
28package com.jogamp.opengl.test.junit.jogl.demos.gl2.awt;
29
30import java.awt.ComponentOrientation;
31import java.awt.Dimension;
32import java.awt.GridLayout;
33import java.awt.event.WindowAdapter;
34import java.awt.event.WindowEvent;
35import java.io.InputStream;
36import java.nio.ByteBuffer;
37import java.nio.ByteOrder;
38import java.nio.FloatBuffer;
39
40import com.jogamp.opengl.GL;
41import com.jogamp.opengl.GL2;
42import com.jogamp.opengl.GL2ES1;
43import com.jogamp.opengl.GL2GL3;
44import com.jogamp.opengl.GLAutoDrawable;
45import com.jogamp.opengl.GLEventListener;
46import com.jogamp.opengl.awt.GLCanvas;
47import com.jogamp.opengl.awt.GLJPanel;
48import com.jogamp.opengl.fixedfunc.GLLightingFunc;
49import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
50import com.jogamp.opengl.fixedfunc.GLPointerFunc;
51import javax.swing.JApplet;
52import javax.swing.JLabel;
53import javax.swing.JPanel;
54import javax.swing.JFrame;
55import javax.swing.SwingConstants;
56
57import com.jogamp.common.util.VersionUtil;
58import com.jogamp.opengl.JoglVersion;
59import com.jogamp.opengl.util.Animator;
60import com.jogamp.opengl.util.texture.Texture;
61import com.jogamp.opengl.util.texture.TextureIO;
62
63/**
64 * Bug 818: OSX GLJPanel [and GLCanvas] Crash
65 * <pre>
66 * - NVIDIA GeForce GT 330M
67 * - GL_VENDOR: "NVIDIA Corporation"
68 * - GL_RENDERER: "NVIDIA GeForce GT 330M OpenGL Engine"
69 * - GL_VERSION: "2.1 NVIDIA-8.12.47 310.40.00.05f01"
70 * - Mac OSX 10.6.8
71 * </pre>
72 */
73public class Bug818GLJPanelAndGLCanvasApplet extends JApplet {
74
75 private static final long serialVersionUID = 1L;
76
77 private Animator animatorCanvas;
78
79 private Animator animatorPanel;
80
81 public static JFrame frame;
82 public static JPanel appletHolder;
83 public static boolean isApplet = true;
84
85 static public void main(final String args[]) {
86 isApplet = false;
87
88 final JApplet myApplet = new Bug818GLJPanelAndGLCanvasApplet();
89
90 appletHolder = new JPanel();
91
92 frame = new JFrame("Bug818GLJPanelApplet");
93 frame.getContentPane().add(myApplet);
94
95 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
96 frame.addWindowListener(new WindowAdapter() {
97 public void windowClosing(final WindowEvent e) {
98 System.exit(0);
99 }
100 });
101
102 try {
103 javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
104 public void run() {
105 myApplet.init();
106 frame.validate();
107 frame.pack();
108 frame.setVisible(true);
109 } } );
110 } catch( final Throwable throwable ) {
111 throwable.printStackTrace();
112 }
113
114 myApplet.start();
115 }
116
117
118 @Override
119 public void init() {
120
121 final JPanel panel = new JPanel();
122 panel.setLayout(new GridLayout(2, 2));
123 System.err.println("Pre Orientation L2R: "+panel.getComponentOrientation().isLeftToRight());
124 panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
125 System.err.println("Post Orientation L2R: "+panel.getComponentOrientation().isLeftToRight());
126 setContentPane(panel);
127 panel.add(new JLabel("GLJPanel", SwingConstants.CENTER));
128 panel.add(new JLabel("GLCanvas", SwingConstants.CENTER));
129
130 final GLJPanel gljPanel = new GLJPanel();
131 gljPanel.addGLEventListener(new JOGLQuad(false));
132 animatorPanel = new Animator(gljPanel);
133 gljPanel.setPreferredSize(new Dimension(300, 300));
134 panel.add(gljPanel);
135
136 final GLCanvas glCanvas = new GLCanvas();
137 glCanvas.addGLEventListener(new JOGLQuad(true));
138 animatorCanvas = new Animator(glCanvas);
139 glCanvas.setPreferredSize(new Dimension(300, 300));
140 panel.add(glCanvas);
141 }
142
143 @Override
144 public void start() {
145
146 animatorCanvas.start();
147 animatorCanvas.setUpdateFPSFrames(60, System.err);
148 animatorPanel.start();
149 animatorPanel.setUpdateFPSFrames(60, System.err);
150 }
151
152 @Override
153 public void stop() {
154
155 animatorCanvas.stop();
156 animatorPanel.stop();
157 }
158
159 @Override
160 public void destroy() {}
161
162 /**
163 * Self-contained example (within a single class only to keep it simple) displaying a rotating quad
164 */
165 static class JOGLQuad implements GLEventListener {
166
167 private static final float[] VERTEX_DATA = {
168 -1.0f, 1.0f, 0.0f, // Top Left
169 1.0f, 1.0f, 0.0f, // Top Right
170 1.0f, -1.0f, 0.0f, // Bottom Right
171 -1.0f, -1.0f, 0.0f // Bottom Left
172 };
173
174 private static final float[] TEXCOORD_DATA = {
175 0.0f, 1.0f, // Top Left
176 1.0f, 1.0f, // Top Right
177 1.0f, 0.0f, // Bottom Right
178 0.0f, 0.0f // Bottom Left
179 };
180
181 private final FloatBuffer vertexBuf;
182
183 private final FloatBuffer texCoordBuf;
184
185 private int vertexVBO;
186
187 private int texCoordVBO;
188
189 private float rotateT = 0.0f;
190
191 private final boolean canvas;
192
193 private Texture texture;
194
195 JOGLQuad(final boolean canvas) {
196
197 this.canvas = canvas;
198
199 ByteBuffer bb = ByteBuffer.allocateDirect(VERTEX_DATA.length * 4);
200 bb.order(ByteOrder.nativeOrder());
201 vertexBuf = bb.asFloatBuffer();
202 vertexBuf.put(VERTEX_DATA);
203 vertexBuf.rewind();
204
205 bb = ByteBuffer.allocateDirect(TEXCOORD_DATA.length * 4);
206 bb.order(ByteOrder.nativeOrder());
207 texCoordBuf = bb.asFloatBuffer();
208 texCoordBuf.put(TEXCOORD_DATA);
209 texCoordBuf.rewind();
210 }
211
212 @Override
213 public void init(final GLAutoDrawable glDrawable) {
214
215 final GL2 gl = glDrawable.getGL().getGL2();
216
217 System.err.println(VersionUtil.getPlatformInfo());
218 System.err.println(JoglVersion.getGLInfo(gl, null, false /* withCapabilitiesAndExtensionInfo */).toString());
219
220 gl.glShadeModel(GLLightingFunc.GL_SMOOTH);
221 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
222 gl.glClearDepth(1.0f);
226
227 final int[] tmp = new int[2];
228 gl.glGenBuffers(tmp.length, tmp, 0);
229 vertexVBO = tmp[0];
230 texCoordVBO = tmp[1];
231
232 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexVBO);
233 gl.glBufferData(GL.GL_ARRAY_BUFFER, VERTEX_DATA.length * 4, vertexBuf, GL.GL_STATIC_DRAW);
234 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, texCoordVBO);
235 gl.glBufferData(GL.GL_ARRAY_BUFFER, TEXCOORD_DATA.length * 4, texCoordBuf, GL.GL_STATIC_DRAW);
237
238 try {
239 final InputStream stream = getClass().getClassLoader().getResourceAsStream("com/jogamp/opengl/test/junit/jogl/util/texture/test-ntscN_3-01-160x90-90pct-yuv444-base.jpg");
240 texture = TextureIO.newTexture(stream, true, TextureIO.JPG);
241 } catch (final Exception exc) {
242 exc.printStackTrace(System.err);
243 }
244 }
245
246 @Override
247 public void dispose(final GLAutoDrawable drawable) {
248
249 final GL2 gl = drawable.getGL().getGL2();
250 final int[] tmp = new int[] {vertexVBO, texCoordVBO};
251 gl.glGenBuffers(tmp.length, tmp, 0);
252 }
253
254 @Override
255 public void reshape(final GLAutoDrawable gLDrawable, final int x, final int y, final int width, final int height) {
256
257 final GL2 gl = gLDrawable.getGL().getGL2();
258 final float aspect = (float) width / (float) height;
259 gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
260 gl.glLoadIdentity();
261 final float fh = 0.5f;
262 final float fw = fh * aspect;
263 gl.glFrustumf(-fw, fw, -fh, fh, 1.0f, 1000.0f);
264 gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
265 gl.glLoadIdentity();
266 }
267
268 @Override
269 public void display(final GLAutoDrawable gLDrawable) {
270
271 final GL2 gl = gLDrawable.getGL().getGL2();
272
273 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
274 gl.glLoadIdentity();
275 gl.glTranslatef(0.0f, 0.0f, -5.0f);
276
277 // rotate about the three axes
278 gl.glRotatef(rotateT, 1.0f, 0.0f, 0.0f);
279 gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);
280 gl.glRotatef(rotateT, 0.0f, 0.0f, 1.0f);
281
282 // set the color of the quad
283 if (canvas) {
284 gl.glColor3f(0.2f, 0.2f, 1.0f);
285 } else {
286 gl.glColor3f(1.0f, 0.2f, 0.2f);
287 }
288
289 if (texture != null) {
290 texture.bind(gl);
291 texture.enable(gl);
292 } else {
293 System.err.println("no texture");
294 }
295
296 // Draw A Quad
297 gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
298 gl.glEnableClientState(GLPointerFunc.GL_TEXTURE_COORD_ARRAY);
299 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexVBO);
300 gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
301 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, texCoordVBO);
302 gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);
303 gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
304 gl.glDrawArrays(GL2GL3.GL_QUADS, 0, 4);
305 gl.glDisableClientState(GLPointerFunc.GL_TEXTURE_COORD_ARRAY);
306 gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
307
308 if (texture != null) {
309 texture.disable(gl);
310 }
311
312 // increasing rotation for the next iteration
313 rotateT += 0.2f;
314 }
315
316 }
317}
318
static StringBuilder getGLInfo(final GL gl, final StringBuilder sb)
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
A lightweight Swing component which provides OpenGL rendering support.
Definition: GLJPanel.java:189
void addGLEventListener(final GLEventListener listener)
Adds the given listener to the end of this drawable queue.
Definition: GLJPanel.java:989
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
Represents an OpenGL texture object.
Definition: Texture.java:173
void bind(final GL gl)
Binds this texture to the given GL context.
Definition: Texture.java:377
void disable(final GL gl)
Disables this texture's target (e.g., GL_TEXTURE_2D) in the given GL state.
Definition: Texture.java:357
void enable(final GL gl)
Enables this texture's target (e.g., GL_TEXTURE_2D) in the given GL context's state.
Definition: Texture.java:330
static final int GL_PERSPECTIVE_CORRECTION_HINT
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_PERSPECTIVE_CORRECTION_HINT" with expression '0x0C50',...
Definition: GL2ES1.java:124
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL getGL()
Casts this object to the GL interface.
void glClearDepth(double depth)
Aliased entrypoint of void {@native glClearDepth}(GLclampd depth); and void {@native glClearDepthf...
GL2 getGL2()
Casts this object to the GL2 interface.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void glGenBuffers(int n, IntBuffer buffers)
Entry point to C language function: void {@native glGenBuffers}(GLsizei n, GLuint * buffers) Part ...
static final int GL_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
Definition: GL.java:673
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
Definition: GL.java:43
void glHint(int target, int mode)
Entry point to C language function: void {@native glHint}(GLenum target, GLenum mode) Part of GL_E...
static final int GL_NICEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_NICEST" with expressi...
Definition: GL.java:801
void glDepthFunc(int func)
Entry point to C language function: void {@native glDepthFunc}(GLenum func) Part of GL_ES_VERSION_...
static final int GL_LEQUAL
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LEQUAL" with expressi...
Definition: GL.java:469
void glBindBuffer(int target, int buffer)
Entry point to C language function: void {@native glBindBuffer}(GLenum target, GLuint buffer) Part...
void glBufferData(int target, long size, Buffer data, int usage)
Entry point to C language function: void {@native glBufferData}(GLenum target, GLsizeiptr size,...
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Definition: GL.java:633
void glMatrixMode(int mode)
Sets the current matrix mode.