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