Search in sources :

Example 46 with Image

use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.

the class TextureUtil method uploadSubTexture.

/**
     * Update the texture currently bound to target at with data from the given Image at position x and y. The parameter
     * index is used as the zoffset in case a 3d texture or texture 2d array is being updated.
     *
     * @param image Image with the source data (this data will be put into the texture)
     * @param target the target texture
     * @param index the mipmap level to update
     * @param x the x position where to put the image in the texture
     * @param y the y position where to put the image in the texture
     */
public static void uploadSubTexture(Image image, int target, int index, int x, int y, boolean linearizeSrgb) {
    GL gl = GLContext.getCurrentGL();
    Image.Format fmt = image.getFormat();
    GLImageFormat glFmt = getImageFormatWithError(fmt, image.getColorSpace() == ColorSpace.sRGB && linearizeSrgb);
    ByteBuffer data = null;
    if (index >= 0 && image.getData() != null && image.getData().size() > 0) {
        data = image.getData(index);
    }
    int width = image.getWidth();
    int height = image.getHeight();
    int depth = image.getDepth();
    if (data != null) {
        gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
    }
    int[] mipSizes = image.getMipMapSizes();
    int pos = 0;
    // TODO: Remove unneccessary allocation
    if (mipSizes == null) {
        if (data != null) {
            mipSizes = new int[] { data.capacity() };
        } else {
            mipSizes = new int[] { width * height * fmt.getBitsPerPixel() / 8 };
        }
    }
    int samples = image.getMultiSamples();
    for (int i = 0; i < mipSizes.length; i++) {
        int mipWidth = Math.max(1, width >> i);
        int mipHeight = Math.max(1, height >> i);
        int mipDepth = Math.max(1, depth >> i);
        if (data != null) {
            data.position(pos);
            data.limit(pos + mipSizes[i]);
        }
        // to remove the cumbersome if/then/else stuff below we'll update the pos right here and use continue after each
        // gl*Image call in an attempt to unclutter things a bit
        pos += mipSizes[i];
        int glFmtInternal = glFmt.internalFormat;
        int glFmtFormat = glFmt.format;
        int glFmtDataType = glFmt.dataType;
        if (glFmt.compressed && data != null) {
            if (target == GL2ES2.GL_TEXTURE_3D) {
                gl.getGL2ES2().glCompressedTexSubImage3D(target, i, x, y, index, mipWidth, mipHeight, mipDepth, glFmtInternal, data.limit(), data);
                continue;
            }
            // all other targets use 2D: array, cubemap, 2d
            gl.getGL2ES2().glCompressedTexSubImage2D(target, i, x, y, mipWidth, mipHeight, glFmtInternal, data.limit(), data);
            continue;
        }
        if (target == GL2ES2.GL_TEXTURE_3D) {
            gl.getGL2ES2().glTexSubImage3D(target, i, x, y, index, mipWidth, mipHeight, mipDepth, glFmtFormat, glFmtDataType, data);
            continue;
        }
        if (samples > 1) {
            throw new IllegalStateException("Cannot update multisample textures");
        }
        gl.glTexSubImage2D(target, i, x, y, mipWidth, mipHeight, glFmtFormat, glFmtDataType, data);
        continue;
    }
}
Also used : GL(com.jogamp.opengl.GL) Format(com.jme3.texture.Image.Format) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Example 47 with Image

use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.

the class MipMapGenerator method scaleImage.

