Example usage for javax.media.j3d Texture2D Texture2D

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

Introduction

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

Prototype

public Texture2D(int mipMapMode, int format, int width, int height) 

Source Link

Document

Constructs an empty Texture2D object with specified mipmapMode format, width and height.

Usage

From source file:MultiTextureTest.java

public Texture createLightMap() {

    int width = 128;
    int height = 128;
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    int[] rgbArray = new int[width * height];
    int index, index2;
    int rgbInc = 256 / (width / 2 - 20);
    int rgbValue = 0;
    int k = width / 2 - 5;
    int i, j, rgb;

    rgb = 0xff;//  w  w w  . ja  v  a2s  .  co m
    rgbValue = rgb | (rgb << 8) | (rgb << 16) | (rgb << 24);
    for (i = width / 2 - 1, j = 0; j < 10; j++, i--) {
        rgbArray[i] = rgbValue;
    }

    for (; i > 8; i--, rgb -= rgbInc) {
        rgbValue = rgb | (rgb << 8) | (rgb << 16) | (rgb << 24);
        rgbArray[i] = rgbValue;
    }

    for (; i >= 0; i--) {
        rgbArray[i] = rgbValue;
    }

    for (i = 0; i < width / 2; i++) {
        rgbValue = rgbArray[i];
        index = i;
        index2 = (width - i - 1);
        for (j = 0; j < height; j++) {
            rgbArray[index] = rgbArray[index2] = rgbValue;
            index += width;
            index2 += width;
        }
    }

    bimage.setRGB(0, 0, width, height, rgbArray, 0, width);

    ImageComponent2D grayImage = new ImageComponent2D(ImageComponent.FORMAT_RGB, bimage);

    lightTex = new Texture2D(Texture.BASE_LEVEL, Texture.RGB, width, height);
    lightTex.setImage(0, grayImage);

    return lightTex;
}

From source file:SimpleTexture.java

/**
 * This defines the appearance with a texture. The texture is loaded from an
 * external file./*from w  ww.  j  av a  2 s  . co  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.
 * //  w  w w  .j a  v  a 2s . 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:ImageComponentByReferenceTest.java

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

    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);

    objRoot.addChild(objTrans);//from   w  ww  .  j a  va 2  s .  co m

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

    app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

    textureCube = new Box(0.4f, 0.4f, 0.4f, Box.GENERATE_TEXTURE_COORDS | Box.GENERATE_NORMALS, app);
    boxShape = textureCube.getShape(Box.FRONT);
    boxShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    objTrans.addChild(textureCube);

    checkBoard = new TiledImage();
    TextureLoader texLoader = new TextureLoader(texImage, this);
    ImageComponent2D oneImage = texLoader.getImage();
    bImage1 = oneImage.getImage();

    int index = 0;
    image[index++] = new ImageComponent2D(oneImage.getFormat(), (RenderedImage) bImage1, false, true);

    image[index++] = new ImageComponent2D(oneImage.getFormat(), (RenderedImage) bImage1, true, true);

    image[index++] = new ImageComponent2D(oneImage.getFormat(), (RenderedImage) bImage1, false, false);

    image[index++] = new ImageComponent2D(oneImage.getFormat(), (RenderedImage) bImage1, true, false);

    createRaster(objRoot);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA, checkBoard, false, true);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA, checkBoard, true, true);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA, checkBoard, false, false);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA, checkBoard, true, false);

    texOne = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image[2].getWidth(), image[2].getHeight());

    texOne.setCapability(Texture.ALLOW_IMAGE_WRITE);
    texOne.setImage(0, image[2]);

    app.setTexture(texOne);

    texCheckBoard = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image[4].getWidth(), image[4].getHeight());

    texCheckBoard.setCapability(Texture.ALLOW_IMAGE_WRITE);
    objRoot.compile();
    return objRoot;
}

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 av a 2 s .c om*/
    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:J3dSwingFrame.java

/**
 * Set the texture on our goemetry//from w w w.  j a  v a 2s. c  o  m
 * <P>
 * Always specified as a URL so that we may fetch it from anywhere.
 * 
 * @param url
 *            The url to the image.
 */
public void setTexture(URL url) {
    Toolkit tk = Toolkit.getDefaultToolkit();
    Image src_img = tk.createImage(url);
    BufferedImage buf_img = null;

    if (!(src_img instanceof BufferedImage)) {
        // create a component anonymous inner class to give us the image
        // observer we need to get the width and height of the source image.
        Component obs = new Component() {
        };

        int width = src_img.getWidth(obs);
        int height = src_img.getHeight(obs);

        // construct the buffered image from the source data.
        buf_img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics g = buf_img.getGraphics();
        g.drawImage(src_img, 0, 0, null);
        g.dispose();
    } else
        buf_img = (BufferedImage) src_img;

    src_img.flush();

    ImageComponent img_comp = new ImageComponent2D(ImageComponent.FORMAT_RGB, buf_img);

    texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGB, img_comp.getWidth(), img_comp.getHeight());

    appearance.setTexture(texture);

    buf_img.flush();
}

From source file:AppearanceTest.java

protected NodeComponent createComponent() {
    TextureLoader texLoader = new TextureLoader("texture00.jpg", m_Component);
    ImageComponent2D image = texLoader.getImage();
    Texture2D tex2D = new Texture2D(Texture.MULTI_LEVEL_MIPMAP, Texture.RGBA, image.getWidth(),
            image.getHeight());/* www.  ja  va2  s . c om*/

    for (int n = 0; n <= 6; n++) {
        texLoader = new TextureLoader("texture0" + n + ".jpg", m_Component);
        tex2D.setImage(n, texLoader.getImage());
    }

    return (NodeComponent) tex2D;
}