Hi all,
To continue with this talk, I wrote a j3d program which is much simpler
than the
previous "J3DAxis.java". I attached the program "BallCylinder.java" with
this email. It compiles and runs directly. As its name says, this program
creates a ball and cylinder. Also OrbitBehavior is added for convenience.
When I played with it, I found 2 interesting phenomina:
1. The ouput was different when compiled with jdk1.4 or jdk1.3.
2. If I compiled the scenegraph, the cylinder looked transparent. If I
don't compile, the ball looked transparent.

I strongly suspect that it is a java3d bug. I hope I am wrong.

Thanks.


Mingtian


import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import com.sun.j3d.utils.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.picking.behaviors.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.vp.*;
import javax.media.j3d.*;
import javax.vecmath.*;


public class BallCylinder extends JFrame
{
    private Container m_contentPane = null;

        // 3d view box dimension.
        private float m_viewWidth = 3.0f;
        private float m_viewHeight = 2.0f;
        private final float m_viewDepth = 1.0f;

        // Axis radius
        public final float m_AXISRADIUS = m_viewDepth * 0.05f;

        // Arrow radius and length.
        public final float m_ARROWRADIUS = m_AXISRADIUS * 2.0f;
        public final float m_ARROWLEN = m_ARROWRADIUS * 3.0f;

    //Construct the application
    public BallCylinder()
        {
                // Set layout manager.
                m_contentPane = getContentPane();
                m_contentPane.setLayout(new BorderLayout());

                // Create Universe.
                SimpleUniverse universe = createUniverse();
                javax.media.j3d.Locale locale = universe.getLocale();

                // Attach the scene graph to the virtual universe
                locale.addBranchGraph(createSceneGraph());

                // Create a group of lights.
                BranchGroup lights = createLights();
                locale.addBranchGraph(lights);

                // Set visible.
                ((JPanel)m_contentPane).setPreferredSize(new Dimension(600, 500));
        pack();
        validate();
        setVisible(true);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

        /**
         *
         * @return
         */
        private SimpleUniverse createUniverse()
        {
                // Create a Canvas3D.
                GraphicsConfigTemplate3D gct3D = new GraphicsConfigTemplate3D();
                gct3D.setSceneAntialiasing(GraphicsConfigTemplate.PREFERRED);
                GraphicsConfiguration gc = 
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(gct3D);

                Canvas3D canvas3D = new Canvas3D(gc);
                m_contentPane.add(canvas3D, BorderLayout.CENTER);

                // Create a Universe.
                SimpleUniverse universe = new SimpleUniverse(canvas3D);

                // Set initial transform to the ViewingPlatform.
                ViewingPlatform viewingPlatform = universe.getViewingPlatform();
                viewingPlatform.setNominalViewingTransform();
                
viewingPlatform.getViewPlatform().setViewAttachPolicy(View.NOMINAL_HEAD);

                // add orbit behavior to ViewingPlatform
                OrbitBehavior orbitBehavior = new OrbitBehavior(canvas3D, 
OrbitBehavior.REVERSE_ALL|OrbitBehavior.STOP_ZOOM);
                BoundingSphere bounds =  new BoundingSphere(new Point3d(0.0, 0.0, 
0.0), Double.MAX_VALUE);
                orbitBehavior.setSchedulingBounds(bounds);
//              orbitBehavior.setRotationCenter(new Point3d(m_viewWidth/2.0, 
m_viewHeight/2.0, m_viewDepth/2.0));
                viewingPlatform.setViewPlatformBehavior(orbitBehavior);

                View view = universe.getViewer().getView();
                view.setFrontClipPolicy(View.VIRTUAL_EYE);
                view.setBackClipPolicy(View.VIRTUAL_EYE);
                view.setBackClipDistance(4 * (m_viewWidth + m_viewHeight + 
m_viewDepth));
                view.setFrontClipDistance(1.0e-5);

                view.setWindowEyepointPolicy(View.RELATIVE_TO_WINDOW);
                view.setWindowResizePolicy(View.VIRTUAL_WORLD);
                view.setWindowMovementPolicy(View.PHYSICAL_WORLD);

