Example usage for javax.media.j3d Shape3D setAppearance

List of usage examples for javax.media.j3d Shape3D setAppearance

Introduction

In this page you can find the example usage for javax.media.j3d Shape3D setAppearance.

Prototype

public void setAppearance(Appearance appearance) 

Source Link

Document

Sets the appearance component of this Shape3D node.

Usage

From source file:RedGreenGriffin.java

public BranchGroup createSceneGraph(int i) {
    System.out.println("Creating scene for: " + URLString);
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();
    try {/*from   w w  w.ja v a 2  s.  c  o m*/
        Transform3D myTransform3D = new Transform3D();
        myTransform3D.setTranslation(new Vector3f(+0.0f, -0.15f, -3.6f));
        TransformGroup objTrans = new TransformGroup(myTransform3D);
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        Transform3D t = new Transform3D();
        TransformGroup tg = new TransformGroup(t);
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objTrans.addChild(tg);
        URL url = new URL(URLString);
        ObjectFile f = new ObjectFile();
        f.setFlags(ObjectFile.RESIZE | ObjectFile.TRIANGULATE | ObjectFile.STRIPIFY);
        System.out.println("About to load");

        Scene s = f.load(url);
        Transform3D myTrans = new Transform3D();
        myTrans.setTranslation(new Vector3f(eyeOffset, -eyeOffset, 0F));
        TransformGroup mytg = new TransformGroup(myTrans);
        //mytg.addChild(s.getSceneGroup());
        tg.addChild(mytg);
        Transform3D myTrans2 = new Transform3D();
        myTrans2.setTranslation(new Vector3f(-eyeOffset, +eyeOffset, 0F));
        TransformGroup mytg2 = new TransformGroup(myTrans2);
        //mytg2.addChild(s.getSceneGroup());
        Hashtable table = s.getNamedObjects();
        for (Enumeration e = table.keys(); e.hasMoreElements();) {
            Object key = e.nextElement();
            System.out.println(key);
            Object obj = table.get(key);
            System.out.println(obj.getClass().getName());
            Shape3D shape = (Shape3D) obj;
            //shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
            Appearance ap = new Appearance();
            Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
            Color3f red = new Color3f(0.7f, .0f, .15f);
            Color3f green = new Color3f(0f, .7f, .15f);
            ap.setMaterial(new Material(green, black, green, black, 1.0f));
            Appearance ap2 = new Appearance();
            ap2.setMaterial(new Material(red, black, red, black, 1.0f));
            float transparencyValue = 0.5f;
            TransparencyAttributes t_attr = new TransparencyAttributes(TransparencyAttributes.BLENDED,
                    transparencyValue, TransparencyAttributes.BLEND_SRC_ALPHA,
                    TransparencyAttributes.BLEND_ONE);
            ap2.setTransparencyAttributes(t_attr);
            ap2.setRenderingAttributes(new RenderingAttributes());
            ap.setTransparencyAttributes(t_attr);
            ap.setRenderingAttributes(new RenderingAttributes());
            // bg.addChild(ap);
            shape.setAppearance(ap);
            mytg2.addChild(new Shape3D(shape.getGeometry(), ap2));
            mytg.addChild(new Shape3D(shape.getGeometry(), ap));
        }
        tg.addChild(mytg2);
        System.out.println("Finished Loading");
        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
        Color3f light1Color = new Color3f(.9f, 0.9f, 0.9f);
        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
        DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
        light1.setInfluencingBounds(bounds);
        objTrans.addChild(light1);
        // Set up the ambient light
        Color3f ambientColor = new Color3f(1.0f, .4f, 0.3f);
        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
        ambientLightNode.setInfluencingBounds(bounds);
        objTrans.addChild(ambientLightNode);

        MouseRotate behavior = new MouseRotate();
        behavior.setTransformGroup(tg);
        objTrans.addChild(behavior);
        // Create the translate behavior node
        MouseTranslate behavior3 = new MouseTranslate();
        behavior3.setTransformGroup(tg);
        objTrans.addChild(behavior3);
        behavior3.setSchedulingBounds(bounds);

        KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(tg);
        keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000.0));
        objTrans.addChild(keyNavBeh);

        behavior.setSchedulingBounds(bounds);
        objRoot.addChild(objTrans);
    } catch (Throwable t) {
        System.out.println("Error: " + t);
    }
    return objRoot;
}

