diff options
Diffstat (limited to 'ardor3d-jogl-awt')
10 files changed, 918 insertions, 0 deletions
diff --git a/ardor3d-jogl-awt/.settings/org.eclipse.jdt.core.prefs b/ardor3d-jogl-awt/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..52f1f2e --- /dev/null +++ b/ardor3d-jogl-awt/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,15 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.codeComplete.argumentPrefixes= +org.eclipse.jdt.core.codeComplete.argumentSuffixes= +org.eclipse.jdt.core.codeComplete.fieldPrefixes= +org.eclipse.jdt.core.codeComplete.fieldSuffixes= +org.eclipse.jdt.core.codeComplete.localPrefixes= +org.eclipse.jdt.core.codeComplete.localSuffixes= +org.eclipse.jdt.core.codeComplete.staticFieldPrefixes= +org.eclipse.jdt.core.codeComplete.staticFieldSuffixes= +org.eclipse.jdt.core.codeComplete.staticFinalFieldPrefixes= +org.eclipse.jdt.core.codeComplete.staticFinalFieldSuffixes= +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/ardor3d-jogl-awt/.settings/org.eclipse.jdt.ui.prefs b/ardor3d-jogl-awt/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000..9cac248 --- /dev/null +++ b/ardor3d-jogl-awt/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,113 @@ +cleanup.add_default_serial_version_id=true +cleanup.add_generated_serial_version_id=false +cleanup.add_missing_annotations=true +cleanup.add_missing_deprecated_annotations=true +cleanup.add_missing_methods=true +cleanup.add_missing_nls_tags=false +cleanup.add_missing_override_annotations=true +cleanup.add_serial_version_id=true +cleanup.always_use_blocks=true +cleanup.always_use_parentheses_in_expressions=true +cleanup.always_use_this_for_non_static_field_access=false +cleanup.always_use_this_for_non_static_method_access=false +cleanup.convert_to_enhanced_for_loop=false +cleanup.correct_indentation=true +cleanup.format_source_code=true +cleanup.format_source_code_changes_only=false +cleanup.make_local_variable_final=true +cleanup.make_parameters_final=true +cleanup.make_private_fields_final=true +cleanup.make_type_abstract_if_missing_method=false +cleanup.make_variable_declarations_final=true +cleanup.never_use_blocks=false +cleanup.never_use_parentheses_in_expressions=false +cleanup.organize_imports=true +cleanup.qualify_static_field_accesses_with_declaring_class=false +cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +cleanup.qualify_static_member_accesses_with_declaring_class=true +cleanup.qualify_static_method_accesses_with_declaring_class=false +cleanup.remove_private_constructors=true +cleanup.remove_trailing_whitespaces=true +cleanup.remove_trailing_whitespaces_all=true +cleanup.remove_trailing_whitespaces_ignore_empty=false +cleanup.remove_unnecessary_casts=true +cleanup.remove_unnecessary_nls_tags=true +cleanup.remove_unused_imports=true +cleanup.remove_unused_local_variables=false +cleanup.remove_unused_private_fields=true +cleanup.remove_unused_private_members=false +cleanup.remove_unused_private_methods=true +cleanup.remove_unused_private_types=true +cleanup.sort_members=false +cleanup.sort_members_all=false +cleanup.use_blocks=true +cleanup.use_blocks_only_for_return_and_throw=false +cleanup.use_parentheses_in_expressions=false +cleanup.use_this_for_non_static_field_access=true +cleanup.use_this_for_non_static_field_access_only_if_necessary=true +cleanup.use_this_for_non_static_method_access=true +cleanup.use_this_for_non_static_method_access_only_if_necessary=true +cleanup_profile=_ArdorLabs +cleanup_settings_version=2 +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +formatter_profile=_ArdorLabs +formatter_settings_version=11 +org.eclipse.jdt.ui.exception.name=ex +org.eclipse.jdt.ui.gettersetter.use.is=true +org.eclipse.jdt.ui.javadoc=false +org.eclipse.jdt.ui.keywordthis=false +org.eclipse.jdt.ui.overrideannotation=true +org.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?><templates><template autoinsert\="true" context\="gettercomment_context" deleted\="false" description\="Comment for getter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name\="gettercomment">/**\r\n * @return the ${bare_field_name}\r\n */</template><template autoinsert\="true" context\="settercomment_context" deleted\="false" description\="Comment for setter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.settercomment" name\="settercomment">/**\r\n * @param ${param} the ${bare_field_name} to set\r\n */</template><template autoinsert\="true" context\="constructorcomment_context" deleted\="false" description\="Comment for created constructors" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name\="constructorcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="filecomment_context" deleted\="false" description\="Comment for created Java files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.filecomment" name\="filecomment">/**\r\n * \r\n */</template><template autoinsert\="false" context\="typecomment_context" deleted\="false" description\="Comment for created types" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.typecomment" name\="typecomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="true" context\="fieldcomment_context" deleted\="false" description\="Comment for fields" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name\="fieldcomment">/**\r\n * \r\n */</template><template autoinsert\="true" context\="methodcomment_context" deleted\="false" description\="Comment for non-overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name\="methodcomment">/**\r\n * ${tags}\r\n */</template><template autoinsert\="false" context\="overridecomment_context" deleted\="false" description\="Comment for overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name\="overridecomment"/><template autoinsert\="true" context\="delegatecomment_context" deleted\="false" description\="Comment for delegate methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name\="delegatecomment">/**\r\n * ${tags}\r\n * ${see_to_target}\r\n */</template><template autoinsert\="false" context\="newtype_context" deleted\="false" description\="Newly created files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.newtype" name\="newtype">/**\r\n * Copyright (c) 2008-2014 Ardor Labs, Inc.\r\n *\r\n * This file is part of Ardor3D.\r\n *\r\n * Ardor3D is free software\: you can redistribute it and/or modify it \r\n * under the terms of its license which may be found in the accompanying\r\n * LICENSE file or at <http\://www.ardor3d.com/LICENSE>.\r\n */\r\n\r\n${filecomment}\r\n${package_declaration}\r\n\r\n${typecomment}\r\n${type_declaration}</template><template autoinsert\="true" context\="classbody_context" deleted\="false" description\="Code in new class type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.classbody" name\="classbody">\r\n</template><template autoinsert\="true" context\="interfacebody_context" deleted\="false" description\="Code in new interface type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.interfacebody" name\="interfacebody">\r\n</template><template autoinsert\="true" context\="enumbody_context" deleted\="false" description\="Code in new enum type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.enumbody" name\="enumbody">\r\n</template><template autoinsert\="true" context\="annotationbody_context" deleted\="false" description\="Code in new annotation type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.annotationbody" name\="annotationbody">\r\n</template><template autoinsert\="true" context\="catchblock_context" deleted\="false" description\="Code in new catch blocks" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.catchblock" name\="catchblock">// ${todo} Auto-generated catch block\r\n${exception_var}.printStackTrace();</template><template autoinsert\="true" context\="methodbody_context" deleted\="false" description\="Code in created method stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodbody" name\="methodbody">// ${todo} Auto-generated method stub\r\n${body_statement}</template><template autoinsert\="true" context\="constructorbody_context" deleted\="false" description\="Code in created constructor stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name\="constructorbody">${body_statement}\r\n// ${todo} Auto-generated constructor stub</template><template autoinsert\="true" context\="getterbody_context" deleted\="false" description\="Code in created getters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.getterbody" name\="getterbody">return ${field};</template><template autoinsert\="true" context\="setterbody_context" deleted\="false" description\="Code in created setters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.setterbody" name\="setterbody">${field} \= ${param};</template></templates> +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=false +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.correct_indentation=true +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=false +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=true +sp_cleanup.make_private_fields_final=true +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=true +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=false +sp_cleanup.on_save_use_additional_actions=true +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=true +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_trailing_whitespaces=true +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=true +sp_cleanup.remove_unused_imports=true +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_blocks=true +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=true +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=true +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/ardor3d-jogl-awt/build.gradle b/ardor3d-jogl-awt/build.gradle new file mode 100644 index 0000000..c443fa6 --- /dev/null +++ b/ardor3d-jogl-awt/build.gradle @@ -0,0 +1,7 @@ + +description = 'Ardor 3D JOGL AWT' +dependencies { + compile project(':ardor3d-core') + compile project(':ardor3d-awt') + compile project(':ardor3d-jogl') +} diff --git a/ardor3d-jogl-awt/pom.xml b/ardor3d-jogl-awt/pom.xml new file mode 100644 index 0000000..d76a750 --- /dev/null +++ b/ardor3d-jogl-awt/pom.xml @@ -0,0 +1,32 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>com.ardor3d</groupId> + <artifactId>ardor3d</artifactId> + <relativePath>../pom.xml</relativePath> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>ardor3d-jogl-awt</artifactId> + <packaging>bundle</packaging> + <name>Ardor 3D JOGL AWT</name> + + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>ardor3d-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>ardor3d-awt</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>ardor3d-jogl</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + +</project> diff --git a/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtCanvas.java b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtCanvas.java new file mode 100644 index 0000000..c92b275 --- /dev/null +++ b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtCanvas.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2008-2014 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.awt; + +import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.CountDownLatch; + +import com.jogamp.nativewindow.ScalableSurface; +import com.jogamp.opengl.GLAutoDrawable; +import com.jogamp.opengl.GLRunnable; +import com.jogamp.opengl.awt.GLCanvas; + +import javax.swing.SwingUtilities; + +import com.ardor3d.annotation.MainThread; +import com.ardor3d.framework.Canvas; +import com.ardor3d.framework.DisplaySettings; +import com.ardor3d.framework.jogl.CapsUtil; +import com.ardor3d.framework.jogl.JoglCanvasRenderer; +import com.ardor3d.framework.jogl.JoglDrawerRunnable; + +/** + * Ardor3D JOGL AWT heavyweight canvas, AWT component for the OpenGL rendering of Ardor3D with JOGL that supports the + * AWT input system directly and its abstraction in Ardor3D (com.ardor3d.input.awt) + * + */ +public class JoglAwtCanvas extends GLCanvas implements Canvas { + + private static final long serialVersionUID = 1L; + + private final JoglCanvasRenderer _canvasRenderer; + private boolean _inited = false; + + private final DisplaySettings _settings; + + private final JoglDrawerRunnable _drawerGLRunnable; + + private final JoglAwtInitializerRunnable _initializerRunnable; + + public JoglAwtCanvas(final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer) { + this(settings, canvasRenderer, new CapsUtil()); + } + + public JoglAwtCanvas(final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer, + final CapsUtil capsUtil) { + super(capsUtil.getCapsForSettings(settings)); + _drawerGLRunnable = new JoglDrawerRunnable(canvasRenderer); + _initializerRunnable = new JoglAwtInitializerRunnable(this, settings); + _settings = settings; + _canvasRenderer = canvasRenderer; + + setFocusable(true); + requestFocus(); + setSize(_settings.getWidth(), _settings.getHeight()); + setIgnoreRepaint(true); + setAutoSwapBufferMode(false); + // disables HiDPI, see https://github.com/gouessej/Ardor3D/issues/14 + setSurfaceScale(new float[] { ScalableSurface.IDENTITY_PIXELSCALE, + ScalableSurface.IDENTITY_PIXELSCALE }); + } + + @Override + @MainThread + public void init() { + if (_inited) { + return; + } + + // Calling setVisible(true) on the GLCanvas not from the AWT-EDT can freeze the Intel GPU under Windows + if (!SwingUtilities.isEventDispatchThread()) { + try { + SwingUtilities.invokeAndWait(_initializerRunnable); + } catch (final InterruptedException ex) { + ex.printStackTrace(); + } catch (final InvocationTargetException ex) { + ex.printStackTrace(); + } + } else { + _initializerRunnable.run(); + } + + _inited = isRealized(); + } + + @Override + public void draw(final CountDownLatch latch) { + if (!_inited) { + init(); + } + + if (isShowing()) { + invoke(true, _drawerGLRunnable); + } + if (latch != null) { + latch.countDown(); + } + } + + @Override + public JoglCanvasRenderer getCanvasRenderer() { + return _canvasRenderer; + } + + public void setVSyncEnabled(final boolean enabled) { + invoke(true, new GLRunnable() { + @Override + public boolean run(final GLAutoDrawable glAutoDrawable) { + glAutoDrawable.getGL().setSwapInterval(enabled ? 1 : 0); + return false; + } + }); + } +} diff --git a/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtInitializerRunnable.java b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtInitializerRunnable.java new file mode 100644 index 0000000..98c1949 --- /dev/null +++ b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtInitializerRunnable.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2008-2014 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.awt; + +import com.ardor3d.framework.DisplaySettings; + +public class JoglAwtInitializerRunnable implements Runnable { + + private final JoglAwtCanvas _joglAwtCanvas; + + private final DisplaySettings _settings; + + public JoglAwtInitializerRunnable(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()) { + // 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 diff --git a/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtWindow.java b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtWindow.java new file mode 100644 index 0000000..8e8ca09 --- /dev/null +++ b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglAwtWindow.java @@ -0,0 +1,285 @@ +/** + * Copyright (c) 2008-2014 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.awt; + +import java.awt.Dimension; +import java.awt.DisplayMode; +import java.awt.Frame; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Toolkit; +import java.awt.event.FocusListener; +import java.awt.event.KeyListener; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.util.concurrent.CountDownLatch; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.jogamp.opengl.GLContext; +import com.jogamp.opengl.GLException; +import com.ardor3d.annotation.MainThread; +import com.ardor3d.framework.CanvasRenderer; +import com.ardor3d.framework.DisplaySettings; +import com.ardor3d.framework.NativeCanvas; +import com.ardor3d.framework.jogl.JoglCanvasRenderer; +import com.ardor3d.image.Image; +import com.ardor3d.renderer.jogl.JoglPbufferTextureRenderer; + +/** + * A canvas implementation for use with AWT JOGL windows. + */ +public class JoglAwtWindow extends Frame implements NativeCanvas { + + private static final long serialVersionUID = 1L; + + private static final Logger logger = Logger.getLogger(JoglAwtWindow.class.getName()); + + private final DisplaySettings _settings; + private boolean _inited = false; + private boolean _isClosing = false; + + private JoglAwtCanvas _glCanvas; + + public JoglAwtWindow(final JoglCanvasRenderer canvasRenderer, final DisplaySettings settings) { + _settings = settings; + + // Create the OpenGL canvas + _glCanvas = new JoglAwtCanvas(_settings, canvasRenderer); + + // Default is not-resizeable. If you turn on resizeable, know what you are doing. + setResizable(false); + } + + @Override + public synchronized void addKeyListener(final KeyListener l) { + _glCanvas.addKeyListener(l); + } + + @Override + public synchronized void addMouseListener(final MouseListener l) { + _glCanvas.addMouseListener(l); + } + + @Override + public synchronized void addMouseMotionListener(final MouseMotionListener l) { + _glCanvas.addMouseMotionListener(l); + } + + @Override + public synchronized void addMouseWheelListener(final MouseWheelListener l) { + _glCanvas.addMouseWheelListener(l); + } + + @Override + public synchronized void addFocusListener(final FocusListener l) { + _glCanvas.addFocusListener(l); + } + + @Override + @MainThread + public void init() { + privateInit(); + } + + @MainThread + protected void privateInit() { + if (_inited) { + return; + } + + // FIXME: remove need for directly setting _parentContext. + JoglPbufferTextureRenderer._parentContext = _glCanvas.getContext(); + + this.add(_glCanvas); + + final boolean isDisplayModeModified; + final GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); + // Get the current display mode + final DisplayMode previousDisplayMode = gd.getDisplayMode(); + // Handle full screen mode if requested. + if (_settings.isFullScreen()) { + setUndecorated(true); + // Check if the full-screen mode is supported by the OS + boolean isFullScreenSupported = gd.isFullScreenSupported(); + if (isFullScreenSupported) { + gd.setFullScreenWindow(this); + // Check if display mode changes are supported by the OS + if (gd.isDisplayChangeSupported()) { + // Get all available display modes + final DisplayMode[] displayModes = gd.getDisplayModes(); + DisplayMode multiBitsDepthSupportedDisplayMode = null; + DisplayMode refreshRateUnknownDisplayMode = null; + DisplayMode multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode = null; + DisplayMode matchingDisplayMode = null; + DisplayMode currentDisplayMode; + // Look for the display mode that matches with our parameters + // Look for some display modes that are close to these parameters + // and that could be used as substitutes + // On some machines, the refresh rate is unknown and/or multi bit + // depths are supported. If you try to force a particular refresh + // rate or a bit depth, you might find no available display mode + // that matches exactly with your parameters + for (int i = 0; i < displayModes.length && matchingDisplayMode == null; i++) { + currentDisplayMode = displayModes[i]; + if (currentDisplayMode.getWidth() == _settings.getWidth() + && currentDisplayMode.getHeight() == _settings.getHeight()) { + if (currentDisplayMode.getBitDepth() == _settings.getColorDepth()) { + if (currentDisplayMode.getRefreshRate() == _settings.getFrequency()) { + matchingDisplayMode = currentDisplayMode; + } else if (currentDisplayMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) { + refreshRateUnknownDisplayMode = currentDisplayMode; + } + } else if (currentDisplayMode.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) { + if (currentDisplayMode.getRefreshRate() == _settings.getFrequency()) { + multiBitsDepthSupportedDisplayMode = currentDisplayMode; + } else if (currentDisplayMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) { + multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode = currentDisplayMode; + } + } + } + } + DisplayMode nextDisplayMode = null; + if (matchingDisplayMode != null) { + nextDisplayMode = matchingDisplayMode; + } else if (multiBitsDepthSupportedDisplayMode != null) { + nextDisplayMode = multiBitsDepthSupportedDisplayMode; + } else if (refreshRateUnknownDisplayMode != null) { + nextDisplayMode = refreshRateUnknownDisplayMode; + } else if (multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode != null) { + nextDisplayMode = multiBitsDepthSupportedAndRefreshRateUnknownDisplayMode; + } else { + isFullScreenSupported = false; + } + // If we have found a display mode that approximatively matches + // with the input parameters, use it + if (nextDisplayMode != null) { + gd.setDisplayMode(nextDisplayMode); + isDisplayModeModified = true; + } else { + isDisplayModeModified = false; + } + } else { + isDisplayModeModified = false; + // Resize the canvas if the display mode cannot be changed + // and the screen size is not equal to the canvas size + final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + if (screenSize.width != _settings.getWidth() || screenSize.height != _settings.getHeight()) { + _glCanvas.setSize(screenSize); + } + } + } else { + isDisplayModeModified = false; + } + + // Software windowed full-screen mode + if (!isFullScreenSupported) { + final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + // Resize the canvas + _glCanvas.setSize(screenSize); + // Resize the frame so that it occupies the whole screen + this.setSize(screenSize); + // Set its location at the top left corner + this.setLocation(0, 0); + } + } + // Otherwise, center the window on the screen. + else { + isDisplayModeModified = false; + pack(); + + int x, y; + x = (Toolkit.getDefaultToolkit().getScreenSize().width - _settings.getWidth()) / 2; + y = (Toolkit.getDefaultToolkit().getScreenSize().height - _settings.getHeight()) / 2; + this.setLocation(x, y); + } + + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(final WindowEvent e) { + _isClosing = true; + // If required, restore the previous display mode + if (isDisplayModeModified) { + gd.setDisplayMode(previousDisplayMode); + } + // If required, get back to the windowed mode + if (gd.getFullScreenWindow() == JoglAwtWindow.this) { + gd.setFullScreenWindow(null); + } + } + }); + + // Make the window visible to realize the OpenGL surface. + setVisible(true); + + _glCanvas.init(); // true - do swap in renderer. + _inited = true; + } + + @Override + public void draw(final CountDownLatch latch) { + if (!_inited) { + privateInit(); + } + + _glCanvas.draw(latch); + } + + @Override + public CanvasRenderer getCanvasRenderer() { + return _glCanvas.getCanvasRenderer(); + } + + @Override + public void close() { + try { + if (GLContext.getCurrent() != null) { + // Release the OpenGL resources. + GLContext.getCurrent().release(); + } + } catch (final GLException releaseFailure) { + logger.log(Level.WARNING, "Failed to release OpenGL Context: " + _glCanvas, releaseFailure); + } finally { + _glCanvas = null; + } + + // Dispose of any window resources. + dispose(); + } + + @Override + public boolean isActive() { + return hasFocus(); + } + + @Override + public boolean isClosing() { + return _isClosing; + } + + @Override + public void moveWindowTo(final int locX, final int locY) { + setLocation(locX, locY); + } + + @Override + public void setIcon(final Image[] iconImages) { + // not implemented, not supported by AWT anyway + } + + @Override + public void setVSyncEnabled(final boolean enabled) { + _glCanvas.setVSyncEnabled(enabled); + } +} diff --git a/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglNewtAwtCanvas.java b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglNewtAwtCanvas.java new file mode 100644 index 0000000..e72f045 --- /dev/null +++ b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglNewtAwtCanvas.java @@ -0,0 +1,131 @@ +/** + * Copyright (c) 2008-2014 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.awt; + +import java.util.concurrent.CountDownLatch; + +import com.jogamp.opengl.GLAutoDrawable; +import com.jogamp.opengl.GLRunnable; +import com.ardor3d.annotation.MainThread; +import com.ardor3d.framework.Canvas; +import com.ardor3d.framework.DisplaySettings; +import com.ardor3d.framework.jogl.CapsUtil; +import com.ardor3d.framework.jogl.JoglCanvasRenderer; +import com.ardor3d.framework.jogl.JoglDrawerRunnable; +import com.ardor3d.framework.jogl.NewtWindowContainer; +import com.jogamp.nativewindow.ScalableSurface; +import com.jogamp.newt.awt.NewtCanvasAWT; +import com.jogamp.newt.opengl.GLWindow; + +public class JoglNewtAwtCanvas extends NewtCanvasAWT implements Canvas, NewtWindowContainer { + + private static final long serialVersionUID = 1L; + + private final JoglCanvasRenderer _canvasRenderer; + private boolean _inited = false; + + private final DisplaySettings _settings; + + private final JoglDrawerRunnable _drawerGLRunnable; + + public JoglNewtAwtCanvas(final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer) { + this(settings, canvasRenderer, new CapsUtil()); + } + + public JoglNewtAwtCanvas(final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer, + final CapsUtil capsUtil) { + this(createNewtChildWindow(settings, capsUtil), settings, canvasRenderer, capsUtil); + } + + public JoglNewtAwtCanvas(final GLWindow newtChildWindow, final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer, + final CapsUtil capsUtil) { + super(newtChildWindow); + _drawerGLRunnable = new JoglDrawerRunnable(canvasRenderer); + getNewtWindow().setUndecorated(true); + _settings = settings; + _canvasRenderer = canvasRenderer; + + setFocusable(true); + setSize(_settings.getWidth(), _settings.getHeight()); + setIgnoreRepaint(true); + getNewtWindow().setAutoSwapBufferMode(false); + } + + private static GLWindow createNewtChildWindow(final DisplaySettings settings, final CapsUtil capsUtil) { + final GLWindow newtChildWindow = GLWindow.create(capsUtil.getCapsForSettings(settings)); + // disables HiDPI, see https://github.com/gouessej/Ardor3D/issues/14 + newtChildWindow.setSurfaceScale(new float[] { ScalableSurface.IDENTITY_PIXELSCALE, + ScalableSurface.IDENTITY_PIXELSCALE }); + return newtChildWindow; + } + + @Override + @MainThread + public void init() { + if (_inited) { + return; + } + + // Make the window visible to realize the OpenGL surface. + setVisible(true); + if (getNewtWindow().isRealized()) { + // Request the focus here as it cannot work when the window is not visible + requestFocus(); + /** + * I do not understand why I cannot get the context earlier, I failed in getting it from addNotify() and + * setVisible(true) + * */ + _canvasRenderer.setContext(getNewtWindow().getContext()); + getNewtWindow().invoke(true, new GLRunnable() { + @Override + public boolean run(final GLAutoDrawable glAutoDrawable) { + _canvasRenderer.init(_settings, true);// true - do swap in renderer. + return true; + } + }); + _inited = true; + } + } + + @Override + public void draw(final CountDownLatch latch) { + if (!_inited) { + init(); + } + + if (isShowing()) { + getNewtWindow().invoke(true, _drawerGLRunnable); + } + if (latch != null) { + latch.countDown(); + } + } + + @Override + public JoglCanvasRenderer getCanvasRenderer() { + return _canvasRenderer; + } + + @Override + public GLWindow getNewtWindow() { + return (GLWindow) getNEWTChild(); + } + + public void setVSyncEnabled(final boolean enabled) { + getNewtWindow().invoke(true, new GLRunnable() { + @Override + public boolean run(final GLAutoDrawable glAutoDrawable) { + glAutoDrawable.getGL().setSwapInterval(enabled ? 1 : 0); + return false; + } + }); + } +} diff --git a/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglSwingCanvas.java b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglSwingCanvas.java new file mode 100644 index 0000000..c9ef568 --- /dev/null +++ b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglSwingCanvas.java @@ -0,0 +1,121 @@ +/** + * Copyright (c) 2008-2014 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.awt; + +import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.CountDownLatch; + +import javax.swing.SwingUtilities; + +import com.ardor3d.annotation.MainThread; +import com.ardor3d.framework.Canvas; +import com.ardor3d.framework.DisplaySettings; +import com.ardor3d.framework.jogl.CapsUtil; +import com.ardor3d.framework.jogl.JoglCanvasRenderer; +import com.ardor3d.framework.jogl.JoglDrawerRunnable; +import com.jogamp.nativewindow.ScalableSurface; +import com.jogamp.opengl.GLAutoDrawable; +import com.jogamp.opengl.GLRunnable; +import com.jogamp.opengl.awt.GLJPanel; + +/** + * Ardor3D JOGL Swing lightweight canvas, Swing component for the OpenGL rendering of Ardor3D with JOGL that supports + * the AWT input system directly and its abstraction in Ardor3D (com.ardor3d.input.awt). As this canvas is generally + * slower and heavier (in term of memory footprint) than JoglAwtCanvas, use it if and only if you have some problems + * when mixing heavyweight and lightweight components. + */ +public class JoglSwingCanvas extends GLJPanel implements Canvas { + + private static final long serialVersionUID = 1L; + + private final JoglCanvasRenderer _canvasRenderer; + private boolean _inited = false; + + private final DisplaySettings _settings; + + private final JoglDrawerRunnable _drawerGLRunnable; + + private final JoglSwingInitializerRunnable _initializerRunnable; + + public JoglSwingCanvas(final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer) { + this(settings, canvasRenderer, new CapsUtil()); + } + + public JoglSwingCanvas(final DisplaySettings settings, final JoglCanvasRenderer canvasRenderer, + final CapsUtil capsUtil) { + super(capsUtil.getCapsForSettings(settings)); + _drawerGLRunnable = new JoglDrawerRunnable(canvasRenderer); + _initializerRunnable = new JoglSwingInitializerRunnable(this, settings); + _settings = settings; + _canvasRenderer = canvasRenderer; + + setFocusable(true); + requestFocus(); + setSize(_settings.getWidth(), _settings.getHeight()); + setIgnoreRepaint(true); + setAutoSwapBufferMode(false); + // disables HiDPI, see https://github.com/gouessej/Ardor3D/issues/14 + setSurfaceScale(new float[] { ScalableSurface.IDENTITY_PIXELSCALE, + ScalableSurface.IDENTITY_PIXELSCALE }); + } + + @Override + @MainThread + public void init() { + if (_inited) { + return; + } + + // Calling setVisible(true) on the GLCanvas not from the AWT-EDT can freeze the Intel GPU under Windows + if (!SwingUtilities.isEventDispatchThread()) { + try { + SwingUtilities.invokeAndWait(_initializerRunnable); + } catch (final InterruptedException ex) { + ex.printStackTrace(); + } catch (final InvocationTargetException ex) { + ex.printStackTrace(); + } + } else { + _initializerRunnable.run(); + } + + _inited = isRealized(); + } + + @Override + public void draw(final CountDownLatch latch) { + if (!_inited) { + init(); + } + + if (isShowing()) { + invoke(true, _drawerGLRunnable); + } + if (latch != null) { + latch.countDown(); + } + } + + @Override + public JoglCanvasRenderer getCanvasRenderer() { + return _canvasRenderer; + } + + public void setVSyncEnabled(final boolean enabled) { + invoke(true, new GLRunnable() { + @Override + public boolean run(final GLAutoDrawable glAutoDrawable) { + glAutoDrawable.getGL().setSwapInterval(enabled ? 1 : 0); + return false; + } + }); + } +} diff --git a/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglSwingInitializerRunnable.java b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglSwingInitializerRunnable.java new file mode 100644 index 0000000..fc73179 --- /dev/null +++ b/ardor3d-jogl-awt/src/main/java/com/ardor3d/framework/jogl/awt/JoglSwingInitializerRunnable.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2008-2014 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.awt; + +import com.ardor3d.framework.DisplaySettings; + +public class JoglSwingInitializerRunnable implements Runnable { + + private final JoglSwingCanvas _joglSwingCanvas; + + private final DisplaySettings _settings; + + public JoglSwingInitializerRunnable(final JoglSwingCanvas joglSwingCanvas, final DisplaySettings settings) { + _joglSwingCanvas = joglSwingCanvas; + _settings = settings; + } + + @Override + public void run() { + // Make the window visible to realize the OpenGL surface. + _joglSwingCanvas.setVisible(true); + // Force the initialization of the backend used inside the GLJPanel + if (_joglSwingCanvas.getDelegatedDrawable() == null) { + // Do it on the current thread, return only when it's done + _joglSwingCanvas.initializeBackend(false); + } + // Check whether the backend has been successfully initialized + if (_joglSwingCanvas.getDelegatedDrawable() != null) { + // Force the realization + _joglSwingCanvas.display(); + if (_joglSwingCanvas.getDelegatedDrawable().isRealized()) { + // Request the focus here as it cannot work when the window is + // not visible + _joglSwingCanvas.requestFocus(); + // The OpenGL context has been created after the realization of + // the surface + _joglSwingCanvas.getCanvasRenderer().setContext(_joglSwingCanvas.getContext()); + // As the canvas renderer knows the OpenGL context, it can be + // initialized + _joglSwingCanvas.getCanvasRenderer().init(_settings, true); + } + } + } +}
\ No newline at end of file |