Example usage for javax.media.j3d Appearance setCapability

List of usage examples for javax.media.j3d Appearance setCapability

Introduction

In this page you can find the example usage for javax.media.j3d Appearance setCapability.

Prototype

public final void setCapability(int bit) 

Source Link

Document

Sets the specified capability bit.

Usage

From source file:TickTockCollision.java

private Group createBox(double scale, Vector3d pos) {
    // Create a transform group node to scale and position the object.
    Transform3D t = new Transform3D();
    t.set(scale, pos);//from www  . j av  a2  s .c  om
    TransformGroup objTrans = new TransformGroup(t);

    // Create a simple shape leaf node and add it to the scene graph
    Shape3D shape = new Box(0.5, 5.0, 1.0);
    objTrans.addChild(shape);

    // Create a new ColoringAttributes object for the shape's
    // appearance and make it writable at runtime.
    Appearance app = shape.getAppearance();
    ColoringAttributes ca = new ColoringAttributes();
    ca.setColor(0.6f, 0.3f, 0.0f);
    app.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE);
    app.setColoringAttributes(ca);

    // Create a new Behavior object that will perform the collision
    // detection on the specified object, and add it into
    // the scene graph.
    CollisionDetector cd = new CollisionDetector(shape);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    cd.setSchedulingBounds(bounds);

    // Add the behavior to the scene graph
    objTrans.addChild(cd);

    return objTrans;
}

From source file:edu.uci.ics.jung.visualization3d.VisualizationViewer.java

