Java/3D/Object VRML File

Материал из Java эксперт
Перейти к: навигация, поиск

Java 3D"s built in object loader to load a Lightwave file

/**********************************************************
Copyright (C) 2001   Daniel Selman
First distributed with the book "Java 3D Programming"
by Daniel Selman and published by Manning Publications.
http://manning.ru/selman
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
The license can be found on the WWW at:
http://www.fsf.org/copyleft/gpl.html
Or by writing to:
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
Authors can be contacted at:
 Daniel Selman: daniel@selman.org
If you make changes you think others would like, please 
contact one of the authors or someone at the 
www.j3d.org web site.
**************************************************************/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfigTemplate;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.AudioDevice;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.media.j3d.Group;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.audioengines.javasound.JavaSoundMixer;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
/**
* Simple example that illustrates using Java 3D"s
* built in object loader to load a Lightwave file.
*/
public class LoaderTest extends Java3dApplet implements ActionListener
{
  private static int       m_kWidth = 400;
  private static int       m_kHeight = 400;
  public LoaderTest( )
  {
    initJava3d( );
  }
  public void actionPerformed( ActionEvent event )
  {
  }
  protected BranchGroup createSceneBranchGroup( )
  {
    BranchGroup objRoot = super.createSceneBranchGroup( );
    // create a TransformGroup to flip the hand onto its end and enlarge it.
    TransformGroup objTrans1 = new TransformGroup( );
    Transform3D tr = new Transform3D( );
    objTrans1.getTransform( tr );
    tr.rotX( 90.0 * Math.PI / 180.0 );
    tr.setScale( 10.0 );
    objTrans1.setTransform( tr );
    // create a TransformGroup to rotate the hand
    TransformGroup objTrans2 = new TransformGroup( );
    objTrans2.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
    objTrans2.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
    BoundingSphere bounds =  new BoundingSphere( new Point3d( 0.0,0.0,0.0 ), 100.0 );
    // create a RotationInterpolator behavior to rotate the hand
    Transform3D yAxis = new Transform3D( );
    Alpha rotationAlpha = new Alpha( -1, Alpha.INCREASING_ENABLE,
      0, 0,
      4000, 0, 0,
      0, 0, 0 );
    RotationInterpolator rotator = new RotationInterpolator( rotationAlpha, objTrans2, yAxis, 0.0f, (float) Math.PI*2.0f );
    rotator.setSchedulingBounds( bounds );
    objTrans2.addChild( rotator );
    // Set up the global lights
    Color3f lColor1 = new Color3f( 0.7f, 0.7f, 0.7f );
    Vector3f lDir1  = new Vector3f( -1.0f, -1.0f, -1.0f );
    Color3f alColor = new Color3f( 0.2f, 0.2f, 0.2f );
    AmbientLight aLgt = new AmbientLight( alColor );
    aLgt.setInfluencingBounds( bounds );
    DirectionalLight lgt1 = new DirectionalLight( lColor1, lDir1 );
    lgt1.setInfluencingBounds( bounds );
    objRoot.addChild( aLgt );
    objRoot.addChild( lgt1 );
    // load the object file
    Scene scene = null;
    Shape3D shape = null;
    // read in the geometry information from the data file
    ObjectFile objFileloader = new ObjectFile( ObjectFile.RESIZE );
    try
    {
      scene = objFileloader.load( "hand1.obj" );
    }
    catch ( Exception e )
    {
      scene = null;
      System.err.println( e );
    }
    if( scene == null )
      System.exit( 1 );
    // retrieve the Shape3D object from the scene
    BranchGroup branchGroup = scene.getSceneGroup( );
    shape = (Shape3D) branchGroup.getChild( 0 );
    // create an Appearance and Material
    Appearance app = new Appearance( );
    Color3f objColor = new Color3f( 1.0f, 0.7f, 0.8f );
    Color3f black = new Color3f( 0.0f, 0.0f, 0.0f );
    app.setMaterial( new Material( objColor, black, objColor, black, 80.0f ) );
    // assign the appearance to the Shape
    shape.setAppearance( app );
    // connect the scenegraph
    objTrans2.addChild( scene.getSceneGroup( ) );
    objTrans1.addChild( objTrans2 );
    objRoot.addChild( objTrans1 );
    return objRoot;
  }

  public static void main( String[] args )
  {
    LoaderTest loaderTest = new LoaderTest( );
    loaderTest.saveCommandLineArguments( args );
    new MainFrame( loaderTest, m_kWidth, m_kHeight );
  }
}

/*******************************************************************************
 * Copyright (C) 2001 Daniel Selman
 * 
 * First distributed with the book "Java 3D Programming" by Daniel Selman and
 * published by Manning Publications. http://manning.ru/selman
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, version 2.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * The license can be found on the WWW at: http://www.fsf.org/copyleft/gpl.html
 * 
 * Or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA 02111-1307, USA.
 * 
 * Authors can be contacted at: Daniel Selman: daniel@selman.org
 * 
 * If you make changes you think others would like, please contact one of the
 * authors or someone at the www.j3d.org web site.
 ******************************************************************************/
//*****************************************************************************
/**
 * Java3dApplet
 * 
 * Base class for defining a Java 3D applet. Contains some useful methods for
 * defining views and scenegraphs etc.
 * 
 * @author Daniel Selman
 * @version 1.0
 */
