aboutsummaryrefslogtreecommitdiffstats
path: root/ardor3d-jogl
diff options
context:
space:
mode:
Diffstat (limited to 'ardor3d-jogl')
-rw-r--r--ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglAwtCanvas.java28
-rw-r--r--ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java142
-rw-r--r--ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglInitializerRunnable.java42
3 files changed, 119 insertions, 93 deletions
diff --git a/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglAwtCanvas.java b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglAwtCanvas.java
index df55d06..58fef71 100644
--- a/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglAwtCanvas.java
+++ b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglAwtCanvas.java
@@ -13,8 +13,6 @@ package com.ardor3d.framework.jogl;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.CountDownLatch;
-import javax.media.opengl.GLAutoDrawable;
-import javax.media.opengl.GLRunnable;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.SwingUtilities;
@@ -38,9 +36,12 @@ public class JoglAwtCanvas extends GLCanvas implements Canvas {
private final JoglDrawerRunnable _drawerGLRunnable;
+ private final JoglInitializerRunnable _initializerRunnable;
+
public JoglAwtCanvas(final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer) {
super(CapsUtil.getCapsForSettings(settings));
_drawerGLRunnable = new JoglDrawerRunnable(canvasRenderer);
+ _initializerRunnable = new JoglInitializerRunnable(this, settings);
_settings = settings;
_canvasRenderer = canvasRenderer;
@@ -60,35 +61,16 @@ public class JoglAwtCanvas extends GLCanvas implements Canvas {
// Calling setVisible(true) on the GLCanvas not from the AWT-EDT can freeze the Intel GPU under Windows
if (!SwingUtilities.isEventDispatchThread()) {
try {
- SwingUtilities.invokeAndWait(new Runnable() {
- @Override
- public void run() {
- // Make the window visible to realize the OpenGL surface.
- setVisible(true);
- }
- });
+ SwingUtilities.invokeAndWait(_initializerRunnable);
} catch (final InterruptedException ex) {
ex.printStackTrace();
} catch (final InvocationTargetException ex) {
ex.printStackTrace();
}
} else {
- // Make the window visible to realize the OpenGL surface.
- setVisible(true);
+ _initializerRunnable.run();
}
- // Request the focus here as it cannot work when the window is not visible
- requestFocus();
-
- _canvasRenderer.setContext(getContext());
-
- invoke(true, new GLRunnable() {
- @Override
- public boolean run(final GLAutoDrawable glAutoDrawable) {
- _canvasRenderer.init(_settings, true);// true - do swap in renderer.
- return true;
- }
- });
_inited = true;
}
diff --git a/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java
index e6d8b00..ec5a60b 100644
--- a/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java
+++ b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglCanvasRenderer.java
@@ -27,11 +27,11 @@ import com.ardor3d.framework.Scene;
import com.ardor3d.math.ColorRGBA;
import com.ardor3d.math.Vector3;
import com.ardor3d.renderer.Camera;
+import com.ardor3d.renderer.Camera.ProjectionMode;
import com.ardor3d.renderer.ContextCapabilities;
import com.ardor3d.renderer.ContextManager;
import com.ardor3d.renderer.RenderContext;
import com.ardor3d.renderer.Renderer;
-import com.ardor3d.renderer.Camera.ProjectionMode;
import com.ardor3d.renderer.jogl.JoglContextCapabilities;
import com.ardor3d.renderer.jogl.JoglRenderer;
import com.ardor3d.util.Ardor3dException;
@@ -72,42 +72,43 @@ public class JoglCanvasRenderer implements CanvasRenderer {
int value = GLContext.CONTEXT_NOT_CURRENT;
int attempt = 0;
do {
- try {
- value = _context.makeCurrent();
- } catch(GLException gle) {
- gle.printStackTrace();
- } finally {
- attempt++;
- if (attempt == MAX_CONTEXT_GRAB_ATTEMPTS) {
+ try {
+ value = _context.makeCurrent();
+ } catch (final GLException gle) {
+ gle.printStackTrace();
+ } finally {
+ attempt++;
+ if (attempt == MAX_CONTEXT_GRAB_ATTEMPTS) {
// failed, throw exception
throw new Ardor3dException("Failed to claim OpenGL context.");
}
- }
- try {
- Thread.sleep(5);
- } catch (final InterruptedException e1) {
- e1.printStackTrace();
- }
- }
- while(value == GLContext.CONTEXT_NOT_CURRENT);
- if (value == GLContext.CONTEXT_CURRENT_NEW) {
- ContextManager.getCurrentContext().contextLost();
+ }
+ try {
+ Thread.sleep(5);
+ } catch (final InterruptedException e1) {
+ e1.printStackTrace();
+ }
+ } while (value == GLContext.CONTEXT_NOT_CURRENT);
+ if (ContextManager.getCurrentContext() != null) {
+ if (value == GLContext.CONTEXT_CURRENT_NEW) {
+ ContextManager.getCurrentContext().contextLost();
+
+ // Whenever the context is created or replaced, the GL chain
+ // is lost. Debug will have to be added if desired.
+ _debugEnabled = false;
+ }
- // Whenever the context is created or replaced, the GL chain
- // is lost. Debug will have to be added if desired.
- _debugEnabled = false;
+ ContextManager.switchContext(_context);
}
-
- ContextManager.switchContext(_context);
}
public void releaseCurrentContext() {
- if (_context.equals(GLContext.getCurrent())) {
+ if (_context.equals(GLContext.getCurrent())) {
try {
- _context.release();
- } catch(GLException gle) {
- gle.printStackTrace();
- }
+ _context.release();
+ } catch (final GLException gle) {
+ gle.printStackTrace();
+ }
}
}
@@ -123,52 +124,53 @@ public class JoglCanvasRenderer implements CanvasRenderer {
_context = GLDrawableFactory.getFactory(GLProfile.getMaxFixedFunc(true)).createExternalGLContext();
}
- _context.makeCurrent();
+ makeCurrentContext();
try {
- // Look up a shared context, if a shared JoglCanvasRenderer is given.
- RenderContext sharedContext = null;
- if (settings.getShareContext() != null) {
- sharedContext = ContextManager.getContextForKey(settings.getShareContext().getRenderContext()
- .getContextKey());
- }
-
- final ContextCapabilities caps = createContextCapabilities();
- _currentContext = new RenderContext(_context, caps, sharedContext);
-
- ContextManager.addContext(_context, _currentContext);
- ContextManager.switchContext(_context);
-
- _renderer = new JoglRenderer();
-
- if (settings.getSamples() != 0 && caps.isMultisampleSupported()) {
- final GL gl = GLU.getCurrentGL();
- gl.glEnable(GL.GL_MULTISAMPLE);
- }
-
- _renderer.setBackgroundColor(ColorRGBA.BLACK);
-
- if (_camera == null) {
- /** Set up how our camera sees. */
- _camera = new Camera(settings.getWidth(), settings.getHeight());
- _camera.setFrustumPerspective(45.0f, (float) settings.getWidth() / (float) settings.getHeight(), 1, 1000);
- _camera.setProjectionMode(ProjectionMode.Perspective);
-
- final Vector3 loc = new Vector3(0.0f, 0.0f, 10.0f);
- final Vector3 left = new Vector3(-1.0f, 0.0f, 0.0f);
- final Vector3 up = new Vector3(0.0f, 1.0f, 0.0f);
- final Vector3 dir = new Vector3(0.0f, 0f, -1.0f);
- /** Move our camera to a correct place and orientation. */
- _camera.setFrame(loc, left, up, dir);
- } else {
- // use new width and height to set ratio.
- _camera.setFrustumPerspective(_camera.getFovY(),
- (float) settings.getWidth() / (float) settings.getHeight(), _camera.getFrustumNear(), _camera
- .getFrustumFar());
- }
+ // Look up a shared context, if a shared JoglCanvasRenderer is given.
+ RenderContext sharedContext = null;
+ if (settings.getShareContext() != null) {
+ sharedContext = ContextManager.getContextForKey(settings.getShareContext().getRenderContext()
+ .getContextKey());
+ }
+
+ final ContextCapabilities caps = createContextCapabilities();
+ _currentContext = new RenderContext(_context, caps, sharedContext);
+
+ ContextManager.addContext(_context, _currentContext);
+ ContextManager.switchContext(_context);
+
+ _renderer = new JoglRenderer();
+
+ if (settings.getSamples() != 0 && caps.isMultisampleSupported()) {
+ final GL gl = GLU.getCurrentGL();
+ gl.glEnable(GL.GL_MULTISAMPLE);
+ }
+
+ _renderer.setBackgroundColor(ColorRGBA.BLACK);
+
+ if (_camera == null) {
+ /** Set up how our camera sees. */
+ _camera = new Camera(settings.getWidth(), settings.getHeight());
+ _camera.setFrustumPerspective(45.0f, (float) settings.getWidth() / (float) settings.getHeight(), 1,
+ 1000);
+ _camera.setProjectionMode(ProjectionMode.Perspective);
+
+ final Vector3 loc = new Vector3(0.0f, 0.0f, 10.0f);
+ final Vector3 left = new Vector3(-1.0f, 0.0f, 0.0f);
+ final Vector3 up = new Vector3(0.0f, 1.0f, 0.0f);
+ final Vector3 dir = new Vector3(0.0f, 0f, -1.0f);
+ /** Move our camera to a correct place and orientation. */
+ _camera.setFrame(loc, left, up, dir);
+ } else {
+ // use new width and height to set ratio.
+ _camera.setFrustumPerspective(_camera.getFovY(),
+ (float) settings.getWidth() / (float) settings.getHeight(), _camera.getFrustumNear(),
+ _camera.getFrustumFar());
+ }
} finally {
- _context.release();
+ releaseCurrentContext();
}
}
diff --git a/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglInitializerRunnable.java b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglInitializerRunnable.java
new file mode 100644
index 0000000..2a93f38
--- /dev/null
+++ b/ardor3d-jogl/src/main/java/com/ardor3d/framework/jogl/JoglInitializerRunnable.java
@@ -0,0 +1,42 @@
+/**
+ * Copyright (c) 2008-2010 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.framework.jogl;
+
+import com.ardor3d.framework.DisplaySettings;
+
+public class JoglInitializerRunnable implements Runnable {
+
+ private final JoglAwtCanvas _joglAwtCanvas;
+
+ private final DisplaySettings _settings;
+
+ public JoglInitializerRunnable(final JoglAwtCanvas joglAwtCanvas, final DisplaySettings settings) {
+ _joglAwtCanvas = joglAwtCanvas;
+ _settings = settings;
+ }
+
+ @Override
+ public void run() {
+ // Make the window visible to realize the OpenGL surface.
+ _joglAwtCanvas.setVisible(true);
+ // Force the realization
+ _joglAwtCanvas.display();
+ if (!_joglAwtCanvas.getDelegatedDrawable().isRealized()) {
+ throw new RuntimeException("The heavyweight AWT drawable cannot be realized");
+ }
+ // Request the focus here as it cannot work when the window is not visible
+ _joglAwtCanvas.requestFocus();
+ // The OpenGL context has been created after the realization of the surface
+ _joglAwtCanvas.getCanvasRenderer().setContext(_joglAwtCanvas.getContext());
+ // As the canvas renderer knows the OpenGL context, it can be initialized
+ _joglAwtCanvas.getCanvasRenderer().init(_settings, true);
+ }
+} \ No newline at end of file