Hi Florin,
Thanks for explanation. I tried what you said for quite a while.
The problem still exists. I checked the transformations, finding nothing
unreasonable. 
I think this is an interesting problem. So, I wrapped around the code
and made a compilable/runnable program called "J3DAxis.java" as attached.
I hope that someone will be interested in play with and look into
the program. 
Basically, the program creates a 3d coordinate frame by using Cylinders
and Cones. OrbitBehavior is added for playing around. When you see
result, you will find Cones and Cylinders look transparent and the object
depth is not correctly set. I just couldn't figure out the problem.

I am looking forward to responses!

Thanks

Mingtian Ni


On Mon, 28 Oct 2002, Florin Herinean wrote:

> Well, from the picture it seems that both your cylinder and the cone are
> somehow created in a wrong way. They are not transparent, but you are seing
> the "inside" of them, not the "outside". Apparently, it looks like your
> entire axis group labeled DN, although is pointing in the correct direction,
> is mirrored against the plane defined by the other two axes, so the outside
> is inside and vice-versa. The resulting pseudo-transparency is because of
> the face culling. You can see the interior of both the cone and the
> cylinder. Check your transforms!
> 
> Cheers,
> 
> Florin
> 
> -----Urspr�ngliche Nachricht-----
> Von: Mingtian Ni [mailto:mni@;CSE.UNL.EDU]
> Gesendet: Sonntag, 27. Oktober 2002 06:04
> An: [EMAIL PROTECTED]
> Betreff: [JAVA3D] Geometry looks transparent
> 
> 
> Hi there,
> I am developing an application with java3d.
> It worked very good so far. However, I found that
> whatever I created in the scene graph looked transparent.
> An example is given in the attached image where the cone
> is supposed to block the cylinder. But we can still see
> the cylinder through the top cone.
> What happens there? I didn't turn on the transparency attribute.
> I has been struggling with this for quite a few days.
> Some body can tell me?
> Any response is appreciated! To make the question clearer, I attach
> the code I am using here. Sorry for the long email.
> 
> Mingtian Ni
> 
> 
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.universe.*;
import com.sun.j3d.utils.behaviors.vp.*;
import javax.media.j3d.*;
import javax.vecmath.*;


public class J3DAxis 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 J3DAxis()
        {
                // Set layout manager.
                m_contentPane = getContentPane();
                m_contentPane.setLayout(new BorderLayout());

                // Create the scene
                BranchGroup scene = createSceneGraph();

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

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

                // 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);
                Transform3D transform = new Transform3D();
                
viewingPlatform.getMultiTransformGroup().getTransformGroup(0).getTransform(transform);
                Transform3D t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, 0.0f, Math.max(m_viewWidth, 
m_viewHeight)*2.0f));
                transform.mul(t);
                t.setRotation(new AxisAngle4d(1.0, 0.0, 0.0, Math.PI/4.0));
                transform.mul(t);
                