//*****************************************************************************
abstract class Java3dApplet extends Applet {
  public static int m_kWidth = 300;
  public static int m_kHeight = 300;
  protected String[] m_szCommandLineArray = null;
  protected VirtualUniverse m_Universe = null;
  protected BranchGroup m_SceneBranchGroup = null;
  protected Bounds m_ApplicationBounds = null;
  //  protected com.tornadolabs.j3dtree.Java3dTree m_Java3dTree = null;
  public Java3dApplet() {
  }
  public boolean isApplet() {
    try {
      System.getProperty("user.dir");
      System.out.println("Running as Application.");
      return false;
    } catch (Exception e) {
    }
    System.out.println("Running as Applet.");
    return true;
  }
  public URL getWorkingDirectory() throws java.net.MalformedURLException {
    URL url = null;
    try {
      File file = new File(System.getProperty("user.dir"));
      System.out.println("Running as Application:");
      System.out.println("   " + file.toURL());
      return file.toURL();
    } catch (Exception e) {
    }
    System.out.println("Running as Applet:");
    System.out.println("   " + getCodeBase());
    return getCodeBase();
  }
  public VirtualUniverse getVirtualUniverse() {
    return m_Universe;
  }
  //public com.tornadolabs.j3dtree.Java3dTree getJ3dTree() {
  //return m_Java3dTree;
  //  }
  public Locale getFirstLocale() {
    java.util.Enumeration e = m_Universe.getAllLocales();
    if (e.hasMoreElements() != false)
      return (Locale) e.nextElement();
    return null;
  }
  protected Bounds getApplicationBounds() {
    if (m_ApplicationBounds == null)
      m_ApplicationBounds = createApplicationBounds();
    return m_ApplicationBounds;
  }
  protected Bounds createApplicationBounds() {
    m_ApplicationBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    return m_ApplicationBounds;
  }
  protected Background createBackground() {
    Background back = new Background(new Color3f(0.9f, 0.9f, 0.9f));
    back.setApplicationBounds(createApplicationBounds());
    return back;
  }
  public void initJava3d() {
    //  m_Java3dTree = new com.tornadolabs.j3dtree.Java3dTree();
    m_Universe = createVirtualUniverse();
    Locale locale = createLocale(m_Universe);
    BranchGroup sceneBranchGroup = createSceneBranchGroup();
    ViewPlatform vp = createViewPlatform();
    BranchGroup viewBranchGroup = createViewBranchGroup(
        getViewTransformGroupArray(), vp);
    createView(vp);
    Background background = createBackground();
    if (background != null)
      sceneBranchGroup.addChild(background);
    //    m_Java3dTree.recursiveApplyCapability(sceneBranchGroup);
    //  m_Java3dTree.recursiveApplyCapability(viewBranchGroup);
    locale.addBranchGraph(sceneBranchGroup);
    addViewBranchGroup(locale, viewBranchGroup);
    onDoneInit();
  }
  protected void onDoneInit() {
    //  m_Java3dTree.updateNodes(m_Universe);
  }
  protected double getScale() {
    return 1.0;
  }
  public TransformGroup[] getViewTransformGroupArray() {
    TransformGroup[] tgArray = new TransformGroup[1];
    tgArray[0] = new TransformGroup();
    // move the camera BACK a little...
    // note that we have to invert the matrix as
    // we are moving the viewer
    Transform3D t3d = new Transform3D();
    t3d.setScale(getScale());
    t3d.setTranslation(new Vector3d(0.0, 0.0, -20.0));
    t3d.invert();
    tgArray[0].setTransform(t3d);
    return tgArray;
  }
  protected void addViewBranchGroup(Locale locale, BranchGroup bg) {
    locale.addBranchGraph(bg);
  }
  protected Locale createLocale(VirtualUniverse u) {
    return new Locale(u);
  }
  protected BranchGroup createSceneBranchGroup() {
    m_SceneBranchGroup = new BranchGroup();
    return m_SceneBranchGroup;
  }
  protected View createView(ViewPlatform vp) {
    View view = new View();
    PhysicalBody pb = createPhysicalBody();
    PhysicalEnvironment pe = createPhysicalEnvironment();
    AudioDevice audioDevice = createAudioDevice(pe);
    if (audioDevice != null) {
      pe.setAudioDevice(audioDevice);
      audioDevice.initialize();
    }
    view.setPhysicalEnvironment(pe);
    view.setPhysicalBody(pb);
    if (vp != null)
      view.attachViewPlatform(vp);
    view.setBackClipDistance(getBackClipDistance());
    view.setFrontClipDistance(getFrontClipDistance());
    Canvas3D c3d = createCanvas3D();
    view.addCanvas3D(c3d);
    addCanvas3D(c3d);
    return view;
  }
  protected PhysicalBody createPhysicalBody() {
    return new PhysicalBody();
  }
  protected AudioDevice createAudioDevice(PhysicalEnvironment pe) {
    JavaSoundMixer javaSoundMixer = new JavaSoundMixer(pe);
    if (javaSoundMixer == null)
      System.out.println("create of audiodevice failed");
    return javaSoundMixer;
  }
  protected PhysicalEnvironment createPhysicalEnvironment() {
    return new PhysicalEnvironment();
  }
  protected float getViewPlatformActivationRadius() {
    return 100;
  }
  protected ViewPlatform createViewPlatform() {
    ViewPlatform vp = new ViewPlatform();
    vp.setViewAttachPolicy(View.RELATIVE_TO_FIELD_OF_VIEW);
    vp.setActivationRadius(getViewPlatformActivationRadius());
    return vp;
  }
  protected Canvas3D createCanvas3D() {
    GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();
    gc3D.setSceneAntialiasing(GraphicsConfigTemplate.PREFERRED);
    GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getScreenDevices();
    Canvas3D c3d = new Canvas3D(gd[0].getBestConfiguration(gc3D));
    c3d.setSize(getCanvas3dWidth(c3d), getCanvas3dHeight(c3d));
    return c3d;
  }
  protected int getCanvas3dWidth(Canvas3D c3d) {
    return m_kWidth;
  }
  protected int getCanvas3dHeight(Canvas3D c3d) {
    return m_kHeight;
  }
  protected double getBackClipDistance() {
    return 100.0;
  }
  protected double getFrontClipDistance() {
    return 1.0;
  }
  protected BranchGroup createViewBranchGroup(TransformGroup[] tgArray,
      ViewPlatform vp) {
    BranchGroup vpBranchGroup = new BranchGroup();
    if (tgArray != null && tgArray.length > 0) {
      Group parentGroup = vpBranchGroup;
      TransformGroup curTg = null;
      for (int n = 0; n < tgArray.length; n++) {
        curTg = tgArray[n];
        parentGroup.addChild(curTg);
        parentGroup = curTg;
      }
      tgArray[tgArray.length - 1].addChild(vp);
    } else
      vpBranchGroup.addChild(vp);
    return vpBranchGroup;
  }
  protected void addCanvas3D(Canvas3D c3d) {
    setLayout(new BorderLayout());
    add(c3d, BorderLayout.CENTER);
    doLayout();
  }
  protected VirtualUniverse createVirtualUniverse() {
    return new VirtualUniverse();
  }
  protected void saveCommandLineArguments(String[] szArgs) {
    m_szCommandLineArray = szArgs;
  }
  protected String[] getCommandLineArguments() {
    return m_szCommandLineArray;
  }
}