public BranchGroup createSceneGraph(final Canvas3D canvas) {

    objRoot = new BranchGroup();
    objRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);

    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    //      t3d.setScale(0.05);
    objScale.setTransform(t3d);/*from ww  w.  ja  v  a  2 s.c om*/
    objRoot.addChild(objScale);

    Transform3D tt = new Transform3D();
    tt.setScale(.05);
    tt.setTranslation(new Vector3f(0, 0, -30.f));
    objTrans = new TransformGroup(tt);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
    objScale.addChild(objTrans);
    //      objRoot.addChild(objTrans);

    // Create Colors, Materials,  and Appearances.
    Appearance look = new Appearance();
    Color3f objColor = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f black = new Color3f(0.f, 0.f, 0.f);
    Color3f white = new Color3f(1.0f, 1.0f, 0.6f);
    Color3f gray = new Color3f(.2f, .2f, .2f);
    Color3f red = new Color3f(1.0f, 0, 0);
    Color3f yellow = new Color3f(1, 1, 0);

    Material objMaterial = new Material(objColor, black, objColor, white, 100.0f);
    Material blackMaterial = new Material(objColor, black, black, objColor, 10.0f);
    Material whiteMaterial = new Material(white, white, white, white, 100.0f);
    Material grayMaterial = new Material(gray, black, gray, gray, 100.0f);

    Material redMaterial = new Material(red, black, red, red, 100.0f);
    Material yellowMaterial = new Material(yellow, black, yellow, yellow, 100);

    look.setMaterial(new Material(objColor, black, objColor, white, 100.0f));
    Appearance blackLook = new Appearance();
    blackLook.setMaterial(blackMaterial);

    Appearance whiteLook = new Appearance();
    whiteLook.setMaterial(whiteMaterial);

    Appearance grayLook = new Appearance();
    grayLook.setMaterial(grayMaterial);
    grayLook.setCapability(Appearance.ALLOW_MATERIAL_READ);
    grayLook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);

    final Appearance redLook = new Appearance();
    redLook.setMaterial(redMaterial);
    //      vertexLook = redLook;

    Appearance objLook = new Appearance();
    objLook.setMaterial(objMaterial);
    grayLook = objLook;
    final Appearance yellowLook = new Appearance();
    yellowLook.setMaterial(yellowMaterial);
    Bounds bounds = new BoundingSphere(new Point3d(), 300);

    MouseRotate behavior1 = new MouseRotate();
    behavior1.setTransformGroup(objTrans);
    objTrans.addChild(behavior1);
    behavior1.setSchedulingBounds(bounds);

    MouseWheelZoom behavior2 = new MouseWheelZoom();
    behavior2.setTransformGroup(objTrans);
    //      behavior2.setFactor(10);
    objTrans.addChild(behavior2);
    behavior2.setSchedulingBounds(bounds);

    MouseTranslate behavior3 = new MouseTranslate();
    behavior3.setTransformGroup(objTrans);
    objTrans.addChild(behavior3);
    behavior3.setSchedulingBounds(bounds);

    PickTranslateBehavior ptb = new PickTranslateBehavior(objRoot, canvas, bounds, PickTool.GEOMETRY);
    ptb.setSchedulingBounds(bounds);
    //      objTrans.addChild(ptb);
    ptb.setupCallback(new PickingCallback() {

        public void transformChanged(int type, TransformGroup tg) {
            if (tg == null)
                return;
            Transform3D t3d = new Transform3D();
            tg.getTransform(t3d);
            //            System.err.println(tg+" transformChanged \n"+t3d);
            Point3f p1 = new Point3f();
            V v = vertexMap.getKey(tg);
            //            Transform3D lvw = new Transform3D();
            //            tg.getLocalToVworld(lvw);
            //            System.err.println("lvw = \n"+lvw);
            //            lvw.invert();
            //            System.err.println("invert lvw = \n"+lvw);
            Point3f p0 = layout.transform(v);
            //            Transform3D vwip = new Transform3D();
            //            canvas.getVworldToImagePlate(vwip);
            //            System.err.println("vwip=\n"+vwip);
            //            t3d.mul(lvw);
            t3d.transform(p1);
            //            scale.transform(p1);
            System.err.println(
                    "change location for vertex " + v + ", transformGroup " + tg + " from " + p0 + " to " + p1);
            //            p1.set(p1.getX()*2,p1.getY()*2,p1.getZ()*2);
            //            layout.setLocation(v, p1);

        }
    });

    PickSphereBehavior psb = new PickSphereBehavior(objRoot, canvas, bounds);

    PickVertexBehavior pvb = new PickVertexBehavior(objRoot, canvas, bounds,
            renderContext.getPickedVertexState());
    objTrans.addChild(pvb);
    pvb.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            for (V v : graph.getVertices()) {
                VertexGroup<V> vg = vertexMap.get(v);
                Appearance look = redLook;
                if (renderContext.getPickedVertexState().isPicked(v)) {
                    look = yellowLook;
                }
                Node node = vg.getShape();
                if (node instanceof Primitive) {
                    ((Primitive) node).setAppearance(look);
                }
            }

        }
    });

    //Shine it with two colored lights.
    Color3f lColor1 = new Color3f(.5f, .5f, .5f);
    Color3f lColor2 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lDir2 = new Vector3f(-1.0f, 0.0f, -1.0f);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    AmbientLight ambient = new AmbientLight(lColor1);
    lgt2.setInfluencingBounds(bounds);
    ambient.setInfluencingBounds(bounds);
    objRoot.addChild(lgt2);
    objRoot.addChild(ambient);

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

    //      VisRunner runner = new VisRunner((IterativeContext)elayout);
    //      runner.relax();

    return objRoot;
}

From source file:TextureByReference.java

