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

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

File size: 4.9 KB
Line 
1package com.framsticks.visualization;
2
3import com.framsticks.util.io.Encoding;
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.vecmath.Color3f;
16import javax.vecmath.Point3d;
17import javax.vecmath.Vector3f;
18import java.awt.*;
19import java.io.*;
20
21import org.apache.log4j.Logger;
22
23public class Viewer {
24
25        private static Logger log = Logger.getLogger(Viewer.class);
26
27        Canvas3D canvas3d;
28        SimpleUniverse universe;
29
30        public Viewer() {
31                super();
32
33                init();
34        }
35
36        public BranchGroup createSceneGraph() {
37                // Create the root of the branch graph
38                BranchGroup objRoot = new BranchGroup();
39
40                // Create a Transform group to scale all objects so they
41                // appear in the scene.
42                TransformGroup objScale = new TransformGroup();
43                Transform3D t3d = new Transform3D();
44                t3d.setScale(0.7);
45                objScale.setTransform(t3d);
46                objRoot.addChild(objScale);
47
48                TransformGroup objTrans = new TransformGroup();
49                objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
50                objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
51                objScale.addChild(objTrans);
52
53                String filename = "/visualization/models/cylinder.obj";
54                int flags = ObjectFile.RESIZE;
55                flags |= ObjectFile.TRIANGULATE;
56                flags |= ObjectFile.STRIPIFY;
57                double creaseAngle = 60.0;
58                ObjectFile f = new ObjectFile(flags,
59                                (float) (creaseAngle * Math.PI / 180.0));
60                Scene s = null;
61                try {
62                        InputStream is = this.getClass().getResourceAsStream(filename);
63                        s = f.load(new InputStreamReader(is, Encoding.getDefaultCharset()));
64                } catch (FileNotFoundException | ParsingErrorException | IncorrectFormatException e) {
65                        log.fatal("fatal error", e);
66                        return null;
67                }
68
69                objTrans.addChild(s.getSceneGroup());
70
71                BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
72
73                Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
74                Background bgNode = new Background(bgColor);
75                bgNode.setApplicationBounds(bounds);
76                objRoot.addChild(bgNode);
77
78                return objRoot;
79        }
80
81        private void init() {
82
83                JTextPane textPane = new JTextPane();
84                textPane.setEditable(false);
85
86                GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
87
88                // Create a Canvas3D using the preferred configuration
89                canvas3d = new Canvas3D(config);
90
91                // Create simple universe with view branch
92                universe = new SimpleUniverse(canvas3d);
93                BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
94
95                // add mouse behaviours to the ViewingPlatform
96                ViewingPlatform viewingPlatform = universe.getViewingPlatform();
97
98                PlatformGeometry platformGeometry = new PlatformGeometry();
99
100                // Set up the ambient light
101                Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
102                AmbientLight ambientLightNode = new AmbientLight(ambientColor);
103                ambientLightNode.setInfluencingBounds(bounds);
104                platformGeometry.addChild(ambientLightNode);
105
106                // Set up the directional lights
107                Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
108                Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
109                Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
110                Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
111
112                DirectionalLight light1 = new DirectionalLight(light1Color,
113                                light1Direction);
114                light1.setInfluencingBounds(bounds);
115                platformGeometry.addChild(light1);
116
117                DirectionalLight light2 = new DirectionalLight(light2Color,
118                                light2Direction);
119                light2.setInfluencingBounds(bounds);
120                platformGeometry.addChild(light2);
121
122                viewingPlatform.setPlatformGeometry(platformGeometry);
123
124                viewingPlatform.setNominalViewingTransform();
125
126                // Ensure at least 5 msec per frame (i.e., < 200Hz)
127                universe.getViewer().getView().setMinimumFrameCycleTime(5);
128
129                BranchGroup scene = new BranchGroup();
130                BranchGroup content = createSceneGraph();
131
132                // canvas3d.addMouseListener(behaviour);
133                TransformGroup transformGroup = new TransformGroup(new Transform3D());
134                transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
135                transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
136                MouseRotate behaviour = new MouseRotate();
137                behaviour.setTransformGroup(transformGroup);
138                transformGroup.addChild(behaviour);
139                // behaviour.addListener(canvas3d);
140                // behaviour.initialize();
141                behaviour.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
142                transformGroup.addChild(content);
143                // transformGroup.addChild(behaviour);
144                scene.addChild(transformGroup);
145
146                universe.addBranchGraph(scene);
147                canvas3d.startRenderer();
148                Dimension d = new Dimension(500, 500);
149                canvas3d.setMinimumSize(d);
150                canvas3d.setSize(d);
151        }
152
153        public Component getViewComponent() {
154                return canvas3d;
155        }
156
157
158}
Note: See TracBrowser for help on using the repository browser.