Example usage for javax.media.j3d Appearance Appearance

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

Introduction

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

Prototype

public Appearance() 

Source Link

Document

Constructs an Appearance component object using defaults for all state variables.

Usage

From source file:Demo3D.java

/**
 * Construction of the desired borders of the virtual universe (cube).
 * //w  ww  .ja  v a2 s.  c o m
 * @return javax.media.j3d.Shape3D myUniverse - the constructed borders of
 *         the virtual universe
 */
public Shape3D myExternalUniverse() {
    cube = new QuadArray(cubeFaces.length, QuadArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);

    ////////////////////// Geometric part ///////////////////////////

    // Scaling of the faces.
    for (int i = 0; i < cubeFaces.length; i++)
        cubeFaces[i].scale(scale_XYZ);

    cube.setCoordinates(0, cubeFaces);

    for (int i = 0; i < cubeFaces.length; i++) {
        // With i mod 4 ==> 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 for
        // the 4 vertices of the 6 faces, thus each vertex has
        // a point in the texture space. In this case, each cube's
        // face has the same texture coordinates.
        cube.setTextureCoordinate(0, i, textCoord[i % 4]);
    }

    // The geometry is passed to the instance this of the cube.
    this.setGeometry(cube);

    ////////////////////// Appearance part ///////////////////////////

    Appearance appearance = new Appearance();

    // This code block is only necessary to insure, in all cases, the
    // correct
    // rendering of the 6 faces of the cube (bug in Java3D version 1.2.0 !).
    // Set up the polygon's rendering-mode
    PolygonAttributes polygonAttributes = new PolygonAttributes();
    polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL);
    appearance.setPolygonAttributes(polygonAttributes);

    // Loading the texture for the 6 cube's faces.
    newTextureLoader = new NewTextureLoader("Images/Ciel_Outside.jpg");
    newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());
    texture = newTextureLoader.getTexture();
    appearance.setTexture(texture);

    // Application modes of the texture
    textAttr = new TextureAttributes();
    textAttr.setTextureMode(TextureAttributes.MODULATE); // there still are:
    // BLEND, COMBINE,
    // DECAL, and REPLACE
    appearance.setTextureAttributes(textAttr);

    // The appearance is passed to the instance this of the cube.
    this.setAppearance(appearance);

    return this;
}

From source file:Demo3D.java

/**
 * This methode serves to construct the earth.
 * //from  w w w .  j av a 2 s .  c  o  m
 * @return com.sun.j3d.utils.geometry.Sphere earth - the constructed earth
 */
public Sphere myEarth() {
    // Optical properties of the earth.

    // Ambient-diffuse-reflection coefficient
    diffAmb = new Color3f(1.0f, 1.0f, 1.0f);
    // Diffuse-reflection coefficient
    reflDiff = new Color3f(1.0f, 1.0f, 1.0f);
    // Specular-reflection coefficient (reflectance function)
    reflSpec = new Color3f(0.0f, 0.0f, 0.1f);
    // c = shininess: cos^c in the specular reflection
    c = 1;
    // Emitted light
    emittedLight = new Color3f(0.0f, 0.0f, 0.0f);

    appearance = new Appearance();

    // Create the material and set up the optical properties.
    material = new Material(diffAmb, emittedLight, reflDiff, reflSpec, c);
    appearance.setMaterial(material);

    // Set up the polygon's rendering-mode (with the polygonAttributes) and
    // the shading-mode (with the coloringAttributes).
    polygonAttributes = new PolygonAttributes();
    coloringAttributes = new ColoringAttributes();

    // Points
    if (renderingType.compareTo("points") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_POINT);
    }

    /* Lines*/
    else if (renderingType.compareTo("lines") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    }

    /* Polygons */
    else if (renderingType.compareTo("polygons") == 0) {
        /* is the default value*/
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL);
        coloringAttributes.setShadeModel(ColoringAttributes.SHADE_FLAT);
    }

    /* Gouraud */
    else if (renderingType.compareTo("gouraud") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL); /* is the default value*/
        coloringAttributes.setShadeModel(ColoringAttributes.SHADE_GOURAUD); /* is the default value*/
    }

    else if (renderingType.compareTo("texture") == 0) {
        polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_FILL); /* is the default value*/
        coloringAttributes.setShadeModel(ColoringAttributes.SHADE_GOURAUD); /* is the default value*/

        /* Loading of the texture*/
        newTextureLoader = new NewTextureLoader("Images/Earth.jpg");
        newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());
        texture = newTextureLoader.getTexture();

        appearance.setTexture(texture);

        /* Application mode of the texture */
        textAttr = new TextureAttributes();
        textAttr.setTextureMode(TextureAttributes.REPLACE); /* there still are:
                                                            BLEND, COMBINE,
                                                            DECAL, and MODULATE*/
        appearance.setTextureAttributes(textAttr);
    }

    appearance.setPolygonAttributes(polygonAttributes);
    appearance.setColoringAttributes(coloringAttributes);

    /* Construction of the earth with all its features.*/
    earth = new Sphere(scale_XYZ, Sphere.GENERATE_NORMALS | Sphere.GENERATE_TEXTURE_COORDS, 10, appearance);
    return earth;
}

