Example usage for javax.media.j3d DirectionalLight DirectionalLight

List of usage examples for javax.media.j3d DirectionalLight DirectionalLight

Introduction

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

Prototype

public DirectionalLight(Color3f color, Vector3f direction) 

Source Link

Document

Constructs and initializes a directional light.

Usage

From source file:InterpolatorTest.java

protected BranchGroup createSceneBranchGroup() {
    BranchGroup objRoot = super.createSceneBranchGroup();

    // create a root TG in case we need to scale the scene
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    // create the Appearance for the Shape3D
    Appearance app = new Appearance();

    // create a Material, modified by the ColorInterpolator
    Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Material mat = new Material(objColor, black, objColor, black, 80.0f);
    mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
    app.setMaterial(mat);//from w w w . j a  v  a  2 s  .c  o m

    // create a TransparencyAttributes, modified by the
    // TransparencyInterpolator
    TransparencyAttributes transparency = new TransparencyAttributes();
    transparency.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
    transparency.setTransparencyMode(TransparencyAttributes.NICEST);
    app.setTransparencyAttributes(transparency);

    // create a Switch Node and set capabilities
    Switch switchNode = new Switch();
    switchNode.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // create a Alpha object for the Interpolators
    Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 500, 100, 5000, 2000, 1000,
            5000, 2000, 500);

    // add each BG and Interpolator as a child of the Switch Node
    TransformGroup tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new ColorInterpolator(alpha, app.getMaterial())));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new PositionInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new RotationInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg, new ScaleInterpolator(alpha, tg)));

    tg = createSharedGroup(app);
    switchNode.addChild(createBranchGroup(tg,
            new TransparencyInterpolator(alpha, app.getTransparencyAttributes(), 0, 0.8f)));

    // define the data for the RotPosScalePathInterpolator
    float[] knots = { 0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.6f, 0.8f, 0.9f, 1.0f };
    float[] scales = { 0.2f, 0.5f, 0.8f, 2.3f, 5.4f, 0.6f, 0.4f, 0.2f, 0.1f };
    Quat4f[] quats = new Quat4f[9];
    Point3f[] positions = new Point3f[9];

    quats[0] = new Quat4f(0.3f, 1.0f, 1.0f, 0.0f);
    quats[1] = new Quat4f(1.0f, 0.0f, 0.0f, 0.3f);
    quats[2] = new Quat4f(0.2f, 1.0f, 0.0f, 0.0f);
    quats[3] = new Quat4f(0.0f, 0.2f, 1.0f, 0.0f);
    quats[4] = new Quat4f(1.0f, 0.0f, 0.4f, 0.0f);
    quats[5] = new Quat4f(0.0f, 1.0f, 1.0f, 0.2f);
    quats[6] = new Quat4f(0.3f, 0.3f, 0.0f, 0.0f);
    quats[7] = new Quat4f(1.0f, 0.0f, 1.0f, 1.0f);
    quats[8] = quats[0];

    positions[0] = new Point3f(0.0f, 0.0f, -1.0f);
    positions[1] = new Point3f(1.0f, -2.0f, -2.0f);
    positions[2] = new Point3f(-2.0f, 2.0f, -3.0f);
    positions[3] = new Point3f(1.0f, 1.0f, -4.0f);
    positions[4] = new Point3f(-4.0f, -2.0f, -5.0f);
    positions[5] = new Point3f(2.0f, 0.3f, -6.0f);
    positions[6] = new Point3f(-4.0f, 0.5f, -7.0f);
    positions[7] = new Point3f(0.0f, -1.5f, -4.0f);
    positions[8] = positions[0];

    tg = createSharedGroup(app);

    // create the Interpolator
    RotPosScalePathInterpolator rotPosScalePathInterplator = new RotPosScalePathInterpolator(alpha, tg,
            new Transform3D(), knots, quats, positions, scales);

    // add a BG for the Interpolator
    switchNode.addChild(createBranchGroup(tg, rotPosScalePathInterplator));

    // create a RandomAlpha object to control a SwitchInterpolator
    // to set the Switches active child node randomly
    RandomAlpha randomAlpha = new RandomAlpha();

    // create the interpolator
    SwitchValueInterpolator switchInterpolator = new SwitchValueInterpolator(randomAlpha, switchNode);
    switchInterpolator.setSchedulingBounds(getApplicationBounds());

    // connect the scenegraph
    objTrans.addChild(switchNode);
    objTrans.addChild(switchInterpolator);

    // 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(getApplicationBounds());
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(getApplicationBounds());

    // add the lights
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    // connect
    objRoot.addChild(objTrans);

    return objRoot;
}

