Example usage for javax.media.j3d TextureAttributes MODULATE

List of usage examples for javax.media.j3d TextureAttributes MODULATE

Introduction

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

Prototype

int MODULATE

To view the source code for javax.media.j3d TextureAttributes MODULATE.

Click Source Link

Document

Modulate the object color with the texture color.

Usage

From source file:PictureBall.java

public PictureBall() {

    // Create the universe
    SimpleUniverse universe = new SimpleUniverse();

    // Create a structure to contain objects
    BranchGroup group = new BranchGroup();

    // Set up colors
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red = new Color3f(0.7f, .15f, .15f);

    // Set up the texture map
    TextureLoader loader = new TextureLoader("K:\\3d\\Arizona.jpg", "LUMINANCE", new Container());
    Texture texture = loader.getTexture();
    texture.setBoundaryModeS(Texture.WRAP);
    texture.setBoundaryModeT(Texture.WRAP);
    texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));

    // Set up the texture attributes
    //could be REPLACE, BLEND or DECAL instead of MODULATE
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    Appearance ap = new Appearance();
    ap.setTexture(texture);//from ww w . j a  v a 2s. c o  m
    ap.setTextureAttributes(texAttr);

    //set up the material
    ap.setMaterial(new Material(red, black, red, black, 1.0f));

    // Create a ball to demonstrate textures
    int primflags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
    Sphere sphere = new Sphere(0.5f, primflags, ap);
    group.addChild(sphere);

    // Create lights
    Color3f light1Color = new Color3f(1f, 1f, 1f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
    DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    group.addChild(light1);

    AmbientLight ambientLight = new AmbientLight(new Color3f(.5f, .5f, .5f));
    ambientLight.setInfluencingBounds(bounds);
    group.addChild(ambientLight);

    // look towards the ball
    universe.getViewingPlatform().setNominalViewingTransform();

    // add the group of objects to the Universe
    universe.addBranchGraph(group);
}

From source file:TextureImage.java

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

    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);/*  www. j  av  a 2  s  .co m*/

    // Create appearance object for textured cube
    Appearance app = new Appearance();
    Texture tex = new TextureLoader(texImage, this).getTexture();
    app.setTexture(tex);
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    app.setTextureAttributes(texAttr);

    // Create textured cube and add it to the scene graph.
    Box textureCube = new Box(0.4f, 0.4f, 0.4f, Box.GENERATE_TEXTURE_COORDS, app);
    objTrans.addChild(textureCube);

    // 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, 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;
}

From source file:MultiTextureTest.java

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

    // Create a Transformgroup to scale all objects so they
    // appear in the scene.
    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setScale(0.4);// w w  w.j  a v  a2  s. c om
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    TransformGroup objTrans = new TransformGroup();
    //write-enable for behaviors
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    objScale.addChild(objTrans);

    Appearance ap = new Appearance();

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

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

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

    lightTex = createLightMap();

    textureUnitState[0] = new TextureUnitState(stoneTex, texAttr1, null);
    textureUnitState[0].setCapability(TextureUnitState.ALLOW_STATE_WRITE);

    textureUnitState[1] = new TextureUnitState(lightTex, texAttr2, null);
    textureUnitState[1].setCapability(TextureUnitState.ALLOW_STATE_WRITE);

    ap.setTextureUnitState(textureUnitState);

    //Create a Box
    Box BoxObj = new Box(1.5f, 1.5f, 0.8f, Box.GENERATE_NORMALS | Box.GENERATE_TEXTURE_COORDS, ap, 2);
    // add it to the scene graph.
    objTrans.addChild(BoxObj);

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

    //Shine it with two lights.
    Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
    Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Vector3f lDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    lgt1.setInfluencingBounds(bounds);
    lgt2.setInfluencingBounds(bounds);
    objScale.addChild(lgt1);
    objScale.addChild(lgt2);

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

    return objRoot;
}

From source file:OrientedTest.java