From source file:Demo3D.java

/**
 * Construction of the desired tetrahedron.
 * /*w  w w.j av  a 2s .  co m*/
 * @return javax.media.j3d.Shape3D myTetrahedron - the constructed
 *         tetrahedron
 */
public Shape3D myTetrahedron() {

    ////////////////////// Geometric part ///////////////////////////

    // The 4 vertices p0, p1, p2 and p3 of the tetrahedron.
    vertices = new Point3f[lengthVertices]; // 4
    vertices[0] = new Point3f(0.0f, 0.0f, 0.0f);
    vertices[1] = new Point3f(1.0f, 0.0f, 0.0f);
    vertices[2] = new Point3f(0.0f, 1.0f, 0.0f);
    vertices[3] = new Point3f(0.0f, 0.0f, 1.0f);

    // Scaling of vertices
    for (int i = 0; i < lengthVertices; i++)
        // lengthVertices = 4
        vertices[i].scale(scale_XYZ);

    // Set the face's indices for the tetrahedron (referenced to the array
    // of vertices
    // by setCoordinates(vertices) and
    // setCoordinateIndices(tetraFaceIndices)).
    tetraFaceIndices = new int[lengthTetraFaceIndices]; // 12
    // From the camera in the view coordinate system
    // bottom
    tetraFaceIndices[0] = 0;
    tetraFaceIndices[1] = 1;
    tetraFaceIndices[2] = 3;
    // back-left face
    tetraFaceIndices[3] = 0;
    tetraFaceIndices[4] = 3;
    tetraFaceIndices[5] = 2;
    // back face
    tetraFaceIndices[6] = 0;
    tetraFaceIndices[7] = 2;
    tetraFaceIndices[8] = 1;
    // front face
    tetraFaceIndices[9] = 1;
    tetraFaceIndices[10] = 2;
    tetraFaceIndices[11] = 3;

    // Create the GeometryInfo instance and set the vertices
    tetra_GeometryInfo = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
    tetra_GeometryInfo.setCoordinates(vertices);
    tetra_GeometryInfo.setCoordinateIndices(tetraFaceIndices);

    //      triangulator = new Triangulator(); // only for polygons:
    // POLYGON_ARRAY
    //      triangulator.triangulate(tetra_GeometryInfo); // and with: int
    // stripCounts[]
    //           gi.setStripCounts(...)
    //           int contourCounts[]

    // Set the parameters (1 texture with dimension 2) for the texture's
    // coordinates
    tetra_GeometryInfo.setTextureCoordinateParams(1, 2);

    //    case #1: each face of the tetrahedron has the same texture portion.

    // The coordinates of the 3 points in the 2D texture space.
    textCoord2f = new TexCoord2f[3];
    textCoord2f[0] = new TexCoord2f(0.0f, 0.2f);
    textCoord2f[1] = new TexCoord2f(0.5f, 1.0f);
    textCoord2f[2] = new TexCoord2f(1.0f, 0.5f);

    // Set the texture coordinate's indices (referenced to the array of 2D
    // points
    // in the texture space by setTextureCoordinates(0, textCoord2f) and
    // setTextureCoordinateIndices(0, textCoordIndices)).
    textCoordIndices = new int[lengthTetraFaceIndices]; // 12

    // From the camera in the view coordinate system (inverse of
    // tetraFaceIndices !!!)
    // front face
    textCoordIndices[0] = 0;
    textCoordIndices[1] = 1;
    textCoordIndices[2] = 2;
    // back face
    textCoordIndices[3] = 0;
    textCoordIndices[4] = 1;
    textCoordIndices[5] = 2;
    // back-left face
    textCoordIndices[6] = 2;
    textCoordIndices[7] = 0;
    textCoordIndices[8] = 1;
    // bottom
    textCoordIndices[9] = 0;
    textCoordIndices[10] = 1;
    textCoordIndices[11] = 2;

    /*
     * // case #2: each face of the tetrahedron has a different part of the
     * texture.
     *  // The coordinates of the 4 points in the 2D texture space.
     * textCoord2f = new TexCoord2f[4]; textCoord2f[0] = new
     * TexCoord2f(0.0f, 0.5f); textCoord2f[1] = new TexCoord2f(1.0f, 0.5f);
     * textCoord2f[2] = new TexCoord2f(0.6f, 0.7f); textCoord2f[3] = new
     * TexCoord2f(0.6f, 0.3f);
     * 
     *  // Set the texture coordinate's indices (referenced to the array of
     * 2D points // in the texture space by setTextureCoordinates(0,
     * textCoord2f) and // setTextureCoordinateIndices(0,
     * textCoordIndices)). textCoordIndices = new
     * int[lengthTetraFaceIndices]; // 12
     *  // From the camera in the view coordinate system (inverse of
     * tetraFaceIndices !!!) // front face textCoordIndices[0] = 3;
     * textCoordIndices[1] = 2; textCoordIndices[2] = 0; // back face
     * textCoordIndices[3] = 1; textCoordIndices[4] = 2; textCoordIndices[5] =
     * 3; // back-left face textCoordIndices[6] = 1; textCoordIndices[7] =
     * 0; textCoordIndices[8] = 2; // bottom textCoordIndices[9] = 1;
     * textCoordIndices[10]= 3; textCoordIndices[11]= 0;
     */
    // just one set
    tetra_GeometryInfo.setTextureCoordinates(0, textCoord2f);
    // just one set
    tetra_GeometryInfo.setTextureCoordinateIndices(0, textCoordIndices);

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

    if (crAngle)
        normalGenerator.setCreaseAngle(0.0f); // with 0 radian ===> creased

    stripifier = new Stripifier();
    stripifier.stripify(tetra_GeometryInfo);

    tetra_GeometryArray = tetra_GeometryInfo.getGeometryArray();

    // The geonometry is passed to the instance this of the tetrahedron.
    this.setGeometry(tetra_GeometryArray);

    ////////////////////// Appearance part ///////////////////////////

    appearance = new Appearance();

    // Optical properties of the tetrahedron.

    // Ambient-diffuse-reflection coefficient
    diffAmb = new Color3f(1.0f, 0.5f, 1.0f);
    // Diffuse-reflection coefficient
    reflDiff = new Color3f(1.0f, 0.5f, 1.0f);
    // Specular-reflection coefficient (reflectance function)
    reflSpec = new Color3f(1.0f, 0.5f, 1.0f);
    // c = shininess: cos^c in the specular reflection
    float c = 15;
    // Emitted light
    emittedLight = new Color3f(0.0f, 0.0f, 0.0f);

    material = new Material(diffAmb, emittedLight, reflDiff, reflSpec, c);
    appearance.setMaterial(material);

    // This instance acts only on the tetrahedron and not on its texture.
    trAttr = new TransparencyAttributes(TransparencyAttributes.NICEST, 0.0f);
    // 0.0 = fully opaque
    // 1.0 = fully transparent
    appearance.setTransparencyAttributes(trAttr);

    // Loading the texture
    newTextureLoader = new NewTextureLoader("Images/Claude.jpg");
    newTextureLoader.setImageObserver(newTextureLoader.getImageObserver());

    texture = newTextureLoader.getTexture();

    appearance.setTexture(texture);

    // Application mode of the texture
    textAttr = new TextureAttributes();
    textAttr.setTextureMode(TextureAttributes.MODULATE); // there still are:
    // BLEND, COMBINE,
    // DECAL, and REPLACE
    appearance.setTextureAttributes(textAttr);

    // The appearance is passed to the instance this of the tetrahedron.
    this.setAppearance(appearance);

    return this;
}

