Example usage for javax.media.j3d Appearance setMaterial

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

Introduction

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

Prototype

public void setMaterial(Material material) 

Source Link

Document

Sets the material object to the specified object.

Usage

From source file:SimpleSwitch.java

/**
 * This creates the shapes used in the program. A switch node is created
 * that has its write capability set so that we can swap the rendered shape.
 * Then a box and a cone are created and added to the switch.
 * /*from   w  w w. j  av a2 s  .c  o  m*/
 * @return Node that is the switch node
 */
protected Node buildShape() {
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    //Set the capability so that we can change the switch value
    firstSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
    //Add the two shapes to the switch
    firstSwitch.addChild(new Box(2.0f, 2.0f, 2.0f, Box.GENERATE_NORMALS, app));
    firstSwitch.addChild(new Cone(2.0f, 4.0f, Cone.GENERATE_NORMALS, app));
    return firstSwitch;
}

From source file:SimpleTransform.java

/**
 * This defines the shapes used in the scene. It creates a simple cube using
 * a Box utility class.//from   ww w  .  j av  a2s . c  o m
 * 
 * @return Node that is the root of the shape hierarchy.
 */
protected Node buildShape() {
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    return new Box(2.0f, 2.0f, 2.0f, app);
}

From source file:SimpleMouse.java

/**
 * Build the content branch for the scene graph
 * //from w  w w.j a  va2s.co  m
 * @return BranchGroup that is the root of the content
 */
protected BranchGroup buildContentBranch() {
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));

    Box cube = new Box(2.0f, 2.0f, 2.0f, app);

    BranchGroup contentBranch = new BranchGroup();
    addLights(contentBranch);
    //Create the transform groups that will be
    //affected by the mouse utiltities
    TransformGroup spinGroup = new TransformGroup();
    TransformGroup zoomGroup = new TransformGroup();
    TransformGroup moveGroup = new TransformGroup();
    //Set the capabilities of the groups so that we can
    //manipulate them
    spinGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    spinGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    zoomGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    zoomGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    moveGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    //Create and use the rotation utility
    MouseRotate mouseSpin = new MouseRotate();
    mouseSpin.setTransformGroup(spinGroup);
    contentBranch.addChild(mouseSpin);
    mouseSpin.setSchedulingBounds(bounds);
    //Create and use the zoom utility
    MouseZoom mouseSize = new MouseZoom();
    mouseSize.setTransformGroup(zoomGroup);
    contentBranch.addChild(mouseSize);
    mouseSize.setSchedulingBounds(bounds);
    //Create and use the translation utility
    MouseTranslate mouseMove = new MouseTranslate();
    mouseMove.setTransformGroup(moveGroup);
    contentBranch.addChild(mouseMove);
    mouseMove.setSchedulingBounds(bounds);
    //Put it all together
    spinGroup.addChild(cube);
    moveGroup.addChild(spinGroup);
    zoomGroup.addChild(moveGroup);
    contentBranch.addChild(zoomGroup);
    return contentBranch;

}

From source file:SimpleBillboard.java

/**
 * Build the content branch for the scene graph. This creates two cubes and
 * uses a billboard node to keep one face of one of the cubes facing the
 * viewer./*from w  ww  . ja  v  a2 s .com*/
 * 
 * @return BranchGroup that is the root of the content
 */
protected BranchGroup buildContentBranch() {
    //Create the appearance
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 1.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    //Make the cubes
    Box leftCube = new Box(1.0f, 1.0f, 1.0f, app);
    ColorCube rightCube = new ColorCube();
    //Create the transformgroup used for the billboard
    TransformGroup billBoardGroup = new TransformGroup();
    //Set the access rights to the group
    billBoardGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    //Add the cube to the group
    billBoardGroup.addChild(rightCube);
    //Create and activate the billboard
    Billboard myBillboard = new Billboard(billBoardGroup, Billboard.ROTATE_ABOUT_AXIS,
            new Vector3f(0.0f, 1.0f, 0.0f));
    myBillboard.setSchedulingBounds(bounds);
    BranchGroup contentBranch = new BranchGroup();
    contentBranch.addChild(myBillboard);
    addLights(contentBranch);
    //Position the cubes
    TransformGroup bothGroup = new TransformGroup();
    Transform3D leftGroupXfm = new Transform3D();
    leftGroupXfm.set(new Vector3d(-1.5, 0.0, 0.0));
    TransformGroup leftGroup = new TransformGroup(leftGroupXfm);
    Transform3D rightGroupXfm = new Transform3D();
    rightGroupXfm.set(new Vector3d(1.5, 0.0, 0.0));
    TransformGroup rightGroup = new TransformGroup(rightGroupXfm);
    //Put it all together
    bothGroup.addChild(leftGroup);
    leftGroup.addChild(leftCube);
    bothGroup.addChild(rightGroup);
    rightGroup.addChild(billBoardGroup);
    contentBranch.addChild(bothGroup);
    return contentBranch;

}

