aboutsummaryrefslogtreecommitdiffstats
path: root/src/classes/org/jdesktop/j3d/examples/jcanvas3d
diff options
context:
space:
mode:
authorphil <[email protected]>2016-11-04 23:04:11 +1300
committerphil <[email protected]>2016-11-04 23:04:11 +1300
commit7a8ff3f043c5140e0f40a4df72fc27085b8e8b71 (patch)
tree9f420f033a8160a82089ef4c60c6c953d2c133f6 /src/classes/org/jdesktop/j3d/examples/jcanvas3d
parent3a573d43a87978741d03d7aecc2bf638a43d77c3 (diff)
Added a lot more GL2ES2 examples
And converted to struct based lights and material in shaders
Diffstat (limited to 'src/classes/org/jdesktop/j3d/examples/jcanvas3d')
-rw-r--r--src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExampleGL2ES2.java175
-rw-r--r--src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorldGL2ES2.java211
2 files changed, 386 insertions, 0 deletions
diff --git a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExampleGL2ES2.java b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExampleGL2ES2.java
new file mode 100644
index 0000000..45dccc0
--- /dev/null
+++ b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JCanvas3DExampleGL2ES2.java
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2016 JogAmp Community. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+package org.jdesktop.j3d.examples.jcanvas3d;
+
+
+/**
+ * Simple Java 3D example program that displays universes within lightweight swing components, layed in JInternalFrame objects.
+ */
+import java.awt.Toolkit;
+
+public class JCanvas3DExampleGL2ES2 extends javax.swing.JFrame implements java.awt.event.ActionListener
+{
+
+ /**
+ * Creates new form JCanvas3DExample
+ */
+ public JCanvas3DExampleGL2ES2()
+ {
+ initComponents();
+ Toolkit.getDefaultToolkit().setDynamicLayout( true );
+ setDefaultCloseOperation( EXIT_ON_CLOSE );
+ }
+
+ /** This method is called from within the constructor to
+ * initialize the form.
+ * WARNING: Do NOT modify this code. The content of this method is
+ * always regenerated by the Form Editor.
+ */
+ // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
+ private void initComponents() {
+ java.awt.GridBagConstraints gridBagConstraints;
+
+ splitPane = new javax.swing.JSplitPane();
+ scrollPane = new javax.swing.JScrollPane();
+ panel = new javax.swing.JPanel();
+ addButton = new javax.swing.JButton();
+ delayCheckBox = new javax.swing.JCheckBox();
+ interactiveCheckBox = new javax.swing.JCheckBox();
+ randomCheckBox = new javax.swing.JCheckBox();
+ desktopPane = new javax.swing.JDesktopPane();
+
+ setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
+ splitPane.setDividerLocation(300);
+ splitPane.setDividerSize(8);
+ splitPane.setContinuousLayout(true);
+ splitPane.setOneTouchExpandable(true);
+ panel.setLayout(new java.awt.GridBagLayout());
+
+ addButton.setText("Create New Frame");
+ addButton.setToolTipText("Adds a new frame containing an universe into the desktop pane");
+ addButton.addActionListener(this);
+
+ panel.add(addButton, new java.awt.GridBagConstraints());
+
+ delayCheckBox.setText("Resize Delayed");
+ delayCheckBox.setToolTipText("Shows the effect of using a delayed resizing to the internal frames.");
+ gridBagConstraints = new java.awt.GridBagConstraints();
+ gridBagConstraints.gridx = 0;
+ gridBagConstraints.gridy = 1;
+ gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
+ panel.add(delayCheckBox, gridBagConstraints);
+
+ interactiveCheckBox.setSelected(true);
+ interactiveCheckBox.setText("Interactive Cube");
+ interactiveCheckBox.setToolTipText("Tests the use of AWT behaviors on the displayed component.");
+ interactiveCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
+ interactiveCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
+ interactiveCheckBox.addActionListener(this);
+
+ gridBagConstraints = new java.awt.GridBagConstraints();
+ gridBagConstraints.gridx = 0;
+ gridBagConstraints.gridy = 2;
+ gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
+ panel.add(interactiveCheckBox, gridBagConstraints);
+
+ randomCheckBox.setText("Random start angle");
+ randomCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
+ randomCheckBox.setEnabled(false);
+ randomCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
+ gridBagConstraints = new java.awt.GridBagConstraints();
+ gridBagConstraints.gridx = 0;
+ gridBagConstraints.gridy = 3;
+ gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
+ panel.add(randomCheckBox, gridBagConstraints);
+
+ scrollPane.setViewportView(panel);
+
+ splitPane.setLeftComponent(scrollPane);
+
+ desktopPane.setBackground(new java.awt.Color(153, 153, 201));
+ desktopPane.setPreferredSize(new java.awt.Dimension(300, 300));
+ splitPane.setRightComponent(desktopPane);
+
+ getContentPane().add(splitPane, java.awt.BorderLayout.CENTER);
+
+ java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
+ setBounds((screenSize.width-1011)/2, (screenSize.height-733)/2, 1011, 733);
+ }
+
+ // Code for dispatching events from components to event handlers.
+
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ if (evt.getSource() == addButton) {
+ JCanvas3DExampleGL2ES2.this.addButtonActionPerformed(evt);
+ }
+ else if (evt.getSource() == interactiveCheckBox) {
+ JCanvas3DExampleGL2ES2.this.interactiveCheckBoxActionPerformed(evt);
+ }
+ }// </editor-fold>//GEN-END:initComponents
+
+ private void interactiveCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_interactiveCheckBoxActionPerformed
+ randomCheckBox.setEnabled( interactiveCheckBox.isSelected() ? false:true );
+ }//GEN-LAST:event_interactiveCheckBoxActionPerformed
+
+ private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
+ JInternalWorldGL2ES2 iWorld;
+ // we create an internal world to be added within the JDesktop.
+ iWorld = new JInternalWorldGL2ES2( interactiveCheckBox.isSelected(),
+ delayCheckBox.isSelected(),
+ randomCheckBox.isSelected() );
+ iWorld.setSize( 256, 256 );
+ iWorld.setLocation( 50, 50 );
+ iWorld.setResizable( true );
+ desktopPane.add( iWorld );
+ iWorld.setVisible(true);
+ }//GEN-LAST:event_addButtonActionPerformed
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String args[])
+ {System.setProperty("sun.awt.noerasebackground", "true");
+ System.setProperty("j3d.rend", "jogl2es2");
+ System.setProperty("j3d.displaylist", "false");
+ java.awt.EventQueue.invokeLater(new Runnable()
+ {
+ public void run()
+ {
+ new JCanvas3DExampleGL2ES2().setVisible(true);
+ }
+ });
+ }
+
+ // Variables declaration - do not modify//GEN-BEGIN:variables
+ private javax.swing.JButton addButton;
+ private javax.swing.JCheckBox delayCheckBox;
+ private javax.swing.JDesktopPane desktopPane;
+ private javax.swing.JCheckBox interactiveCheckBox;
+ private javax.swing.JPanel panel;
+ private javax.swing.JCheckBox randomCheckBox;
+ private javax.swing.JScrollPane scrollPane;
+ private javax.swing.JSplitPane splitPane;
+ // End of variables declaration//GEN-END:variables
+
+}
diff --git a/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorldGL2ES2.java b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorldGL2ES2.java
new file mode 100644
index 0000000..d38dc6e
--- /dev/null
+++ b/src/classes/org/jdesktop/j3d/examples/jcanvas3d/JInternalWorldGL2ES2.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2016 JogAmp Community. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+package org.jdesktop.j3d.examples.jcanvas3d;
+
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+
+import javax.swing.JInternalFrame;
+
+import org.jdesktop.j3d.examples.gl2es2pipeline.SimpleShaderAppearance;
+import org.jogamp.java3d.Alpha;
+import org.jogamp.java3d.AmbientLight;
+import org.jogamp.java3d.Appearance;
+import org.jogamp.java3d.BoundingSphere;
+import org.jogamp.java3d.BranchGroup;
+import org.jogamp.java3d.DirectionalLight;
+import org.jogamp.java3d.Font3D;
+import org.jogamp.java3d.FontExtrusion;
+import org.jogamp.java3d.GraphicsConfigTemplate3D;
+import org.jogamp.java3d.Material;
+import org.jogamp.java3d.RotationInterpolator;
+import org.jogamp.java3d.Shape3D;
+import org.jogamp.java3d.Text3D;
+import org.jogamp.java3d.Transform3D;
+import org.jogamp.java3d.TransformGroup;
+import org.jogamp.java3d.exp.swing.JCanvas3D;
+import org.jogamp.java3d.utils.behaviors.mouse.MouseRotate;
+import org.jogamp.java3d.utils.universe.SimpleUniverse;
+import org.jogamp.vecmath.Color3f;
+import org.jogamp.vecmath.Point3d;
+import org.jogamp.vecmath.Point3f;
+import org.jogamp.vecmath.Vector3d;
+import org.jogamp.vecmath.Vector3f;
+
+
+/**
+ * This is a JInternalFrame holding an universe, which can be configured to
+ * be interactive -that is, where user can interact with object- or automatic
+ * -where the object spins only-. When in automatic mode, spinning speed is
+ * changed so that they look less the same. Changing the spinning start angle
+ * helps unsynchronizing the rotations too.
+ *
+ * @author pepe
+ */
+public class JInternalWorldGL2ES2 extends JInternalFrame {
+ /** DOCUMENT ME! */
+ private Component comp;
+
+ /**
+ * Creates a new JInternalWorld object.
+ *
+ * @param isInteractive tells the world to be constructed as interactive
+ * @param isDelayed tells the rotator to start at a random alpha.
+ */
+ public JInternalWorldGL2ES2(boolean isInteractive, boolean isDelayed, boolean isRandom) {
+ super();
+ setSize(256, 256);
+ setClosable(true);
+
+ JCanvas3D canvas = new JCanvas3D(new GraphicsConfigTemplate3D());
+
+ if (true == isDelayed) {
+ canvas.setResizeMode(canvas.RESIZE_DELAYED);
+ }
+
+ comp = canvas;
+
+ Dimension dim = new Dimension(256, 256);
+ comp.setPreferredSize(dim);
+ comp.setSize(dim);
+ getContentPane().setLayout(new BorderLayout());
+ getContentPane().add(comp, BorderLayout.CENTER);
+ pack();
+
+ // Create a simple scene and attach it to the virtual universe
+ BranchGroup scene = createSceneGraph(isInteractive, isRandom);
+ SimpleUniverse universe = new SimpleUniverse(canvas.getOffscreenCanvas3D()); //TODO: this is awful and must not be done like that in final version
+
+ // This will move the ViewPlatform back a bit so the
+ // objects in the scene can be viewed.
+ universe.getViewingPlatform().setNominalViewingTransform();
+ universe.getViewer().getView().setMinimumFrameCycleTime(30);
+ universe.addBranchGraph(scene);
+ }
+
+ /**
+ * Creates the world. Only exists to cleanup the source a bit
+ *
+ * @param isInteractive tells the world to be constructed as interactive
+ * @param isDelayed tells the rotator to start at a random alpha.
+ *
+ * @return a global branchgroup containing the world, as desired.
+ */
+ private BranchGroup createSceneGraph(boolean isInteractive, boolean isRandom) {
+ // Create the root of the branch graph
+ BranchGroup objRoot = new BranchGroup();
+
+ // Create the TransformGroup node and initialize it to the
+ // identity. Enable the TRANSFORM_WRITE capability so that
+ // our behavior code can modify it at run time. Add it to
+ // the root of the subgraph.
+ TransformGroup objTrans = new TransformGroup();
+ Transform3D t3dTrans = new Transform3D();
+ t3dTrans.setTranslation(new Vector3d(0, 0, -1));
+ objTrans.setTransform(t3dTrans);
+
+ TransformGroup objRot = new TransformGroup();
+ objRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
+ objRoot.addChild(objTrans);
+ objTrans.addChild(objRot);
+
+ // Create a simple Shape3D node; add it to the scene graph.
+ // issue 383: changed the cube to a text, so that any graphical problem related to Yup can be seen.
+ Font3D f3d = new Font3D(new Font("dialog", Font.PLAIN, 1),
+ new FontExtrusion());
+ Text3D text = new Text3D(f3d, "JCanvas3D",
+ new Point3f( -2.3f, -0.5f, 0.f));
+
+ Shape3D sh = new Shape3D();
+ Appearance app = new SimpleShaderAppearance(false,false);
+ Material mm = new Material();
+ mm.setLightingEnable(true);
+ app.setMaterial(mm);
+ sh.setGeometry(text);
+ sh.setAppearance(app);
+
+ objRot.addChild( sh );
+
+ BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
+ 100.0);
+
+ // Set up the ambient light
+ Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f);
+ AmbientLight ambientLightNode = new AmbientLight(ambientColor);
+ ambientLightNode.setInfluencingBounds(bounds);
+ objRoot.addChild(ambientLightNode);
+
+ // Set up the directional lights
+ Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
+ Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
+ Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
+ Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
+
+ DirectionalLight light1
+ = new DirectionalLight(light1Color, light1Direction);
+ light1.setInfluencingBounds(bounds);
+ objRoot.addChild(light1);
+
+ DirectionalLight light2
+ = new DirectionalLight(light2Color, light2Direction);
+ light2.setInfluencingBounds(bounds);
+ objRoot.addChild(light2);
+
+
+ if (true == isInteractive) {
+ MouseRotate mr = new MouseRotate(comp, objRot);
+ mr.setSchedulingBounds(bounds);
+ mr.setSchedulingInterval(1);
+ objRoot.addChild(mr);
+ } else {
+ // Create a new Behavior object that will perform the
+ // desired operation on the specified transform and add
+ // it into the scene graph.
+ Transform3D yAxis = new Transform3D();
+
+ // rotation speed is randomized a bit so that it does not go at the same speed on every canvases,
+ // which will make it more natural and express the differences between every present universes
+ Alpha rotationAlpha = null;
+
+ if (true == isRandom) {
+ int duration = Math.max(2000, (int) (Math.random() * 8000.));
+ rotationAlpha = new Alpha(-1,
+ (int) ((double) duration * Math.random()), 0, duration,
+ 0, 0);
+ } else {
+ rotationAlpha = new Alpha(-1, 4000);
+ }
+
+ RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
+ objRot, yAxis, 0.0f, (float) Math.PI * 2.0f);
+
+ rotator.setSchedulingBounds(bounds);
+ objRoot.addChild(rotator);
+ }
+
+ return objRoot;
+ }
+
+}