public BranchGroup createSceneGraph() {

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

    TransformGroup objScale = new TransformGroup();
    Transform3D textMat = new Transform3D();
    // Assuming uniform size chars, set scale to fit string in view
    textMat.setScale(1.2 / sl);//  ww  w .  j av a2s  .co  m
    objScale.setTransform(textMat);

    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior 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);
    objRoot.addChild(objTrans);

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

    Appearance apText = new Appearance();
    Material m = new Material();
    m.setLightingEnable(true);
    apText.setMaterial(m);

    Appearance apEarth = new Appearance();
    Material mm = new Material();
    mm.setLightingEnable(true);
    apEarth.setMaterial(mm);

    Appearance apStone = new Appearance();
    apStone.setMaterial(mm);

    // create 3D text
    Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2), new FontExtrusion());
    Text3D txt = new Text3D(f3d, textString, new Point3f(-sl / 2.0f, 3.0f, 0.0f));
    OrientedShape3D textShape = new OrientedShape3D();
    textShape.setGeometry(txt);
    textShape.setAppearance(apText);
    textShape.setAlignmentAxis(0.0f, 1.0f, 0.0f);
    objScale.addChild(textShape);

    // Create a simple shape leaf node, add it to the scene graph.

    Transform3D cubeMat = new Transform3D();
    TransformGroup cubeTrans = new TransformGroup(cubeMat);
    cubeMat.set(new Vector3d(0.9, 0.0, -1.0));
    cubeTrans.setTransform(cubeMat);
    cubeTrans.addChild(new ColorCube(0.3));
    objTrans.addChild(cubeTrans);

    TextureLoader stoneTex = new TextureLoader(stoneImage, new String("RGB"), this);
    if (stoneTex != null)
        apStone.setTexture(stoneTex.getTexture());

    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    apStone.setTextureAttributes(texAttr);

    Transform3D coneMat = new Transform3D();
    TransformGroup coneTrans = new TransformGroup(coneMat);
    coneMat.set(new Vector3d(0.0, 0.0, 0.0));
    coneTrans.setTransform(coneMat);
    coneTrans.addChild(new Cone(.2f, 0.8f, Cone.GENERATE_NORMALS | Cone.GENERATE_TEXTURE_COORDS, apStone));
    objTrans.addChild(coneTrans);

    TextureLoader earthTex = new TextureLoader(earthImage, new String("RGB"), this);
    if (earthTex != null)
        apEarth.setTexture(earthTex.getTexture());

    apEarth.setTextureAttributes(texAttr);

    Transform3D cylinderMat = new Transform3D();
    TransformGroup cylinderTrans = new TransformGroup(cylinderMat);
    cylinderMat.set(new Vector3d(-0.9, 0.5, -1.0));
    cylinderTrans.setTransform(cylinderMat);
    cylinderTrans.addChild(
            new Cylinder(.35f, 2.0f, Cylinder.GENERATE_NORMALS | Cylinder.GENERATE_TEXTURE_COORDS, apEarth));
    objTrans.addChild(cylinderTrans);

    objTrans.addChild(objScale);

    // Set up the background
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
    Background bgNode = new Background(bgColor);
    bgNode.setApplicationBounds(bounds);
    objRoot.addChild(bgNode);

    // 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, 0.9f);
    Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
    Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
    Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);

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

    DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);

    apText.setMaterial(mm);

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

    return objRoot;
}

From source file:AppearanceTest.java

