1 | package com.framsticks.visualization; |
---|
2 | |
---|
3 | import com.framsticks.util.io.Encoding; |
---|
4 | import com.sun.j3d.loaders.IncorrectFormatException; |
---|
5 | import com.sun.j3d.loaders.ParsingErrorException; |
---|
6 | import com.sun.j3d.loaders.Scene; |
---|
7 | import com.sun.j3d.loaders.objectfile.ObjectFile; |
---|
8 | import com.sun.j3d.utils.behaviors.mouse.MouseRotate; |
---|
9 | import com.sun.j3d.utils.universe.PlatformGeometry; |
---|
10 | import com.sun.j3d.utils.universe.SimpleUniverse; |
---|
11 | import com.sun.j3d.utils.universe.ViewingPlatform; |
---|
12 | |
---|
13 | import javax.media.j3d.*; |
---|
14 | import javax.swing.*; |
---|
15 | import javax.vecmath.Color3f; |
---|
16 | import javax.vecmath.Point3d; |
---|
17 | import javax.vecmath.Vector3f; |
---|
18 | import java.awt.*; |
---|
19 | import java.io.*; |
---|
20 | |
---|
21 | import org.apache.log4j.Logger; |
---|
22 | |
---|
23 | public 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 | } |
---|