From source file:AppearanceExplorer.java

BackgroundTool(String codeBaseString) {

    bgSwitch = new Switch(Switch.CHILD_NONE);
    bgSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // set up the dark grey BG color node
    Background bgDarkGrey = new Background(darkGrey);
    bgDarkGrey.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgDarkGrey);/*w ww . j  a v  a  2  s .co  m*/

    // set up the grey BG color node
    Background bgGrey = new Background(grey);
    bgGrey.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgGrey);

    // set up the light grey BG color node
    Background bgLightGrey = new Background(lightGrey);
    bgLightGrey.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgLightGrey);

    // set up the white BG color node
    Background bgWhite = new Background(white);
    bgWhite.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgWhite);

    // set up the blue BG color node
    Background bgBlue = new Background(skyBlue);
    bgBlue.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgBlue);

    // set up the image
    java.net.URL bgImageURL = null;
    try {
        bgImageURL = new java.net.URL(codeBaseString + "bg.jpg");
    } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
    }
    if (bgImageURL == null) { // application, try file URL
        try {
            bgImageURL = new java.net.URL("file:./bg.jpg");
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }
    TextureLoader bgTexture = new TextureLoader(bgImageURL, null);

    // Create a background with the static image
    Background bgImage = new Background(bgTexture.getImage());
    bgImage.setApplicationBounds(infiniteBounds);
    bgSwitch.addChild(bgImage);

    // create a background with the image mapped onto a sphere which
    // will enclose the world
    Background bgGeo = new Background();
    bgGeo.setApplicationBounds(infiniteBounds);
    BranchGroup bgGeoBG = new BranchGroup();
    Appearance bgGeoApp = new Appearance();
    bgGeoApp.setTexture(bgTexture.getTexture());
    Sphere sphereObj = new Sphere(1.0f,
            Sphere.GENERATE_NORMALS | Sphere.GENERATE_NORMALS_INWARD | Sphere.GENERATE_TEXTURE_COORDS, 45,
            bgGeoApp);
    bgGeoBG.addChild(sphereObj);
    bgGeo.setGeometry(bgGeoBG);
    bgSwitch.addChild(bgGeo);

    // Create the chooser GUI
    String[] bgNames = { "No Background (Black)", "Dark Grey", "Grey", "Light Grey", "White", "Blue",
            "Sky Image", "Sky Geometry", };
    int[] bgValues = { Switch.CHILD_NONE, 0, 1, 2, 3, 4, 5, 6 };

    bgChooser = new IntChooser("Background:", bgNames, bgValues, 0);
    bgChooser.addIntListener(new IntListener() {
        public void intChanged(IntEvent event) {
            int value = event.getValue();
            bgSwitch.setWhichChild(value);
        }
    });
    bgChooser.setValue(Switch.CHILD_NONE);
}

