source: java/main/src/main/java/com/framsticks/visualization/Viewer.java @ 78

Last change on this file since 78 was 78, checked in by psniegowski, 11 years ago

Add f0 parsing and f0->Model transformation.

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