public BranchGroup createSceneGraph() {

    // create the root of the branch group
    BranchGroup objRoot = new BranchGroup();

    // create the transform group node and initialize it
    // enable the TRANSFORM_WRITE capability so that it can be modified
    // at runtime. Add it to the root of the subgraph
    Transform3D rotate = new Transform3D();
    TransformGroup objTrans = new TransformGroup(rotate);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);/*w w  w . j  a v  a2  s  .c om*/

    // bounds
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // set up some light
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Vector3f lDir1 = new Vector3f(-1.0f, -0.5f, -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);

    Appearance appearance = new Appearance();

    // enable the TEXTURE_WRITE so we can modify it at runtime
    appearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

    // load the first texture
    TextureLoader loader = new TextureLoader(urls[0], TextureLoader.BY_REFERENCE | TextureLoader.Y_UP, this);
    // get the texture from the loader
    Texture2D tex = (Texture2D) loader.getTexture();

    // get the BufferedImage to convert to TYPE_4BYTE_ABGR and flip
    // get the ImageComponent because we need it anyway
    ImageComponent2D imageComp = (ImageComponent2D) tex.getImage(0);
    BufferedImage bImage = imageComp.getImage();
    // convert the image
    bImage = ImageOps.convertImage(bImage, BufferedImage.TYPE_4BYTE_ABGR);
    // flip the image
    ImageOps.flipImage(bImage);
    imageComp.set(bImage);

    tex.setCapability(Texture.ALLOW_IMAGE_WRITE);
    tex.setBoundaryModeS(Texture.CLAMP);
    tex.setBoundaryModeT(Texture.CLAMP);
    tex.setBoundaryColor(1.0f, 1.0f, 1.0f, 1.0f);

    // set the image of the texture
    tex.setImage(0, imageComp);

    // set the texture on the appearance
    appearance.setTexture(tex);

    // set texture attributes
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    appearance.setTextureAttributes(texAttr);

    // set material properties
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    appearance.setMaterial(new Material(white, black, white, black, 1.0f));

    // create a scale transform
    Transform3D scale = new Transform3D();
    scale.set(.6);
    TransformGroup objScale = new TransformGroup(scale);
    objTrans.addChild(objScale);

    tetra = new Tetrahedron(true);
    tetra.setAppearance(appearance);
    objScale.addChild(tetra);

    // create the behavior
    animate = new AnimateTexturesBehavior(tex, urls, appearance, this);
    animate.setSchedulingBounds(bounds);

    objTrans.addChild(animate);

    // add a rotation behavior so we can see all sides of the tetrahedron
    Transform3D yAxis = new Transform3D();
    Alpha rotorAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
    RotationInterpolator rotator = new RotationInterpolator(rotorAlpha, objTrans, yAxis, 0.0f,
            (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);

    // have java3d perform optimizations on this scene graph
    objRoot.compile();

    return objRoot;
}

From source file:ffx.potential.MolecularAssembly.java

/**
 * <p>//from  w  w  w. ja va  2  s.c  o  m
 * createBox</p>
 */