From source file:FourByFour.java

public Positions() {

    // Define colors for lighting
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f red = new Color3f(0.9f, 0.1f, 0.2f);
    Color3f blue = new Color3f(0.3f, 0.3f, 0.8f);
    Color3f yellow = new Color3f(1.0f, 1.0f, 0.0f);
    Color3f ambRed = new Color3f(0.3f, 0.03f, 0.03f);
    Color3f ambBlue = new Color3f(0.03f, 0.03f, 0.3f);
    Color3f ambYellow = new Color3f(0.3f, 0.3f, 0.03f);
    Color3f ambWhite = new Color3f(0.3f, 0.3f, 0.3f);
    Color3f specular = new Color3f(1.0f, 1.0f, 1.0f);

    // Create the red appearance node
    redMat = new Material(ambRed, black, red, specular, 100.f);
    redMat.setLightingEnable(true);/*from ww  w .  j  av a  2 s. com*/
    redApp = new Appearance();
    redApp.setMaterial(redMat);

    // Create the blue appearance node
    blueMat = new Material(ambBlue, black, blue, specular, 100.f);
    blueMat.setLightingEnable(true);
    blueApp = new Appearance();
    blueApp.setMaterial(blueMat);

    // Create the yellow appearance node
    yellowMat = new Material(ambYellow, black, yellow, specular, 100.f);
    yellowMat.setLightingEnable(true);
    yellowApp = new Appearance();
    yellowApp.setMaterial(yellowMat);

    // Create the white appearance node
    whiteMat = new Material(ambWhite, black, white, specular, 100.f);
    whiteMat.setLightingEnable(true);
    whiteApp = new Appearance();
    whiteApp.setMaterial(whiteMat);

    // Load the point array with the offset (coordinates) for each of
    // the 64 positions.
    point = new Vector3f[64];
    int count = 0;
    for (int i = -30; i < 40; i += 20) {
        for (int j = -30; j < 40; j += 20) {
            for (int k = -30; k < 40; k += 20) {
                point[count] = new Vector3f((float) k, (float) j, (float) i);
                count++;
            }
        }
    }

    // Create the switch nodes
    posSwitch = new Switch(Switch.CHILD_MASK);
    humanSwitch = new Switch(Switch.CHILD_MASK);
    machineSwitch = new Switch(Switch.CHILD_MASK);

    // Set the capability bits
    posSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
    posSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    humanSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
    humanSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    machineSwitch.setCapability(Switch.ALLOW_SWITCH_READ);
    machineSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);

    // Create the bit masks
    posMask = new BitSet();
    humanMask = new BitSet();
    machineMask = new BitSet();

    // Create the small white spheres that mark unoccupied
    // positions.
    posSphere = new Sphere[64];
    for (int i = 0; i < 64; i++) {
        Transform3D transform3D = new Transform3D();
        transform3D.set(point[i]);
        TransformGroup transformGroup = new TransformGroup(transform3D);
        posSphere[i] = new Sphere(2.0f, Sphere.GENERATE_NORMALS | Sphere.ENABLE_APPEARANCE_MODIFY, 12,
                whiteApp);
        Shape3D shape = posSphere[i].getShape();
        ID id = new ID(i);
        shape.setUserData(id);
        transformGroup.addChild(posSphere[i]);
        posSwitch.addChild(transformGroup);
        posMask.set(i);
    }

    // Create the red spheres that mark the user's positions.
    for (int i = 0; i < 64; i++) {
        Transform3D transform3D = new Transform3D();
        transform3D.set(point[i]);
        TransformGroup transformGroup = new TransformGroup(transform3D);
        transformGroup.addChild(new Sphere(7.0f, redApp));
        humanSwitch.addChild(transformGroup);
        humanMask.clear(i);
    }

    // Create the blue cubes that mark the computer's positions.
    for (int i = 0; i < 64; i++) {
        Transform3D transform3D = new Transform3D();
        transform3D.set(point[i]);
        TransformGroup transformGroup = new TransformGroup(transform3D);
        BigCube cube = new BigCube(blueApp);
        transformGroup.addChild(cube.getChild());
        machineSwitch.addChild(transformGroup);
        machineMask.clear(i);
    }

    // Set the positions mask
    posSwitch.setChildMask(posMask);
    humanSwitch.setChildMask(humanMask);
    machineSwitch.setChildMask(machineMask);

    // Throw everything into a single group
    group = new Group();
    group.addChild(posSwitch);
    group.addChild(humanSwitch);
    group.addChild(machineSwitch);
}

From source file:FourByFour.java

PickDragBehavior(Canvas2D canvas2D, Canvas3D canvas3D, Positions positions, BranchGroup branchGroup,
        TransformGroup transformGroup) {

    this.canvas2D = canvas2D;
    this.canvas3D = canvas3D;
    this.positions = positions;
    this.branchGroup = branchGroup;
    this.transformGroup = transformGroup;

    modelTrans = new Transform3D();
    transformX = new Transform3D();
    transformY = new Transform3D();

    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f green = new Color3f(0.0f, 1.0f, 0.0f);

    highlight = new Appearance();
    highlight.setMaterial(new Material(green, black, green, white, 80.f));

    parallel = true;//from  www .  j a  va 2  s .co m
}