1package com.jogamp.opengl.test.bugs;
3import java.applet.Applet;
4import java.awt.BorderLayout;
6import java.awt.Dimension;
7import java.awt.EventQueue;
9import java.awt.GraphicsDevice;
10import java.awt.GraphicsEnvironment;
11import java.awt.Insets;
12import java.awt.Rectangle;
13import java.awt.event.WindowAdapter;
14import java.awt.event.WindowEvent;
15import java.nio.FloatBuffer;
16import java.util.concurrent.CountDownLatch;
17import java.util.concurrent.TimeUnit;
19import com.jogamp.opengl.GL;
20import com.jogamp.opengl.GL2ES2;
21import com.jogamp.opengl.GLAutoDrawable;
22import com.jogamp.opengl.GLCapabilities;
23import com.jogamp.opengl.GLContext;
24import com.jogamp.opengl.GLException;
25import com.jogamp.opengl.GLProfile;
26import com.jogamp.opengl.GLRunnable;
27import com.jogamp.opengl.GLUniformData;
28import com.jogamp.opengl.awt.GLCanvas;
29import com.jogamp.opengl.glu.GLU;
30import com.jogamp.common.os.Clock;
31import com.jogamp.common.util.InterruptSource;
32import com.jogamp.newt.awt.NewtCanvasAWT;
33import com.jogamp.newt.opengl.GLWindow;
34import com.jogamp.opengl.test.junit.jogl.demos.es2.LandscapeES2;
35import com.jogamp.opengl.util.GLArrayDataServer;
36import com.jogamp.opengl.util.glsl.ShaderCode;
37import com.jogamp.opengl.util.glsl.ShaderProgram;
38import com.jogamp.opengl.util.glsl.ShaderState;
49@SuppressWarnings(
"serial")
51 static public final int AWT = 0;
52 static public final int NEWT = 1;
54 static public final int APPLET_WIDTH = 500;
55 static public final int APPLET_HEIGHT = 290;
56 static public final int TARGET_FPS = 120;
57 static public final int TOOLKIT = NEWT;
58 static public final boolean MANUAL_FRAME_HANDLING =
true;
62 static private Frame frame;
67 private DrawRunnable drawRunnable;
73 private Thread thread;
75 private boolean doneInit =
false;
76 private boolean doneSetup =
false;
78 private final long frameRatePeriod = 1000000000L / TARGET_FPS;
79 private long millisOffset;
80 private int frameCount;
81 private float frameRate;
91 private int fcount = 0, lastm = 0;
92 private final int fint = 1;
96 setSize(APPLET_WIDTH, APPLET_HEIGHT);
97 setPreferredSize(
new Dimension(APPLET_WIDTH, APPLET_HEIGHT));
99 height = APPLET_HEIGHT;
104 thread =
new InterruptSource.Thread(
null,
this,
"Animation Thread");
113 final int NO_DELAYS_PER_YIELD = 15;
114 final int TIMEOUT_SECONDS = 2;
116 long beforeTime = Clock.currentNanos();
117 long overSleepTime = 0L;
119 millisOffset = System.currentTimeMillis();
121 while (Thread.currentThread() == thread) {
122 final CountDownLatch latch =
new CountDownLatch(1);
125 latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
126 }
catch (
final InterruptedException e) {
130 if (frameCount == 1) {
131 EventQueue.invokeLater(
new Runnable() {
134 requestFocusInWindow();
139 final long afterTime = Clock.currentNanos();
140 final long timeDiff = afterTime - beforeTime;
141 final long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
144 Thread.sleep(sleepTime / 1000000L, (
int) (sleepTime % 1000000L));
146 }
catch (
final InterruptedException ex) { }
147 overSleepTime = (Clock.currentNanos() - afterTime) - sleepTime;
151 if (noDelays > NO_DELAYS_PER_YIELD) {
156 beforeTime = Clock.currentNanos();
165 if (TOOLKIT == AWT) {
166 awtCanvas.
invoke(
true, drawRunnable);
167 }
else if (TOOLKIT == NEWT) {
168 newtWindow.
invoke(
true, drawRunnable);
176 private class DrawRunnable
implements GLRunnable {
177 private boolean notCurrent;
181 if (MANUAL_FRAME_HANDLING) {
182 makeContextCurrent();
190 checkGLErrors(drawable.
getGL());
192 if (MANUAL_FRAME_HANDLING) {
194 releaseCurrentContext();
200 private void makeContextCurrent() {
201 final int MAX_CONTEXT_GRAB_ATTEMPTS = 10;
207 int value = GLContext.CONTEXT_NOT_CURRENT;
212 System.out.println(
"Made context current");
213 }
catch (
final GLException gle) {
214 gle.printStackTrace();
217 if (attempt == MAX_CONTEXT_GRAB_ATTEMPTS) {
218 throw new RuntimeException(
"Failed to claim OpenGL context.");
223 }
catch (
final InterruptedException e) {
227 }
while (value == GLContext.CONTEXT_NOT_CURRENT);
231 private void swapBuffers() {
232 final GL gl = GLContext.getCurrentGL();
234 GLContext.getCurrent().getGLDrawable().swapBuffers();
237 private void releaseCurrentContext() {
241 System.out.println(
"Released context");
242 }
catch (
final GLException gle) {
243 gle.printStackTrace();
249 private void initGL() {
250 final GLProfile profile = GLProfile.getDefault();
251 final GLCapabilities caps =
new GLCapabilities(profile);
252 caps.setBackgroundOpaque(
true);
253 caps.setOnscreen(
true);
254 caps.setSampleBuffers(
false);
256 if (TOOLKIT == AWT) {
257 awtCanvas =
new GLCanvas(caps);
258 awtCanvas.setBounds(0, 0, applet.width, applet.height);
259 awtCanvas.setBackground(
new Color(0xFFCCCCCC,
true));
260 awtCanvas.setFocusable(
true);
262 applet.setLayout(
new BorderLayout());
263 applet.add(awtCanvas, BorderLayout.CENTER);
265 if (MANUAL_FRAME_HANDLING) {
266 awtCanvas.setIgnoreRepaint(
true);
269 }
else if (TOOLKIT == NEWT) {
270 newtWindow = GLWindow.
create(caps);
271 newtCanvas =
new NewtCanvasAWT(newtWindow);
272 newtCanvas.setBounds(0, 0, applet.width, applet.height);
273 newtCanvas.setBackground(
new Color(0xFFCCCCCC,
true));
274 newtCanvas.setFocusable(
true);
276 applet.setLayout(
new BorderLayout());
277 applet.add(newtCanvas, BorderLayout.CENTER);
279 if (MANUAL_FRAME_HANDLING) {
280 newtCanvas.setIgnoreRepaint(
true);
286 private void initDraw() {
287 if (TOOLKIT == AWT) {
288 awtCanvas.setVisible(
true);
293 awtCanvas.requestFocus();
296 }
else if (TOOLKIT == NEWT) {
297 newtCanvas.setVisible(
true);
302 newtCanvas.requestFocus();
307 drawRunnable =
new DrawRunnable();
312 private void setup(
final GL2ES2 gl) {
313 if (60 < TARGET_FPS) {
315 gl.setSwapInterval(0);
319 vertShader = ShaderCode.
create(gl, GL2ES2.GL_VERTEX_SHADER, LandscapeES2.class,
"shader",
"shader/bin",
"landscape",
true);
320 fragShader = ShaderCode.
create(gl, GL2ES2.GL_FRAGMENT_SHADER, LandscapeES2.class,
"shader",
"shader/bin",
"landscape",
true);
323 shaderProg =
new ShaderProgram();
324 shaderProg.
add(gl, vertShader, System.err);
325 shaderProg.
add(gl, fragShader, System.err);
327 shaderState =
new ShaderState();
330 resolution =
new GLUniformData(
"iResolution", 3, FloatBuffer.wrap(
new float[] {width, height, 0}));
332 shaderState.
uniform(gl, resolution);
334 time =
new GLUniformData(
"iGlobalTime", 0.0f);
337 vertices = GLArrayDataServer.
createGLSL(
"inVertex", 2, GL.GL_FLOAT,
false, 4, GL.GL_STATIC_DRAW);
338 vertices.
putf(-1.0f); vertices.
putf(-1.0f);
339 vertices.
putf(+1.0f); vertices.
putf(-1.0f);
340 vertices.
putf(-1.0f); vertices.
putf(+1.0f);
341 vertices.
putf(+1.0f); vertices.
putf(+1.0f);
342 vertices.
seal(gl,
true);
349 private void draw(
final GL2ES2 gl) {
355 time.
setData((System.currentTimeMillis() - millisOffset) / 1000.0f);
358 gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4);
366 final int m = (int) (System.currentTimeMillis() - millisOffset);
367 if (m - lastm > 1000 * fint) {
368 frameRate = (float)(fcount) / fint;
372 if (frameCount % TARGET_FPS == 0) {
373 System.out.println(
"FrameCount: " + frameCount +
" - " +
374 "FrameRate: " + frameRate);
378 private void checkGLErrors(
final GL gl) {
379 final int err = gl.glGetError();
382 System.out.println(errString);
386 static public void main(
final String[] args) {
387 final GraphicsEnvironment environment =
388 GraphicsEnvironment.getLocalGraphicsEnvironment();
389 final GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
391 frame =
new Frame(displayDevice.getDefaultConfiguration());
392 frame.setBackground(
new Color(0xCC, 0xCC, 0xCC));
393 frame.setTitle(
"TestBug735Inv0AppletAWT");
396 final Class<?> c = Thread.currentThread().getContextClassLoader().
399 }
catch (
final Exception e) {
400 throw new RuntimeException(e);
403 frame.setLayout(
null);
406 frame.setResizable(
false);
410 final Insets insets = frame.getInsets();
411 final int windowW = applet.width + insets.left + insets.right;
412 final int windowH = applet.height + insets.top + insets.bottom;
413 frame.setSize(windowW, windowH);
415 final Rectangle screenRect = displayDevice.getDefaultConfiguration().getBounds();
416 frame.setLocation(screenRect.x + (screenRect.width - applet.width) / 2,
417 screenRect.y + (screenRect.height - applet.height) / 2);
419 final int usableWindowH = windowH - insets.top - insets.bottom;
420 applet.setBounds((windowW - applet.width)/2,
421 insets.top + (usableWindowH - applet.height)/2,
422 applet.width, applet.height);
425 frame.addWindowListener(
new WindowAdapter() {
427 public void windowClosing(
final WindowEvent e) {
433 frame.setVisible(
true);
AWT Canvas containing a NEWT Window using native parenting.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Abstraction for an OpenGL rendering context.
abstract int makeCurrent()
Makes this GLContext current on the calling thread.
abstract void release()
Releases control of this GLContext from the current thread.
final boolean isCurrent()
A heavyweight AWT component which provides OpenGL rendering support.
GLContext getContext()
Returns the context associated with this drawable.
void setAutoSwapBufferMode(final boolean onOrOff)
Enables or disables automatic buffer swapping for this drawable.
final GLDrawable getDelegatedDrawable()
If the implementation uses delegation, return the delegated GLDrawable instance, otherwise return thi...
boolean invoke(final boolean wait, final GLRunnable glRunnable)
Enqueues a one-shot GLRunnable, which will be executed within the next display() call after all regis...
Provides access to the OpenGL Utility Library (GLU).
final String gluErrorString(int errorCode)
static void main(final String[] args)
void requestDraw(final CountDownLatch latch)
void seal(final GL gl, final boolean seal)
Convenience method calling seal(boolean) and enableBuffer(GL, boolean).
void enableBuffer(final GL gl, final boolean enable)
Enables the buffer if enable is true, and transfers the data if required.
static GLArrayDataServer createGLSL(final String name, final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO, using a custom GLSL array attribute name and starting with a new created Buffer object ...
Convenient shader code class to use and instantiate vertex or fragment programs.
final int defaultShaderCustomization(final GL2ES2 gl, final boolean preludeVersion, final boolean addDefaultPrecision)
Default customization of this shader source code.
static ShaderCode create(final GL2ES2 gl, final int type, final int count, final Class<?> context, final String[] sourceFiles, final boolean mutableStringBuilder)
Creates a complete ShaderCode object while reading all shader source of sourceFiles,...
synchronized void add(final ShaderCode shaderCode)
Adds a new shader to this program.
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
void ownAttribute(final GLArrayData attribute, final boolean own)
Binds or unbinds the GLArrayData lifecycle to this ShaderState.
synchronized void useProgram(final GL2ES2 gl, final boolean on)
Turns the shader program on or off.
synchronized boolean attachShaderProgram(final GL2ES2 gl, final ShaderProgram prog, final boolean enable)
Attach or switch a shader program.
boolean uniform(final GL2ES2 gl, final GLUniformData data)
Set the uniform data, if it's location is valid, i.e.
void ownUniform(final GLUniformData uniform)
Bind the GLUniform lifecycle to this ShaderState.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
boolean invoke(boolean wait, GLRunnable glRunnable)
Enqueues a one-shot GLRunnable, which will be executed within the next display() call after all regis...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GLContext getContext()
Returns the context associated with this drawable.
void setAutoSwapBufferMode(boolean enable)
Enables or disables automatic buffer swapping for this drawable.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
boolean isRealized()
Returns true if this drawable is realized, otherwise false.