001 package com.croftsoft.apps.tag3d; 002 003 import java.applet.*; 004 import java.awt.*; 005 import java.awt.event.*; 006 import java.rmi.Naming; 007 import java.util.HashMap; 008 import java.util.Map; 009 010 import javax.media.j3d.*; 011 import javax.vecmath.*; 012 013 import com.sun.j3d.utils.applet.MainFrame; 014 import com.sun.j3d.utils.behaviors.mouse.*; 015 import com.sun.j3d.utils.geometry.Box; 016 import com.sun.j3d.utils.geometry.ColorCube; 017 import com.sun.j3d.utils.image.TextureLoader; 018 import com.sun.j3d.utils.universe.*; 019 020 import com.croftsoft.core.media.j3d.Transform3DLib; 021 import com.croftsoft.core.media.j3d.Transform3DState; 022 import com.croftsoft.core.util.state.*; 023 024 /********************************************************************* 025 * 026 * The Tag3D universe and methods to build it. 027 * 028 * @author 029 * <A HREF="http://www.alumni.caltech.edu/~croft/">David W. Croft</A> 030 * @version 031 * 1999-02-07 032 *********************************************************************/ 033 034 public class Tag3DWorld 035 ////////////////////////////////////////////////////////////////////// 036 ////////////////////////////////////////////////////////////////////// 037 { 038 039 protected Canvas3D canvas3D; 040 protected Locale locale; 041 protected TransformGroup viewTransformGroup; 042 043 ////////////////////////////////////////////////////////////////////// 044 ////////////////////////////////////////////////////////////////////// 045 046 public static Tag3DWorld build ( ) 047 ////////////////////////////////////////////////////////////////////// 048 { 049 Canvas3D canvas3D = new Canvas3D ( null ); 050 051 VirtualUniverse virtualUniverse = new VirtualUniverse ( ); 052 Locale locale = new Locale ( virtualUniverse ); 053 054 PhysicalBody physicalBody = new PhysicalBody ( ); 055 PhysicalEnvironment physicalEnvironment 056 = new PhysicalEnvironment ( ); 057 058 View view = new View ( ); 059 060 // Antialiasing slows things down dramatically. 061 // view.setSceneAntialiasingEnable ( true ); 062 063 view.addCanvas3D ( canvas3D ); 064 view.setPhysicalBody ( physicalBody ); 065 view.setPhysicalEnvironment ( physicalEnvironment ); 066 BranchGroup viewBranchGroup = new BranchGroup ( ); 067 068 Transform3D transform3D = new Transform3D ( ); 069 070 ViewPlatform viewPlatform = new ViewPlatform ( ); 071 072 TransformGroup viewTransformGroup 073 = new TransformGroup ( transform3D ); 074 075 viewTransformGroup.setCapability ( 076 TransformGroup.ALLOW_TRANSFORM_READ ); 077 viewTransformGroup.setCapability ( 078 TransformGroup.ALLOW_TRANSFORM_WRITE ); 079 080 viewTransformGroup.addChild ( viewPlatform ); 081 viewBranchGroup.addChild ( viewTransformGroup ); 082 083 view.attachViewPlatform ( viewPlatform ); 084 085 locale.addBranchGraph ( viewBranchGroup ); 086 locale.addBranchGraph ( createSceneGraph ( ) ); 087 088 return new Tag3DWorld ( canvas3D, locale, viewTransformGroup ); 089 } 090 091 ////////////////////////////////////////////////////////////////////// 092 ////////////////////////////////////////////////////////////////////// 093 094 public static BranchGroup createSceneGraph ( ) 095 ////////////////////////////////////////////////////////////////////// 096 { 097 BranchGroup rootBranchGroup = new BranchGroup ( ); 098 099 BoundingSphere boundingSphere 100 = new BoundingSphere ( new Point3d ( 0.0, 0.0, 0.0 ), 10.0 ); 101 102 Clip clip = new Clip ( 10000.0 ); 103 clip.setApplicationBounds ( 104 new BoundingSphere ( new Point3d ( 0.0, 0.0, 0.0 ), 10000.0 ) ); 105 rootBranchGroup.addChild ( clip ); 106 107 /* 108 Background background = new Background ( 0.9f, 1.0f, 0.9f ); 109 background.setApplicationBounds ( 110 new BoundingSphere ( new Point3d ( 0.0, 0.0, 0.0 ), 5.0 ) ); 111 rootBranchGroup.addChild ( background ); 112 */ 113 114 115 /* 116 Fog fog = new ExponentialFog ( 0.0f, 0.1f, 0.2f ); 117 fog.setInfluencingBounds ( boundingSphere ); 118 rootBranchGroup.addChild ( fog ); 119 120 Light light = new PointLight ( ); 121 light.setInfluencingBounds ( boundingSphere ); 122 rootBranchGroup.addChild ( light ); 123 */ 124 125 int distance = 10; 126 127 SharedGroup sharedGroup = new SharedGroup ( ); 128 sharedGroup.addChild ( new ColorCube ( 0.5 ) ); 129 sharedGroup.compile ( ); 130 131 for ( int x = -2 * distance; x < 3 * distance; x += distance ) 132 for ( int y = -2 * distance; y < 3 * distance; y += distance ) 133 for ( int z = -2 * distance; z < 3 * distance; z += distance ) 134 { 135 if ( ( x == 0 ) && ( y == 0 ) && ( z == 0 ) ) continue; 136 Transform3D transform3D = new Transform3D ( ); 137 transform3D.setTranslation ( new Vector3d ( x, y, z ) ); 138 139 TransformGroup transformGroup 140 = new TransformGroup ( transform3D ); 141 142 transformGroup.addChild ( new Link ( sharedGroup ) ); 143 144 rootBranchGroup.addChild ( transformGroup ); 145 } 146 147 rootBranchGroup.compile ( ); 148 149 return rootBranchGroup; 150 } 151 152 public static void addHead ( 153 String id, 154 TransformGroup transformGroup, 155 Component component ) 156 ////////////////////////////////////////////////////////////////////// 157 { 158 Font3D font3D = new Font3D ( 159 new Font ( "Courier", Font.PLAIN, 1 ), 160 new FontExtrusion ( ) ); 161 Text3D text3D = new Text3D ( font3D, id, 162 new Point3f ( 0.0f, -1.2f, 0.0f ), 163 Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT ); 164 Shape3D shape3D = new Shape3D ( ); 165 Appearance appearance = new Appearance ( ); 166 Material material = new Material ( ); 167 material.setEmissiveColor ( new Color3f ( 1.0f, 0.0f, 0.0f ) ); 168 material.setLightingEnable ( true ); 169 appearance.setMaterial ( material ); 170 shape3D.setGeometry ( text3D ); 171 shape3D.setAppearance ( appearance ); 172 173 BoundingSphere boundingSphere 174 = new BoundingSphere ( new Point3d ( 0.0, 0.0, 0.0 ), 10.0 ); 175 176 TransformGroup textTransformGroup 177 = new TransformGroup ( new Transform3D ( ) ); 178 Alpha rotationAlpha = new Alpha ( 179 -1, Alpha.INCREASING_ENABLE, 180 0, 0, 181 4000, 0, 0, 182 0, 0, 0 ); 183 RotationInterpolator rotationInterpolator 184 = new RotationInterpolator ( 185 rotationAlpha, textTransformGroup, new Transform3D ( ), 186 0.0f, 2.0f * (float) Math.PI ); 187 rotationInterpolator.setSchedulingBounds ( boundingSphere ); 188 textTransformGroup.setCapability ( 189 TransformGroup.ALLOW_TRANSFORM_WRITE ); 190 textTransformGroup.addChild ( rotationInterpolator ); 191 textTransformGroup.addChild ( shape3D ); 192 transformGroup.addChild ( textTransformGroup ); 193 194 Text3D chatText3D = new Text3D ( font3D, "Hello!", 195 new Point3f ( 0.0f, 1.2f, 0.0f ), 196 Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT ); 197 Shape3D chatShape3D = new Shape3D ( ); 198 chatShape3D.setGeometry ( chatText3D ); 199 chatShape3D.setAppearance ( appearance ); 200 TransformGroup chatTransformGroup 201 = new TransformGroup ( new Transform3D ( ) ); 202 chatTransformGroup.setCapability ( 203 TransformGroup.ALLOW_TRANSFORM_WRITE ); 204 Billboard billboard = new Billboard ( chatTransformGroup ); 205 billboard.setSchedulingBounds ( boundingSphere ); 206 chatTransformGroup.addChild ( chatShape3D ); 207 // chatTransformGroup.addChild ( billboard ); 208 transformGroup.addChild ( chatTransformGroup ); 209 210 211 transformGroup.setCapability ( 212 TransformGroup.ALLOW_TRANSFORM_READ ); 213 transformGroup.setCapability ( 214 TransformGroup.ALLOW_TRANSFORM_WRITE ); 215 216 MouseRotate mouseRotate = new MouseRotate ( ); 217 mouseRotate.setTransformGroup ( transformGroup ); 218 transformGroup.addChild ( mouseRotate ); 219 mouseRotate.setSchedulingBounds ( boundingSphere ); 220 221 // Create the zoom behavior node 222 MouseZoom mouseZoom = new MouseZoom ( ); 223 mouseZoom.setTransformGroup ( transformGroup ); 224 transformGroup.addChild ( mouseZoom ); 225 mouseZoom.setSchedulingBounds ( boundingSphere ); 226 227 // Create the translate behavior node 228 MouseTranslate mouseTranslate = new MouseTranslate ( ); 229 mouseTranslate.setTransformGroup ( transformGroup ); 230 transformGroup.addChild ( mouseTranslate ); 231 mouseTranslate.setSchedulingBounds ( boundingSphere ); 232 233 234 Appearance app = new Appearance ( ); 235 Texture tex = new TextureLoader ( 236 "images/face.jpg", component ).getTexture ( ); 237 app.setTexture(tex); 238 if ( tex == null ) 239 { 240 System.out.println("Warning: Texture is disabled"); 241 } 242 Box textureCube = new Box ( 0.5f, 0.5f, 0.5f, 243 Box.GENERATE_TEXTURE_COORDS, app ); 244 transformGroup.addChild ( textureCube ); 245 } 246 247 ////////////////////////////////////////////////////////////////////// 248 ////////////////////////////////////////////////////////////////////// 249 250 public Tag3DWorld ( 251 Canvas3D canvas3D, 252 Locale locale, 253 TransformGroup viewTransformGroup ) 254 ////////////////////////////////////////////////////////////////////// 255 { 256 this.canvas3D = canvas3D; 257 this.locale = locale; 258 this.viewTransformGroup = viewTransformGroup; 259 } 260 261 ////////////////////////////////////////////////////////////////////// 262 ////////////////////////////////////////////////////////////////////// 263 264 public TransformGroup addHead ( 265 String id, 266 Transform3D transform3D ) 267 ////////////////////////////////////////////////////////////////////// 268 { 269 TransformGroup transformGroup = new TransformGroup ( transform3D ); 270 addHead ( id, transformGroup, canvas3D ); 271 272 BranchGroup rootBranchGroup = new BranchGroup ( ); 273 rootBranchGroup.addChild ( transformGroup ); 274 rootBranchGroup.compile ( ); 275 locale.addBranchGraph ( rootBranchGroup ); 276 277 return transformGroup; 278 } 279 280 ////////////////////////////////////////////////////////////////////// 281 ////////////////////////////////////////////////////////////////////// 282 }