//File: hand1.obj
/*

# Mon Jun 02 17:09:33 1997
#
#
g hand1
v -0.551223 0.307562 -3.946050
v -0.215465 0.030475 -3.703516
v -0.059140 0.068255 -1.551531
v -0.487235 0.455792 -4.006029
v -0.168743 0.090975 -3.755638
v -0.338689 0.569450 -4.244848
v -0.639211 0.502297 -4.165243
v -0.760687 0.647871 -4.482797
v -0.529792 0.073768 -3.945949
v -0.707612 0.258064 -4.192451
v -0.866004 0.446161 -4.511733
v -0.367733 -0.147323 -4.191223
v -0.717413 0.071719 -4.433171
v -0.893823 0.253971 -4.589350
v -0.172976 -0.188212 -3.754734
v -0.089148 -0.103984 -1.450071
v -0.234491 -0.100206 -3.756646
v -0.131305 -0.032994 -1.531782
v -0.589492 0.641054 -4.376035
v -0.050594 -0.257375 -4.417058
v 0.208857 -0.205637 -4.768191
v 0.007882 -0.231806 -4.028160
v 0.230232 -0.186379 -4.335583
v 0.390435 -0.043258 -4.060836
v 0.002626 -0.129308 -1.430840
v -0.058465 -0.217994 -3.717670
v 0.087189 -0.100005 -1.425612
v 0.044163 -0.171797 -3.707954
v 0.044240 0.657156 -4.536777
v -0.137070 0.604272 -4.344090
v 0.041139 0.604901 -4.118776
v -0.080844 0.531805 -3.993031
v 0.165095 0.484110 -3.906230
v -0.081629 0.087632 -3.782753
v -0.013872 0.088823 -1.554481
v 0.008630 0.041677 -3.708195
v 0.070162 0.059225 -1.467241
v 0.349126 0.784380 -4.327246
v 0.192656 0.707311 -4.316467
v 0.240026 0.669896 -4.092967
v 0.614077 0.770599 -4.245997
v 0.448901 0.789330 -4.242595
v 0.390683 0.635870 -4.050064
v 0.823568 1.030378 -5.230011
v 0.583262 0.935220 -4.789198
v 0.936485 1.075914 -5.144932
v 0.708930 0.972489 -4.713959
v 1.079595 1.050653 -5.079602
v 0.863290 0.942616 -4.657226
v 0.731356 0.950572 -5.335061
v 0.698927 0.816156 -5.569689
v 0.452954 0.862935 -4.860037
v 0.354245 0.777679 -5.103039
v 0.109515 -0.020305 -1.439012
v 0.321579 0.190660 -3.847763
v 0.056950 -0.068344 -3.678991
v 0.551677 0.397382 -4.027330
v 0.748943 0.261978 -4.201552
v 0.797463 0.611225 -4.241810
v 0.934536 0.472987 -4.298673
v 0.919641 0.370477 -5.597496
v 1.079582 0.375487 -5.341094
v 1.225601 0.456396 -5.207421
v 0.593068 0.043314 -5.202118
v 0.711852 0.101000 -4.865152
v 0.871615 0.211454 -4.690717
v 1.063296 0.399320 -4.647913
v 1.342637 0.585582 -5.122607
v 1.150690 0.608137 -4.641154
v 1.366648 0.748651 -5.071675
v 0.745367 0.246406 -5.988852
v 0.494019 -0.022198 -5.740361
v 0.141480 -0.193021 -5.470416
v 0.693354 0.148862 -6.455598
v 0.458179 -0.058425 -6.342125
v 0.092861 -0.121753 -6.334044
v 0.001903 0.615014 -5.359071
v 0.310613 0.694999 -5.659912
v 0.602326 0.700149 -5.960638
v 0.014377 0.478587 -6.304075
v 0.335968 0.619833 -6.312831
v 0.600780 0.612337 -6.446789
v -0.303419 0.594566 -6.098518
v -0.599974 0.655996 -6.132385
v -0.241202 0.626083 -5.200386
v -0.457099 0.677790 -5.161324
v -0.259616 -0.163854 -6.116562
v -0.183392 -0.223625 -5.241480
v -0.635209 0.061396 -6.121774
v -0.504788 -0.103060 -5.122423
v -0.950019 0.234128 -5.837874
v -0.810067 0.092804 -5.080121
v -1.144829 0.478824 -5.746136
v -0.998398 0.334709 -5.084644
v -0.994164 0.856674 -5.773610
v -0.863354 0.756330 -5.102725
v -0.801998 0.842826 -5.870856
v -0.671650 0.764754 -5.123487
v 1.045049 0.802118 -4.632644
v 1.252493 0.924338 -5.055513
v -0.995449 0.583125 -5.083862
v -1.137996 0.713069 -5.735577
v 1.598002 0.816988 -5.564189
v 1.599381 0.700758 -5.624741
v 1.532017 0.614642 -5.716787
v 1.715526 0.933794 -5.717422
v 1.699604 0.816034 -5.787023
v 1.629737 0.731598 -5.898082
v 1.883043 1.059492 -5.899169
v 1.855109 0.932664 -5.998640
v 1.774384 0.842472 -6.140775
v 1.424351 0.559021 -5.523375
v 1.505227 0.658051 -5.434678
v 1.502338 0.787710 -5.373565
v 1.297397 0.577850 -5.903827
v 1.425129 0.560695 -5.822476
v 1.155962 0.509306 -5.718982
v 1.300644 0.494656 -5.623770
v 1.509245 0.835318 -6.384689
v 1.646807 0.801147 -6.286009
v 1.406474 0.713747 -6.133959
v 1.525574 0.687385 -6.031679
v 1.380351 1.065119 -5.565239
v 1.564172 1.172023 -5.714888
v 1.780605 1.304970 -5.893034
v 1.255719 1.087198 -5.619839
v 1.438416 1.182678 -5.766468
v 1.652418 1.312790 -5.952035
v 1.154437 1.043031 -5.702960
v 1.320461 1.134701 -5.855917
v 1.512782 1.262635 -6.051604
v 1.003945 1.036752 -5.521133
v 1.104476 1.081637 -5.435655
v 1.236055 1.056913 -5.374795
v 1.089111 0.843044 -5.888583
v 1.089661 0.962064 -5.803318
v 1.230625 0.966122 -6.097103
v 1.242952 1.066407 -5.981094
v 1.362597 1.098246 -6.323909
v 1.405432 1.196541 -6.191591
v 0.929064 0.824679 -5.709339
v 0.934340 0.953520 -5.615597
v 1.859089 1.203879 -5.868989
v 1.664971 1.072200 -5.695913
v 1.510868 0.957577 -5.544997
v 1.391678 0.940769 -5.352942
v 1.170557 0.692380 -5.926214
v 1.027666 0.653090 -5.757548
v 0.869972 0.619560 -5.748698
v 1.295478 0.827279 -6.155195
v 1.403801 0.956111 -6.395917
v 1.981874 0.878914 -6.465936
v 1.802272 0.826665 -6.577847
v 1.615752 0.851027 -6.620514
v 2.128560 0.944598 -6.721747
v 1.907593 0.869058 -6.790360
v 1.708489 0.849226 -6.769275
v 2.238363 1.116521 -6.349912
v 2.094190 1.105140 -6.141595
v 2.237261 1.035889 -6.535950
v 2.075505 0.976585 -6.291769
v 1.727506 1.346013 -6.328338
v 1.936762 1.311864 -6.600124
v 1.576315 1.256378 -6.447272
v 1.741231 1.205721 -6.680005
v 1.483882 1.132384 -6.546886
v 1.595348 1.106596 -6.700094
v 2.143625 1.363027 -6.343407
v 2.083770 1.364678 -6.472173
v 1.997907 1.370713 -6.136065
v 1.880283 1.392692 -6.219747
v 2.220069 1.245977 -6.358671
v 2.223785 1.204368 -6.587201
v 2.097329 1.131385 -6.757537
v 1.644286 0.966614 -6.763514
v 1.867680 1.032451 -6.812611
v 0.883363 0.124343 -7.228944
v 0.646839 -0.024425 -7.259110
v 0.390212 -0.005959 -7.292361
v 1.070393 0.135169 -7.703971
v 0.802047 0.006235 -7.763816
v 0.511476 0.026061 -7.813804
v 1.163011 0.161832 -8.064369
v 0.918797 0.093289 -8.161346
v 0.636154 0.076768 -8.160398
v 0.269275 -0.049054 -6.787587
v 0.524823 -0.050661 -6.806285
v 0.744409 0.117607 -6.839493
v 0.827585 0.571806 -7.238711
v 1.016438 0.562215 -7.711038
v 1.115469 0.520019 -8.067395
v 0.573132 0.588769 -7.270417
v 0.731449 0.578986 -7.772986
v 0.865027 0.502425 -8.164807
v 0.338782 0.438496 -7.299040
v 0.460865 0.446711 -7.820236
v 0.590458 0.430524 -8.163703
v 0.208617 0.434283 -6.784674
v 0.437333 0.591011 -6.807528
v 0.678646 0.577267 -6.844694
v 0.947532 0.372788 -7.222228
v 0.797216 0.376391 -6.856188
v 0.734974 0.417910 -6.504792
v 1.142651 0.368565 -7.694477
v 1.195867 0.347815 -8.107895
v 0.272439 0.191555 -7.307303
v 0.163854 0.163934 -6.807784
v 0.050485 0.143298 -6.551455
v 0.390845 0.216656 -7.842326
v 0.587957 0.247899 -8.218260
v -0.187159 -0.021301 -8.489817
v -0.144520 -0.066015 -8.115219
v -0.135056 -0.086523 -7.519433
v -0.455856 -0.036286 -8.505733
v -0.424768 -0.127085 -8.081474
v -0.379623 -0.149656 -7.480232
v -0.721089 0.002465 -8.418228
v -0.711949 -0.032964 -8.033904
v -0.633023 -0.036474 -7.439595
v -0.083764 -0.101907 -6.888419
v -0.321252 -0.155635 -6.827525
v -0.570598 -0.013135 -6.792706
v -0.205536 0.346922 -8.488331
v -0.478068 0.389386 -8.503635
v -0.741261 0.375015 -8.416031
v -0.166164 0.372104 -8.112885
v -0.454556 0.470532 -8.077909
v -0.734413 0.413380 -8.030973
v -0.161420 0.377122 -7.515023
v -0.412007 0.496828 -7.474960
v -0.653762 0.440220 -7.436335
v -0.124968 0.400042 -6.878002
v -0.359615 0.536135 -6.818819
v -0.582637 0.502600 -6.791322
v -0.736234 0.212481 -7.423101
v -0.655151 0.259193 -6.812274
v -0.663333 0.364684 -6.389957
v -0.775997 0.191334 -8.463245
v -0.822719 0.196893 -8.024588
v -0.031942 0.130966 -6.924860
v -0.055396 0.134692 -7.532089
v -0.056604 0.146553 -8.134798
v -0.157928 0.161766 -8.546125
v -1.836226 1.016590 -7.410828
v -1.627684 0.872918 -7.538481
v -1.370280 0.758218 -7.595252
v -1.747767 0.932613 -7.142950
v -1.528535 0.732919 -7.246038
v -1.256016 0.658972 -7.340291
v -1.538345 0.794709 -6.746178
v -1.346968 0.594312 -6.836720
v -1.106538 0.540492 -6.926594
v -1.327304 0.638226 -6.296957
v -1.144801 0.423090 -6.386161
v -0.907648 0.349198 -6.500935
v -1.381548 1.162123 -6.749738
v -1.167106 1.131764 -6.834267
v -1.001718 0.954827 -6.919466
v -1.579033 1.276060 -7.151250
v -1.336962 1.223187 -7.246882
v -1.140912 1.040889 -7.333368
v -1.682336 1.295016 -7.423892
v -1.479018 1.212733 -7.544489
v -1.266431 1.072053 -7.592119
v -1.173291 1.003628 -6.309747
v -0.980917 0.984092 -6.395974
v -0.826961 0.810684 -6.501183
v -0.978037 0.704393 -6.955856
v -1.119137 0.806439 -7.374477
v -1.298471 0.903687 -7.643332
v -0.811802 0.542748 -6.562383
v -1.320080 0.864534 -6.271852
v -1.536038 1.021683 -6.715133
v -1.746360 1.149667 -7.118443
v -1.808232 1.181964 -7.439637
v 2.071132 1.255908 -6.105393
v 1.505985 0.975476 -6.611821
v 0.931009 0.299286 -8.285854
v -0.474965 0.178488 -8.633644
v -1.589683 1.061216 -7.631387
v 0.763920 0.520049 -6.078023
# 281 vertices
# 0 texture vertices
# 0 normals
usemtl hand
f 2 4 1
f 5 4 2
f 3 5 2
f 5 6 4
f 1 10 9
f 1 7 10
f 7 11 10
f 7 8 11
f 10 12 9
f 12 10 13
f 10 14 13
f 10 11 14
f 12 17 9
f 12 15 17
f 18 15 16
f 15 18 17
f 9 2 1
f 9 17 2
f 18 2 17
f 2 18 3
f 19 4 6
f 7 4 19
f 8 7 19
f 7 1 4
f 20 22 12
f 23 22 20
f 21 23 20
f 23 24 22
f 16 26 25
f 16 15 26
f 15 22 26
f 15 12 22
f 25 28 27
f 25 26 28
f 22 28 26
f 28 22 24
f 30 31 29
f 32 31 30
f 6 32 30
f 32 33 31
f 5 32 6
f 32 5 34
f 5 35 34
f 5 3 35
f 34 33 32
f 33 34 36
f 35 36 34
f 36 35 37
f 39 40 38
f 31 40 39
f 29 31 39
f 31 33 40
f 42 43 41
f 40 43 42
f 38 40 42
f 40 33 43
f 45 46 44
f 46 45 47
f 38 47 45
f 47 38 42
f 46 49 48
f 46 47 49
f 47 41 49
f 47 42 41
f 44 52 45
f 44 50 52
f 50 53 52
f 50 51 53
f 52 38 45
f 38 52 39
f 52 29 39
f 52 53 29
f 36 55 33
f 55 36 56
f 37 56 36
f 56 37 54
f 56 24 55
f 24 56 28
f 27 56 54
f 56 27 28
f 33 57 43
f 33 55 57
f 55 58 57
f 55 24 58
f 57 41 43
f 41 57 59
f 58 59 57
f 59 58 60
f 62 64 61
f 64 62 65
f 63 65 62
f 65 63 66
f 65 21 64
f 21 65 23
f 66 23 65
f 23 66 24
f 66 58 24
f 58 66 67
f 63 67 66
f 67 63 68
f 67 60 58
f 60 67 69
f 68 69 67
f 69 68 70
f 61 72 71
f 61 64 72
f 64 73 72
f 64 21 73
f 71 75 74
f 71 72 75
f 72 76 75
f 72 73 76
f 53 77 29
f 77 53 78
f 51 78 53
f 78 51 79
f 78 80 77
f 80 78 81
f 79 81 78
f 81 79 82
f 83 77 80
f 77 83 85
f 83 86 85
f 83 84 86
f 85 29 77
f 29 85 30
f 86 30 85
f 30 86 6
f 73 87 76
f 87 73 88
f 21 88 73
f 88 21 20
f 88 89 87
f 89 88 90
f 20 90 88
f 90 20 12
f 90 91 89
f 91 90 92
f 90 13 92
f 90 12 13
f 91 94 93
f 91 92 94
f 92 14 94
f 92 13 14
f 95 98 97
f 95 96 98
f 8 98 96
f 98 8 19
f 97 86 84
f 97 98 86
f 19 86 98
f 86 19 6
f 59 49 41
f 49 59 99
f 59 69 99
f 59 60 69
f 99 48 49
f 48 99 100
f 99 70 100
f 99 69 70
f 8 101 11
f 8 96 101
f 96 102 101
f 96 95 102
f 101 14 11
f 14 101 94
f 101 93 94
f 101 102 93
f 103 107 106
f 103 104 107
f 105 107 104
f 107 105 108
f 107 109 106
f 109 107 110
f 108 110 107
f 110 108 111
f 63 113 68
f 63 112 113
f 112 104 113
f 112 105 104
f 68 114 70
f 68 113 114
f 113 103 114
f 113 104 103
f 115 118 117
f 115 116 118
f 105 118 116
f 118 105 112
f 117 62 61
f 117 118 62
f 112 62 118
f 62 112 63
f 120 121 119
f 121 120 122
f 111 122 120
f 122 111 108
f 122 115 121
f 115 122 116
f 108 116 122
f 116 108 105
f 123 127 126
f 123 124 127
f 124 128 127
f 124 125 128
f 126 130 129
f 126 127 130
f 127 131 130
f 127 128 131
f 44 133 132
f 44 46 133
f 48 133 46
f 133 48 134
f 132 126 129
f 132 133 126
f 134 126 133
f 126 134 123
f 135 138 137
f 135 136 138
f 136 130 138
f 136 129 130
f 137 140 139
f 137 138 140
f 138 131 140
f 138 130 131
f 51 142 141
f 51 50 142
f 50 132 142
f 50 44 132
f 141 136 135
f 141 142 136
f 142 129 136
f 142 132 129
f 109 144 106
f 109 143 144
f 125 144 143
f 144 125 124
f 106 145 103
f 106 144 145
f 124 145 144
f 145 124 123
f 100 134 48
f 134 100 146
f 70 146 100
f 146 70 114
f 146 123 134
f 123 146 145
f 114 145 146
f 145 114 103
f 135 148 141
f 135 147 148
f 147 117 148
f 147 115 117
f 141 149 51
f 141 148 149
f 148 61 149
f 148 117 61
f 115 150 121
f 115 147 150
f 135 150 147
f 150 135 137
f 121 151 119
f 121 150 151
f 137 151 150
f 151 137 139
f 120 152 111
f 152 120 153
f 119 153 120
f 153 119 154
f 152 156 155
f 152 153 156
f 153 157 156
f 153 154 157
f 158 161 160
f 158 159 161
f 159 110 161
f 159 109 110
f 160 152 155
f 160 161 152
f 161 111 152
f 161 110 111
f 162 140 131
f 140 162 164
f 162 165 164
f 162 163 165
f 164 139 140
f 139 164 166
f 164 167 166
f 164 165 167
f 168 171 170
f 168 169 171
f 163 171 169
f 171 163 162
f 171 125 170
f 125 171 128
f 162 128 171
f 128 162 131
f 172 169 168
f 169 172 173
f 158 173 172
f 173 158 160
f 169 174 163
f 169 173 174
f 160 174 173
f 174 160 155
f 157 176 156
f 157 175 176
f 167 176 175
f 176 167 165
f 176 155 156
f 155 176 174
f 176 163 174
f 176 165 163
f 177 181 180
f 177 178 181
f 178 182 181
f 178 179 182
f 181 183 180
f 183 181 184
f 181 185 184
f 181 182 185
f 186 75 76
f 75 186 187
f 179 187 186
f 187 179 178
f 187 74 75
f 74 187 188
f 178 188 187
f 188 178 177
f 189 193 192
f 189 190 193
f 191 193 190
f 193 191 194
f 192 196 195
f 192 193 196
f 193 197 196
f 193 194 197
f 81 198 80
f 198 81 199
f 82 199 81
f 199 82 200
f 199 195 198
f 195 199 192
f 200 192 199
f 192 200 189
f 200 201 189
f 201 200 202
f 200 203 202
f 200 82 203
f 202 177 201
f 177 202 188
f 202 74 188
f 202 203 74
f 177 204 201
f 177 180 204
f 183 204 180
f 204 183 205
f 204 189 201
f 189 204 190
f 204 191 190
f 204 205 191
f 195 207 198
f 195 206 207
f 179 207 206
f 207 179 186
f 198 208 80
f 198 207 208
f 186 208 207
f 208 186 76
f 206 182 179
f 182 206 209
f 206 196 209
f 206 195 196
f 209 185 182
f 185 209 210
f 209 197 210
f 209 196 197
f 211 215 214
f 211 212 215
f 213 215 212
f 215 213 216
f 215 217 214
f 217 215 218
f 216 218 215
f 218 216 219
f 220 216 213
f 216 220 221
f 76 221 220
f 221 76 87
f 221 219 216
f 219 221 222
f 87 222 221
f 222 87 89
f 223 227 226
f 223 224 227
f 225 227 224
f 227 225 228
f 227 229 226
f 229 227 230
f 228 230 227
f 230 228 231
f 230 232 229
f 232 230 233
f 231 233 230
f 233 231 234
f 233 80 232
f 80 233 83
f 233 84 83
f 233 234 84
f 222 235 219
f 235 222 236
f 222 237 236
f 222 89 237
f 236 231 235
f 231 236 234
f 237 234 236
f 234 237 84
f 217 239 238
f 217 218 239
f 219 239 218
f 239 219 235
f 239 225 238
f 225 239 228
f 239 231 228
f 239 235 231
f 220 208 76
f 208 220 240
f 213 240 220
f 240 213 241
f 208 232 80
f 208 240 232
f 240 229 232
f 240 241 229
f 241 226 229
f 226 241 242
f 241 212 242
f 241 213 212
f 242 223 226
f 223 242 243
f 242 211 243
f 242 212 211
f 244 248 247
f 244 245 248
f 246 248 245
f 248 246 249
f 248 250 247
f 250 248 251
f 249 251 248
f 251 249 252
f 250 254 253
f 250 251 254
f 252 254 251
f 254 252 255
f 253 91 93
f 253 254 91
f 254 89 91
f 254 255 89
f 256 260 259
f 256 257 260
f 258 260 257
f 260 258 261
f 260 262 259
f 262 260 263
f 260 264 263
f 260 261 264
f 97 265 95
f 265 97 266
f 84 266 97
f 266 84 267
f 265 257 256
f 265 266 257
f 267 257 266
f 257 267 258
f 249 268 252
f 268 249 269
f 249 270 269
f 249 246 270
f 269 258 268
f 258 269 261
f 269 264 261
f 269 270 264
f 255 237 89
f 237 255 271
f 252 271 255
f 271 252 268
f 271 84 237
f 84 271 267
f 268 267 271
f 267 268 258
f 102 253 93
f 253 102 272
f 102 265 272
f 102 95 265
f 272 250 253
f 250 272 273
f 272 256 273
f 272 265 256
f 259 273 256
f 273 259 274
f 262 274 259
f 274 262 275
f 273 247 250
f 273 274 247
f 274 244 247
f 274 275 244
f 172 159 158
f 159 172 276
f 172 170 276
f 172 168 170
f 276 109 159
f 109 276 143
f 170 143 276
f 143 170 125
f 151 154 119
f 154 151 277
f 151 166 277
f 151 139 166
f 154 175 157
f 154 277 175
f 277 167 175
f 277 166 167
f 197 278 210
f 197 194 278
f 191 278 194
f 278 191 205
f 278 185 210
f 185 278 184
f 278 183 184
f 278 205 183
f 223 279 224
f 223 243 279
f 211 279 243
f 279 211 214
f 279 225 224
f 225 279 238
f 279 217 238
f 279 214 217
f 262 280 275
f 262 263 280
f 264 280 263
f 280 264 270
f 280 244 275
f 244 280 245
f 270 245 280
f 245 270 246
f 74 281 71
f 74 203 281
f 82 281 203
f 281 82 79
f 71 149 61
f 71 281 149
f 79 149 281
f 149 79 51
# 552 elements

*/