From source file:LineTypes.java

BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    // Create a TransformGroup to scale the scene down by 3.5x
    // TODO: move view platform instead of scene using orbit behavior
    TransformGroup objScale = new TransformGroup();
    Transform3D scaleTrans = new Transform3D();
    //scaleTrans.set(1 / 3.5f); // scale down by 3.5x
    objScale.setTransform(scaleTrans);/*from  ww  w .  ja v  a  2  s  .co  m*/
    objRoot.addChild(objScale);

    // Create a TransformGroup and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // the mouse behaviors code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objScale.addChild(objTrans);

    // Add the primitives to the scene
    objTrans.addChild(createLineTypes());

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

    Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f));
    bg.setApplicationBounds(bounds);
    objTrans.addChild(bg);

    // set up the mouse rotation behavior
    MouseRotate mr = new MouseRotate();
    mr.setTransformGroup(objTrans);
    mr.setSchedulingBounds(bounds);
    mr.setFactor(0.007);
    objTrans.addChild(mr);

    // Set up the ambient light
    Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    objRoot.addChild(ambientLightNode);

    // Set up the directional lights
    Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f);

    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    return objRoot;
}

From source file:GeometryByReferenceNIOBuffer.java

BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();

    // Set up attributes to render lines
    app = new Appearance();

    transp = new TransparencyAttributes();
    transp.setTransparency(0.5f);//from   w  w  w  . j ava2  s  . com
    transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
    transp.setTransparencyMode(TransparencyAttributes.NONE);
    app.setTransparencyAttributes(transp);

    //create the direct nio buffer
    createJ3DBuffers();

    tetraRegular = createGeometry(1);
    tetraStrip = createGeometry(2);
    tetraIndexed = createGeometry(3);
    tetraIndexedStrip = createGeometry(4);

    geoArrays[0] = tetraRegular;
    geoArrays[1] = tetraStrip;
    geoArrays[2] = tetraIndexed;
    geoArrays[3] = tetraIndexedStrip;

    shape = new Shape3D(tetraRegular, app);
    shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);

    Transform3D t = new Transform3D();
    // move the object upwards
    t.set(new Vector3f(0.0f, 0.3f, 0.0f));

    // rotate the shape
    Transform3D temp = new Transform3D();
    temp.rotX(Math.PI / 4.0d);
    t.mul(temp);
    temp.rotY(Math.PI / 4.0d);
    t.mul(temp);

    // Shrink the object
    t.setScale(0.6);

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

    objRoot.addChild(trans);
    trans.addChild(shape);

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

    // 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);

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

    return objRoot;
}

From source file:TexCoordTest.java