From source file:SimpleIndexedQuad.java

/**
 * Build a cube from an IndexedQuadArray. This method creates the vertices
 * as a set of eight points and the normals as a set of six vectors (one for
 * each face). The data is then defined such that each vertex has a
 * different normal associated with it when it is being used for a different
 * face./*from   w w w.  j  av  a2  s  .c o  m*/
 * 
 * @return Node that is the shape.
 */
protected Node buildShape() {
    //The shape. The constructor specifies 8 vertices, that both
    //vertices and normals are to be defined and that there are
    //24 normals to be specified (4 for each of the 6 faces).
    IndexedQuadArray indexedCube = new IndexedQuadArray(8,
            IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
    //The vertex coordinates defined as an array of points.
    Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f),
            new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f),
            new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f),
            new Point3f(1.0f, -1.0f, -1.0f) };
    //The vertex normals defined as an array of vectors
    Vector3f[] normals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f),
            new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f),
            new Vector3f(0.0f, -1.0f, 0.0f) };
    //Define the indices used to reference vertex array
    int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 };
    //Define the indices used to reference normal array
    int normalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 };
    //Set the data
    indexedCube.setCoordinates(0, cubeCoordinates);
    indexedCube.setNormals(0, normals);
    indexedCube.setCoordinateIndices(0, coordIndices);
    indexedCube.setNormalIndices(0, normalIndices);
    //Define an appearance for the shape
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    //Create and return the shape
    return new Shape3D(indexedCube, app);
}

From source file:SimpleIndexedQuadSmooth.java

/**
 * Build a cube from an IndexedQuadArray. This method creates the vertices
 * as a set of eight points and the normals as a set of six vectors (one for
 * each face). The data is then defined such that each vertex has a
 * different normal associated with it when it is being used for a different
 * face./*from   w  w w  .j a  v  a 2 s.  co m*/
 * 
 * @return Node that is the shape.
 */
protected Node buildShape() {
    //The shape. The constructor specifies 8 vertices, that both
    //vertices and normals are to be defined and that there are
    //24 normals to be specified (4 for each of the 6 faces).
    IndexedQuadArray indexedCube = new IndexedQuadArray(8,
            IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
    //The vertex coordinates defined as an array of points.
    Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f),
            new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f),
            new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f),
            new Point3f(1.0f, -1.0f, -1.0f) };
    //The vertex normals defined as an array of vectors
    Vector3f[] normals = { new Vector3f(1.0f, 1.0f, 1.0f), new Vector3f(-1.0f, 1.0f, 1.0f),
            new Vector3f(-1.0f, -1.0f, 1.0f), new Vector3f(1.0f, -1.0f, 1.0f), new Vector3f(1.0f, 1.0f, -1.0f),
            new Vector3f(-1.0f, 1.0f, -1.0f), new Vector3f(-1.0f, -1.0f, -1.0f),
            new Vector3f(1.0f, -1.0f, -1.0f) };
    //Define the indices used to reference vertex array
    int coordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 };
    //Define the indices used to reference normal array
    int normalIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 };
    //Set the data
    indexedCube.setCoordinates(0, cubeCoordinates);
    indexedCube.setNormals(0, normals);
    indexedCube.setCoordinateIndices(0, coordIndices);
    indexedCube.setNormalIndices(0, normalIndices);
    //Define an appearance for the shape
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    //Create and return the shape
    return new Shape3D(indexedCube, app);
}

From source file:cgview.java

public BranchGroup createSceneGraph(CompressedGeometry cg) {
    // 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.7);//from w  w w  .  j  a  v  a 2 s  . c om
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    // 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);
    objScale.addChild(objTrans);

    // Add compressed geometry to the scene graph.
    CompressedGeometryHeader hdr = new CompressedGeometryHeader();
    cg.getCompressedGeometryHeader(hdr);

    // There isn't really enough information in the compressed geometry
    // header to unamiguously determine the proper rendering attributes.
    // The bufferDataPresent field specifies whether or not normals are
    // bundled with vertices, but the compressed buffer can still contain
    // normals that should be lit. Assume that any surface geometry
    // should be lit and that lines and points should not unless the
    // header contains the NORMAL_IN_BUFFER bit.
    Material m = new Material();
    if ((hdr.bufferType == hdr.TRIANGLE_BUFFER) || ((hdr.bufferDataPresent & hdr.NORMAL_IN_BUFFER) == 1))
        m.setLightingEnable(true);
    else
        m.setLightingEnable(false);

    Appearance a = new Appearance();
    a.setMaterial(m);

    objTrans.addChild(new Shape3D(cg, a));

    // Create mouse behavior scheduling bounds.
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

    // 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, -0.9f);

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

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

    return objRoot;
}