Loading a VRML file

/**********************************************************
 Copyright (C) 2001   Daniel Selman
 First distributed with the book "Java 3D Programming"
 by Daniel Selman and published by Manning Publications.
 http://manning.ru/selman
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation, version 2.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 The license can be found on the WWW at:
 http://www.fsf.org/copyleft/gpl.html
 Or by writing to:
 Free Software Foundation, Inc.,
 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 Authors can be contacted at:
 Daniel Selman: daniel@selman.org
 If you make changes you think others would like, please 
 contact one of the authors or someone at the 
 www.j3d.org web site.
 **************************************************************/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.GraphicsConfigTemplate;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.AudioDevice;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.media.j3d.Group;
import javax.media.j3d.Locale;
import javax.media.j3d.Morph;
import javax.media.j3d.Node;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.SceneGraphObject;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import com.sun.j3d.audioengines.javasound.JavaSoundMixer;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.vrml97.VrmlLoader;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
/**
 * This example loads a VRML file, automatically computes the view point to view
 * the objects in the file, and then mouse picks. For each pick all the selected
 * components of the scene are reported (by their VRML name). The VRML scene can
 * be rotates, scaled and translated using the mouse.
 */
public class VrmlPickingTest extends Java3dApplet implements MouseListener {
  PickCanvas pickCanvas = null;
  public VrmlPickingTest() {
  }
  public VrmlPickingTest(String[] args) {
    saveCommandLineArguments(args);
    initJava3d();
  }
  public void start() {
    if (pickCanvas == null)
      initJava3d();
  }
  protected void addCanvas3D(Canvas3D c3d) {
    setLayout(new BorderLayout());
    add(c3d, BorderLayout.CENTER);
    doLayout();
    if (m_SceneBranchGroup != null) {
      c3d.addMouseListener(this);
      pickCanvas = new PickCanvas(c3d, m_SceneBranchGroup);
      pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
      pickCanvas.setTolerance(4.0f);
    }
    c3d.setCursor(new Cursor(Cursor.HAND_CURSOR));
  }
  public TransformGroup[] getViewTransformGroupArray() {
    TransformGroup[] tgArray = new TransformGroup[1];
    tgArray[0] = new TransformGroup();
    Transform3D viewTrans = new Transform3D();
    Transform3D eyeTrans = new Transform3D();
    BoundingSphere sceneBounds = (BoundingSphere) m_SceneBranchGroup
        .getBounds();
    // point the view at the center of the object
    Point3d center = new Point3d();
    sceneBounds.getCenter(center);
    double radius = sceneBounds.getRadius();
    Vector3d temp = new Vector3d(center);
    viewTrans.set(temp);
    // pull the eye back far enough to see the whole object
    double eyeDist = 1.4 * radius / Math.tan(Math.toRadians(40) / 2.0);
    temp.x = 0.0;
    temp.y = 0.0;
    temp.z = eyeDist;
    eyeTrans.set(temp);
    viewTrans.mul(eyeTrans);
    // set the view transform
    tgArray[0].setTransform(viewTrans);
    return tgArray;
  }
  protected BranchGroup createSceneBranchGroup() {
    BranchGroup objRoot = super.createSceneBranchGroup();
    Bounds lightBounds = getApplicationBounds();
    AmbientLight ambLight = new AmbientLight(true, new Color3f(1.0f, 1.0f,
        1.0f));
    ambLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(ambLight);
    DirectionalLight headLight = new DirectionalLight();
    headLight.setInfluencingBounds(lightBounds);
    objRoot.addChild(headLight);
    TransformGroup mouseGroup = createMouseBehaviorsGroup();
    String vrmlFile = null;
    try {
      URL codebase = getWorkingDirectory();
      vrmlFile = codebase.toExternalForm() + "BoxConeSphere.wrl";
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (m_szCommandLineArray != null) {
      switch (m_szCommandLineArray.length) {
      case 0:
        break;
      case 1:
        vrmlFile = m_szCommandLineArray[0];
        break;
      default:
        System.err.println("Usage: VrmlPickingTest [pathname|URL]");
        System.exit(-1);
      }
    }
    BranchGroup sceneRoot = loadVrmlFile(vrmlFile);
    if (sceneRoot != null)
      mouseGroup.addChild(sceneRoot);
    objRoot.addChild(mouseGroup);
    return objRoot;
  }
  private TransformGroup createMouseBehaviorsGroup() {
    TransformGroup examineGroup = new TransformGroup();
    examineGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    examineGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    Bounds behaviorBounds = getApplicationBounds();
    MouseRotate mr = new MouseRotate(examineGroup);
    mr.setSchedulingBounds(behaviorBounds);
    examineGroup.addChild(mr);
    MouseTranslate mt = new MouseTranslate(examineGroup);
    mt.setSchedulingBounds(behaviorBounds);
    examineGroup.addChild(mt);
    MouseZoom mz = new MouseZoom(examineGroup);
    mz.setSchedulingBounds(behaviorBounds);
    examineGroup.addChild(mz);
    return examineGroup;
  }
  private BranchGroup loadVrmlFile(String location) {
    BranchGroup sceneGroup = null;
    Scene scene = null;
    VrmlLoader loader = new VrmlLoader();
    try {
      URL loadUrl = new URL(location);
      try {
        // load the scene
        scene = loader.load(new URL(location));
      } catch (Exception e) {
        System.out.println("Exception loading URL:" + e);
        e.printStackTrace();
      }
    } catch (MalformedURLException badUrl) {
      // location may be a path name
      try {
        // load the scene
        scene = loader.load(location);
      } catch (Exception e) {
        System.out.println("Exception loading file from path:" + e);
        e.printStackTrace();
      }
    }
    if (scene != null) {
      // get the scene group
      sceneGroup = scene.getSceneGroup();
      sceneGroup.setCapability(BranchGroup.ALLOW_BOUNDS_READ);
      sceneGroup.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
      Hashtable namedObjects = scene.getNamedObjects();
      System.out.println("*** Named Objects in VRML file: \n"
          + namedObjects);
      // recursively set the user data here
      // so we can find our objects when they are picked
      java.util.Enumeration enumValues = namedObjects.elements();
      java.util.Enumeration enumKeys = namedObjects.keys();
      if (enumValues != null) {
        while (enumValues.hasMoreElements() != false) {
          Object value = enumValues.nextElement();
          Object key = enumKeys.nextElement();
          recursiveSetUserData(value, key);
        }
      }
    }
    return sceneGroup;
  }
  // method to recursively set the user data for objects in the scenegraph
  // tree
  // we also set the capabilites on Shape3D and Morph objects required by the
  // PickTool
  void recursiveSetUserData(Object value, Object key) {
    if (value instanceof SceneGraphObject != false) {
      // set the user data for the item
      SceneGraphObject sg = (SceneGraphObject) value;
      sg.setUserData(key);
      // recursively process group
      if (sg instanceof Group) {
        Group g = (Group) sg;
        // recurse on child nodes
        java.util.Enumeration enumKids = g.getAllChildren();
        while (enumKids.hasMoreElements() != false)
          recursiveSetUserData(enumKids.nextElement(), key);
      } else if (sg instanceof Shape3D || sg instanceof Morph) {
        PickTool.setCapabilities((Node) sg, PickTool.INTERSECT_FULL);
      }
    }
  }
  // rewritten to check for user data
  public void mouseClicked(MouseEvent e) {
    System.out.println("*** MouseClick ***");
    pickCanvas.setShapeLocation(e);
    PickResult[] results = pickCanvas.pickAllSorted();
    if (results != null) {
      for (int n = 0; n < results.length; n++) {
        PickResult pickResult = results[n];
        System.out
            .println("Sorted PickResult " + n + ": " + pickResult);
        Node actualNode = pickResult.getObject();
        if (actualNode.getUserData() != null) {
          System.out.println("Sorted Object " + n + ": "
              + actualNode.getUserData());
        }
      }
    }
    PickResult pickResult = pickCanvas.pickClosest();
    if (pickResult != null) {
      System.out.println("Closest PickResult: " + pickResult);
      Node actualNode = pickResult.getObject();
      if (actualNode.getUserData() != null) {
        System.out.println("Closest Object: "
            + actualNode.getUserData());
      }
    }
  }
  public void mouseEntered(MouseEvent e) {
  }
  public void mouseExited(MouseEvent e) {
  }
  public void mousePressed(MouseEvent e) {
  }
  public void mouseReleased(MouseEvent e) {
  }
  public static void main(String[] args) {
    System.out.println("Current user.dir: "
        + System.getProperty("user.dir"));
    VrmlPickingTest pickingTest = new VrmlPickingTest(args);
    new MainFrame(pickingTest, 400, 400);
  }
}
/*******************************************************************************
 * Copyright (C) 2001 Daniel Selman
 * 
 * First distributed with the book "Java 3D Programming" by Daniel Selman and
 * published by Manning Publications. http://manning.ru/selman
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, version 2.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * The license can be found on the WWW at: http://www.fsf.org/copyleft/gpl.html
 * 
 * Or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA 02111-1307, USA.
 * 
 * Authors can be contacted at: Daniel Selman: daniel@selman.org
 * 
 * If you make changes you think others would like, please contact one of the
 * authors or someone at the www.j3d.org web site.
 ******************************************************************************/
//*****************************************************************************
/**
 * Java3dApplet
 * 
 * Base class for defining a Java 3D applet. Contains some useful methods for
 * defining views and scenegraphs etc.
 * 
 * @author Daniel Selman
 * @version 1.0
 */
//*****************************************************************************
abstract class Java3dApplet extends Applet {
  public static int m_kWidth = 300;
  public static int m_kHeight = 300;
  protected String[] m_szCommandLineArray = null;
  protected VirtualUniverse m_Universe = null;
  protected BranchGroup m_SceneBranchGroup = null;
  protected Bounds m_ApplicationBounds = null;
  //  protected com.tornadolabs.j3dtree.Java3dTree m_Java3dTree = null;
  public Java3dApplet() {
  }
  public boolean isApplet() {
    try {
      System.getProperty("user.dir");
      System.out.println("Running as Application.");
      return false;
    } catch (Exception e) {
    }
    System.out.println("Running as Applet.");
    return true;
  }
  public URL getWorkingDirectory() throws java.net.MalformedURLException {
    URL url = null;
    try {
      File file = new File(System.getProperty("user.dir"));
      System.out.println("Running as Application:");
      System.out.println("   " + file.toURL());
      return file.toURL();
    } catch (Exception e) {
    }
    System.out.println("Running as Applet:");
    System.out.println("   " + getCodeBase());
    return getCodeBase();
  }
  public VirtualUniverse getVirtualUniverse() {
    return m_Universe;
  }
  //public com.tornadolabs.j3dtree.Java3dTree getJ3dTree() {
  //return m_Java3dTree;
  //  }
  public Locale getFirstLocale() {
    java.util.Enumeration e = m_Universe.getAllLocales();
    if (e.hasMoreElements() != false)
      return (Locale) e.nextElement();
    return null;
  }
  protected Bounds getApplicationBounds() {
    if (m_ApplicationBounds == null)
      m_ApplicationBounds = createApplicationBounds();
    return m_ApplicationBounds;
  }
  protected Bounds createApplicationBounds() {
    m_ApplicationBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    return m_ApplicationBounds;
  }
  protected Background createBackground() {
    Background back = new Background(new Color3f(0.9f, 0.9f, 0.9f));
    back.setApplicationBounds(createApplicationBounds());
    return back;
  }
  public void initJava3d() {
    //  m_Java3dTree = new com.tornadolabs.j3dtree.Java3dTree();
    m_Universe = createVirtualUniverse();
    Locale locale = createLocale(m_Universe);
    BranchGroup sceneBranchGroup = createSceneBranchGroup();
    ViewPlatform vp = createViewPlatform();
    BranchGroup viewBranchGroup = createViewBranchGroup(
        getViewTransformGroupArray(), vp);
    createView(vp);
    Background background = createBackground();
    if (background != null)
      sceneBranchGroup.addChild(background);
    //    m_Java3dTree.recursiveApplyCapability(sceneBranchGroup);
    //  m_Java3dTree.recursiveApplyCapability(viewBranchGroup);
    locale.addBranchGraph(sceneBranchGroup);
    addViewBranchGroup(locale, viewBranchGroup);
    onDoneInit();
  }
  protected void onDoneInit() {
    //  m_Java3dTree.updateNodes(m_Universe);
  }
  protected double getScale() {
    return 1.0;
  }
  public TransformGroup[] getViewTransformGroupArray() {
    TransformGroup[] tgArray = new TransformGroup[1];
    tgArray[0] = new TransformGroup();
    // move the camera BACK a little...
    // note that we have to invert the matrix as
    // we are moving the viewer
    Transform3D t3d = new Transform3D();
    t3d.setScale(getScale());
    t3d.setTranslation(new Vector3d(0.0, 0.0, -20.0));
    t3d.invert();
    tgArray[0].setTransform(t3d);
    return tgArray;
  }
  protected void addViewBranchGroup(Locale locale, BranchGroup bg) {
    locale.addBranchGraph(bg);
  }
  protected Locale createLocale(VirtualUniverse u) {
    return new Locale(u);
  }
  protected BranchGroup createSceneBranchGroup() {
    m_SceneBranchGroup = new BranchGroup();
    return m_SceneBranchGroup;
  }
  protected View createView(ViewPlatform vp) {
    View view = new View();
    PhysicalBody pb = createPhysicalBody();
    PhysicalEnvironment pe = createPhysicalEnvironment();
    AudioDevice audioDevice = createAudioDevice(pe);
    if (audioDevice != null) {
      pe.setAudioDevice(audioDevice);
      audioDevice.initialize();
    }
    view.setPhysicalEnvironment(pe);
    view.setPhysicalBody(pb);
    if (vp != null)
      view.attachViewPlatform(vp);
    view.setBackClipDistance(getBackClipDistance());
    view.setFrontClipDistance(getFrontClipDistance());
    Canvas3D c3d = createCanvas3D();
    view.addCanvas3D(c3d);
    addCanvas3D(c3d);
    return view;
  }
  protected PhysicalBody createPhysicalBody() {
    return new PhysicalBody();
  }
  protected AudioDevice createAudioDevice(PhysicalEnvironment pe) {
    JavaSoundMixer javaSoundMixer = new JavaSoundMixer(pe);
    if (javaSoundMixer == null)
      System.out.println("create of audiodevice failed");
    return javaSoundMixer;
  }
  protected PhysicalEnvironment createPhysicalEnvironment() {
    return new PhysicalEnvironment();
  }
  protected float getViewPlatformActivationRadius() {
    return 100;
  }
  protected ViewPlatform createViewPlatform() {
    ViewPlatform vp = new ViewPlatform();
    vp.setViewAttachPolicy(View.RELATIVE_TO_FIELD_OF_VIEW);
    vp.setActivationRadius(getViewPlatformActivationRadius());
    return vp;
  }
  protected Canvas3D createCanvas3D() {
    GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();
    gc3D.setSceneAntialiasing(GraphicsConfigTemplate.PREFERRED);
    GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getScreenDevices();
    Canvas3D c3d = new Canvas3D(gd[0].getBestConfiguration(gc3D));
    c3d.setSize(getCanvas3dWidth(c3d), getCanvas3dHeight(c3d));
    return c3d;
  }
  protected int getCanvas3dWidth(Canvas3D c3d) {
    return m_kWidth;
  }
  protected int getCanvas3dHeight(Canvas3D c3d) {
    return m_kHeight;
  }
  protected double getBackClipDistance() {
    return 100.0;
  }
  protected double getFrontClipDistance() {
    return 1.0;
  }
  protected BranchGroup createViewBranchGroup(TransformGroup[] tgArray,
      ViewPlatform vp) {
    BranchGroup vpBranchGroup = new BranchGroup();
    if (tgArray != null && tgArray.length > 0) {
      Group parentGroup = vpBranchGroup;
      TransformGroup curTg = null;
      for (int n = 0; n < tgArray.length; n++) {
        curTg = tgArray[n];
        parentGroup.addChild(curTg);
        parentGroup = curTg;
      }
      tgArray[tgArray.length - 1].addChild(vp);
    } else
      vpBranchGroup.addChild(vp);
    return vpBranchGroup;
  }
  protected void addCanvas3D(Canvas3D c3d) {
    setLayout(new BorderLayout());
    add(c3d, BorderLayout.CENTER);
    doLayout();
  }
  protected VirtualUniverse createVirtualUniverse() {
    return new VirtualUniverse();
  }
  protected void saveCommandLineArguments(String[] szArgs) {
    m_szCommandLineArray = szArgs;
  }
  protected String[] getCommandLineArguments() {
    return m_szCommandLineArray;
  }
}

//File: BoxConeSphere.wrl
/*
#VRML V2.0 utf8
#Cosmo Worlds V2.0
DEF Box Transform {
  children  Shape {
    appearance  Appearance {
      material  Material {
  ambientIntensity  0.25
  diffuseColor  0.33 0.34 0.6
  specularColor  0.29 0.29 0.29
  emissiveColor  0 0 0
  shininess  0.79
  transparency  0
      }
    }
    geometry  Box {
    }
  }
  translation  0 1 0
}
DEF Cone Transform {
  children  Shape {
    appearance  Appearance {
      material  Material {
  ambientIntensity  0.238636
  diffuseColor  0.88 0.15 0.01
  specularColor  0.19 0.03 0.03
  emissiveColor  0 0 0
  shininess  0.08
  transparency  0
      }
    }
    geometry  Cone {
    }
  }
  translation  0 3 0
}
DEF Sphere Transform {
  children  Shape {
    appearance  Appearance {
      material  Material {
  ambientIntensity  0.25
  diffuseColor  1 0.85 0
  specularColor  0.87 0.25 0.25
  emissiveColor  0 0 0
  shininess  1
  transparency  0
      }
    }
    geometry  Sphere {
    }
  }
  translation  0 5 0
}
DEF Start Viewpoint {
  position  -6.83953 4.47054 7.64913
  orientation  -0.237334 -0.967284 -0.0896321  0.744468
  fieldOfView  0.785398
  description  "Start"
}

*/





Object File Loader

/*
 * @(#)ConfigObjLoad.java 1.3 02/04/01 15:04:14
 * 
 * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *  - Redistribution in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGES.
 * 
 * You acknowledge that Software is not designed,licensed or intended for use in
 * the design, construction, operation or maintenance of any nuclear facility.
 */
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Sensor;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.behaviors.sensor.Mouse6DPointerBehavior;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.universe.ConfiguredUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
public class ConfigObjLoad {
  private boolean spin = false;
  private boolean noTriangulate = false;
  private boolean noStripify = false;
  private double creaseAngle = 60.0;
  private URL filename = null;
  private ConfiguredUniverse u;
  public BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();
    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.7);
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);
    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objScale.addChild(objTrans);
    int flags = ObjectFile.RESIZE;
    if (!noTriangulate)
      flags |= ObjectFile.TRIANGULATE;
    if (!noStripify)
      flags |= ObjectFile.STRIPIFY;
    ObjectFile f = new ObjectFile(flags,
        (float) (creaseAngle * Math.PI / 180.0));
    Scene s = null;
    try {
      s = f.load(filename);
    } catch (FileNotFoundException e) {
      System.err.println(e);
      System.exit(1);
    } catch (ParsingErrorException e) {
      System.err.println(e);
      System.exit(1);
    } catch (IncorrectFormatException e) {
      System.err.println(e);
      System.exit(1);
    }
    objTrans.addChild(s.getSceneGroup());
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    if (spin) {
      Transform3D yAxis = new Transform3D();
      Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
          4000, 0, 0, 0, 0, 0);
      RotationInterpolator rotator = new RotationInterpolator(
          rotationAlpha, objTrans, yAxis, 0.0f,
          (float) Math.PI * 2.0f);
      rotator.setSchedulingBounds(bounds);
      objTrans.addChild(rotator);
    }
    // Set up the background
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
    Background bgNode = new Background(bgColor);
    bgNode.setApplicationBounds(bounds);
    objRoot.addChild(bgNode);
    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    objRoot.addChild(ambientLightNode);
    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
    Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight light1 = new DirectionalLight(light1Color,
        light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);
    DirectionalLight light2 = new DirectionalLight(light2Color,
        light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);
    return objRoot;
  }
  private void usage() {
    System.out
        .println("Usage: java ObjLoad [-s] [-n] [-t] [-c degrees] <.obj file>");
    System.out.println("  -s Spin (no user interaction)");
    System.out.println("  -n No triangulation");
    System.out.println("  -t No stripification");
    System.out
        .println("  -c Set crease angle for normal generation (default is 60 without");
    System.out
        .println("     smoothing group info, otherwise 180 within smoothing groups)");
    System.exit(0);
  } // End of usage
  public void init() {
    if (filename == null) {
      try {
        filename = new URL("file:galleon.obj");
      } catch (MalformedURLException e) {
        System.err.println(e);
        System.exit(1);
      }
    }
    // Get the config file URL from the j3d.configURL property or use the
    // default config file "j3d1x1-window" in the current directory.
    URL configURL = ConfiguredUniverse.getConfigURL("file:j3d1x1-window");
    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    u = new ConfiguredUniverse(configURL);
    // Get the ViewingPlatform.
    ViewingPlatform viewingPlatform = u.getViewingPlatform();
    // This will move the ViewPlatform back a bit so the objects in the
    // scene can be viewed. This will only have an effect if the config
    // file sets the window eyepoint policy to something other than
    // RELATIVE_TO_COEXISTENCE, which is the default eyepoint policy
    // applied by ConfiguredUniverse.
    // 
    // The default view attach policy for ConfiguredUniverse applications
    // is NOMINAL_SCREEN. This sets the view platform origin in the
    // physical world to the center of coexistence, which allows eye
    // positions expressed relative to coexistence to see the appropriate
    // field of view automatically.
    viewingPlatform.setNominalViewingTransform();
    // Add a ViewPlatformBehavior if not specified in the config file.
    if (!spin && viewingPlatform.getViewPlatformBehavior() == null) {
      OrbitBehavior orbit = new OrbitBehavior(u.getCanvas(),
          OrbitBehavior.REVERSE_ALL);
      BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
          0.0), 100.0);
      orbit.setSchedulingBounds(bounds);
      viewingPlatform.setViewPlatformBehavior(orbit);
    }
    // See if there"s a 6 degree of freedom mouse in the environment.
    // We look for one named "mouse6d".
    Map sensorMap = null;
    sensorMap = u.getNamedSensors();
    if (sensorMap != null) {
      Sensor mouse6d = (Sensor) sensorMap.get("mouse6d");
      if (mouse6d != null) {
        Mouse6DPointerBehavior behavior = new Mouse6DPointerBehavior(
            mouse6d, 1.0, true);
        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
            0.0, 0.0), 100.0);
        behavior.setSchedulingBounds(bounds);
        scene.addChild(behavior);
        scene.addChild(behavior.getEcho());
      }
    }
    // Listen for a typed "q", "Q", or "Escape" key on each canvas to
    // allow a convenient exit from full screen configurations.
    Canvas3D[] canvases;
    canvases = u.getViewer().getCanvas3Ds();
    class QuitListener extends KeyAdapter {
      public void keyTyped(KeyEvent e) {
        char c = e.getKeyChar();
        if (c == "q" || c == "Q" || c == 27)
          System.exit(0);
      }
    }
    QuitListener quitListener = new QuitListener();
    for (int i = 0; i < canvases.length; i++)
      canvases[i].addKeyListener(quitListener);
    // Make the scenegraph live.
    u.addBranchGraph(scene);
  }
  public ConfigObjLoad(String[] args) {
    if (args.length != 0) {
      for (int i = 0; i < args.length; i++) {
        if (args[i].startsWith("-")) {
          if (args[i].equals("-s")) {
            spin = true;
          } else if (args[i].equals("-n")) {
            noTriangulate = true;
          } else if (args[i].equals("-t")) {
            noStripify = true;
          } else if (args[i].equals("-c")) {
            if (i < args.length - 1) {
              creaseAngle = (new Double(args[++i])).doubleValue();
            } else
              usage();
          } else {
            usage();
          }
        } else {
          try {
            if ((args[i].indexOf("file:") == 0)
                || (args[i].indexOf("http") == 0)) {
              filename = new URL(args[i]);
            } else if (args[i].charAt(0) != "/") {
              filename = new URL("file:./" + args[i]);
            } else {
              filename = new URL("file:" + args[i]);
            }
          } catch (MalformedURLException e) {
            System.err.println(e);
            System.exit(1);
          }
        }
      }
    }
    init();
  }
  public static void main(String[] args) {
    new ConfigObjLoad(args);
  }
}