protected BranchGroup createSceneBranchGroup() {
    BranchGroup objRoot = super.createSceneBranchGroup();

    TransformGroup objPosition = new TransformGroup();
    objPosition.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    TransformGroup objRotate = new TransformGroup();
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

    Transform3D axisTranslate = new Transform3D();
    axisTranslate.rotZ(Math.toRadians(90));

    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 6000, 0, 0, 0, 0, 0);

    m_PositionInterpolator = new PositionInterpolator(rotationAlpha, objPosition, axisTranslate, 0, 70);

    m_PositionInterpolator.setSchedulingBounds(createApplicationBounds());
    objPosition.addChild(m_PositionInterpolator);
    m_PositionInterpolator.setEnable(false);

    m_RotationInterpolator = new RotationInterpolator(rotationAlpha, objRotate, new Transform3D(), 0.0f,
            (float) Math.PI * 2.0f);

    m_RotationInterpolator.setSchedulingBounds(getApplicationBounds());
    objRotate.addChild(m_RotationInterpolator);
    m_RotationInterpolator.setEnable(true);

    TransformGroup tgLand = new TransformGroup();
    Transform3D t3dLand = new Transform3D();
    t3dLand.setTranslation(new Vector3d(0, -30, 0));
    tgLand.setTransform(t3dLand);//w  ww.j  a  v a2 s . c o  m

    tgLand.addChild(createDemLandscape());
    objRotate.addChild(tgLand);

    objPosition.addChild(objRotate);

    objRoot.addChild(objPosition);

    // create some lights for the scene
    Color3f lColor1 = new Color3f(0.3f, 0.3f, 0.3f);
    Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f alColor = new Color3f(0.1f, 0.1f, 0.1f);

    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(getApplicationBounds());
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(getApplicationBounds());

    // add the lights to the parent BranchGroup
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    return objRoot;
}

From source file:InterleavedTest.java

BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();

    // Set up attributes to render lines
    app = new Appearance();
    app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);

    transp = new TransparencyAttributes();
    transp.setTransparency(0.5f);/* w  ww. j  a  va 2s. co  m*/
    transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
    transp.setTransparencyMode(TransparencyAttributes.NONE);
    app.setTransparencyAttributes(transp);

    // load textures
    TextureAttributes texAttr1 = new TextureAttributes();
    texAttr1.setTextureMode(TextureAttributes.DECAL);
    TextureAttributes texAttr2 = new TextureAttributes();
    texAttr2.setTextureMode(TextureAttributes.MODULATE);

    TextureLoader tex = new TextureLoader(texImage1, new String("RGB"), this);
    if (tex == null)
        return null;
    tex1 = tex.getTexture();

    tex = new TextureLoader(texImage2, new String("RGB"), this);
    if (tex == null)
        return null;
    tex2 = tex.getTexture();

    textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null);
    textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null);

    tetraRegular = createGeometry(1);
    tetraStrip = createGeometry(2);
    tetraIndexed = createGeometry(3);
    tetraIndexedStrip = createGeometry(4);

    geoArrays[0] = tetraRegular;
    geoArrays[1] = tetraStrip;
    geoArrays[2] = tetraIndexed;
    geoArrays[3] = tetraIndexedStrip;

    shape = new Shape3D(tetraRegular, app);
    shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);

    Transform3D t = new Transform3D();
    // move the object upwards
    t.set(new Vector3f(0.0f, 0.3f, 0.0f));

    // rotate the shape
    Transform3D temp = new Transform3D();
    temp.rotX(Math.PI / 4.0d);
    t.mul(temp);
    temp.rotY(Math.PI / 4.0d);
    t.mul(temp);

    // Shrink the object
    t.setScale(0.6);

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

    objRoot.addChild(trans);
    trans.addChild(shape);

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

    // 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);

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

    return objRoot;
}

From source file:InterleavedNIOBuffer.java

BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();

    // Set up attributes to render lines
    app = new Appearance();
    app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);

    transp = new TransparencyAttributes();
    transp.setTransparency(0.5f);//from   ww  w.j  a v  a  2s.  c  o  m
    transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
    transp.setTransparencyMode(TransparencyAttributes.NONE);
    app.setTransparencyAttributes(transp);

    // load textures
    TextureAttributes texAttr1 = new TextureAttributes();
    texAttr1.setTextureMode(TextureAttributes.DECAL);
    TextureAttributes texAttr2 = new TextureAttributes();
    texAttr2.setTextureMode(TextureAttributes.MODULATE);

    TextureLoader tex = new TextureLoader(texImage1, new String("RGB"), this);
    if (tex == null)
        return null;
    tex1 = tex.getTexture();

    tex = new TextureLoader(texImage2, new String("RGB"), this);
    if (tex == null)
        return null;
    tex2 = tex.getTexture();

    textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null);
    textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null);

    createInterleavedBuffers();

    tetraRegular = createGeometry(1);
    tetraStrip = createGeometry(2);
    tetraIndexed = createGeometry(3);
    tetraIndexedStrip = createGeometry(4);

    geoArrays[0] = tetraRegular;
    geoArrays[1] = tetraStrip;
    geoArrays[2] = tetraIndexed;
    geoArrays[3] = tetraIndexedStrip;

    shape = new Shape3D(tetraRegular, app);
    shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);

    Transform3D t = new Transform3D();
    // move the object upwards
    t.set(new Vector3f(0.0f, 0.3f, 0.0f));

    // rotate the shape
    Transform3D temp = new Transform3D();
    temp.rotX(Math.PI / 4.0d);
    t.mul(temp);
    temp.rotY(Math.PI / 4.0d);
    t.mul(temp);

    // Shrink the object
    t.setScale(0.6);

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

    objRoot.addChild(trans);
    trans.addChild(shape);

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

    // 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);

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

    return objRoot;
}

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);/*  w  w  w  .  j  a  va2s.c o m*/
    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:SwingTest.java

/**
 * Create the scene side of the scenegraph
 *//*from   w  w w  .  ja  v  a2 s . c  o  m*/
protected BranchGroup createSceneBranchGroup() {
    // create the root of the scene side scenegraph
    BranchGroup objRoot = new BranchGroup();

    // create a TransformGroup to rotate the objects in the scene
    // set the capability bits on the TransformGroup so that it
    // can be modified at runtime
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    // create a spherical bounding volume
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // create a 4x4 transformation matrix
    Transform3D yAxis = new Transform3D();

    // create an Alpha interpolator to automatically generate
    // modifications to the rotation component of the transformation matrix
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);

    // create a RotationInterpolator behavior to effect the TransformGroup
    rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);

    // set the scheduling bounds on the behavior
    rotator.setSchedulingBounds(bounds);

    // add the behavior to the scenegraph
    objTrans.addChild(rotator);

    // create the BranchGroup which contains the objects
    // we add/remove to and from the scenegraph
    sceneBranchGroup = new BranchGroup();

    // allow the BranchGroup to have children added at runtime
    sceneBranchGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    sceneBranchGroup.setCapability(Group.ALLOW_CHILDREN_READ);
    sceneBranchGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);

    // add both the cube and the sphere to the scenegraph
    sceneBranchGroup.addChild(createCube());
    sceneBranchGroup.addChild(createSphere());

    // create the colors for the 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);

    // create the ambient light
    AmbientLight aLgt = new AmbientLight(alColor);
    aLgt.setInfluencingBounds(bounds);

    // create the directional light
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(bounds);

    // add the lights to the scenegraph
    objRoot.addChild(aLgt);
    objRoot.addChild(lgt1);

    // wire the scenegraph together
    objTrans.addChild(sceneBranchGroup);
    objRoot.addChild(objTrans);

    // return the root of the scene side of the scenegraph
    return objRoot;
}

From source file:PlatformTest.java