public void createBox() {
    int vertices = 8;
    LineArray la = new LineArray(4 * vertices,
            GeometryArray.COORDINATES | GeometryArray.COLOR_4 | GeometryArray.NORMALS);
    la.setCapability(LineArray.ALLOW_COORDINATE_WRITE);
    la.setCapability(LineArray.ALLOW_COORDINATE_READ);
    la.setCapability(LineArray.ALLOW_COLOR_WRITE);
    la.setCapability(LineArray.ALLOW_COUNT_READ);
    la.setCapability(LineArray.ALLOW_INTERSECT);
    la.setCapability(LineArray.ALLOW_FORMAT_READ);
    // Create a normal
    // for (ListIterator li = bondlist.listIterator(); li.hasNext(); ){
    // la.setCoordinate(i, a1);
    // la.setColor(i, col);
    // la.setNormal(i++, a1);
    // }
    ColoringAttributes cola = new ColoringAttributes(new Color3f(), ColoringAttributes.SHADE_GOURAUD);
    Appearance app = new Appearance();
    lineAttributes = new LineAttributes();
    lineAttributes.setLineWidth(RendererCache.bondwidth);
    lineAttributes.setCapability(LineAttributes.ALLOW_WIDTH_WRITE);
    lineAttributes.setLineAntialiasingEnable(true);
    app.setLineAttributes(lineAttributes);
    app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_READ);
    app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE);
    RenderingAttributes ra = new RenderingAttributes();
    ra.setAlphaTestValue(0.1f);
    ra.setAlphaTestFunction(RenderingAttributes.GREATER);
    ra.setDepthBufferEnable(true);
    ra.setDepthBufferWriteEnable(true);
    app.setRenderingAttributes(ra);
    app.setColoringAttributes(cola);
    Shape3D wireframe = new Shape3D(la, app);
    // PickTool.setCapabilities(wire, PickTool.INTERSECT_COORD);
    wireframe.setUserData(this);
    wireframe.setBounds(new BoundingSphere(new Point3d(0, 0, 0), 10.0));
    try {
        wireframe.setBoundsAutoCompute(false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    wireframe.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    wireframe.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    // return wire;
}

From source file:ffx.potential.MolecularAssembly.java

private Shape3D renderWire() {
    ArrayList<ROLS> bonds = getBondList();
    int numbonds = bonds.size();
    if (numbonds < 1) {
        return null;
    }/* www  . j a v  a2 s. co  m*/

    Vector3d bondmidpoint = new Vector3d();
    double[] mid = { 0, 0, 0 };
    Vector3d v1 = new Vector3d();
    Vector3d v2 = new Vector3d();
    float[] a1 = { 0, 0, 0 };
    float[] a2 = { 0, 0, 0 };
    float[] col = new float[4];
    Bond bond;

    Atom atom1, atom2;
    LineArray la = new LineArray(4 * numbonds,
            GeometryArray.COORDINATES | GeometryArray.COLOR_4 | GeometryArray.NORMALS);
    la.setCapability(LineArray.ALLOW_COORDINATE_WRITE);
    la.setCapability(LineArray.ALLOW_COORDINATE_READ);
    la.setCapability(LineArray.ALLOW_COLOR_WRITE);
    la.setCapability(LineArray.ALLOW_COUNT_READ);
    la.setCapability(LineArray.ALLOW_INTERSECT);
    la.setCapability(LineArray.ALLOW_FORMAT_READ);
    atomLookUp = new Atom[4 * numbonds];
    int i = 0;
    col[3] = 0.9f;
    for (ListIterator li = bonds.listIterator(); li.hasNext();) {
        bond = (Bond) li.next();
        bond.setWire(la, i);
        atom1 = bond.getAtom(0);
        atom2 = bond.getAtom(1);
        atom1.getV3D(v1);
        atom2.getV3D(v2);
        a1[0] = (float) v1.x;
        a1[1] = (float) v1.y;
        a1[2] = (float) v1.z;
        a2[0] = (float) v2.x;
        a2[1] = (float) v2.y;
        a2[2] = (float) v2.z;
        // Find the bond center
        bondmidpoint.add(v1, v2);
        bondmidpoint.scale(0.5d);
        bondmidpoint.get(mid);
        // Atom #1
        Atom.AtomColor.get(atom1.getAtomicNumber()).get(col);
        atomLookUp[i] = atom1;
        la.setCoordinate(i, a1);
        la.setColor(i, col);
        la.setNormal(i, a2);
        i++;

        atomLookUp[i] = atom1;
        la.setCoordinate(i, mid);
        la.setColor(i, col);
        la.setNormal(i, a2);
        i++;
        // Atom #2

        Atom.AtomColor.get(atom2.getAtomicNumber()).get(col);
        atomLookUp[i] = atom2;
        la.setCoordinate(i, a2);
        la.setColor(i, col);
        la.setNormal(i, a1);
        i++;

        atomLookUp[i] = atom2;
        la.setCoordinate(i, mid);
        la.setColor(i, col);
        la.setNormal(i, a1);
        i++;

    }

    ColoringAttributes cola = new ColoringAttributes(new Color3f(), ColoringAttributes.SHADE_GOURAUD);
    Appearance app = new Appearance();
    lineAttributes = new LineAttributes();
    lineAttributes.setLineWidth(RendererCache.bondwidth);
    lineAttributes.setCapability(LineAttributes.ALLOW_WIDTH_WRITE);
    lineAttributes.setLineAntialiasingEnable(true);
    app.setLineAttributes(lineAttributes);
    app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_READ);
    app.setCapability(Appearance.ALLOW_LINE_ATTRIBUTES_WRITE);
    RenderingAttributes ra = new RenderingAttributes();
    ra.setAlphaTestValue(0.1f);
    ra.setAlphaTestFunction(RenderingAttributes.GREATER);
    ra.setDepthBufferEnable(true);
    ra.setDepthBufferWriteEnable(true);
    app.setRenderingAttributes(ra);
    app.setColoringAttributes(cola);
    Shape3D wireframe = new Shape3D(la, app);
    // PickTool.setCapabilities(wire, PickTool.INTERSECT_COORD);
    wireframe.setUserData(this);
    wireframe.setBounds(new BoundingSphere(new Point3d(0, 0, 0), 1000.0));
    try {
        wireframe.setBoundsAutoCompute(false);
    } catch (Exception e) {
        e.printStackTrace();
    }

    wireframe.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    wireframe.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
    wireframe.setCapability(Shape3D.ALLOW_LOCAL_TO_VWORLD_READ);
    return wireframe;
}