public static Image scaleImage(Image inputImage, int outputWidth, int outputHeight) {
    int size = outputWidth * outputHeight * inputImage.getFormat().getBitsPerPixel() / 8;
    ByteBuffer buffer = BufferUtils.createByteBuffer(size);
    Image outputImage = new Image(inputImage.getFormat(), outputWidth, outputHeight, buffer, inputImage.getColorSpace());
    ImageRaster input = ImageRaster.create(inputImage, 0, 0, false);
    ImageRaster output = ImageRaster.create(outputImage, 0, 0, false);
    float xRatio = ((float) (input.getWidth() - 1)) / output.getWidth();
    float yRatio = ((float) (input.getHeight() - 1)) / output.getHeight();
    ColorRGBA outputColor = new ColorRGBA();
    ColorRGBA bottomLeft = new ColorRGBA();
    ColorRGBA bottomRight = new ColorRGBA();
    ColorRGBA topLeft = new ColorRGBA();
    ColorRGBA topRight = new ColorRGBA();
    for (int y = 0; y < outputHeight; y++) {
        for (int x = 0; x < outputWidth; x++) {
            float x2f = x * xRatio;
            float y2f = y * yRatio;
            int x2 = (int) x2f;
            int y2 = (int) y2f;
            float xDiff = x2f - x2;
            float yDiff = y2f - y2;
            input.getPixel(x2, y2, bottomLeft);
            input.getPixel(x2 + 1, y2, bottomRight);
            input.getPixel(x2, y2 + 1, topLeft);
            input.getPixel(x2 + 1, y2 + 1, topRight);
            bottomLeft.multLocal((1f - xDiff) * (1f - yDiff));
            bottomRight.multLocal((xDiff) * (1f - yDiff));
            topLeft.multLocal((1f - xDiff) * (yDiff));
            topRight.multLocal((xDiff) * (yDiff));
            outputColor.set(bottomLeft).addLocal(bottomRight).addLocal(topLeft).addLocal(topRight);
            output.setPixel(x, y, outputColor);
        }
    }
    return outputImage;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) ImageRaster(com.jme3.texture.image.ImageRaster) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Example 48 with Image

use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.

the class MipMapGenerator method generateMipMaps.

public static void generateMipMaps(Image image) {
    int width = image.getWidth();
    int height = image.getHeight();
    Image current = image;
    ArrayList<ByteBuffer> output = new ArrayList<ByteBuffer>();
    int totalSize = 0;
    while (height >= 1 || width >= 1) {
        output.add(current.getData(0));
        totalSize += current.getData(0).capacity();
        if (height == 1 || width == 1) {
            break;
        }
        height /= 2;
        width /= 2;
        current = scaleImage(current, width, height);
    }
    ByteBuffer combinedData = BufferUtils.createByteBuffer(totalSize);
    int[] mipSizes = new int[output.size()];
    for (int i = 0; i < output.size(); i++) {
        ByteBuffer data = output.get(i);
        data.clear();
        combinedData.put(data);
        mipSizes[i] = data.capacity();
    }
    combinedData.flip();
    // insert mip data into image
    image.setData(0, combinedData);
    image.setMipMapSizes(mipSizes);
}
Also used : ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Example 49 with Image

use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.

the class PlaceholderAssets method getPlaceholderImage.

@Deprecated
public static Image getPlaceholderImage() {
    ByteBuffer tempData = BufferUtils.createByteBuffer(3 * 4 * 4);
    tempData.put(imageData).flip();
    return new Image(Format.RGB8, 4, 4, tempData, null, ColorSpace.Linear);
}
Also used : Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Example 50 with Image

use of com.jme3.texture.Image in project jmonkeyengine by jMonkeyEngine.

the class SkyFactory method checkImagesForCubeMap.

private static void checkImagesForCubeMap(Image... images) {
    if (images.length == 1) {
        return;
    }
    Format fmt = images[0].getFormat();
    int width = images[0].getWidth();
    int height = images[0].getHeight();
    ByteBuffer data = images[0].getData(0);
    int size = data != null ? data.capacity() : 0;
    checkImage(images[0]);
    for (int i = 1; i < images.length; i++) {
        Image image = images[i];
        checkImage(images[i]);
        if (image.getFormat() != fmt) {
            throw new IllegalArgumentException("Images must have same format");
        }
        if (image.getWidth() != width || image.getHeight() != height) {
            throw new IllegalArgumentException("Images must have same resolution");
        }
        ByteBuffer data2 = image.getData(0);
        if (data2 != null) {
            if (data2.capacity() != size) {
                throw new IllegalArgumentException("Images must have same size");
            }
        }
    }
}
Also used : Format(com.jme3.texture.Image.Format) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Image (com.jme3.texture.Image)68 ByteBuffer (java.nio.ByteBuffer)38 Texture (com.jme3.texture.Texture)27 Texture2D (com.jme3.texture.Texture2D)19 ArrayList (java.util.ArrayList)19 Material (com.jme3.material.Material)18 TextureKey (com.jme3.asset.TextureKey)17 Vector3f (com.jme3.math.Vector3f)17 Format (com.jme3.texture.Image.Format)15 TextureCubeMap (com.jme3.texture.TextureCubeMap)14 ColorRGBA (com.jme3.math.ColorRGBA)13 PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)12 BufferedImage (java.awt.image.BufferedImage)12 Geometry (com.jme3.scene.Geometry)10 InputStream (java.io.InputStream)10 IOException (java.io.IOException)8 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)7 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)7 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)7 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)7