Has anyone been able to get both sides of a polygon to respond to
illumination? The FAQ says that to make both sides of a polygon respond to
illumination one needs to the following in a Shape3D's apperance object:

        in PolygonAttributes:
                setPolygonMode(PolygonAttributes.POLYGON_FILL);
                setCullFace(polyatt.CULL_NONE);
                setBackFaceNormalFlip(true);

        in RenderingAttributes:
                setDepthBufferEnable(true);

        a material.

I have tried all of these things, but my polygons only respond to
illumination on one side. Here is a sourcefile that illustrates my problem.
Any help would be greatly appreciated. Thanks.



// File begins here

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

// an appearance class that sets all the attrubutes I believe are
// necessary to render two sided shaded polygons
class PCHAppearance extends Appearance{
        Color3f PATCH_COLOR = new Color3f(0.3f,0.3f,1.0f);

        public PCHAppearance(){
                PolygonAttributes polyatt = new PolygonAttributes();
                polyatt.setPolygonMode(PolygonAttributes.POLYGON_FILL);
                polyatt.setCullFace(polyatt.CULL_NONE);
                polyatt.setBackFaceNormalFlip(true);
                setPolygonAttributes(polyatt);

                RenderingAttributes ra = new RenderingAttributes();
                ra.setDepthBufferEnable(true);
                setRenderingAttributes(ra);

                ColoringAttributes c = new ColoringAttributes();
                c.setShadeModel(c.SHADE_FLAT);
                c.setColor(PATCH_COLOR);
                setColoringAttributes(c);

                setMaterial( new Material(PATCH_COLOR, new Color3f(0.1f,
0.1f, 0.1f), PATCH_COLOR, new Color3f(0.0f, 0.0f, 0.0f), 80.0f ));
                }
        }

public class HelloUniverse extends Applet {
        static final int NUM_VERTS = 4;

        // Create the root of the branch graph
    public BranchGroup createSceneGraph() {
        BranchGroup objRoot = new BranchGroup();
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

//------------------------------------
// Create a universal bounds for the light source,
    BoundingSphere uniBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),
10 );

    // Set up the global, ambient light
    Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(uniBounds);
    objRoot.addChild(aLgt);

    // Set up the directional (infinite) light source
    Color3f lColor1 = new Color3f(0.9f, 0.9f, 0.9f);
    Vector3f lDir1  = new Vector3f(1.0f, 1.0f, -1.0f);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(uniBounds);
    objRoot.addChild(lgt1);

// ----------------------
        // This section creates a pair of TriangleStripArrays
        int[] strips = new int[1];
        strips[0] = NUM_VERTS;
        Point3f verts[] = new Point3f[NUM_VERTS];
        Vector3f norms[] = new Vector3f[NUM_VERTS];

        // set up array 1 with normals pointing in positive z direction
        TriangleStripArray p1 = new TriangleStripArray(NUM_VERTS,
GeometryArray.COORDINATES | GeometryArray.NORMALS , strips);
        for(int i = 0; i < NUM_VERTS; i++){
                verts[i] = new Point3f(0.0f, 0.0f,  0.0f);
                norms[i] = new Vector3f(1.0f, 0.0f,  0.0f);
                }

        verts[0] = new Point3f(0.0f, 0.5f,  0.0f);
        verts[1] = new Point3f(0.5f, 0.5f,  0.0f);
        verts[2] = new Point3f(0.0f, 0.0f,  0.0f);
        verts[3] = new Point3f(0.5f, 0.0f,  0.0f);

        for(int i = 0; i < NUM_VERTS; i++){
                norms[i] = new Vector3f(0.0f, 0.0f,  1.0f);
                }

        p1.setCoordinates(0,verts);
        p1.setNormals(0, norms);

        // set up patch 2 offset on the x axis with normals in -ive z direction
        TriangleStripArray p2 = new TriangleStripArray(NUM_VERTS,
GeometryArray.COORDINATES | GeometryArray.NORMALS , strips);

        verts[0] = new Point3f(-0.5f, 0.0f,  0.0f);
        verts[1] = new Point3f(0.0f, 0.0f,  0.0f);
        verts[2] = new Point3f(-0.5f, -0.5f,  0.0f);
        verts[3] = new Point3f(0.0f, -0.5f,  0.0f);

        for(int i = 0; i < NUM_VERTS; i++){
                norms[i] = new Vector3f(0.0f, 0.0f,  -1.0f);
                }

        p2.setCoordinates(0,verts);
        p2.setNormals(0,norms);


// create a pair of objects with Appearances
        objTrans.addChild(new Shape3D(p1, new PCHAppearance()));
        objTrans.addChild(new Shape3D(p2, new PCHAppearance()));
        // ------------------------------


        // Create rotation behavior
        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);
        BoundingSphere bounds =
            new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        rotator.setSchedulingBounds(bounds);
        objTrans.addChild(rotator);

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

        return objRoot;
    }

    public HelloUniverse() {
        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();
        SimpleUniverse u = new SimpleUniverse(c);
    u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);
    }

    // The following allows HelloUniverse to be run as an application
    // as well as an applet
    public static void main(String[] args) {
        new MainFrame(new HelloUniverse(), 256, 256);
    }
}

// File ends here


=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/

Reply via email to