From source file:SimpleTexture.java

/**
 * This defines the appearance with a texture. The texture is loaded from an
 * external file.//from  w w  w  .j  a  v a  2 s . com
 * 
 * @return Appearance that uses the texture.
 */
protected Appearance DefineAppearance() {
    //Load the texture from the external image file
    TextureLoader textLoad = new TextureLoader("housebrick.jpg", this);
    //Access the image from the loaded texture
    ImageComponent2D textImage = textLoad.getImage();
    //Create a two dimensional texture
    Texture2D texture = new Texture2D(Texture2D.BASE_LEVEL, Texture.RGB, textImage.getWidth(),
            textImage.getHeight());
    //Set the texture from the image loaded
    texture.setImage(0, textImage);
    //Create the appearance that will use the texture
    Appearance app = new Appearance();
    app.setTexture(texture);
    //Define how the texture will be mapped onto the surface
    //by creating the appropriate texture attributes
    TextureAttributes textAttr = new TextureAttributes();
    textAttr.setTextureMode(TextureAttributes.REPLACE);
    app.setTextureAttributes(textAttr);
    app.setMaterial(new Material());
    return app;
}

From source file:SimpleSounds.java

protected BranchGroup buildContentBranch() {
    //Create the appearance
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess));
    //Make the cube
    Box myCube = new Box(1.0f, 1.0f, 1.0f, app);

    TransformGroup cubeGroup = new TransformGroup();

    BranchGroup contentBranch = new BranchGroup();
    addLights(contentBranch);/*from w  w  w . j av a  2 s  .c om*/
    addObjectSound(cubeGroup, sound1, new String("file:./loop1.wav"), 10.0f);
    addObjectSound(cubeGroup, sound2, new String("file:./loop2.wav"), 20.0f);
    addBackgroundSound(contentBranch, new String("file:./loop3.wav"));

    cubeGroup.addChild(myCube);
    contentBranch.addChild(cubeGroup);
    return contentBranch;

}

From source file:BackgroundGeometry.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);/*from  www.  j a  va 2 s  . c o m*/
    objScale.setTransform(t3d);
    objRoot.addChild(objScale);

    // 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.
    TransformGroup objTrans = new TransformGroup();
    objScale.addChild(objTrans);

    Background bg = new Background();
    bg.setApplicationBounds(bounds);
    BranchGroup backGeoBranch = new BranchGroup();
    Sphere sphereObj = new Sphere(1.0f,
            Sphere.GENERATE_NORMALS | Sphere.GENERATE_NORMALS_INWARD | Sphere.GENERATE_TEXTURE_COORDS, 45);
    Appearance backgroundApp = sphereObj.getAppearance();
    backGeoBranch.addChild(sphereObj);
    bg.setGeometry(backGeoBranch);
    objTrans.addChild(bg);

    TextureLoader tex = new TextureLoader(bgImage, new String("RGB"), this);
    if (tex != null)
        backgroundApp.setTexture(tex.getTexture());

    Vector3f tranlation = new Vector3f(2.0f, 0.0f, 0.0f);
    Transform3D modelTransform = new Transform3D();
    Transform3D tmpTransform = new Transform3D();
    double angleInc = Math.PI / 8.0;
    double angle = 0.0;
    int numBoxes = 16;

    float scaleX[] = { 0.1f, 0.2f, 0.2f, 0.3f, 0.2f, 0.1f, 0.2f, 0.3f, 0.1f, 0.3f, 0.2f, 0.3f, 0.1f, 0.3f, 0.2f,
            0.3f };

    float scaleY[] = { 0.3f, 0.4f, 0.3f, 0.4f, 0.3f, 0.4f, 0.3f, 0.4f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f, 0.3f,
            0.4f };

    float scaleZ[] = { 0.3f, 0.2f, 0.1f, 0.1f, 0.3f, 0.2f, 0.1f, 0.3f, 0.3f, 0.2f, 0.1f, 0.3f, 0.3f, 0.2f, 0.1f,
            0.2f };

    Appearance a1 = new Appearance();
    Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f sColor = new Color3f(0.5f, 0.5f, 1.0f);
    Color3f oColor = new Color3f(0.5f, 0.5f, 0.3f);

    Material m = new Material(oColor, eColor, oColor, sColor, 100.0f);
    m.setLightingEnable(true);
    a1.setMaterial(m);

    for (int i = 0; i < numBoxes; i++, angle += angleInc) {
        modelTransform.rotY(angle);
        tmpTransform.set(tranlation);
        modelTransform.mul(tmpTransform);

        TransformGroup tgroup = new TransformGroup(modelTransform);
        objTrans.addChild(tgroup);

        tgroup.addChild(new Box(scaleX[i], scaleY[i], scaleZ[i], Box.GENERATE_NORMALS, a1));
    }

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

    return objRoot;
}