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;
51@SuppressWarnings(
"serial")
53 static public final int AWT = 0;
54 static public final int NEWT = 1;
56 static public final int APPLET_WIDTH = 500;
57 static public final int APPLET_HEIGHT = 290;
58 static public final int TARGET_FPS = 120;
59 static public final int TOOLKIT = NEWT;
60 static public final boolean MANUAL_FRAME_HANDLING =
true;
64 static private Frame frame;
69 private DrawRunnable drawRunnable;
75 private Thread thread;
77 private boolean doneInit =
false;
78 private boolean doneSetup =
false;
80 private final long frameRatePeriod = 1000000000L / TARGET_FPS;
81 private long millisOffset;
82 private int frameCount;
83 private float frameRate;
93 private int fcount = 0, lastm = 0;
94 private final int fint = 1;
98 setSize(APPLET_WIDTH, APPLET_HEIGHT);
99 setPreferredSize(
new Dimension(APPLET_WIDTH, APPLET_HEIGHT));
100 width = APPLET_WIDTH;
101 height = APPLET_HEIGHT;
106 thread =
new InterruptSource.Thread(
null,
this,
"Animation Thread");
115 final int NO_DELAYS_PER_YIELD = 15;
116 final int TIMEOUT_SECONDS = 2;
118 long beforeTime = Clock.currentNanos();
119 long overSleepTime = 0L;
121 millisOffset = System.currentTimeMillis();
123 while (Thread.currentThread() == thread) {
124 final CountDownLatch latch =
new CountDownLatch(1);
127 latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
128 }
catch (
final InterruptedException e) {
132 if (frameCount == 1) {
133 EventQueue.invokeLater(
new Runnable() {
136 requestFocusInWindow();
141 final long afterTime = Clock.currentNanos();
142 final long timeDiff = afterTime - beforeTime;
143 final long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
146 Thread.sleep(sleepTime / 1000000L, (
int) (sleepTime % 1000000L));
148 }
catch (
final InterruptedException ex) { }
149 overSleepTime = (Clock.currentNanos() - afterTime) - sleepTime;
153 if (noDelays > NO_DELAYS_PER_YIELD) {
158 beforeTime = Clock.currentNanos();
167 if (TOOLKIT == AWT) {
168 awtCanvas.
invoke(
true, drawRunnable);
169 }
else if (TOOLKIT == NEWT) {
170 newtWindow.
invoke(
true, drawRunnable);
178 private class DrawRunnable
implements GLRunnable {
179 private boolean notCurrent;
183 if (MANUAL_FRAME_HANDLING) {
191 checkGLErrors(drawable.
getGL());
193 if (MANUAL_FRAME_HANDLING) {
201 private void makeContextCurrent(
final GLContext context) {
202 final int MAX_CONTEXT_GRAB_ATTEMPTS = 10;
204 if (context.isCurrent()) {
208 int value = GLContext.CONTEXT_NOT_CURRENT;
212 value = context.makeCurrent();
213 System.out.println(
"Made context current");
214 }
catch (
final GLException gle) {
215 gle.printStackTrace();
218 if (attempt == MAX_CONTEXT_GRAB_ATTEMPTS) {
219 throw new RuntimeException(
"Failed to claim OpenGL context.");
224 }
catch (
final InterruptedException e) {
228 }
while (value == GLContext.CONTEXT_NOT_CURRENT);
232 private void swapBuffers(
final GLContext context) {
233 final GL gl = context.getGL();
235 context.getGLDrawable().swapBuffers();
238 private void releaseCurrentContext(
final GLContext context) {
242 System.out.println(
"Released context");
243 }
catch (
final GLException gle) {
244 gle.printStackTrace();
250 private void initGL() {
251 final GLProfile profile = GLProfile.getDefault();
252 final GLCapabilities caps =
new GLCapabilities(profile);
253 caps.setBackgroundOpaque(
true);
254 caps.setOnscreen(
true);
255 caps.setSampleBuffers(
false);
257 if (TOOLKIT == AWT) {
258 awtCanvas =
new GLCanvas(caps);
259 awtCanvas.setBounds(0, 0, applet.width, applet.height);
260 awtCanvas.setBackground(
new Color(0xFFCCCCCC,
true));
261 awtCanvas.setFocusable(
true);
263 applet.setLayout(
new BorderLayout());
264 applet.add(awtCanvas, BorderLayout.CENTER);
266 if (MANUAL_FRAME_HANDLING) {
267 awtCanvas.setIgnoreRepaint(
true);
270 }
else if (TOOLKIT == NEWT) {
271 newtWindow = GLWindow.
create(caps);
272 newtCanvas =
new NewtCanvasAWT(newtWindow);
273 newtCanvas.setBounds(0, 0, applet.width, applet.height);
274 newtCanvas.setBackground(
new Color(0xFFCCCCCC,
true));
275 newtCanvas.setFocusable(
true);
277 applet.setLayout(
new BorderLayout());
278 applet.add(newtCanvas, BorderLayout.CENTER);
280 if (MANUAL_FRAME_HANDLING) {
281 newtCanvas.setIgnoreRepaint(
true);
287 private void initDraw() {
288 if (TOOLKIT == AWT) {
289 awtCanvas.setVisible(
true);
294 awtCanvas.requestFocus();
296 }
else if (TOOLKIT == NEWT) {
297 newtCanvas.setVisible(
true);
302 newtCanvas.requestFocus();
306 drawRunnable =
new DrawRunnable();
311 private void setup(
final GL2ES2 gl) {
312 if (60 < TARGET_FPS) {
314 gl.setSwapInterval(0);
318 vertShader = ShaderCode.
create(gl, GL2ES2.GL_VERTEX_SHADER, LandscapeES2.class,
"shader",
"shader/bin",
"landscape",
true);
319 fragShader = ShaderCode.
create(gl, GL2ES2.GL_FRAGMENT_SHADER, LandscapeES2.class,
"shader",
"shader/bin",
"landscape",
true);
322 shaderProg =
new ShaderProgram();
323 shaderProg.
add(gl, vertShader, System.err);
324 shaderProg.
add(gl, fragShader, System.err);
326 shaderState =
new ShaderState();
329 resolution =
new GLUniformData(
"iResolution", 3, FloatBuffer.wrap(
new float[] {width, height, 0}));
331 shaderState.
uniform(gl, resolution);
333 time =
new GLUniformData(
"iGlobalTime", 0.0f);
336 vertices = GLArrayDataServer.
createGLSL(
"inVertex", 2, GL.GL_FLOAT,
false, 4, GL.GL_STATIC_DRAW);
337 vertices.
putf(-1.0f); vertices.
putf(-1.0f);
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.
seal(gl,
true);
348 private void draw(
final GL2ES2 gl) {
354 time.
setData((System.currentTimeMillis() - millisOffset) / 1000.0f);
357 gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4);
365 final int m = (int) (System.currentTimeMillis() - millisOffset);
366 if (m - lastm > 1000 * fint) {
367 frameRate = (float)(fcount) / fint;
371 if (frameCount % TARGET_FPS == 0) {
372 System.out.println(
"FrameCount: " + frameCount +
" - " +
373 "FrameRate: " + frameRate);
377 private void checkGLErrors(
final GL gl) {
378 final int err = gl.glGetError();
381 System.out.println(errString);
385 static public void main(
final String[] args) {
386 final GraphicsEnvironment environment =
387 GraphicsEnvironment.getLocalGraphicsEnvironment();
388 final GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
390 frame =
new Frame(displayDevice.getDefaultConfiguration());
391 frame.setBackground(
new Color(0xCC, 0xCC, 0xCC));
392 frame.setTitle(
"TestBug735Inv1AppletAWT");
395 final Class<?> c = Thread.currentThread().getContextClassLoader().
398 }
catch (
final Exception e) {
399 throw new RuntimeException(e);
402 frame.setLayout(
null);
405 frame.setResizable(
false);
409 final Insets insets = frame.getInsets();
410 final int windowW = applet.width + insets.left + insets.right;
411 final int windowH = applet.height + insets.top + insets.bottom;
412 frame.setSize(windowW, windowH);
414 final Rectangle screenRect = displayDevice.getDefaultConfiguration().getBounds();
415 frame.setLocation(screenRect.x + (screenRect.width - applet.width) / 2,
416 screenRect.y + (screenRect.height - applet.height) / 2);
418 final int usableWindowH = windowH - insets.top - insets.bottom;
419 applet.setBounds((windowW - applet.width)/2,
420 insets.top + (usableWindowH - applet.height)/2,
421 applet.width, applet.height);
424 frame.addWindowListener(
new WindowAdapter() {
426 public void windowClosing(
final WindowEvent e) {
432 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...
A heavyweight AWT component which provides OpenGL rendering support.
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.