viewingPlatform.getMultiTransformGroup().getTransformGroup(0).setTransform(transform);

                // 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.02f, 0.02f);
                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);


                // Create 2 directional light
                Color3f dLightColor = new Color3f(0.3f, 0.3f, 0.3f);
                Vector3f lightDirection  = new Vector3f(0.5f, -0.5f, -0.6f);
                DirectionalLight dLight = new DirectionalLight(dLightColor, 
lightDirection);
                dLight.setInfluencingBounds(bounds);
                root.addChild(dLight);

                lightDirection  = new Vector3f(-0.5f, 0.5f, -0.6f);
                dLight = new DirectionalLight(dLightColor, lightDirection);
                dLight.setInfluencingBounds(bounds);
                root.addChild(dLight);

                return root;
        }

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

                // Create the transform group node and initialize it to the Identity.
                TransformGroup tg = new TransformGroup();
                root.addChild(tg);

                // Set up axises.
                root.addChild(createAxises(m_viewWidth, m_viewHeight, m_viewDepth));

                // Compile the scene graph to improve performance.
                root.setPickable(false);
                root.compile();

                return root;
        }

        /**
         *
         * @param width view box widht in x axis
         * @param height view box height in y axis
         * @param depth view box depth in z axis
         * @return a TransformGroup representing 3 axises and a view box frame
         */
        private TransformGroup createAxises(float width, float height, float depth)
        {
                TransformGroup root = new TransformGroup();

                // Create appearence.
                Appearance app = new Appearance();
                Material mm = new Material();
                mm.setLightingEnable(true);
                mm.setAmbientColor(0.0f, 1.0f, 0.0f);
                mm.setDiffuseColor(0.0f, 1.0f, 0.0f);
                mm.setSpecularColor(0.0f, 1.0f, 0.0f);
                mm.setShininess(100.0f);
                app.setMaterial(mm);

                Cylinder cy;
                Cone arrow;
                TransformGroup axisGroup, cyGroup, arrowGroup, labelGroup;
                Transform3D t;
                Shape3D sh;

                // Create y axis.
                axisGroup = new TransformGroup();

                cyGroup = new TransformGroup();
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, height/2.0f, 0.0f));
                cyGroup.setTransform(t);

                cy = new Cylinder(m_AXISRADIUS, height, Cylinder.GENERATE_NORMALS, 
app);
                cyGroup.addChild(cy);
                axisGroup.addChild(cyGroup);

                arrowGroup = new TransformGroup();
                arrowGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, height + m_ARROWLEN/2.0f, 0.0f));
                arrowGroup.setTransform(t);

                arrow = new Cone(m_ARROWRADIUS, m_ARROWLEN, Cylinder.GENERATE_NORMALS, 
app);
                arrowGroup.addChild(arrow);
                axisGroup.addChild(arrowGroup);

                labelGroup = new TransformGroup();
                labelGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, height + m_ARROWLEN, 0.0f));
                labelGroup.setTransform(t);

                sh = new Shape3D(getRaster("2nd Col"));
                labelGroup.addChild(sh);

                axisGroup.addChild(labelGroup);

                root.addChild(axisGroup);

                // Create x axis.
                axisGroup = new TransformGroup();
                axisGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setRotation(new AxisAngle4d(0.0, 0.0, 1.0, -Math.PI/2.0));
                axisGroup.setTransform(t);

                cyGroup = new TransformGroup();
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, width/2.0f, 0.0f));
                cyGroup.setTransform(t);

                cy = new Cylinder(m_AXISRADIUS, width, Cylinder.GENERATE_NORMALS, 
app);
                cyGroup.addChild(cy);
                axisGroup.addChild(cyGroup);

                arrowGroup = new TransformGroup();
                arrowGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, width + m_ARROWLEN/2.0f, 0.0f));
                arrowGroup.setTransform(t);

                arrow = new Cone(m_ARROWRADIUS, m_ARROWLEN, Cylinder.GENERATE_NORMALS, 
app);
                arrowGroup.addChild(arrow);

                axisGroup.addChild(arrowGroup);

                labelGroup = new TransformGroup();
                labelGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, width + m_ARROWLEN, 0.0f));
                labelGroup.setTransform(t);

                sh = new Shape3D(getRaster("1st Col"));
                labelGroup.addChild(sh);

                axisGroup.addChild(labelGroup);

                root.addChild(axisGroup);

                // Create z axis.
                axisGroup = new TransformGroup();
                axisGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setRotation(new AxisAngle4d(1.0, 0.0, 0.0, Math.PI/2.0));
                axisGroup.setTransform(t);

                cyGroup = new TransformGroup();
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, depth/2.0f, 0.0f));
                cyGroup.setTransform(t);

                cy = new Cylinder(m_AXISRADIUS, depth, Cylinder.GENERATE_NORMALS, 
app);
                cyGroup.addChild(cy);
                axisGroup.addChild(cyGroup);

                arrowGroup = new TransformGroup();
                arrowGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, depth + m_ARROWLEN/2.0f, 0.0f));
                arrowGroup.setTransform(t);

                arrow = new Cone(m_ARROWRADIUS, m_ARROWLEN, Cylinder.GENERATE_NORMALS, 
app);
                arrowGroup.addChild(arrow);

                axisGroup.addChild(arrowGroup);

                labelGroup = new TransformGroup();
                labelGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                t = new Transform3D();
                t.setTranslation(new Vector3f(0.0f, depth + m_ARROWLEN, 0.0f));
                labelGroup.setTransform(t);

                sh = new Shape3D(getRaster("DN"));
                labelGroup.addChild(sh);

                axisGroup.addChild(labelGroup);

                root.addChild(axisGroup);

                // Create the view box.
                app = new Appearance();
                ColoringAttributes ca = new ColoringAttributes(1.0f, 1.0f, 1.0f, 
ColoringAttributes.SHADE_FLAT);
                app.setColoringAttributes(ca);

                float[] vts =
                { 0.0f, 0.0f, 0.0f,
                        width, 0.0f, 0.0f,
                        width, height, 0.0f,
                        0.0f, height, 0.0f,
                        0.0f, 0.0f, depth,
                        width, 0.0f, depth,
                        width, height, depth,
                        0.0f, height, depth
                };
                int[] indices =
                { 0, 1, 2, 3, 0,
                        4, 5, 6, 7, 4,
                        0, 4,
                        1, 5,
                        2, 6,
                        3, 7
                };
                int[] strips = {5, 5, 2, 2, 2, 2};

                IndexedLineStripArray box = new IndexedLineStripArray(8,
                                
IndexedLineStripArray.COORDINATES|IndexedTriangleStripArray.BY_REFERENCE,
                    indices.length,
                strips);
                box.setCoordRefFloat(vts);
                box.setCoordinateIndices(0, indices);

                sh = new Shape3D(box, app);
                root.addChild(sh);

                return root;
        }

        /**
         *
         * @param str
         * @return
         */
        private javax.media.j3d.Raster getRaster(String str)
        {
                // Create an empty raster.
                javax.media.j3d.Raster raster = new javax.media.j3d.Raster();

                // Get a font metrics.
                Font font = new Font("Times", Font.PLAIN, 12);
                FontMetrics fm = getFontMetrics(font);

                // Calculate the raster size.
                int width = SwingUtilities.computeStringWidth(fm, str)+6;
                int height = 18;

                // Create an BufferedImage.
                BufferedImage image = new BufferedImage(width,
                                height,
                    BufferedImage.TYPE_INT_BGR);

                // Draw the input string on the BufferedImage.
                Graphics2D g2d = (Graphics2D)image.getGraphics();
                g2d.setFont(font);
                g2d.setColor(Color.gray);
                g2d.fillRect(0, 0, width, height);
                g2d.setColor(Color.yellow);
                g2d.drawRect(0, 0, width-1, height-1);
                g2d.drawString(str, 3, 14);

                // Set the BufferedImage to the raster.
                raster.setImage(new ImageComponent2D(ImageComponent2D.FORMAT_RGB, 
image));
                raster.setSize(width, height);
                raster.setType(javax.media.j3d.Raster.RASTER_COLOR);
                raster.setCapability( javax.media.j3d.Raster.ALLOW_IMAGE_WRITE );
                raster.setCapability( javax.media.j3d.Raster.ALLOW_SIZE_READ );

                return raster;
        }

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

Reply via email to