private Appearance createAppearance(int idx) {
    Appearance app = new Appearance();

    // Globally used colors
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

    switch (idx) {
    // Unlit solid
    case 0: {/*  w  w  w .  j a  v  a  2  s  .  co  m*/
        // Set up the coloring properties
        Color3f objColor = new Color3f(1.0f, 0.2f, 0.4f);
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(objColor);
        app.setColoringAttributes(ca);
        break;
    }

    // Unlit wire frame
    case 1: {
        // Set up the coloring properties
        Color3f objColor = new Color3f(0.5f, 0.0f, 0.2f);
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(objColor);
        app.setColoringAttributes(ca);

        // Set up the polygon attributes
        PolygonAttributes pa = new PolygonAttributes();
        pa.setPolygonMode(pa.POLYGON_LINE);
        pa.setCullFace(pa.CULL_NONE);
        app.setPolygonAttributes(pa);
        break;
    }

    // Unlit points
    case 2: {
        // Set up the coloring properties
        Color3f objColor = new Color3f(0.2f, 0.2f, 1.0f);
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(objColor);
        app.setColoringAttributes(ca);

        // Set up the polygon attributes
        PolygonAttributes pa = new PolygonAttributes();
        pa.setPolygonMode(pa.POLYGON_POINT);
        pa.setCullFace(pa.CULL_NONE);
        app.setPolygonAttributes(pa);

        // Set up point attributes
        PointAttributes pta = new PointAttributes();
        pta.setPointSize(5.0f);
        app.setPointAttributes(pta);
        break;
    }

    // Lit solid
    case 3: {
        // Set up the material properties
        Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, white, 80.0f));
        break;
    }

    // Texture mapped, lit solid
    case 4: {
        // Set up the texture map
        TextureLoader tex = new TextureLoader(texImage, this);
        app.setTexture(tex.getTexture());

        TextureAttributes texAttr = new TextureAttributes();
        texAttr.setTextureMode(TextureAttributes.MODULATE);
        app.setTextureAttributes(texAttr);

        // Set up the material properties
        app.setMaterial(new Material(white, black, white, black, 1.0f));
        break;
    }

    // Transparent, lit solid
    case 5: {
        // Set up the transparency properties
        TransparencyAttributes ta = new TransparencyAttributes();
        ta.setTransparencyMode(ta.BLENDED);
        ta.setTransparency(0.6f);
        app.setTransparencyAttributes(ta);

        // Set up the polygon attributes
        PolygonAttributes pa = new PolygonAttributes();
        pa.setCullFace(pa.CULL_NONE);
        app.setPolygonAttributes(pa);

        // Set up the material properties
        Color3f objColor = new Color3f(0.7f, 0.8f, 1.0f);
        app.setMaterial(new Material(objColor, black, objColor, black, 1.0f));
        break;
    }

    // Lit solid, no specular
    case 6: {
        // Set up the material properties
        Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, black, 80.0f));
        break;
    }

    // Lit solid, specular only
    case 7: {
        // Set up the material properties
        Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
        app.setMaterial(new Material(black, black, black, white, 80.0f));
        break;
    }

    // Another lit solid with a different color
    case 8: {
        // Set up the material properties
        Color3f objColor = new Color3f(0.8f, 0.8f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, white, 80.0f));
        break;
    }

    default: {
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(new Color3f(0.0f, 1.0f, 0.0f));
        app.setColoringAttributes(ca);
    }
    }

    return app;
}

From source file:ConicWorld.java

private Appearance createAppearance(int idx) {
    Appearance app = new Appearance();

    // Globally used colors
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

    idx = idx % 5;//w w  w. j a  v  a 2s.c o  m

    switch (idx) {
    // Lit solid
    case 0: {
        // Set up the material properties
        Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, white, 80.0f));
        break;
    }
    // Lit solid, no specular
    case 1: {
        // Set up the material properties
        Color3f objColor = new Color3f(0.0f, 0.8f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, white, 80.0f));
        break;
    }

    // Lit solid, specular only
    case 2: {
        // Set up the material properties
        Color3f objColor = new Color3f(0.0f, 0.8f, 0.8f);
        app.setMaterial(new Material(black, black, objColor, white, 80.0f));
        break;
    }

    // Texture mapped, lit solid
    case 3: {
        // Set up the texture map
        TextureLoader tex = new TextureLoader(texImage, this);
        app.setTexture(tex.getTexture());

        // Set up the material properties
        app.setMaterial(new Material(white, black, white, black, 1.0f));
        TextureAttributes texAttr = new TextureAttributes();
        texAttr.setTextureMode(TextureAttributes.MODULATE);
        app.setTextureAttributes(texAttr);

        break;
    }

    // Another lit solid with a different color
    case 4: {
        // Set up the material properties
        Color3f objColor = new Color3f(1.0f, 1.0f, 0.0f);
        app.setMaterial(new Material(objColor, black, objColor, white, 80.0f));
        break;
    }

    default: {
        ColoringAttributes ca = new ColoringAttributes();
        ca.setColor(new Color3f(0.0f, 1.0f, 0.0f));
        app.setColoringAttributes(ca);
    }
    }

    return app;
}

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);/*from w w  w .  j a  v a  2  s .  c om*/
    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);//w  w  w  .java 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:ExHenge.java

