Search in sources :

Example 1 with Image

use of com.ardor3d.image.Image in project energy3d by concord-consortium.

the class SolarRadiation method applyTexture.

private void applyTexture(final Mesh mesh) {
    if (onMesh.get(mesh) == null) {
        mesh.setDefaultColor(ColorRGBA.BLACK);
        mesh.clearRenderState(StateType.Texture);
        return;
    }
    final double[][] solarData = onMesh.get(mesh).dailySolarIntensity;
    final int rows = solarData.length;
    if (rows == 0) {
        return;
    }
    final int cols = solarData[0].length;
    if (cols == 0) {
        return;
    }
    fillBlanksWithNeighboringValues(solarData);
    final ByteBuffer data = BufferUtils.createByteBuffer(cols * rows * 3);
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            final ColorRGBA color = computeColor(solarData[row][col], maxValue);
            data.put((byte) (color.getRed() * 255)).put((byte) (color.getGreen() * 255)).put((byte) (color.getBlue() * 255));
        }
    }
    final Image image = new Image(ImageDataFormat.RGB, PixelDataType.UnsignedByte, cols, rows, data, null);
    final Texture2D texture = new Texture2D();
    texture.setTextureKey(TextureKey.getRTTKey(MinificationFilter.NearestNeighborNoMipMaps));
    texture.setImage(image);
    // texture.setWrap(WrapMode.Clamp);
    final TextureState textureState = new TextureState();
    textureState.setTexture(texture);
    mesh.setDefaultColor(ColorRGBA.WHITE);
    mesh.setRenderState(textureState);
}
Also used : Texture2D(com.ardor3d.image.Texture2D) TextureState(com.ardor3d.renderer.state.TextureState) ReadOnlyColorRGBA(com.ardor3d.math.type.ReadOnlyColorRGBA) ColorRGBA(com.ardor3d.math.ColorRGBA) Image(com.ardor3d.image.Image) ByteBuffer(java.nio.ByteBuffer) CullHint(com.ardor3d.scenegraph.hint.CullHint) TPoint(org.poly2tri.triangulation.point.TPoint) Point(org.poly2tri.geometry.primitives.Point)

Example 2 with Image

use of com.ardor3d.image.Image in project energy3d by concord-consortium.

the class HousePart method getTexture.

private Texture getTexture(final String filename, final boolean isTransparent, final ReadOnlyColorRGBA defaultColor, final boolean grayout) {
    Texture texture = TextureManager.load(filename, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessNoCompressedFormat, true);
    if (isTransparent) {
        final Color color = new Color(defaultColor.getRed(), defaultColor.getGreen(), defaultColor.getBlue());
        final Image image = texture.getImage();
        final ByteBuffer data = image.getData(0);
        byte alpha;
        int i;
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                i = (y * image.getWidth() + x) * 4;
                alpha = data.get(i + 3);
                if (alpha == 0) {
                    // when it is transparent, put the default color of the part
                    data.put(i, (byte) color.getRed());
                    data.put(i + 1, (byte) color.getGreen());
                    data.put(i + 2, (byte) color.getBlue());
                }
            }
        }
        texture.setImage(image);
    }
    if (grayout) {
        final Texture grayoutTexture = cachedGrayTextures.get(filename + ":grayout");
        if (grayoutTexture != null) {
            return grayoutTexture;
        }
        final Image image = texture.getImage();
        // make a copy
        final Image grayImage = new Image();
        grayImage.setDataFormat(image.getDataFormat());
        grayImage.setDataType(image.getDataType());
        grayImage.setWidth(image.getWidth());
        grayImage.setHeight(image.getHeight());
        grayImage.setMipMapByteSizes(image.getMipMapByteSizes());
        final ByteBuffer data = image.getData(0);
        final ByteBuffer grayData = ByteBuffer.allocate(data.capacity());
        byte alpha, red, green, blue, gray;
        int i;
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                i = (y * image.getWidth() + x) * 4;
                red = data.get(i);
                green = data.get(i + 1);
                blue = data.get(i + 2);
                alpha = data.get(i + 3);
                gray = (byte) Math.min(red, green);
                gray = (byte) Math.min(blue, gray);
                grayData.put(i, gray);
                grayData.put(i + 1, gray);
                grayData.put(i + 2, gray);
                grayData.put(i + 3, alpha);
            }
        }
        grayImage.addData(grayData);
        texture = TextureManager.loadFromImage(grayImage, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessNoCompressedFormat);
        cachedGrayTextures.put(filename + ":grayout", texture);
    }
    return texture;
}
Also used : Color(java.awt.Color) Image(com.ardor3d.image.Image) Texture(com.ardor3d.image.Texture) ByteBuffer(java.nio.ByteBuffer) CullHint(com.ardor3d.scenegraph.hint.CullHint)

Aggregations

Image (com.ardor3d.image.Image)2 CullHint (com.ardor3d.scenegraph.hint.CullHint)2 ByteBuffer (java.nio.ByteBuffer)2 Texture (com.ardor3d.image.Texture)1 Texture2D (com.ardor3d.image.Texture2D)1 ColorRGBA (com.ardor3d.math.ColorRGBA)1 ReadOnlyColorRGBA (com.ardor3d.math.type.ReadOnlyColorRGBA)1 TextureState (com.ardor3d.renderer.state.TextureState)1 Color (java.awt.Color)1 Point (org.poly2tri.geometry.primitives.Point)1 TPoint (org.poly2tri.triangulation.point.TPoint)1