public BranchGroup createSceneGraph() {
    final int LAND_WIDTH = 12;
    final float LAND_HEIGHT = -1.0f;
    final int LAND_LENGTH = 12;
    final int nTileSize = 2;

    // calculate how many vertices we need to store all the "tiles"
    // that compose the QuadArray.
    final int nNumTiles = ((LAND_LENGTH / nTileSize) * 2) * ((LAND_WIDTH / nTileSize) * 2);
    final int nVertexCount = 4 * nNumTiles;
    Point3f[] coordArray = new Point3f[nVertexCount];
    Point2f[] texCoordArray = new Point2f[nVertexCount];

    // create an Appearance and load a texture
    Appearance app = new Appearance();
    Texture tex = new TextureLoader("land.jpg", this).getTexture();
    app.setTexture(tex);/*from   w  ww  . jav a2  s.c o m*/

    // create the parent BranchGroup
    BranchGroup bg = new BranchGroup();

    int nItem = 0;

    // loop over all the tiles in the environment
    for (int x = -LAND_WIDTH; x <= LAND_WIDTH; x += nTileSize) {
        for (int z = -LAND_LENGTH; z <= LAND_LENGTH; z += nTileSize) {
            // if we are on the border of the environment create a
            // TransformGroup to position a ColorCube to create a "wall"
            if (x == -LAND_WIDTH || x == LAND_WIDTH || z == -LAND_LENGTH || z == LAND_LENGTH) {
                TransformGroup tg = new TransformGroup();
                Transform3D t3d = new Transform3D();
                t3d.setTranslation(new Vector3d(x, 0, z));
                tg.setTransform(t3d);
                tg.addChild(new ColorCube(nTileSize / 2));
                bg.addChild(tg);
            }

            // if we are not on the last row or column create a "tile"
            // and add to the QuadArray. Use CCW winding and assign texture
            // coordinates.
            if (z < LAND_LENGTH && x < LAND_WIDTH) {
                coordArray[nItem] = new Point3f(x, LAND_HEIGHT, z);
                texCoordArray[nItem++] = new Point2f(0, 0);
                coordArray[nItem] = new Point3f(x, LAND_HEIGHT, z + nTileSize);
                texCoordArray[nItem++] = new Point2f(1, 0);
                coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z + nTileSize);
                texCoordArray[nItem++] = new Point2f(1, 1);
                coordArray[nItem] = new Point3f(x + nTileSize, LAND_HEIGHT, z);
                texCoordArray[nItem++] = new Point2f(0, 1);
            }
        }
    }

    // create a GeometryInfo and generate Normal vectors
    // for the QuadArray that was populated.
    GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY);

    gi.setCoordinates(coordArray);
    gi.setTextureCoordinates(texCoordArray);

    NormalGenerator normalGenerator = new NormalGenerator();
    normalGenerator.generateNormals(gi);

    // wrap the GeometryArray in a Shape3D
    Shape3D shape = new Shape3D(gi.getGeometryArray(), app);

    // add the Shape3D to the parent BranchGroup
    bg.addChild(shape);

    // create some lights for the scene
    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(m_Bounds);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    lgt1.setInfluencingBounds(m_Bounds);

    // add the lights to the parent BranchGroup
    bg.addChild(aLgt);
    bg.addChild(lgt1);

    // create a light gray background
    Background back = new Background(new Color3f(0.9f, 0.9f, 0.9f));
    back.setApplicationBounds(m_Bounds);
    bg.addChild(back);

    // compile the whole scene
    //bg.compile();

    return bg;
}

From source file:SplineAnim.java

public BranchGroup createSceneGraph() {

    // Colors for lights and objects
    Color3f aColor = new Color3f(0.2f, 0.2f, 0.2f);
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f coneColor = new Color3f(0.9f, 0.1f, 0.1f);
    Color3f sphereColor = new Color3f(0.1f, 0.7f, 0.9f);
    Color3f bgColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f lightColor = new Color3f(1.0f, 1.0f, 1.0f);

    // Root of the branch grsph
    BranchGroup root = new BranchGroup();

    // Create transforms such that all objects appears in the scene
    sceneTransform = new Transform3D();
    sceneTransform.setScale(0.14f);/*from w w  w. j av  a 2 s  . c  o  m*/
    Transform3D yrot = new Transform3D();
    yrot.rotY(-Math.PI / 5.0d);
    sceneTransform.mul(yrot);
    sceneTransformGroup = new TransformGroup(sceneTransform);
    sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    sceneTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    root.addChild(sceneTransformGroup);

    // Create bounds for the background and lights
    bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0f);

    // Set up the background
    Background bg = new Background(bgColor);
    bg.setApplicationBounds(bounds);
    sceneTransformGroup.addChild(bg);

    // Create the transform group node for the lights
    lightTransform1 = new Transform3D();
    lightTransform2 = new Transform3D();
    Vector3d lightPos1 = new Vector3d(0.0, 0.0, 2.0);
    Vector3d lightPos2 = new Vector3d(1.0, 0.0, -2.0);
    lightTransform1.set(lightPos1);
    lightTransform2.set(lightPos2);
    light1TransformGroup = new TransformGroup(lightTransform1);
    light2TransformGroup = new TransformGroup(lightTransform2);
    sceneTransformGroup.addChild(light1TransformGroup);
    sceneTransformGroup.addChild(light2TransformGroup);

    // Create lights
    AmbientLight ambLight = new AmbientLight(aColor);
    Light dirLight1;
    Light dirLight2;

    Vector3f lightDir1 = new Vector3f(lightPos1);
    Vector3f lightDir2 = new Vector3f(lightPos2);
    lightDir1.negate();
    lightDir2.negate();
    dirLight1 = new DirectionalLight(lightColor, lightDir1);
    dirLight2 = new DirectionalLight(lightColor, lightDir2);

    // Set the influencing bounds
    ambLight.setInfluencingBounds(bounds);
    dirLight1.setInfluencingBounds(bounds);
    dirLight2.setInfluencingBounds(bounds);

    // Add the lights into the scene graph
    sceneTransformGroup.addChild(ambLight);
    sceneTransformGroup.addChild(dirLight1);
    sceneTransformGroup.addChild(dirLight2);

    // Create a cone and add it to the scene graph.
    objTransform = new Transform3D();
    objTransformGroup = new TransformGroup(objTransform);
    objTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    sceneTransformGroup.addChild(objTransformGroup);

    Material m = new Material(coneColor, eColor, coneColor, sColor, 100.0f);
    Appearance a = new Appearance();
    m.setLightingEnable(true);
    a.setMaterial(m);
    Cone cone = new Cone(0.4f, 1.0f);
    cone.setAppearance(a);
    objTransformGroup.addChild(cone);

    // Create transform groups for each knot point
    // knot point 0
    Transform3D t3dKnot = new Transform3D();
    t3dKnot.set(pos0);
    TransformGroup k0TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k0TransformGroup);

    // knot point 1
    t3dKnot = new Transform3D();
    t3dKnot.set(pos1);
    TransformGroup k1TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k1TransformGroup);

    // knot point 2
    t3dKnot = new Transform3D();
    t3dKnot.set(pos2);
    TransformGroup k2TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k2TransformGroup);

    // knot point 3
    t3dKnot = new Transform3D();
    t3dKnot.set(pos3);
    TransformGroup k3TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k3TransformGroup);

    // knot point 4
    t3dKnot = new Transform3D();
    t3dKnot.set(pos4);
    TransformGroup k4TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k4TransformGroup);

    // knot point 5
    t3dKnot = new Transform3D();
    t3dKnot.set(pos5);
    TransformGroup k5TransformGroup = new TransformGroup(t3dKnot);
    sceneTransformGroup.addChild(k5TransformGroup);

    // Create spheres for each knot point's transform group
    ColoringAttributes sphereColorAttr = new ColoringAttributes();
    sphereColorAttr.setColor(sphereColor);
    Appearance sphereAppearance = new Appearance();
    sphereAppearance.setColoringAttributes(sphereColorAttr);
    k0TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k1TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k2TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k3TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k4TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));
    k5TransformGroup.addChild(new Sphere(0.10f, sphereAppearance));

    return root;
}