⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 texturemanager.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

        if (null == file) {
            logger.warning("Could not load image...  URL was null. defaultTexture used.");
            return TextureState.getDefaultTexture();
        }

        String fileName = file.getFile();
        if (fileName == null) {
            logger.warning("Could not load image...  fileName was null. defaultTexture used.");
            return TextureState.getDefaultTexture();
        }

        TextureKey tkey = new TextureKey(file, flipped, imageType);

        return loadTexture(null, tkey, null, minFilter, magFilter, anisoLevel);
    }

    public static com.jme.image.Texture loadTexture(TextureKey tkey) {
        return loadTexture(null, tkey);
    }

    public static com.jme.image.Texture loadTexture(Texture texture,
            TextureKey tkey) {
        return loadTexture(texture, tkey, null, DEFAULT_MIN_FILTER,
                DEFAULT_MAG_FILTER, DEFAULT_ANISO_LEVEL);
    }

    public static com.jme.image.Texture loadTexture(Texture texture,
            TextureKey tkey, com.jme.image.Image imageData,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter, float anisoLevel) {
        if (tkey == null) {
            logger.warning("TextureKey is null, cannot load");
            return TextureState.getDefaultTexture();
        }

        Texture cache = findCachedTexture(tkey);
        if (cache != null) {
            // look into cache.
            // Uncomment if you want to see when this occurs.
            // logging.info("******** REUSING TEXTURE ******** "+cache);
            if (texture == null) {
                Texture tClone = cache.createSimpleClone();
                if (tClone.getTextureKey() == null) {
                    tClone.setTextureKey(tkey);
                }
                return tClone;
            }
            cache.createSimpleClone(texture);
            return texture;
        }

        if (imageData == null)
            imageData = loadImage(tkey);

        if (null == imageData) {
            logger.warning("(image null) Could not load: "
                    + (tkey.getLocation() != null ? tkey.getLocation()
                            .getFile() : tkey.getFileType()));
            return TextureState.getDefaultTexture();
        }

        // Default to Texture2D
        if (texture == null) {
        	if (imageData.getData().size() == 6) {
        		texture = new TextureCubeMap();
        	} else {
        		texture = new Texture2D();
        	}
        }

        // Use a tex state only to determine if S3TC is available.
        TextureState state = null;
        if (DisplaySystem.getDisplaySystem() != null
                && DisplaySystem.getDisplaySystem().getRenderer() != null) {
            state = (TextureState) Renderer.defaultStateList[RenderState.StateType.Texture.ordinal()];
        }

        // we've already guessed the format. override if given.
        if (tkey.format != Image.Format.GuessNoCompression
                && tkey.format != Image.Format.Guess) {
            imageData.setFormat(tkey.format);
        } else if (tkey.format == Image.Format.Guess && state != null
                && state.isS3TCSupported()) {
            // Enable S3TC DXT1 compression if available and we're guessing
            // format.
            if (imageData.getFormat() == Image.Format.RGB8) {
                imageData.setFormat(Image.Format.RGB_TO_DXT1);
            } else if (imageData.getFormat() == Image.Format.RGBA8) {
                imageData.setFormat(Image.Format.RGBA_TO_DXT5);
            }
        }

        texture.setTextureKey(tkey);
        texture.setMagnificationFilter(magFilter);
        texture.setImage(imageData);
        texture.setAnisotropicFilterPercent(anisoLevel);
        texture.setMinificationFilter(minFilter);
        if (tkey.location != null) {
            texture.setImageLocation(tkey.location.toString());
        }

        addToCache(texture);
        return texture;
    }

    public static void addToCache(Texture t) {
        if (TextureState.getDefaultTexture() == null
                || (t != TextureState.getDefaultTexture() && t.getImage() != TextureState
                        .getDefaultTextureImage())) {
            m_tCache.put(t.getTextureKey(), t);
        }
    }

    public static com.jme.image.Texture loadTexture(java.awt.Image image,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter, boolean flipped) {
        return loadTexture(image, minFilter, magFilter, DEFAULT_ANISO_LEVEL,
                (COMPRESS_BY_DEFAULT ? Image.Format.Guess
                        : Image.Format.GuessNoCompression), flipped);
    }

    public static com.jme.image.Texture loadTexture(java.awt.Image image,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter, float anisoLevel,
            boolean flipped) {
        return loadTexture(image, minFilter, magFilter, anisoLevel,
                (COMPRESS_BY_DEFAULT ? Image.Format.Guess
                        : Image.Format.GuessNoCompression), flipped);
    }

    public static com.jme.image.Texture loadTexture(java.awt.Image image,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter, float anisoLevel,
            Image.Format imageFormat, boolean flipped) {
        com.jme.image.Image imageData = loadImage(image, flipped);

        TextureKey tkey = new TextureKey(null, flipped, imageFormat);
        if (image != null)
            tkey.setFileType("" + image.hashCode());
        return loadTexture(null, tkey, imageData, minFilter, magFilter,
                anisoLevel);
    }

    public static com.jme.image.Image loadImage(TextureKey key) {
        if (key == null) {
            return null;
        }

        if ("savable".equalsIgnoreCase(key.fileType)) {
            Savable s;
            try {
                s = BinaryImporter.getInstance().load(key.location);
            } catch (IOException e) {
                logger.log(Level.WARNING, "Could not load Savable.", e);
                return null;
            }
            if (s instanceof com.jme.image.Image) {
                return (Image) s;
            }
            logger.warning("Savable not of type Image.");
            return TextureState.getDefaultTextureImage();
        }
        return loadImage(key.location, key.flipped);
    }

    public static com.jme.image.Image loadImage(URL file, boolean flipped) {
        if (file == null) {
            logger
                    .warning("loadImage(URL file, boolean flipped): file is null, defaultTexture used.");
            return TextureState.getDefaultTextureImage();
        }

        String fileName = file.getFile();
        if (fileName == null) {
            logger
                    .warning("loadImage(URL file, boolean flipped): fileName is null, defaultTexture used.");
            return TextureState.getDefaultTextureImage();
        }

        int dot = fileName.lastIndexOf('.');
        String fileExt = dot >= 0 ? fileName.substring(dot) : "";
        InputStream is = null;
        try {
            is = file.openStream();
            return loadImage(fileExt, is, flipped);
        } catch (IOException e) {
            logger
                    .log(
                            Level.WARNING,
                            "loadImage(URL file, boolean flipped): defaultTexture used",
                            e);
            return TextureState.getDefaultTextureImage();
        } finally {
            if (is != null) {
                try {
                    is.close();
                }
                catch (IOException ioe) { } // ignore
            }
        }
    }

    public static com.jme.image.Image loadImage(String fileName, boolean flipped) {
        return loadImage(getTextureURL(fileName), flipped);
    }

    public static com.jme.image.Image loadImage(String fileExt,
            InputStream stream, boolean flipped) {

        com.jme.image.Image imageData = null;
        try {
            ImageLoader loader = loaders.get(fileExt.toLowerCase());
            if (loader != null)
                imageData = loader.load(stream);
            else if (".TGA".equalsIgnoreCase(fileExt)) { // TGA, direct to
                // imageData
                imageData = TGALoader.loadImage(stream, flipped);
            } else if (".DDS".equalsIgnoreCase(fileExt)) { // DDS, direct to
                // imageData
                imageData = DDSLoader.loadImage(stream, flipped);
            } else if (".BMP".equalsIgnoreCase(fileExt)) { // BMP, awtImage to
                // imageData
                java.awt.Image image = loadBMPImage(stream);
                imageData = loadImage(image, flipped);
            } else { // Anything else
                java.awt.Image image = ImageIO.read(stream); // readImage(fileExt, stream);
                imageData = loadImage(image, flipped);
            }
            if (imageData == null) {
                logger
                        .warning("loadImage(String fileExt, InputStream stream, boolean flipped): no imageData found.  defaultTexture used.");
                imageData = TextureState.getDefaultTextureImage();
            }
        } catch (IOException e) {
            logger.log(Level.WARNING, "Could not load Image.", e);
            imageData = TextureState.getDefaultTextureImage();
        }
        return imageData;
    }

    /**
     * Load the image as either TYPE_3BYTE_BGR or TYPE_4BYTE_ABGR
     * 
     * @param fileExt
     * @param imageIn
     * @return
     * @throws java.io.IOException
     * TODO Does this need to be removed?
     */
    private static BufferedImage readImage(String fileExt, InputStream imageIn)
            throws IOException {
        BufferedImage image;
        ImageTypeSpecifier imageType;
        int width;
        int height;

        if (imageIn == null)
            throw new IOException("Null Stream");

        String format = fileExt.substring(1); // Remove .
        ImageReader reader = (ImageReader) ImageIO.getImageReadersByFormatName(
                format).next();

        try {
            // Not ideal as we are creating a cache file, but as we
            // are processing
            // a stream we don't have access to the local file info
            reader.setInput(new FileCacheImageInputStream(imageIn, null));
            imageType = reader.getRawImageType(0);
            if (imageType == null) {
                // Workaround for Mac issue getting image type of JPEG images.
                // Look through the list to find the first type with
                // a non-null ColorModel
                for (Iterator<ImageTypeSpecifier> i = reader.getImageTypes(0); i
                        .hasNext();) {
                    ImageTypeSpecifier temp = i.next();
                    if (temp != null && temp.getColorModel() != null) {
                        imageType = temp;
                        break;
                    }
                }

                // if there is still no image type, throw an
                // exception
                if (imageType == null) {
                    throw new IOException("Cannot get image type for "
                            + fileExt);
                }
            }
            width = reader.getWidth(0);
            height = reader.getHeight(0);
        } catch (IndexOutOfBoundsException ioob) {
            logger.warning("Corrupt image file ");
            // The image file is corrupt
            throw new IOException("Image read failure");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -