Java Code Examples for com.jme3.texture.Image#hasMipmaps()

The following examples show how to use com.jme3.texture.Image#hasMipmaps() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MipMapImageRaster.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MipMapImageRaster(Image image, int slice) {
    this.image = image;
    this.slice = slice;
    this.buffer = image.getData(slice);
    this.codec = ImageCodec.lookup(image.getFormat());
    if (image.hasMipmaps()) {
        int nbMipMap = image.getMipMapSizes().length;
        this.width = new int[nbMipMap];
        this.height = new int[nbMipMap];
        this.offsets = new int[nbMipMap];
        for (int i = 0; i < nbMipMap; i++) {
            width[i] = Math.max(1, image.getWidth() >> i);
            height[i] = Math.max(1, image.getHeight() >> i);
            if (i > 0) {
                offsets[i] = image.getMipMapSizes()[i - 1] + offsets[i - 1];
            }
        }

    } else {
        throw new IllegalArgumentException("Image must have MipMapSizes initialized.");
    }

    if (codec instanceof ByteAlignedImageCodec || codec instanceof ByteOffsetImageCodec) {
        this.temp = new byte[codec.bpp];
    } else {
        this.temp = null;
    }
}
 
Example 2
Source File: DefaultImageRaster.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public DefaultImageRaster(Image image, int slice, int mipMapLevel, boolean convertToLinear) {
    int[] mipMapSizes = image.getMipMapSizes();
    int availableMips = mipMapSizes != null ? mipMapSizes.length : 1;
    
    if (mipMapLevel >= availableMips) {
        throw new IllegalStateException("Cannot create image raster for mipmap level #" + mipMapLevel + ". "
                                      + "Image only has " + availableMips + " mipmap levels.");
    }
    
    if (image.hasMipmaps()) {
        this.width  = Math.max(1, image.getWidth()  >> mipMapLevel);
        this.height = Math.max(1, image.getHeight() >> mipMapLevel);
        
        int mipOffset = 0;
        for (int i = 0; i < mipMapLevel; i++) {
            mipOffset += mipMapSizes[i];
        }
        
        this.offset = mipOffset;
    } else {
        this.width = image.getWidth();
        this.height = image.getHeight();
        this.offset = 0;
    }
    
    this.image = image;
    this.slice = slice;
    
    // Conversion to linear only needed if image's color space is sRGB.
    this.convertToLinear = convertToLinear && image.getColorSpace() == ColorSpace.sRGB;
    
    this.buffer = image.getData(slice);
    this.codec = ImageCodec.lookup(image.getFormat());
    
    if (codec instanceof ByteAlignedImageCodec || codec instanceof ByteOffsetImageCodec) {
        this.temp = new byte[codec.bpp];
    } else {
        this.temp = null;
    }
}
 
Example 3
Source File: JoglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void updateTexImageData(Image image, Texture.Type type, boolean mips) {
    int texId = image.getId();
    if (texId == -1) {
        // create texture
        gl.glGenTextures(1, ib1);
        texId = ib1.get(0);
        image.setId(texId);
        objManager.registerForCleanup(image);

        statistics.onNewTexture();
    }

    // bind texture
    int target = convertTextureType(type);
    if (context.boundTextures[0] != image) {
        if (context.boundTextureUnit != 0) {
            gl.glActiveTexture(gl.GL_TEXTURE0);
            context.boundTextureUnit = 0;
        }

        gl.glBindTexture(target, texId);
        context.boundTextures[0] = image;
    }

    boolean generateMips = false;
    if (!image.hasMipmaps() && mips) {
        // No pregenerated mips available,
        // generate from base level if required
        if (hardwareMips) {
            gl.glTexParameteri(target, GL.GL_GENERATE_MIPMAP, gl.GL_TRUE);
        } else {
            generateMips = true;
        }
    }

    TextureUtil.uploadTexture(gl, image, 0, generateMips, powerOf2);


    image.clearUpdateNeeded();
}
 
Example 4
Source File: GLRenderer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("fallthrough")
private void setupTextureParams(int unit, Texture tex) {
    Image image = tex.getImage();
    int samples = image != null ? image.getMultiSamples() : 1;
    int target = convertTextureType(tex.getType(), samples, -1);

    if (samples > 1) {
        bindTextureOnly(target, image, unit);
        return;
    }

    boolean haveMips = true;
    if (image != null) {
        haveMips = image.isGeneratedMipmapsRequired() || image.hasMipmaps();
    }
    
    LastTextureState curState = image.getLastTextureState();

    if (curState.magFilter != tex.getMagFilter()) {
        bindTextureAndUnit(target, image, unit);
        gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, convertMagFilter(tex.getMagFilter()));
        curState.magFilter = tex.getMagFilter();
    }
    if (curState.minFilter != tex.getMinFilter()) {
        bindTextureAndUnit(target, image, unit);
        gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, convertMinFilter(tex.getMinFilter(), haveMips));
        curState.minFilter = tex.getMinFilter();
    }

    int desiredAnisoFilter = tex.getAnisotropicFilter() == 0
            ? defaultAnisotropicFilter
            : tex.getAnisotropicFilter();

    if (caps.contains(Caps.TextureFilterAnisotropic)
            && curState.anisoFilter != desiredAnisoFilter) {
        bindTextureAndUnit(target, image, unit);
        gl.glTexParameterf(target,
                GLExt.GL_TEXTURE_MAX_ANISOTROPY_EXT,
                desiredAnisoFilter);
        curState.anisoFilter = desiredAnisoFilter;
    }

    switch (tex.getType()) {
        case ThreeDimensional:
        case CubeMap: // cubemaps use 3D coords
            if (gl2 != null && (caps.contains(Caps.OpenGL20) || caps.contains(Caps.OpenGLES30)) && curState.rWrap != tex.getWrap(WrapAxis.R)) {
                bindTextureAndUnit(target, image, unit);
                gl.glTexParameteri(target, GL2.GL_TEXTURE_WRAP_R, convertWrapMode(tex.getWrap(WrapAxis.R)));
                curState.rWrap = tex.getWrap(WrapAxis.R);
            }
            //There is no break statement on purpose here
        case TwoDimensional:
        case TwoDimensionalArray:
            if (curState.tWrap != tex.getWrap(WrapAxis.T)) {
                bindTextureAndUnit(target, image, unit);
                gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_T, convertWrapMode(tex.getWrap(WrapAxis.T)));
                image.getLastTextureState().tWrap = tex.getWrap(WrapAxis.T);
            }
            if (curState.sWrap != tex.getWrap(WrapAxis.S)) {
                bindTextureAndUnit(target, image, unit);
                gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_S, convertWrapMode(tex.getWrap(WrapAxis.S)));
                curState.sWrap = tex.getWrap(WrapAxis.S);
            }
            break;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + tex.getType());
    }

    ShadowCompareMode texCompareMode = tex.getShadowCompareMode();
    if ( (gl2 != null || caps.contains(Caps.OpenGLES30)) && curState.shadowCompareMode != texCompareMode) {
        bindTextureAndUnit(target, image, unit);
        if (texCompareMode != ShadowCompareMode.Off) {
            gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_REF_TO_TEXTURE);
            if (texCompareMode == ShadowCompareMode.GreaterOrEqual) {
                gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_GEQUAL);
            } else {
                gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_LEQUAL);
            }
        } else {
            gl.glTexParameteri(target, GL2.GL_TEXTURE_COMPARE_MODE, GL.GL_NONE);
        }
        curState.shadowCompareMode = texCompareMode;
    }
    
    // If at this point we didn't bind the texture, bind it now
    bindTextureOnly(target, image, unit);
}
 
Example 5
Source File: GdxRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
     * <code>updateTexImageData</code> activates and binds the texture
     * @param img
     * @param type
     * @param mips
     */
    public void updateTexImageData(Image img, Texture.Type type, boolean mips) {
        int texId = img.getId();
        if (texId == -1) {
            // create texture
            if (verboseLogging) {
                logger.info("GLES20.glGenTexture(1, buffer)");
            }

            Gdx.gl20.glGenTextures(1, intBuf1);
            texId = intBuf1.get(0);
            img.setId(texId);
            objManager.registerForCleanup(img);

            statistics.onNewTexture();
        }

        // bind texture
        int target = convertTextureType(type);
        if (context.boundTextureUnit != 0) {
            if (verboseLogging) {
                logger.info("GLES20.glActiveTexture(GLES20.GL_TEXTURE0)");
            }

            Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
            context.boundTextureUnit = 0;
        }
        if (context.boundTextures[0] != img) {

            if (verboseLogging) {
                logger.info("GLES20.glBindTexture(" + target + ", " + texId + ")");
            }

            Gdx.gl20.glBindTexture(target, texId);
            context.boundTextures[0] = img;
        }


        if (target == GL20.GL_TEXTURE_CUBE_MAP) {
            // Upload a cube map / sky box
            // TODO
//            @SuppressWarnings("unchecked")
//            List<Bitmap> bmps = (List<Bitmap>) img.getEfficentData();
//            if (bmps != null) {
//                // Native android bitmap
//                if (bmps.size() != 6) {
//                    throw new UnsupportedOperationException("Invalid texture: " + img
//                            + "Cubemap textures must contain 6 data units.");
//                }
//                for (int i = 0; i < 6; i++) {
//                    TextureUtilGdx.uploadTextureBitmap(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, bmps.get(i), false, powerOf2);
//                }
//            } else {
//                // Standard jme3 image data
//                List<ByteBuffer> data = img.getData();
//                if (data.size() != 6) {
//                    logger.log(Level.WARNING, "Invalid texture: {0}\n"
//                            + "Cubemap textures must contain 6 data units.", img);
//                    return;
//                }
//                for (int i = 0; i < 6; i++) {
//                    TextureUtilGdx.uploadTexture(img, GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, 0, tdc, false, powerOf2);
//                }
//            }
        } else {
            // TODO
            TextureUtilGdx.uploadTexture(img, target, 0, 0, tdc, false, powerOf2);

            if (verboseLogging) {
                logger.info("GLES20.glTexParameteri(" + target + "GLES11.GL_GENERATE_MIMAP, GLES20.GL_TRUE)");
            }

            if (!img.hasMipmaps() && mips) {
                // No pregenerated mips available,
                // generate from base level if required
                if (verboseLogging) {
                    logger.info("GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D)");
                }
                Gdx.gl20.glGenerateMipmap(GL20.GL_TEXTURE_2D);
            }
        }

        img.clearUpdateNeeded();
    }
 
Example 6
Source File: OGLESShaderRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * <code>updateTexImageData</code> activates and binds the texture
 * @param img
 * @param type
 * @param mips
 */