                return universe;
        }

        /**
         *
         * @return
         */
        private BranchGroup createLights()
        {
                // Create the root of the branch graph
                BranchGroup root = new BranchGroup();

                // Create light bound.
                BoundingSphere bounds =  new BoundingSphere(new Point3d(0.0,0.0,0.0), 
Double.MAX_VALUE);

                // Set up the ambient light
                Color3f ambientColor = new Color3f(0.4f, 0.4f, 0.4f);
                AmbientLight ambientLightNode = new AmbientLight(ambientColor);
                ambientLightNode.setInfluencingBounds(bounds);
                root.addChild(ambientLightNode);

                // Create a series of point lights
                Color3f lightColor = new Color3f(1.0f, 1.0f, 1.0f);
                Point3f atten = new Point3f(3.0f, 0.01f, 0.01f);
                PointLight light;
                Point3f p3f;

                // 2 corner point lights.
                p3f = new Point3f(0.0f, 0.0f, m_viewDepth*2.0f);
                light = new PointLight(true, lightColor, p3f, atten);
                light.setInfluencingBounds(bounds);
                root.addChild(light);

                p3f = new Point3f(m_viewWidth, m_viewHeight, m_viewDepth*2.0f);
                light = new PointLight(true, lightColor, p3f, atten);
                light.setInfluencingBounds(bounds);
                root.addChild(light);

                // 1 center point light
                p3f = new Point3f(m_viewWidth / 2.0f, m_viewHeight / 2.0f, 
m_viewDepth*2.0f);
                light = new PointLight(true, lightColor, p3f, atten);
                light.setInfluencingBounds(bounds);
                root.addChild(light);

                return root;
        }

        public BranchGroup createSceneGraph()
        {
          Color3f eColor    = new Color3f(0.0f, 0.0f, 0.0f);
          Color3f sColor    = new Color3f(1.0f, 1.0f, 1.0f);
          Color3f objColor  = new Color3f(0.2f, 0.7f, 0.2f);

          Transform3D t;

          // 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(1.0);
          objScale.setTransform(t3d);
          objRoot.addChild(objScale);

          // Create a bounds for the background and lights
          BoundingSphere bounds =
                new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);


          Material m = new Material(objColor, eColor, objColor, sColor, 100.0f);
          Appearance a = new Appearance();
          m.setLightingEnable(true);
          a.setMaterial(m);
//        PolygonAttributes pa = new PolygonAttributes();
//        pa.setCullFace(PolygonAttributes.CULL_NONE);
//        a.setPolygonAttributes(pa);

          // Create transformations for the positional lights
          t = new Transform3D();
          Vector3d lPos1 =  new Vector3d(0.0, 0.3, 0.0);
          t.set(lPos1);
          TransformGroup l1Trans = new TransformGroup(t);
          objScale.addChild(l1Trans);

          t = new Transform3D();
          Vector3d lPos2 = new Vector3d(0.0, 0.0, 0.0);
          t.set(lPos2);
          TransformGroup l2Trans = new TransformGroup(t);
          objScale.addChild(l2Trans);

          l1Trans.addChild(new Sphere(0.05f,
                                                                  
Sphere.GENERATE_NORMALS | Sphere.ENABLE_GEOMETRY_PICKING, 15, a));
//        l1Trans.addChild(new Cylinder(0.05f, 0.8f, Cylinder.GENERATE_NORMALS | 
Cylinder.GEOMETRY_NOT_SHARED, a));
//        l2Trans.addChild(new Sphere(0.1f,
//                                                                
Sphere.GENERATE_NORMALS | Sphere.ENABLE_GEOMETRY_PICKING, 15, a));
          l2Trans.addChild(new Cylinder(0.1f, 0.4f, Cylinder.GENERATE_NORMALS | 
Cylinder.GEOMETRY_NOT_SHARED, a));

          // Let Java 3D perform optimizations on this scene graph.
//        objRoot.compile();

          return objRoot;
  }

    //Main method
    public static void main(String[] args)
    {
       new BallCylinder();
    }
}

Reply via email to