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

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

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