- Timestamp:
- 06/24/13 13:38:40 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/visualization/Viewer.java
r84 r85 1 1 package com.framsticks.visualization; 2 2 3 import com.framsticks.util.io.Encoding; 3 4 import com.sun.j3d.loaders.IncorrectFormatException; 4 5 import com.sun.j3d.loaders.ParsingErrorException; … … 18 19 import java.io.*; 19 20 20 //import org.apache.log4j.Logger;21 import org.apache.log4j.Logger; 21 22 22 23 public class Viewer { 23 24 24 //private static Logger log = Logger.getLogger(Viewer.class);25 private static Logger log = Logger.getLogger(Viewer.class); 25 26 26 27 27 Canvas3D canvas3d; 28 SimpleUniverse universe; 28 29 29 30 30 public Viewer() { 31 super(); 31 32 32 33 33 init(); 34 } 34 35 35 36 public BranchGroup createSceneGraph() { 36 37 37 // Create the root of the branch graph 38 BranchGroup objRoot = new BranchGroup(); 38 39 39 40 41 42 43 44 45 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); 46 47 47 48 49 50 48 TransformGroup objTrans = new TransformGroup(); 49 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 50 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 51 objScale.addChild(objTrans); 51 52 52 53 54 55 53 String filename = "/visualization/models/cylinder.obj"; 54 int flags = ObjectFile.RESIZE; 55 flags |= ObjectFile.TRIANGULATE; 56 flags |= ObjectFile.STRIPIFY; 56 57 double creaseAngle = 60.0; 57 58 ObjectFile f = new ObjectFile(flags, 58 59 60 59 (float) (creaseAngle * Math.PI / 180.0)); 60 Scene s = null; 61 try { 61 62 InputStream is = this.getClass().getResourceAsStream(filename); 62 s = f.load(new InputStreamReader(is)); 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 } 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 } 73 68 74 69 objTrans.addChild(s.getSceneGroup()); 75 70 76 71 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 77 72 78 79 80 81 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); 82 77 83 84 78 return objRoot; 79 } 85 80 86 81 private void init() { 87 82 88 89 83 JTextPane textPane = new JTextPane(); 84 textPane.setEditable(false); 90 85 91 86 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); 92 87 93 94 88 // Create a Canvas3D using the preferred configuration 89 canvas3d = new Canvas3D(config); 95 90 96 97 98 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); 99 94 100 101 95 // add mouse behaviours to the ViewingPlatform 96 ViewingPlatform viewingPlatform = universe.getViewingPlatform(); 102 97 103 98 PlatformGeometry platformGeometry = new PlatformGeometry(); 104 99 105 106 107 108 109 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); 110 105 111 112 113 114 115 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); 116 111 117 118 119 120 112 DirectionalLight light1 = new DirectionalLight(light1Color, 113 light1Direction); 114 light1.setInfluencingBounds(bounds); 115 platformGeometry.addChild(light1); 121 116 122 123 124 125 117 DirectionalLight light2 = new DirectionalLight(light2Color, 118 light2Direction); 119 light2.setInfluencingBounds(bounds); 120 platformGeometry.addChild(light2); 126 121 127 122 viewingPlatform.setPlatformGeometry(platformGeometry); 128 123 129 124 viewingPlatform.setNominalViewingTransform(); 130 125 131 132 126 // Ensure at least 5 msec per frame (i.e., < 200Hz) 127 universe.getViewer().getView().setMinimumFrameCycleTime(5); 133 128 134 135 129 BranchGroup scene = new BranchGroup(); 130 BranchGroup content = createSceneGraph(); 136 131 137 138 139 140 141 142 143 144 145 146 147 148 149 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); 150 145 151 146 universe.addBranchGraph(scene); 152 147 canvas3d.startRenderer(); 153 148 Dimension d = new Dimension(500, 500); 154 149 canvas3d.setMinimumSize(d); 155 150 canvas3d.setSize(d); 156 151 } 157 152 158 159 160 153 public Component getViewComponent() { 154 return canvas3d; 155 } 161 156 162 157
Note: See TracChangeset
for help on using the changeset viewer.