To try either case, please comment out the other part accordingly. You'll
find help information for you to do that in the program. Please try it and
tell me what do you think.   Thanks a lot ---- white



/*  GoemetryTest.java */
import javax.vecmath.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.picking.behaviors.PickRotateBehavior;
import java.util.*;
import java.awt.*;

public class GeometryTest extends javax.swing.JFrame {

    /** Creates new form PickAllTest */
    public GeometryTest() {
        initComponents();
        initGraphics();
    }

    private void initComponents() {

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        pack();
        java.awt.Dimension screenSize = 
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new java.awt.Dimension(640, 480));
        setLocation((screenSize.width-640)/2,(screenSize.height-480)/2);
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }

    private void initGraphics(){
        GraphicsConfiguration config =
        SimpleUniverse.getPreferredConfiguration();

        canvas = new Canvas3D(config);
        u = new SimpleUniverse(canvas);

        imagePanel = new javax.swing.JPanel(new java.awt.BorderLayout());
        getContentPane().add(imagePanel, java.awt.BorderLayout.CENTER);

        imagePanel.add(canvas, java.awt.BorderLayout.CENTER);

        BranchGroup scene = createSceneGraph();

        ViewingPlatform viewingPlatform = u.getViewingPlatform();
        Transform3D t = new Transform3D();
        Point3d cp = new Point3d();
        bounds.getCenter(cp);

        t.set(new Vector3d(cp.x, cp.y, cp.z + 3.0*bounds.getRadius()));
        (viewingPlatform.getViewPlatformTransform()).setTransform(t);

        PointLight light = new PointLight();
        bounds.setRadius(bounds.getRadius()*3.0);
        light.setInfluencingBounds(bounds);
        light.setPosition((float)cp.x, (float)cp.y, (float)(cp.z + 
bounds.getRadius()));
        scene.addChild(light);

        TransformGroup trans = new TransformGroup();
        trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

        trans.addChild(scene);

        BranchGroup group = new BranchGroup();
        group.addChild(trans);

        PickRotateBehavior behavior = new PickRotateBehavior(group, canvas, bounds);
        group.addChild(behavior);

        group.compile();

        u.addBranchGraph(group);

        imagePanel.updateUI();
    }

    private BranchGroup createSceneGraph(){
        double vertices[] = {1.037,-0.4716,1.068,  2.443,-0.5301,1.096,  
1.03678,0.471573,1.06775,
        2.44312,0.530086,1.09561,  1.719,-0.4636,1.17,  1.7188,0.46365,1.16968,
        1.72312,5.68E-17,1.21023,  2.43882,2.6472E-11,1.14196,  
1.00554,2.578E-6,1.08912,
        4.089,3.0,7.1,  4.089,-3.0,7.1,  -1.911,3.0,7.1,
        -1.911,-3.0,7.1,  4.089,3.0,1.1,  4.089,-3.0,1.1,
        -1.911,3.0,1.1,  -1.911,-3.0,1.1,  4.089,3.0,-4.9,
        4.098,-3.0,-4.9,  -1.911,3.0,-4.9,  -1.911,-3.0,-4.9,
        1.0119,0.22479,1.08473,  2.21273,0.46486,1.15069,  1.41353,5.68E-17,1.18693,
        2.02425,5.68E-17,1.20708,  1.72558,-0.3,1.20129,  2.43636,-0.25208,1.13545,
        1.0119,-0.22479,1.08473,  1.72558,0.3,1.20129,  2.43275,0.35636,1.13183,
        1.245,-0.4968,1.116,  1.24463,0.496766,1.11588,  2.213,-0.4649,1.151};



        //Triangluate the following 2 polygons using Java3D GeometryInfo class
        int polyIndices1[] = {6, 28, 5, 31, 21, 8, 23};
        int polyIndices2[] = {30, 4, 25, 6, 23, 8, 27};

        int stripCounts1[] = {7};
        int stripCounts2[] = {7};

        GeometryInfo triangle1 = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
        triangle1.setCoordinates(vertices);
        triangle1.setCoordinateIndices(polyIndices1);
        triangle1.setStripCounts(stripCounts1);

        GeometryInfo triangle2 = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
        triangle2.setCoordinates(vertices);
        triangle2.setCoordinateIndices(polyIndices2);
        triangle2.setStripCounts(stripCounts2);

        IndexedGeometryArray iga1 = triangle1.getIndexedGeometryArray();
        IndexedGeometryArray iga2 = triangle2.getIndexedGeometryArray();

        int numNodes1 = iga1.getIndexCount();
        int numNodes2 = iga2.getIndexCount();
        int []igaIndices1 = new int[numNodes1];
        int []igaIndices2 = new int[numNodes2];
        iga1.getCoordinateIndices(0, igaIndices1);
        iga2.getCoordinateIndices(0, igaIndices2);

        /*
        //********** Comment out this part if you try manually triangulate ******
        //Get the coordinate indices of the triangles for the 2 polygons
        int nodeIndices[] = new int[numNodes1 + numNodes2];
        for (int ii=0; ii<numNodes1+numNodes2; ii++) {
            if (ii < numNodes1)
                nodeIndices[ii] = igaIndices1[ii];
            else
                nodeIndices[ii] = igaIndices2[ii-numNodes1];
        }
        //**********************************************************************
        */

        //****** Comment out this part if you try GeometryInfo triangulate *****
        //Manually triangulate the 2 polygons
        int nodeIndices[] = {8,23,21,  21,23,31,  31,23,28,  28,23,6,  31,28,5,
                        8,27,23,  23,27,30,  23,30,25,  25,30,4,  23,25,6};
        //**********************************************************************

        //Draw the polygons
        GeometryInfo gi1 = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
        gi1.setCoordinates(vertices);
        gi1.setCoordinateIndices(nodeIndices);

        NormalGenerator ng = new NormalGenerator(2.5*Math.PI);
        gi1.recomputeIndices();
        ng.generateNormals(gi1);


        Vector3f[] myNormals = gi1.getNormals();
        int[] myNormalIndices = gi1.getNormalIndices();

        System.out.println("The number of normals are " + myNormals.length);

        IndexedGeometryArray ga1 = gi1.getIndexedGeometryArray();
        ga1.setNormals(0, myNormals);
        ga1.setNormalIndices(0, myNormalIndices);

        javax.media.j3d.Material myMaterial = new javax.media.j3d.Material();
        myMaterial.setAmbientColor(0.2f, 0.2f, 0.2f);
        myMaterial.setDiffuseColor(0.2f, 0.5f, 1.0f);
        myMaterial.setShininess(30.0f);
        myMaterial.setLightingEnable(true);

        PolygonAttributes myPolyAttr = new PolygonAttributes();
        myPolyAttr.setPolygonMode(PolygonAttributes.POLYGON_FILL);//POLYGON_LINE);//

        ColoringAttributes myColorAttr1 = new ColoringAttributes(0.2f, 0.5f, 1.0f,
                                                    ColoringAttributes.FASTEST);

        Appearance myApp1 = new Appearance();
        myApp1.setMaterial(myMaterial);
        myApp1.setPolygonAttributes(myPolyAttr);
        myApp1.setColoringAttributes(myColorAttr1);

        Shape3D part1 = new Shape3D(ga1, myApp1);



        //Draw all the uniqe vertices
        int ii, jj;
        ArrayList nodesList = new ArrayList();

        //Get all the uniqe vertices
        for (ii=0; ii<nodeIndices.length; ii++) {
            if (ii == 0)
                nodesList.add(new Integer(nodeIndices[ii]));
            else {
                for (jj=0; jj<ii; jj++)
                    if (nodeIndices[ii] == nodeIndices[jj])
                        break;
                if (ii == jj)
                    nodesList.add(new Integer(nodeIndices[ii]));
            }
        }
        int []nodes = new int[nodesList.size()];
        for (ii=0; ii<nodes.length; ii++)
            nodes[ii] = ((Integer)(nodesList.get(ii))).intValue();

        System.out.println("\nThe number of vertices are " + nodes.length);

        IndexedPointArray ga2 = new IndexedPointArray(vertices.length/3,
                                  IndexedPointArray.COORDINATES, nodes.length);
        ga2.setCoordinates(0, vertices);
        ga2.setCoordinateIndices(0, nodes);

        Appearance myAppr2 = new Appearance();

        PointAttributes myPtAttr = new PointAttributes();
        myPtAttr.setPointSize(8);

        ColoringAttributes myColorAttr = new ColoringAttributes(0.8f, 0.1f, 0.1f,
                                                    ColoringAttributes.FASTEST);

        myAppr2.setPointAttributes(myPtAttr);
        myAppr2.setColoringAttributes(myColorAttr);

        Shape3D part2 = new Shape3D(ga2, myAppr2);


        BranchGroup group = new BranchGroup();

        group.addChild(part1);
        bounds = new BoundingSphere(group.getBounds());
        group.addChild(part2);

        return group;
    }


    public static void main(String args[]) {
        new GeometryTest().show();
    }


    // Variables declaration - do not modify
    // End of variables declaration

    private BoundingSphere bounds;
    private SimpleUniverse u;
    private Canvas3D canvas;
    private javax.swing.JPanel imagePanel;
}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to