[77] | 1 | package com.framsticks.visualization; |
---|
| 2 | |
---|
| 3 | import com.framsticks.communication.Connection; |
---|
| 4 | import com.framsticks.model.World; |
---|
| 5 | import com.sun.j3d.loaders.IncorrectFormatException; |
---|
| 6 | import com.sun.j3d.loaders.ParsingErrorException; |
---|
| 7 | import com.sun.j3d.loaders.Scene; |
---|
| 8 | import com.sun.j3d.loaders.objectfile.ObjectFile; |
---|
| 9 | import com.sun.j3d.utils.behaviors.mouse.MouseRotate; |
---|
| 10 | import com.sun.j3d.utils.universe.PlatformGeometry; |
---|
| 11 | import com.sun.j3d.utils.universe.SimpleUniverse; |
---|
| 12 | import com.sun.j3d.utils.universe.ViewingPlatform; |
---|
| 13 | |
---|
| 14 | import javax.media.j3d.*; |
---|
| 15 | import javax.swing.*; |
---|
| 16 | import javax.swing.text.StyledDocument; |
---|
| 17 | import javax.vecmath.Color3f; |
---|
| 18 | import javax.vecmath.Point3d; |
---|
| 19 | import javax.vecmath.Vector3f; |
---|
| 20 | import java.awt.*; |
---|
| 21 | import java.awt.event.ActionEvent; |
---|
| 22 | import java.awt.event.ActionListener; |
---|
| 23 | import java.io.FileNotFoundException; |
---|
| 24 | import java.util.ArrayList; |
---|
| 25 | |
---|
| 26 | import org.apache.log4j.Logger; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | public class Viewer { |
---|
| 31 | static final long serialVersionUID = 1; |
---|
| 32 | |
---|
| 33 | private static Logger logger = Logger.getLogger(Viewer.class); |
---|
| 34 | |
---|
| 35 | Canvas3D canvas3d; |
---|
| 36 | SimpleUniverse universe; |
---|
| 37 | |
---|
| 38 | World world; |
---|
| 39 | private JMenuBar menuBar; |
---|
| 40 | |
---|
| 41 | private JScrollPane scrollPane; |
---|
| 42 | private boolean scrollLock = false; |
---|
| 43 | |
---|
| 44 | private JCheckBoxMenuItem loggingItem; |
---|
| 45 | private JCheckBoxMenuItem autorefreshItem; |
---|
| 46 | |
---|
| 47 | private Connection connection; |
---|
| 48 | ArrayList<String> args; |
---|
| 49 | |
---|
| 50 | public Viewer(ArrayList<String> args, JMenuBar menuBar) { |
---|
| 51 | super(); |
---|
| 52 | this.args = args; |
---|
| 53 | this.menuBar = menuBar; |
---|
| 54 | loggingItem = new JCheckBoxMenuItem("Logging"); |
---|
| 55 | loggingItem.setSelected(true); |
---|
| 56 | |
---|
| 57 | init(); |
---|
| 58 | |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | public BranchGroup createSceneGraph() { |
---|
| 62 | // Create the root of the branch graph |
---|
| 63 | BranchGroup objRoot = new BranchGroup(); |
---|
| 64 | |
---|
| 65 | // Create a Transform group to scale all objects so they |
---|
| 66 | // appear in the scene. |
---|
| 67 | TransformGroup objScale = new TransformGroup(); |
---|
| 68 | Transform3D t3d = new Transform3D(); |
---|
| 69 | t3d.setScale(0.7); |
---|
| 70 | objScale.setTransform(t3d); |
---|
| 71 | objRoot.addChild(objScale); |
---|
| 72 | |
---|
| 73 | TransformGroup objTrans = new TransformGroup(); |
---|
| 74 | objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); |
---|
| 75 | objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); |
---|
| 76 | objScale.addChild(objTrans); |
---|
| 77 | |
---|
| 78 | String filename = "/static/shared/res/obj/cylinder.obj"; |
---|
| 79 | int flags = ObjectFile.RESIZE; |
---|
| 80 | flags |= ObjectFile.TRIANGULATE; |
---|
| 81 | flags |= ObjectFile.STRIPIFY; |
---|
| 82 | double creaseAngle = 60.0; |
---|
| 83 | ObjectFile f = new ObjectFile(flags, |
---|
| 84 | (float) (creaseAngle * Math.PI / 180.0)); |
---|
| 85 | Scene s = null; |
---|
| 86 | try { |
---|
| 87 | s = f.load(this.getClass().getResource(filename).getFile()); |
---|
| 88 | } catch (FileNotFoundException e) { |
---|
| 89 | System.err.println(e); |
---|
| 90 | System.exit(1); |
---|
| 91 | } catch (ParsingErrorException e) { |
---|
| 92 | System.err.println(e); |
---|
| 93 | System.exit(1); |
---|
| 94 | } catch (IncorrectFormatException e) { |
---|
| 95 | System.err.println(e); |
---|
| 96 | System.exit(1); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | objTrans.addChild(s.getSceneGroup()); |
---|
| 100 | |
---|
| 101 | BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), |
---|
| 102 | 100.0); |
---|
| 103 | |
---|
| 104 | Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f); |
---|
| 105 | Background bgNode = new Background(bgColor); |
---|
| 106 | bgNode.setApplicationBounds(bounds); |
---|
| 107 | objRoot.addChild(bgNode); |
---|
| 108 | |
---|
| 109 | return objRoot; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | private void init() { |
---|
| 113 | |
---|
| 114 | JTextPane textPane = new JTextPane(); |
---|
| 115 | textPane.setEditable(false); |
---|
| 116 | |
---|
| 117 | StyledDocument styledDocument = textPane.getStyledDocument(); |
---|
| 118 | |
---|
| 119 | scrollPane = new JScrollPane(textPane); |
---|
| 120 | //scrollPane. |
---|
| 121 | //scrollPane |
---|
| 122 | // .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
---|
| 123 | |
---|
| 124 | // paneScrollPane.setMinimumSize(new Dimension(400, 300)); |
---|
| 125 | |
---|
| 126 | createMenuBar(); |
---|
| 127 | |
---|
| 128 | GraphicsConfiguration config = SimpleUniverse |
---|
| 129 | .getPreferredConfiguration(); |
---|
| 130 | |
---|
| 131 | // Create a Canvas3D using the preferred configuration |
---|
| 132 | canvas3d = new Canvas3D(config); |
---|
| 133 | |
---|
| 134 | // Create simple universe with view branch |
---|
| 135 | universe = new SimpleUniverse(canvas3d); |
---|
| 136 | BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), |
---|
| 137 | 100.0); |
---|
| 138 | |
---|
| 139 | // add mouse behaviours to the ViewingPlatform |
---|
| 140 | ViewingPlatform viewingPlatform = universe.getViewingPlatform(); |
---|
| 141 | |
---|
| 142 | PlatformGeometry platformGeometry = new PlatformGeometry(); |
---|
| 143 | |
---|
| 144 | // Set up the ambient light |
---|
| 145 | Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); |
---|
| 146 | AmbientLight ambientLightNode = new AmbientLight(ambientColor); |
---|
| 147 | ambientLightNode.setInfluencingBounds(bounds); |
---|
| 148 | platformGeometry.addChild(ambientLightNode); |
---|
| 149 | |
---|
| 150 | // Set up the directional lights |
---|
| 151 | Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); |
---|
| 152 | Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f); |
---|
| 153 | Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f); |
---|
| 154 | Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f); |
---|
| 155 | |
---|
| 156 | DirectionalLight light1 = new DirectionalLight(light1Color, |
---|
| 157 | light1Direction); |
---|
| 158 | light1.setInfluencingBounds(bounds); |
---|
| 159 | platformGeometry.addChild(light1); |
---|
| 160 | |
---|
| 161 | DirectionalLight light2 = new DirectionalLight(light2Color, |
---|
| 162 | light2Direction); |
---|
| 163 | light2.setInfluencingBounds(bounds); |
---|
| 164 | platformGeometry.addChild(light2); |
---|
| 165 | |
---|
| 166 | viewingPlatform.setPlatformGeometry(platformGeometry); |
---|
| 167 | |
---|
| 168 | viewingPlatform.setNominalViewingTransform(); |
---|
| 169 | |
---|
| 170 | // Ensure at least 5 msec per frame (i.e., < 200Hz) |
---|
| 171 | universe.getViewer().getView().setMinimumFrameCycleTime(5); |
---|
| 172 | |
---|
| 173 | BranchGroup scene = new BranchGroup(); |
---|
| 174 | BranchGroup content = createSceneGraph(); |
---|
| 175 | |
---|
| 176 | // canvas3d.addMouseListener(behaviour); |
---|
| 177 | TransformGroup transformGroup = new TransformGroup(new Transform3D()); |
---|
| 178 | transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); |
---|
| 179 | transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); |
---|
| 180 | MouseRotate behaviour = new MouseRotate(); |
---|
| 181 | behaviour.setTransformGroup(transformGroup); |
---|
| 182 | transformGroup.addChild(behaviour); |
---|
| 183 | // behaviour.addListener(canvas3d); |
---|
| 184 | // behaviour.initialize(); |
---|
| 185 | behaviour.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, |
---|
| 186 | 0.0), 100.0)); |
---|
| 187 | transformGroup.addChild(content); |
---|
| 188 | // transformGroup.addChild(behaviour); |
---|
| 189 | scene.addChild(transformGroup); |
---|
| 190 | |
---|
| 191 | universe.addBranchGraph(scene); |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | private void createMenuBar() { |
---|
| 197 | |
---|
| 198 | JMenu serverItem = new JMenu("Manager"); |
---|
| 199 | menuBar.add(serverItem); |
---|
| 200 | |
---|
| 201 | JMenuItem testModeItem = new JMenuItem("Test mode"); |
---|
| 202 | serverItem.add(testModeItem); |
---|
| 203 | |
---|
| 204 | JMenuItem connectItem = new JMenuItem("Connect"); |
---|
| 205 | serverItem.add(connectItem); |
---|
| 206 | |
---|
| 207 | JMenu simulationItem = new JMenu("Simulation"); |
---|
| 208 | menuBar.add(simulationItem); |
---|
| 209 | |
---|
| 210 | //TODO: move init to main window |
---|
| 211 | JMenuItem initItem = new JMenuItem("Init"); |
---|
| 212 | simulationItem.add(initItem); |
---|
| 213 | initItem.addActionListener(new ActionListener() { |
---|
| 214 | public void actionPerformed(ActionEvent event) { |
---|
| 215 | } |
---|
| 216 | }); |
---|
| 217 | |
---|
| 218 | JMenuItem refreshCreaturesItem = new JMenuItem("Refresh creatures"); |
---|
| 219 | simulationItem.add(refreshCreaturesItem); |
---|
| 220 | refreshCreaturesItem.addActionListener(new ActionListener() { |
---|
| 221 | public void actionPerformed(ActionEvent event) { |
---|
| 222 | |
---|
| 223 | } |
---|
| 224 | }); |
---|
| 225 | |
---|
| 226 | JMenuItem refreshWorldItem = new JMenuItem("Refresh world"); |
---|
| 227 | simulationItem.add(refreshWorldItem); |
---|
| 228 | refreshWorldItem.addActionListener(new ActionListener() { |
---|
| 229 | public void actionPerformed(ActionEvent event) { |
---|
| 230 | refreshWorld(); |
---|
| 231 | } |
---|
| 232 | }); |
---|
| 233 | |
---|
| 234 | JMenu styleMenu = new JMenu("Style"); |
---|
| 235 | menuBar.add(styleMenu); |
---|
| 236 | |
---|
| 237 | JMenu viewMenu = new JMenu("View"); |
---|
| 238 | menuBar.add(viewMenu); |
---|
| 239 | rebuildViewMenu(null); |
---|
| 240 | |
---|
| 241 | JMenu optionsMenu = new JMenu("Options"); |
---|
| 242 | menuBar.add(optionsMenu); |
---|
| 243 | |
---|
| 244 | autorefreshItem = new JCheckBoxMenuItem("Autorefresh"); |
---|
| 245 | autorefreshItem.addActionListener(new ActionListener() { |
---|
| 246 | public void actionPerformed(ActionEvent e) { |
---|
| 247 | if (autorefreshItem.isSelected()) { |
---|
| 248 | |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | }); |
---|
| 252 | optionsMenu.add(autorefreshItem); |
---|
| 253 | |
---|
| 254 | optionsMenu.add(loggingItem); |
---|
| 255 | |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | public void refreshWorld() { |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | public void setCommunication(Connection connection) { |
---|
| 267 | this.connection = connection; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | public void rebuildViewMenu(Object creatures) { |
---|
| 271 | |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | public Component getViewComponent() { |
---|
| 275 | return canvas3d; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | public Component getControlComponent() { |
---|
| 279 | return scrollPane; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | } |
---|