public void updateTexImageData(Image img, Texture.Type type, boolean mips) {
    int texId = img.getId();
    if (texId == -1) {
        // create texture
        if (verboseLogging) {
            logger.info("GLES20.glGenTexture(1, buffer)");
        }

        GLES20.glGenTextures(1, intBuf1);
        texId = intBuf1.get(0);
        img.setId(texId);
        objManager.registerForCleanup(img);

        statistics.onNewTexture();
    }

    // bind texture
    int target = convertTextureType(type);
    if (context.boundTextureUnit != 0) {
        if (verboseLogging) {
            logger.info("GLES20.glActiveTexture(GLES20.GL_TEXTURE0)");
        }

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        context.boundTextureUnit = 0;
    }
    if (context.boundTextures[0] != img) {

        if (verboseLogging) {
            logger.info("GLES20.glBindTexture(" + target + ", " + texId + ")");
        }

        GLES20.glBindTexture(target, texId);
        context.boundTextures[0] = img;
    }


    if (target == GLES20.GL_TEXTURE_CUBE_MAP) {
        // Upload a cube map / sky box
        @SuppressWarnings("unchecked")
        List<Bitmap> bmps = (List<Bitmap>) img.getEfficentData();
        if (bmps != null) {
            // Native android bitmap                                       
            if (bmps.size() != 6) {
                throw new UnsupportedOperationException("Invalid texture: " + img
                        + "Cubemap textures must contain 6 data units.");
            }
            for (int i = 0; i < 6; i++) {
                TextureUtil.uploadTextureBitmap(GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, bmps.get(i), false, powerOf2);
            }
        } else {
            // Standard jme3 image data
            List<ByteBuffer> data = img.getData();
            if (data.size() != 6) {
                logger.log(Level.WARNING, "Invalid texture: {0}\n"
                        + "Cubemap textures must contain 6 data units.", img);
                return;
            }
            for (int i = 0; i < 6; i++) {
                TextureUtil.uploadTexture(img, GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, 0, tdc, false, powerOf2);
            }
        }
    } else {
        TextureUtil.uploadTexture(img, target, 0, 0, tdc, false, powerOf2);

        if (verboseLogging) {
            logger.info("GLES20.glTexParameteri(" + target + "GLES11.GL_GENERATE_MIMAP, GLES20.GL_TRUE)");
        }

        if (!img.hasMipmaps() && mips) {
            // No pregenerated mips available,
            // generate from base level if required
            if (verboseLogging) {
                logger.info("GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D)");
            }
            GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);
        }
    }

    img.clearUpdateNeeded();
}
 
Example 7
Source File: JoglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void updateTextureData(Texture tex) {
    int texId = tex.getId();
    GL gl = GLContext.getCurrentGL();
    if (texId == -1) {
        // create texture
        gl.glGenTextures(1, intBuf1);
        texId = intBuf1.get(0);
        tex.setId(texId);
        objManager.registerForCleanup(tex);
    }

    // bind texture
    int target = convertTextureType(tex.getType());
    if (context.boundTextures[0] != tex) {
        if (context.boundTextureUnit != 0) {
            setActiveTexture(GL.GL_TEXTURE0);
            context.boundTextureUnit = 0;
        }

        bindTexture(target, texId);
        context.boundTextures[0] = tex;
    }

    // filter things
    int minFilter = convertMinFilter(tex.getMinFilter());
    int magFilter = convertMagFilter(tex.getMagFilter());
    gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, minFilter);
    gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, magFilter);

    // repeat modes
    switch (tex.getType()) {
        case ThreeDimensional:
        case CubeMap:
            gl.glTexParameteri(target, GL2GL3.GL_TEXTURE_WRAP_R,
                    convertWrapMode(tex.getWrap(WrapAxis.R)));
        case TwoDimensional:
            gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_T,
                    convertWrapMode(tex.getWrap(WrapAxis.T)));
            // fall down here is intentional..
            // case OneDimensional:
            gl.glTexParameteri(target, GL.GL_TEXTURE_WRAP_S,
                    convertWrapMode(tex.getWrap(WrapAxis.S)));
            break;
        default:
            throw new UnsupportedOperationException("Unknown texture type: " + tex.getType());
    }

    Image img = tex.getImage();
    if (img != null) {
        boolean generateMips = false;
        if (!img.hasMipmaps() && tex.getMinFilter().usesMipMapLevels()) {
            // No pregenerated mips available,
            // generate from base level if required
            if (hardwareMips) {
                gl.glTexParameteri(target, GL2ES1.GL_GENERATE_MIPMAP, GL.GL_TRUE);
            }
            else {
                generateMips = true;
            }
        }

        TextureUtil.uploadTexture(gl, img, tex.getImageDataIndex(), generateMips, powerOf2);
    }

    tex.clearUpdateNeeded();
}