From source file:KeyNavigateTest.java

protected Group loadGeometryGroup(String szModel, Appearance app) throws java.io.FileNotFoundException {
    // load the object file
    Scene scene = null;/*from  w  ww .  j  a  va  2s . c  o m*/
    Shape3D shape = null;

    // read in the geometry information from the data file
    ObjectFile objFileloader = new ObjectFile(ObjectFile.RESIZE);

    scene = objFileloader.load(szModel);

    // retrieve the Shape3D object from the scene
    BranchGroup branchGroup = scene.getSceneGroup();
    shape = (Shape3D) branchGroup.getChild(0);
    shape.setAppearance(app);

    return branchGroup;
}

From source file:LoaderTest.java

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);//from ww w  .ja  v a 2 s. com
    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;
}

From source file:ExAppearance.java

private Group createObject(Appearance app, double scale, double xpos, double ypos) {

    // Create a transform group node to scale and position the object.
    Transform3D t = new Transform3D();
    t.set(scale, new Vector3d(xpos, ypos, 0.0));
    TransformGroup objTrans = new TransformGroup(t);

    // Create a second transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime.
    TransformGroup spinTg = new TransformGroup();
    spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    // Create a simple shape leaf node and set the appearance
    Shape3D shape = new Tetrahedron();
    shape.setAppearance(app);

    // add it to the scene graph.
    spinTg.addChild(shape);/*from   w  ww .  j a  va2  s.  c o m*/

    // Create a new Behavior object that will perform the desired
    // operation on the specified transform object and add it into
    // the scene graph.
    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 5000, 0, 0, 0, 0, 0);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, spinTg, 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);

    // Add the behavior and the transform group to the object
    objTrans.addChild(rotator);
    objTrans.addChild(spinTg);

    return objTrans;
}

From source file:AppearanceExplorer.java

BranchGroup createBeethoven() {
    java.net.URL beethovenURL = null;
    try {/*from w  ww.j  a  v  a2s  .  c om*/
        beethovenURL = new java.net.URL(codeBaseString + "beethoven.obj");
    } catch (Exception e) {
        System.err.println("Exception: " + e);
        System.exit(1);
    }

    int flags = ObjectFile.RESIZE;
    ObjectFile f = new ObjectFile(flags);
    Scene s = null;
    try {
        s = f.load(beethovenURL);
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    Group sceneGroup = s.getSceneGroup();

    Hashtable namedObjects = s.getNamedObjects();
    Enumeration e = namedObjects.keys();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        Shape3D shape = (Shape3D) namedObjects.get(name);
        shape.setAppearance(appearance);
    }

    BranchGroup retVal = new BranchGroup();
    retVal.addChild(s.getSceneGroup());
    return retVal;
}

From source file:AppearanceExplorer.java

BranchGroup createGalleon() {
    java.net.URL galleonURL = null;
    try {//from w  w w. j  av  a  2 s  .  c  om
        galleonURL = new java.net.URL(codeBaseString + "galleon.obj");
    } catch (Exception e) {
        System.err.println("Exception: " + e);
        System.exit(1);
    }

    int flags = ObjectFile.RESIZE;
    ObjectFile f = new ObjectFile(flags);
    Scene s = null;
    try {
        s = f.load(galleonURL);
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    Group sceneGroup = s.getSceneGroup();

    Hashtable namedObjects = s.getNamedObjects();
    Enumeration e = namedObjects.keys();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        //System.out.println("name = " + name);
        Shape3D shape = (Shape3D) namedObjects.get(name);
        shape.setAppearance(appearance);
    }

    BranchGroup retVal = new BranchGroup();
    retVal.addChild(s.getSceneGroup());
    return retVal;
}