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

📄 texturemanager.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme.util;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.stream.FileCacheImageInputStream;

import com.jme.image.BitmapHeader;
import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.image.Texture2D;
import com.jme.image.TextureCubeMap;
import com.jme.image.util.DDSLoader;
import com.jme.image.util.TGALoader;
import com.jme.renderer.Renderer;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.geom.BufferUtils;
import com.jme.util.resource.ResourceLocatorTool;

/**
 * <code>TextureManager</code> provides static methods for building a
 * <code>Texture</code> object. Typically, the information supplied is the
 * filename and the texture properties.
 * 
 * @author Mark Powell
 * @author Joshua Slack -- cache code and enhancements
 * @version $Id: TextureManager.java,v 1.83 2007/10/27 19:45:18 renanse Exp $
 */
final public class TextureManager {
    private static final Logger logger = Logger.getLogger(TextureManager.class
            .getName());

    private static HashMap<TextureKey, Texture> m_tCache = new HashMap<TextureKey, Texture>();
    private static HashMap<String, ImageLoader> loaders = new HashMap<String, ImageLoader>();
    private static ArrayList<Integer> cleanupStore = new ArrayList<Integer>();

    public static boolean COMPRESS_BY_DEFAULT = true;

    private static Texture.MagnificationFilter DEFAULT_MAG_FILTER = Texture.MagnificationFilter.Bilinear;

    private static Texture.MinificationFilter DEFAULT_MIN_FILTER = Texture.MinificationFilter.BilinearNoMipMaps;

    private static float DEFAULT_ANISO_LEVEL = 0.0f;
    
    private static boolean createOnHeap = false;

    private TextureManager() {
    }

    /**
     * <code>loadTexture</code> loads a new texture defined by the parameter
     * string. Filter parameters are used to define the filtering of the
     * texture. If there is an error loading the file, null is returned.
     * 
     * @param file
     *            the filename of the texture image.
     * @param minFilter
     *            the filter for the near values.
     * @param magFilter
     *            the filter for the far values.
     * @return the loaded texture. If there is a problem loading the texture,
     *         null is returned.
     */
    public static com.jme.image.Texture loadTexture(String file,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter) {
        return loadTexture(file, minFilter, magFilter, DEFAULT_ANISO_LEVEL,
                true);
    }

    /**
     * <code>loadTexture</code> loads a new texture defined by the parameter
     * string. Filter parameters are used to define the filtering of the
     * texture. If there is an error loading the file, null is returned.
     * 
     * @param file
     *            the filename of the texture image.
     * @param minFilter
     *            the filter for the near values.
     * @param magFilter
     *            the filter for the far values.
     * @param anisoLevel
     *            the aniso level for this texture
     * @param flipped
     *            If true, the images Y values are flipped.
     * @return the loaded texture. If there is a problem loading the texture,
     *         null is returned.
     */
    public static com.jme.image.Texture loadTexture(String file,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter, float anisoLevel,
            boolean flipped) {
        return loadTexture(file, minFilter, magFilter,
                (COMPRESS_BY_DEFAULT ? Image.Format.Guess
                        : Image.Format.GuessNoCompression), anisoLevel, flipped);
    }

    /**
     * <code>loadTexture</code> loads a new texture defined by the parameter
     * string. Filter parameters are used to define the filtering of the
     * texture. If there is an error loading the file, null is returned.
     * 
     * @param file
     *            the filename of the texture image.
     * @param minFilter
     *            the filter for the near values.
     * @param magFilter
     *            the filter for the far values.
     * @param imageType
     *            the type to use for image data
     * @param anisoLevel
     *            the aniso level for this texture
     * @param flipped
     *            If true, the images Y values are flipped.
     * @return the loaded texture. If there is a problem loading the texture,
     *         null is returned.
     */
    public static com.jme.image.Texture loadTexture(String file,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter, Image.Format imageType,
            float anisoLevel, boolean flipped) {
        URL url = getTextureURL(file);
        return loadTexture(url, minFilter, magFilter, imageType, anisoLevel,
                flipped);
    }

    /**
     * Convert the provided String file name into a Texture URL, first
     * attempting to use the {@link ResourceLocatorTool}, then trying to load
     * it as a direct file path.
     * 
     * @param file
     *            the file name
     * @return a URL
     */
    private static URL getTextureURL(String file) {
        URL url = ResourceLocatorTool.locateResource(
                ResourceLocatorTool.TYPE_TEXTURE, file);
        if (url == null) {
            try {
                url = new URL("file:" + file);
            } catch (MalformedURLException e) {
                logger.logp(Level.SEVERE, TextureManager.class.toString(),
                        "getTextureURL(file)", "Exception", e);
            }
        }
        return url;
    }

    /**
     * <code>loadTexture</code> loads a new texture defined by the parameter
     * url. If there is an error loading the file, null is returned.
     * 
     * @param file
     *            the url of the texture image.
     * @param flipped
     *            If true, the images Y values are flipped.
     * @return the loaded texture. If there is a problem loading the texture,
     *         null is returned.
     */
    public static com.jme.image.Texture loadTexture(URL file) {
        return loadTexture(file, true);
    }

    /**
     * <code>loadTexture</code> loads a new texture defined by the parameter
     * url. If there is an error loading the file, null is returned.
     * 
     * @param file
     *            the url of the texture image.
     * @param flipped
     *            If true, the images Y values are flipped.
     * @return the loaded texture. If there is a problem loading the texture,
     *         null is returned.
     */
    public static com.jme.image.Texture loadTexture(URL file, boolean flipped) {
        return loadTexture(file, DEFAULT_MIN_FILTER, DEFAULT_MAG_FILTER,
                (COMPRESS_BY_DEFAULT ? Image.Format.Guess
                        : Image.Format.GuessNoCompression),
                DEFAULT_ANISO_LEVEL, flipped);
    }

    /**
     * <code>loadTexture</code> loads a new texture defined by the parameter
     * url. Filter parameters are used to define the filtering of the texture.
     * If there is an error loading the file, null is returned.
     * 
     * @param file
     *            the url of the texture image.
     * @param minFilter
     *            the filter for the near values.
     * @param magFilter
     *            the filter for the far values.
     * @return the loaded texture. If there is a problem loading the texture,
     *         null is returned.
     */
    public static com.jme.image.Texture loadTexture(URL file,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter) {
        return loadTexture(file, minFilter, magFilter,
                (COMPRESS_BY_DEFAULT ? Image.Format.Guess
                        : Image.Format.GuessNoCompression),
                DEFAULT_ANISO_LEVEL, true);
    }

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

    /**
     * <code>loadTexture</code> loads a new texture defined by the parameter
     * url. Filter parameters are used to define the filtering of the texture.
     * If there is an error loading the file, null is returned.
     * 
     * @param file
     *            the url of the texture image.
     * @param minFilter
     *            the filter for the near values.
     * @param magFilter
     *            the filter for the far values.
     * @param imageType
     *            the image type to use. if Image.Format.Guess, the type is
     *            determined by jME. If S3TC/DXT is available we use that. if
     *            Image.Format.GuessNoCompression, the type is determined by jME
     *            without using S3TC, even if available. See
     *            com.jme.image.Image.Format for possible types.
     * @param flipped
     *            If true, the images Y values are flipped.
     * @return the loaded texture. If there is a problem loading the texture,
     *         null is returned.
     * @see Image.Format
     */
    public static com.jme.image.Texture loadTexture(URL file,
            Texture.MinificationFilter minFilter,
            Texture.MagnificationFilter magFilter, Image.Format imageType,
            float anisoLevel, boolean flipped) {

⌨️ 快捷键说明

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