public Group buildScene() {
    // Turn off the example headlight
    setHeadlightEnable(false);/*from   ww w  . ja  v  a2  s.c o  m*/

    // Default to walk navigation
    setNavigationType(Walk);

    //
    // Preload the texture images
    //
    if (debug)
        System.err.println("  textures...");
    Texture groundTex = null;
    Texture spurTex = null;
    Texture domeTex = null;
    TextureLoader texLoader = null;
    ImageComponent image = null;

    texLoader = new TextureLoader("mud01.jpg", this);
    image = texLoader.getImage();
    if (image == null)
        System.err.println("Cannot load mud01.jpg texture");
    else {
        groundTex = texLoader.getTexture();
        groundTex.setBoundaryModeS(Texture.WRAP);
        groundTex.setBoundaryModeT(Texture.WRAP);
        groundTex.setMinFilter(Texture.NICEST);
        groundTex.setMagFilter(Texture.NICEST);
        groundTex.setMipMapMode(Texture.BASE_LEVEL);
        groundTex.setEnable(true);
    }

    texLoader = new TextureLoader("stonebrk2.jpg", this);
    image = texLoader.getImage();
    if (image == null)
        System.err.println("Cannot load stonebrk2.jpg texture");
    else {
        spurTex = texLoader.getTexture();
        spurTex.setBoundaryModeS(Texture.WRAP);
        spurTex.setBoundaryModeT(Texture.WRAP);
        spurTex.setMinFilter(Texture.NICEST);
        spurTex.setMagFilter(Texture.NICEST);
        spurTex.setMipMapMode(Texture.BASE_LEVEL);
        spurTex.setEnable(true);
    }

    texLoader = new TextureLoader("fire.jpg", this);
    image = texLoader.getImage();
    if (image == null)
        System.err.println("Cannot load fire.jpg texture");
    else {
        domeTex = texLoader.getTexture();
        domeTex.setBoundaryModeS(Texture.WRAP);
        domeTex.setBoundaryModeT(Texture.WRAP);
        domeTex.setMinFilter(Texture.NICEST);
        domeTex.setMagFilter(Texture.NICEST);
        domeTex.setMipMapMode(Texture.BASE_LEVEL);
        domeTex.setEnable(true);
    }

    //
    // Build some shapes we'll need
    //
    if (debug)
        System.err.println("  flying buttresses...");

    // Build three types of spurs (flying buttresses)
    Appearance spurApp = new Appearance();

    Material spurMat = new Material();
    spurMat.setAmbientColor(0.6f, 0.6f, 0.6f);
    spurMat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    spurMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    spurApp.setMaterial(spurMat);

    Transform3D tr = new Transform3D();
    tr.setIdentity();
    tr.setScale(new Vector3d(1.0, 4.0, 1.0));

    TextureAttributes spurTexAtt = new TextureAttributes();
    spurTexAtt.setTextureMode(TextureAttributes.MODULATE);
    spurTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    spurTexAtt.setTextureTransform(tr);
    spurApp.setTextureAttributes(spurTexAtt);

    if (spurTex != null)
        spurApp.setTexture(spurTex);

    Arch spur1 = new Arch(0.0, // start Phi
            1.571, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            2.5, // start radius
            1.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    Arch spur2 = new Arch(0.0, // start Phi
            1.571, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            1.5, // start radius
            2.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    Arch spur3 = new Arch(0.0, // start Phi
            1.571, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            1.5, // start radius
            1.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    Arch spur4 = new Arch(0.0, // start Phi
            1.178, // end Phi
            9, // nPhi
            -0.0982, // start Theta
            0.0982, // end Theta (11.25 degrees)
            2, // nTheta
            4.0, // start radius
            4.0, // end radius
            0.05, // start phi thickness
            0.025, // end phi thickness
            spurApp); // appearance

    // Put each spur into a shared group so we can instance
    // the spurs multiple times
    SharedGroup spur1Group = new SharedGroup();
    spur1Group.addChild(spur1);
    spur1Group.compile();

    SharedGroup spur2Group = new SharedGroup();
    spur2Group.addChild(spur2);
    spur2Group.compile();

    SharedGroup spur3Group = new SharedGroup();
    spur3Group.addChild(spur3);
    spur3Group.compile();

    SharedGroup spur4Group = new SharedGroup();
    spur4Group.addChild(spur4);
    spur4Group.compile();

    // Build a central dome
    if (debug)
        System.err.println("  central dome...");

    Appearance domeApp = new Appearance();
    // No material needed - we want the dome to glow,
    // so use a REPLACE mode texture only
    TextureAttributes domeTexAtt = new TextureAttributes();
    domeTexAtt.setTextureMode(TextureAttributes.REPLACE);
    domeTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    domeApp.setTextureAttributes(domeTexAtt);

    if (domeTex != null)
        domeApp.setTexture(domeTex);

    Arch dome = new Arch(0.0, // start Phi
            1.571, // end Phi
            5, // nPhi
            0.0, // start Theta
            2.0 * Math.PI, // end Theta (360 degrees)
            17, // nTheta
            1.0, // start radius
            1.0, // end radius
            0.0, // start phi thickness
            0.0, // end phi thickness
            domeApp); // appearance

    // Build the ground. Use a trick to get better lighting
    // effects by using an elevation grid. The idea is this:
    // for interactive graphics systems, such as those
    // controlled by Java3D, lighting effects are computed only
    // at triangle vertexes. Imagine a big rectangular ground
    // underneath a PointLight (added below). If the
    // PointLight is above the center of the square, in the real
    // world we'd expect a bright spot below it, fading to
    // darkness at the edges of the square. Not so in
    // interactive graphics. Since lighting is only computed
    // at vertexes, and the square's vertexes are each
    // equidistant from a centered PointLight, all four square
    // coordinates get the same brightness. That brightness
    // is interpolated across the square, giving a *constant*
    // brightness for the entire square! There is no bright
    // spot under the PointLight. So, here's the trick: use
    // more triangles. Pretty simple. Split the ground under
    // the PointLight into a grid of smaller squares. Each
    // smaller square is shaded using light brightness computed
    // at the square's vertexes. Squares directly under the
    // PointLight get brighter lighting at their vertexes, and
    // thus they are bright. This gives the desired bright
    // spot under the PointLight. The more squares we use
    // (a denser grid), the more accurate the bright spot and
    // the smoother the lighting gradation from bright directly
    // under the PointLight, to dark at the distant edges. Of
    // course, with more squares, we also get more polygons to
    // draw and a performance slow-down. So there is a
    // tradeoff between lighting quality and drawing speed.
    // For this example, we'll use a coarse mesh of triangles
    // created using an ElevationGrid shape.
    if (debug)
        System.err.println("  ground...");

    Appearance groundApp = new Appearance();

    Material groundMat = new Material();
    groundMat.setAmbientColor(0.3f, 0.3f, 0.3f);
    groundMat.setDiffuseColor(0.7f, 0.7f, 0.7f);
    groundMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    groundApp.setMaterial(groundMat);

    tr = new Transform3D();
    tr.setScale(new Vector3d(8.0, 8.0, 1.0));

    TextureAttributes groundTexAtt = new TextureAttributes();
    groundTexAtt.setTextureMode(TextureAttributes.MODULATE);
    groundTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    groundTexAtt.setTextureTransform(tr);
    groundApp.setTextureAttributes(groundTexAtt);

    if (groundTex != null)
        groundApp.setTexture(groundTex);

    ElevationGrid ground = new ElevationGrid(11, // X dimension
            11, // Z dimension
            2.0f, // X spacing
            2.0f, // Z spacing
            // Automatically use zero heights
            groundApp); // Appearance

    //
    // Build the scene using the shapes above. Place everything
    // withing a TransformGroup.
    //
    // Build the scene root
    TransformGroup scene = new TransformGroup();
    tr = new Transform3D();
    tr.setTranslation(new Vector3f(0.0f, -1.6f, 0.0f));
    scene.setTransform(tr);

    // Create influencing bounds
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    // General Ambient light
    ambient = new AmbientLight();
    ambient.setEnable(ambientOnOff);
    ambient.setColor(new Color3f(0.3f, 0.3f, 0.3f));
    ambient.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    ambient.setInfluencingBounds(worldBounds);
    scene.addChild(ambient);

    // Bright Ambient light
    brightAmbient = new AmbientLight();
    brightAmbient.setEnable(brightAmbientOnOff);
    brightAmbient.setColor(new Color3f(1.0f, 1.0f, 1.0f));
    brightAmbient.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    brightAmbient.setInfluencingBounds(worldBounds);
    scene.addChild(brightAmbient);

    // Red directional light
    redDirectional = new DirectionalLight();
    redDirectional.setEnable(redDirectionalOnOff);
    redDirectional.setColor(new Color3f(1.0f, 0.0f, 0.0f));
    redDirectional.setDirection(new Vector3f(1.0f, -0.5f, -0.5f));
    redDirectional.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    redDirectional.setInfluencingBounds(worldBounds);
    scene.addChild(redDirectional);

    // Yellow directional light
    yellowDirectional = new DirectionalLight();
    yellowDirectional.setEnable(yellowDirectionalOnOff);
    yellowDirectional.setColor(new Color3f(1.0f, 0.8f, 0.0f));
    yellowDirectional.setDirection(new Vector3f(-1.0f, 0.5f, 1.0f));
    yellowDirectional.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    yellowDirectional.setInfluencingBounds(worldBounds);
    scene.addChild(yellowDirectional);

    // Orange point light
    orangePoint = new PointLight();
    orangePoint.setEnable(orangePointOnOff);
    orangePoint.setColor(new Color3f(1.0f, 0.5f, 0.0f));
    orangePoint.setPosition(new Point3f(0.0f, 0.5f, 0.0f));
    orangePoint.setCapability(AmbientLight.ALLOW_STATE_WRITE);
    orangePoint.setInfluencingBounds(worldBounds);
    scene.addChild(orangePoint);

    // Ground
    scene.addChild(ground);

    // Dome
    scene.addChild(dome);

    // Spur 1's
    Group g = buildRing(spur1Group);
    scene.addChild(g);

    // Spur 2's
    TransformGroup tg = new TransformGroup();
    tr = new Transform3D();
    tr.rotY(0.3927);
    tg.setTransform(tr);
    g = buildRing(spur2Group);
    tg.addChild(g);
    scene.addChild(tg);

    // Spur 3's
    g = buildRing(spur3Group);
    scene.addChild(g);

    // Spur 4's
    tg = new TransformGroup();
    tg.setTransform(tr);
    g = buildRing(spur4Group);
    tg.addChild(g);
    scene.addChild(tg);

    return scene;
}

From source file:ExSwitch.java

public Group buildScene() {
    // Turn on the example headlight
    setHeadlightEnable(true);//ww  w .j  a  v a 2 s. co m

    // Default to walk navigation
    setNavigationType(Walk);

    // Build the scene group
    Group scene = new Group();

    if (debug)
        System.err.println("  switch shapes...");

    // BEGIN EXAMPLE TOPIC
    // Build the switch group and allow its switch
    // value to be changed via menu items
    swtch = new Switch();
    swtch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    //  Create several shapes to place in a switch group

    // Child 0: a red sphere
    Appearance app0 = new Appearance();
    Material mat0 = new Material();
    mat0.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat0.setDiffuseColor(1.0f, 0.0f, 0.2f);
    mat0.setSpecularColor(0.7f, 0.7f, 0.7f);
    app0.setMaterial(mat0);

    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3f(-2.0f, 1.5f, 0.0f));
    TransformGroup tg0 = new TransformGroup(t3d);
    Sphere sph0 = new Sphere(0.5f, // radius
            Primitive.GENERATE_NORMALS, // components
            16, // facets
            app0); // appearance
    tg0.addChild(sph0);
    swtch.addChild(tg0); // Child 0

    // Child 1: a green sphere
    Appearance app1 = new Appearance();
    Material mat1 = new Material();
    mat1.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat1.setDiffuseColor(0.0f, 1.0f, 0.0f);
    mat1.setSpecularColor(0.7f, 0.7f, 0.7f);
    app1.setMaterial(mat1);
    t3d.setTranslation(new Vector3f(0.0f, 1.5f, 0.0f));
    TransformGroup tg1 = new TransformGroup(t3d);
    Sphere sph1 = new Sphere(0.5f, // radius
            Primitive.GENERATE_NORMALS, // components
            16, // facets
            app1); // appearance
    tg1.addChild(sph1);
    swtch.addChild(tg1); // Child 1

    // Child 2: a blue sphere
    Appearance app2 = new Appearance();
    Material mat2 = new Material();
    mat2.setAmbientColor(0.2f, 0.2f, 0.2f);
    mat2.setDiffuseColor(0.0f, 0.6f, 1.0f);
    mat2.setSpecularColor(0.7f, 0.7f, 0.7f);
    app2.setMaterial(mat2);
    t3d.setTranslation(new Vector3f(2.0f, 1.5f, 0.0f));
    TransformGroup tg2 = new TransformGroup(t3d);
    Sphere sph2 = new Sphere(0.5f, // radius
            Primitive.GENERATE_NORMALS, // components
            16, // facets
            app2); // appearance
    tg2.addChild(sph2);
    swtch.addChild(tg2);

    // Set the initial child choice
    swtch.setWhichChild(options[currentSwitch].child);
    scene.addChild(swtch);
    // END EXAMPLE TOPIC

    // Build foreground geometry including a floor and
    // columns on which the switchable shapes stand

    // Load textures
    TextureLoader texLoader = new TextureLoader("granite07rev.jpg", this);
    Texture columnTex = texLoader.getTexture();
    if (columnTex == null)
        System.err.println("Cannot load granite07rev.jpg texture");
    else {
        columnTex.setBoundaryModeS(Texture.WRAP);
        columnTex.setBoundaryModeT(Texture.WRAP);
        columnTex.setMinFilter(Texture.NICEST);
        columnTex.setMagFilter(Texture.NICEST);
        columnTex.setMipMapMode(Texture.BASE_LEVEL);
        columnTex.setEnable(true);
    }

    texLoader = new TextureLoader("flooring.jpg", this);
    Texture groundTex = texLoader.getTexture();
    if (groundTex == null)
        System.err.println("Cannot load flooring.jpg texture");
    else {
        groundTex.setBoundaryModeS(Texture.WRAP);
        groundTex.setBoundaryModeT(Texture.WRAP);
        groundTex.setMinFilter(Texture.NICEST);
        groundTex.setMagFilter(Texture.NICEST);
        groundTex.setMipMapMode(Texture.BASE_LEVEL);
        groundTex.setEnable(true);
    }

    //
    // Build several columns on the floor
    //
    if (debug)
        System.err.println("  columns...");
    SharedGroup column = new SharedGroup();
    Appearance columnApp = new Appearance();

    Material columnMat = new Material();
    columnMat.setAmbientColor(0.6f, 0.6f, 0.6f);
    columnMat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    columnMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    columnApp.setMaterial(columnMat);

    TextureAttributes columnTexAtt = new TextureAttributes();
    columnTexAtt.setTextureMode(TextureAttributes.MODULATE);
    columnTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    columnApp.setTextureAttributes(columnTexAtt);

    if (columnTex != null)
        columnApp.setTexture(columnTex);

    GothicColumn columnShape = new GothicColumn(1.8f, // height
            0.25f, // radius
            GothicColumn.BUILD_TOP, // flags
            columnApp); // appearance
    column.addChild(columnShape);

    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    // Left
    trans.set(-2.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(new Link(column));
    scene.addChild(tg);

    // Middle
    trans.set(0.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(new Link(column));
    scene.addChild(tg);

    // Right
    trans.set(2.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(new Link(column));
    scene.addChild(tg);

    //
    //  Add the ground
    //
    if (debug)
        System.err.println("  ground...");

    Appearance groundApp = new Appearance();

    Material groundMat = new Material();
    groundMat.setAmbientColor(0.6f, 0.6f, 0.6f);
    groundMat.setDiffuseColor(1.0f, 1.0f, 1.0f);
    groundMat.setSpecularColor(0.0f, 0.0f, 0.0f);
    groundApp.setMaterial(groundMat);

    tr = new Transform3D();
    tr.setScale(new Vector3d(4.0, 4.0, 1.0));

    TextureAttributes groundTexAtt = new TextureAttributes();
    groundTexAtt.setTextureMode(TextureAttributes.MODULATE);
    groundTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST);
    groundTexAtt.setTextureTransform(tr);
    groundApp.setTextureAttributes(groundTexAtt);

    if (groundTex != null)
        groundApp.setTexture(groundTex);

    ElevationGrid ground = new ElevationGrid(11, // X dimension
            11, // Z dimension
            2.0f, // X spacing
            2.0f, // Z spacing
            // Automatically use zero heights
            groundApp); // Appearance

    trans.set(0.0f, -1.0f, 0.0f);
    tr.set(trans);
    tg = new TransformGroup(tr);
    tg.addChild(ground);
    scene.addChild(tg);

    // Add a light
    BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
            1000.0); // Extent

    DirectionalLight light = new DirectionalLight();
    light.setEnable(true);
    light.setColor(new Color3f(1.0f, 1.0f, 1.0f));
    light.setDirection(new Vector3f(0.5f, -1.0f, -0.5f));
    light.setInfluencingBounds(worldBounds);
    scene.addChild(light);

    return scene;
}