Example usage for javax.media.j3d Appearance setTexture

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

Introduction

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

Prototype

public void setTexture(Texture texture) 

Source Link

Document

Sets the texture object to the specified object.

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);
    ap.setTextureAttributes(texAttr);//from   w  w w .ja  v a  2 s.  co m

    //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:SimpleTexture.java

/**
 * This defines the appearance with a texture. The texture is loaded from an
 * external file./* ww  w  . j  a  va  2 s.  c  o  m*/
 * 
 * @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:SimpleTextureGen.java

/**
 * This defines the appearance for the shape using a texture. It uses a
 * TextureLoader to load the texture image from an external file and a
 * TexCoordGeneration to define the texture coordinates.
 * //from w  ww . j a v  a  2  s .  c  o m
 * @return Appearance that uses a texture.
 */
protected Appearance DefineAppearance() {
    //This is used to automatically define the texture coordinates.
    //The coordinates are generated in object coordinates, but by
    //commented out the line with 'OBJECT_LINEAR' and uncommenting
    //the line 'EYE_LINEAR' the program will use eye coordinates.
    TexCoordGeneration textCoorder = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR,
            //TexCoordGeneration.EYE_LINEAR,
            TexCoordGeneration.TEXTURE_COORDINATE_2);
    //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);
    //Pass the coordinate generator to the appearance
    app.setTexCoordGeneration(textCoorder);
    //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: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);//ww  w .  j av a2 s  .  c  om

    // 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:SimpleTest.java

public BranchGroup createBackground() {
    // create a parent BranchGroup for the Background
    BranchGroup backgroundGroup = new BranchGroup();

    // create a new Background node
    Background back = new Background();

    // set the range of influence of the background
    back.setApplicationBounds(getBoundingSphere());

    // create a BranchGroup that will hold
    // our Sphere geometry
    BranchGroup bgGeometry = new BranchGroup();

    // create an appearance for the Sphere
    Appearance app = new Appearance();

    // load a texture image using the Java 3D texture loader
    Texture tex = new TextureLoader("back.jpg", this).getTexture();

    // apply the texture to the Appearance
    app.setTexture(tex);

    // create the Sphere geometry with radius 1.0
    // we tell the Sphere to generate texture coordinates
    // to enable the texture image to be rendered
    // and because we are *inside* the Sphere we have to generate
    // Normal coordinates inwards or the Sphere will not be visible.
    Sphere sphere = new Sphere(1.0f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS_INWARD,
            app);/*from ww w .  ja va  2 s  . co  m*/

    // start wiring everything together
    // add the Sphere to its parent BranchGroup
    bgGeometry.addChild(sphere);

    // assign the BranchGroup to the Background as geometry.
    back.setGeometry(bgGeometry);

    // add the Background node to its parent BranchGroup
    backgroundGroup.addChild(back);

    return backgroundGroup;
}

From source file:BooksDemo.java

private Appearance createTexture(String fileName) {
    Image sourceImage = UIHelper.readImage(fileName);
    if (sourceImage == null)
        System.out.println("Image could not be loaded from " + fileName);

    TextureLoader loader = new TextureLoader(sourceImage, this);
    ImageComponent2D image = loader.getImage();

    if (image == null)
        System.out.println("Texture could not be loaded from " + fileName);

    Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image.getWidth(), image.getHeight());
    texture.setImage(0, image);/*www  .j a  va  2s.c o m*/
    texture.setEnable(true);
    texture.setMagFilter(Texture.BASE_LEVEL_LINEAR);
    texture.setMinFilter(Texture.BASE_LEVEL_LINEAR);

    Appearance appearance = new Appearance();
    PolygonAttributes polyAttributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL,
            PolygonAttributes.CULL_NONE, 0f);
    appearance.setPolygonAttributes(polyAttributes);
    appearance.setTexture(texture);

    TextureAttributes textureAttributes = new TextureAttributes();
    appearance.setTextureAttributes(textureAttributes);

    return appearance;
}

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 w  w w  .  j a  va 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.
    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;
}

From source file:MultiView.java

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

    //Make the scene graph
    try {//from   ww  w .  j av  a2  s  .  co m
        TransformGroup objTrans = new TransformGroup();
        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objRoot.addChild(objTrans);

        // Create appearance object for textured cube
        Appearance app = new Appearance();

        Texture tex = new TextureLoader("Dog.jpg", this).getTexture();
        app.setTexture(tex);

        // Create a simple shape leaf node, add it to the scene graph.
        Box textureCube = new Box(2, 3, 4, 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);
    } catch (RuntimeException e) {
        System.out.println("MultiView.createSceneBranchGroup:" + e.getMessage());
        System.exit(-1);
    }

    return objRoot;
}

From source file:TextureMapping.java

/**
 * Wurfeldarstellung/*from  w  ww  .  j ava 2  s.c  o  m*/
 * 
 * @return Appearance
 */
private Appearance makeAppearance() {
    Appearance a = new Appearance();
    TextureLoader loader = null;
    if (getCodeBase() != null)
        try {
            // Laden der Obj Datei als Applet mit jar
            loader = new TextureLoader(new URL("jar:" + getCodeBase() + "TextureMapping.jar!/burnstone.jpg"),
                    null);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    else
        // Laden der Obj Datei mittels jar
        loader = new TextureLoader(ClassLoader.getSystemResource("burnstone.jpg"), null);

    Texture2D texture = (Texture2D) loader.getTexture();
    a.setTexture(texture);
    return a;
}

From source file:TextureTransformTest.java

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

    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);

    // create the rotation interpolator to rotate the scene
    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f,
            (float) Math.PI * 2.0f);
    rotator.setSchedulingBounds(createApplicationBounds());
    objTrans.addChild(rotator);/*ww w.  j a  v  a2  s.c om*/

    // create the box
    final int nScale = 50;
    Appearance app = new Appearance();
    Box box = new Box(nScale, nScale, nScale, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS,
            app);

    // load the texture image
    TextureLoader texLoader = new TextureLoader("texture.gif", this);
    app.setTexture(texLoader.getTexture());

    // set the texture attributes and ensure we
    // can write to the Transform for the texture attributes
    m_TextureAttributes = new TextureAttributes();
    m_TextureAttributes.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE);
    app.setTextureAttributes(m_TextureAttributes);

    // connect all the elements
    objTrans.addChild(box);
    objRoot.addChild(objTrans);
    objRoot.addChild(createRotator());

    return objRoot;
}