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.logging.log4j.Logger; |
---|
22 | import org.apache.logging.log4j.LogManager; |
---|
23 | |
---|
24 | public class Viewer { |
---|
25 | |
---|
26 | private static Logger log = LogManager.getLogger(Viewer.class); |
---|
27 | |
---|
28 | Canvas3D canvas3d; |
---|
29 | SimpleUniverse universe; |
---|
30 | |
---|
31 | public Viewer() { |
---|
32 | super(); |
---|
33 | |
---|
34 | init(); |
---|
35 | } |
---|
36 | |
---|
37 | public BranchGroup createSceneGraph() { |
---|
38 | // Create the root of the branch graph |
---|
39 | BranchGroup objRoot = new BranchGroup(); |
---|
40 | |
---|
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); |
---|
48 | |
---|
49 | TransformGroup objTrans = new TransformGroup(); |
---|
50 | objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); |
---|
51 | objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); |
---|
52 | objScale.addChild(objTrans); |
---|
53 | |
---|
54 | String filename = "/visualization/models/cylinder.obj"; |
---|
55 | int flags = ObjectFile.RESIZE; |
---|
56 | flags |= ObjectFile.TRIANGULATE; |
---|
57 | flags |= ObjectFile.STRIPIFY; |
---|
58 | double creaseAngle = 60.0; |
---|
59 | ObjectFile f = new ObjectFile(flags, |
---|
60 | (float) (creaseAngle * Math.PI / 180.0)); |
---|
61 | Scene s = null; |
---|
62 | try { |
---|
63 | InputStream is = this.getClass().getResourceAsStream(filename); |
---|
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 | } |
---|
69 | |
---|
70 | objTrans.addChild(s.getSceneGroup()); |
---|
71 | |
---|
72 | BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); |
---|
73 | |
---|
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); |
---|
78 | |
---|
79 | return objRoot; |
---|
80 | } |
---|
81 | |
---|
82 | private void init() { |
---|
83 | |
---|
84 | JTextPane textPane = new JTextPane(); |
---|
85 | textPane.setEditable(false); |
---|
86 | |
---|
87 | GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); |
---|
88 | |
---|
89 | // Create a Canvas3D using the preferred configuration |
---|
90 | canvas3d = new Canvas3D(config); |
---|
91 | |
---|
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); |
---|
95 | |
---|
96 | // add mouse behaviours to the ViewingPlatform |
---|
97 | ViewingPlatform viewingPlatform = universe.getViewingPlatform(); |
---|
98 | |
---|
99 | PlatformGeometry platformGeometry = new PlatformGeometry(); |
---|
100 | |
---|
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); |
---|
106 | |
---|
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); |
---|
112 | |
---|
113 | DirectionalLight light1 = new DirectionalLight(light1Color, |
---|
114 | light1Direction); |
---|
115 | light1.setInfluencingBounds(bounds); |
---|
116 | platformGeometry.addChild(light1); |
---|
117 | |
---|
118 | DirectionalLight light2 = new DirectionalLight(light2Color, |
---|
119 | light2Direction); |
---|
120 | light2.setInfluencingBounds(bounds); |
---|
121 | platformGeometry.addChild(light2); |
---|
122 | |
---|
123 | viewingPlatform.setPlatformGeometry(platformGeometry); |
---|
124 | |
---|
125 | viewingPlatform.setNominalViewingTransform(); |
---|
126 | |
---|
127 | // Ensure at least 5 msec per frame (i.e., < 200Hz) |
---|
128 | universe.getViewer().getView().setMinimumFrameCycleTime(5); |
---|
129 | |
---|
130 | BranchGroup scene = new BranchGroup(); |
---|
131 | BranchGroup content = createSceneGraph(); |
---|
132 | |
---|
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); |
---|
146 | |
---|
147 | universe.addBranchGraph(scene); |
---|
148 | canvas3d.startRenderer(); |
---|
149 | Dimension d = new Dimension(500, 500); |
---|
150 | canvas3d.setMinimumSize(d); |
---|
151 | canvas3d.setSize(d); |
---|
152 | } |
---|
153 | |
---|
154 | public Component getViewComponent() { |
---|
155 | return canvas3d; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | } |
---|