Search in sources :

Example 66 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 67 with Image

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

the class AndroidBufferImageLoader method load.

public Object load(AssetInfo assetInfo) throws IOException {
    Bitmap bitmap = null;
    Image.Format format;
    InputStream in = null;
    int bpp;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferQualityOverSpeed = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inTempStorage = tempData;
    options.inScaled = false;
    options.inDither = false;
    options.inInputShareable = true;
    options.inPurgeable = true;
    options.inSampleSize = 1;
    try {
        in = assetInfo.openStream();
        bitmap = BitmapFactory.decodeStream(in, null, options);
        if (bitmap == null) {
            throw new IOException("Failed to load image: " + assetInfo.getKey().getName());
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    switch(bitmap.getConfig()) {
        case ALPHA_8:
            format = Image.Format.Alpha8;
            bpp = 1;
            break;
        case ARGB_8888:
            format = Image.Format.RGBA8;
            bpp = 4;
            break;
        case RGB_565:
            format = Image.Format.RGB565;
            bpp = 2;
            break;
        default:
            throw new UnsupportedOperationException("Unrecognized Android bitmap format: " + bitmap.getConfig());
    }
    TextureKey texKey = (TextureKey) assetInfo.getKey();
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    ByteBuffer data = BufferUtils.createByteBuffer(bitmap.getWidth() * bitmap.getHeight() * bpp);
    if (format == Image.Format.RGBA8) {
        int[] pixelData = new int[width * height];
        bitmap.getPixels(pixelData, 0, width, 0, 0, width, height);
        if (texKey.isFlipY()) {
            int[] sln = new int[width];
            int y2;
            for (int y1 = 0; y1 < height / 2; y1++) {
                y2 = height - y1 - 1;
                convertARGBtoABGR(pixelData, y1 * width, sln, 0, width);
                convertARGBtoABGR(pixelData, y2 * width, pixelData, y1 * width, width);
                System.arraycopy(sln, 0, pixelData, y2 * width, width);
            }
        } else {
            convertARGBtoABGR(pixelData, 0, pixelData, 0, pixelData.length);
        }
        data.asIntBuffer().put(pixelData);
    } else {
        if (texKey.isFlipY()) {
            // Flip the image, then delete the old one.
            Matrix flipMat = new Matrix();
            flipMat.preScale(1.0f, -1.0f);
            Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipMat, false);
            bitmap.recycle();
            bitmap = newBitmap;
            if (bitmap == null) {
                throw new IOException("Failed to flip image: " + texKey);
            }
        }
        bitmap.copyPixelsToBuffer(data);
    }
    data.flip();
    bitmap.recycle();
    Image image = new Image(format, width, height, data, ColorSpace.sRGB);
    return image;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) Image(com.jme3.texture.Image) ByteBuffer(java.nio.ByteBuffer) TextureKey(com.jme3.asset.TextureKey) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) BitmapFactory(android.graphics.BitmapFactory)

Example 68 with Image

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

the class AndroidNativeImageLoader method load.

public Image load(AssetInfo info) throws IOException {
    boolean flip = ((TextureKey) info.getKey()).isFlipY();
    InputStream in = null;
    try {
        in = info.openStream();
        return load(info.openStream(), flip, tmpArray);
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : TextureKey(com.jme3.asset.TextureKey) InputStream(java.io.InputStream)

Example 69 with Image

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

the class GLRenderer method updateRenderTexture.

public void updateRenderTexture(FrameBuffer fb, RenderBuffer rb) {
    Texture tex = rb.getTexture();
    Image image = tex.getImage();
    if (image.isUpdateNeeded()) {
        // Check NPOT requirements
        checkNonPowerOfTwo(tex);
        updateTexImageData(image, tex.getType(), 0, false);
        // NOTE: For depth textures, sets nearest/no-mips mode
        // Required to fix "framebuffer unsupported"
        // for old NVIDIA drivers!
        setupTextureParams(0, tex);
    }
    if (rb.getLayer() < 0) {
        glfbo.glFramebufferTexture2DEXT(GLFbo.GL_FRAMEBUFFER_EXT, convertAttachmentSlot(rb.getSlot()), convertTextureType(tex.getType(), image.getMultiSamples(), rb.getFace()), image.getId(), 0);
    } else {
        gl3.glFramebufferTextureLayer(GLFbo.GL_FRAMEBUFFER_EXT, convertAttachmentSlot(rb.getSlot()), image.getId(), 0, rb.getLayer());
    }
}
Also used : Image(com.jme3.texture.Image) Texture(com.jme3.texture.Texture)

Example 70 with Image

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

the class Material method getSortId.

/**
     * Returns the sorting ID or sorting index for this material.
     *
     * <p>The sorting ID is used internally by the system to sort rendering
     * of geometries. It sorted to reduce shader switches, if the shaders
     * are equal, then it is sorted by textures.
     *
     * @return The sorting ID used for sorting geometries for rendering.
     */
public int getSortId() {
    if (sortingId == -1 && technique != null) {
        sortingId = technique.getSortId() << 16;
        int texturesSortId = 17;
        for (int i = 0; i < paramValues.size(); i++) {
            MatParam param = paramValues.getValue(i);
            if (!param.getVarType().isTextureType()) {
                continue;
            }
            Texture texture = (Texture) param.getValue();
            if (texture == null) {
                continue;
            }
            Image image = texture.getImage();
            if (image == null) {
                continue;
            }
            int textureId = image.getId();
            if (textureId == -1) {
                textureId = 0;
            }
            texturesSortId = texturesSortId * 23 + textureId;
        }
        sortingId |= texturesSortId & 0xFFFF;
    }
    return sortingId;
}
Also used : Image(com.jme3.texture.Image) Texture(com.jme3.texture.Texture)

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