Object Loader 3

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
/**
 * !Diese Klasse wurde fur das Laden uber ein JAR-Archiv oder Applet welches ein
 * JAR - Archiv nutzt angepasst Um das Programm als einfache Applikation uber
 * einen class-File laufen zu lassen bitte auf den Code zum Einladen der OBJ
 * Datei im Tutorial zuruckgreifen!
 */
public class ObjectLoader extends Applet {
  /**
   * init Methoden fur die Darstellung als Applet
   */
  public void init() {
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();
    canvas3D = new Canvas3D(config);
    add("Center", canvas3D);
    BranchGroup szene = macheSzene();
    szene.rupile();
    universe = new SimpleUniverse(canvas3D);
    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(szene);
  }
  /**
   * Erstellt den Szenegraphen
   * 
   * @return BranchGroup
   */
  public BranchGroup macheSzene() {
    BranchGroup objWurzel = new BranchGroup();
    // Transformation, 2 Rotationen:
    Transform3D drehung = new Transform3D();
    Transform3D drehung2 = new Transform3D();
    drehung.rotX(Math.PI / 4.0d);
    drehung2.rotY(Math.PI / 5.0d);
    drehung.mul(drehung2);
    TransformGroup objDreh = new TransformGroup(drehung);
    //Loader
    ObjectFile file = new ObjectFile(ObjectFile.RESIZE);
    Scene scene = null;
    try {
        scene = file.load(ClassLoader.getSystemResource("teapot.obj"));
    } catch (Exception e) {
    }
    objDreh.addChild(scene.getSceneGroup());
    DirectionalLight d_Licht = new DirectionalLight(new Color3f(1.0f, 0.5f,
        0.3f), new Vector3f(-1.0f, -1.0f, -1.0f));
    d_Licht.setInfluencingBounds(new BoundingSphere(new Point3d(0.0d, 0.0d,
        0.0d), 100.0d));
    //    d_Licht.setColor(new Color3f(1.0f,0.5f,0.3f));
    objDreh.addChild(d_Licht);
    objWurzel.addChild(objDreh);
    return objWurzel;
  }
  /**
   * gibt speicher frei
   */
  public void destroy() {
    universe.removeAllLocales();
  }
  public static void main(String[] args) {
    frame = new MainFrame(new ObjectLoader(), 500, 500);
    frame.setTitle("ObjectLoader");
  }
  //---- Attribute -----------------------
  private SimpleUniverse universe;
  private Canvas3D canvas3D;
  private static Frame frame;
}





Object Loader Example

/*
 * @(#)ObjLoad.java 1.22 02/10/21 13:46:12
 * 
 * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *  - Redistribution in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGES.
 * 
 * You acknowledge that Software is not designed,licensed or intended for use in
 * the design, construction, operation or maintenance of any nuclear facility.
 */
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.universe.PlatformGeometry;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
public class ObjLoad extends Applet {
  private boolean spin = false;
  private boolean noTriangulate = false;
  private boolean noStripify = false;
  private double creaseAngle = 60.0;
  private URL filename = null;
  private SimpleUniverse u;
  private BoundingSphere bounds;
  public BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();
    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.7);
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);
    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objScale.addChild(objTrans);
    int flags = ObjectFile.RESIZE;
    if (!noTriangulate)
      flags |= ObjectFile.TRIANGULATE;
    if (!noStripify)
      flags |= ObjectFile.STRIPIFY;
    ObjectFile f = new ObjectFile(flags,
        (float) (creaseAngle * Math.PI / 180.0));
    Scene s = null;
    try {
      s = f.load(filename);
    } catch (FileNotFoundException e) {
      System.err.println(e);
      System.exit(1);
    } catch (ParsingErrorException e) {
      System.err.println(e);
      System.exit(1);
    } catch (IncorrectFormatException e) {
      System.err.println(e);
      System.exit(1);
    }
    objTrans.addChild(s.getSceneGroup());
    bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    if (spin) {
      Transform3D yAxis = new Transform3D();
      Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
          4000, 0, 0, 0, 0, 0);
      RotationInterpolator rotator = new RotationInterpolator(
          rotationAlpha, objTrans, yAxis, 0.0f,
          (float) Math.PI * 2.0f);
      rotator.setSchedulingBounds(bounds);
      objTrans.addChild(rotator);
    }
    // Set up the background
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
    Background bgNode = new Background(bgColor);
    bgNode.setApplicationBounds(bounds);
    objRoot.addChild(bgNode);
    return objRoot;
  }
  private void usage() {
    System.out
        .println("Usage: java ObjLoad [-s] [-n] [-t] [-c degrees] <.obj file>");
    System.out.println("  -s Spin (no user interaction)");
    System.out.println("  -n No triangulation");
    System.out.println("  -t No stripification");
    System.out
        .println("  -c Set crease angle for normal generation (default is 60 without");
    System.out
        .println("     smoothing group info, otherwise 180 within smoothing groups)");
    System.exit(0);
  } // End of usage
  public void init() {
    if (filename == null) {
      // Applet
      try {
        URL path = getCodeBase();
        filename = new URL(path.toString() + "./galleon.obj");
      } catch (MalformedURLException e) {
        System.err.println(e);
        System.exit(1);
      }
    }
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();
    Canvas3D c = new Canvas3D(config);
    add("Center", c);
    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);
    // add mouse behaviors to the ViewingPlatform
    ViewingPlatform viewingPlatform = u.getViewingPlatform();
    PlatformGeometry pg = new PlatformGeometry();
    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    pg.addChild(ambientLightNode);
    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
    Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight light1 = new DirectionalLight(light1Color,
        light1Direction);
    light1.setInfluencingBounds(bounds);
    pg.addChild(light1);
    DirectionalLight light2 = new DirectionalLight(light2Color,
        light2Direction);
    light2.setInfluencingBounds(bounds);
    pg.addChild(light2);
    viewingPlatform.setPlatformGeometry(pg);
    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    viewingPlatform.setNominalViewingTransform();
    if (!spin) {
      OrbitBehavior orbit = new OrbitBehavior(c,
          OrbitBehavior.REVERSE_ALL);
      BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
          0.0), 100.0);
      orbit.setSchedulingBounds(bounds);
      viewingPlatform.setViewPlatformBehavior(orbit);
    }
    u.addBranchGraph(scene);
  }
  // Caled if running as a program
  public ObjLoad(String[] args) {
    if (args.length != 0) {
      for (int i = 0; i < args.length; i++) {
        if (args[i].startsWith("-")) {
          if (args[i].equals("-s")) {
            spin = true;
          } else if (args[i].equals("-n")) {
            noTriangulate = true;
          } else if (args[i].equals("-t")) {
            noStripify = true;
          } else if (args[i].equals("-c")) {
            if (i < args.length - 1) {
              creaseAngle = (new Double(args[++i])).doubleValue();
            } else
              usage();
          } else {
            usage();
          }
        } else {
          try {
            if ((args[i].indexOf("file:") == 0)
                || (args[i].indexOf("http") == 0)) {
              filename = new URL(args[i]);
            } else if (args[i].charAt(0) != "/") {
              filename = new URL("file:./" + args[i]);
            } else {
              filename = new URL("file:" + args[i]);
            }
          } catch (MalformedURLException e) {
            System.err.println(e);
            System.exit(1);
          }
        }
      }
    }
  }
  // Running as an applet
  public ObjLoad() {
  }
  public void destroy() {
    u.cleanup();
  }
  //
  // The following allows ObjLoad to be run as an application
  // as well as an applet
  //
  public static void main(String[] args) {
    new MainFrame(new ObjLoad(args), 700, 700);
  }
}