/**
         * Creates this instance's {@link Texture} if the image source is a <code>BufferedImage<code>.
         *
         * @param dc the current draw context.
         *
         * @return the newly created texture, or null if the texture was not created.
         *
         * @throws IllegalStateException if the image source is null or not a <code>BufferedImage</code>.
         */
        protected Texture makeBufferedImageTexture(DrawContext dc)
        {
            if (this.getImageSource() == null || !(this.getImageSource() is BufferedImage))
            {
                String message = Logging.getMessage("generic.NotABufferedImage");
                Logging.logger().severe(message);
                throw new IllegalStateException(message);
            }

            try
            {
                TextureData td = AWTTextureIO.newTextureData(Configuration.getMaxCompatibleGLProfile(),
                                                             (BufferedImage)this.getImageSource(), this.isUseMipMaps());
                if (td == null)
                {
                    return(null);
                }

                this.setTextureData(td);

                return(this.makeTextureFromTextureData(dc));
            }
            catch (Exception e)
            {
                String msg = Logging.getMessage("generic.IOExceptionDuringTextureInitialization");
                Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                this.textureInitializationFailed = true;
                return(null);
            }
        }
Exemple #2
0
        protected Texture initializeTexture(DrawContext dc, Object imageSource)
        {
            if (dc == null)
            {
                String message = Logging.getMessage("nullValue.DrawContextIsNull");
                Logging.logger().severe(message);
                throw new IllegalStateException(message);
            }

            if (this.textureInitializationFailed)
            {
                return(null);
            }

            Texture t;
            bool    haveMipMapData;
            GL      gl = dc.getGL();

            if (imageSource is String)
            {
                String path = (String)imageSource;

                Object streamOrException = WWIO.getFileOrResourceAsStream(path, this.GetType());
                if (streamOrException == null || streamOrException is Exception)
                {
                    Logging.logger().log(java.util.logging.Level.SEVERE, "generic.ExceptionAttemptingToReadImageFile",
                                         streamOrException != null ? streamOrException : path);
                    this.textureInitializationFailed = true;
                    return(null);
                }

                try
                {
                    TextureData td = OGLUtil.newTextureData(gl.getGLProfile(), (InputStream)streamOrException,
                                                            this.useMipMaps);
                    t = TextureIO.newTexture(td);
                    haveMipMapData = td.getMipmapData() != null;
                }
                catch (Exception e)
                {
                    String msg = Logging.getMessage("layers.TextureLayer.ExceptionAttemptingToReadTextureFile",
                                                    imageSource);
                    Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                    this.textureInitializationFailed = true;
                    return(null);
                }
            }
            else if (imageSource is BufferedImage)
            {
                try
                {
                    TextureData td = AWTTextureIO.newTextureData(gl.getGLProfile(), (BufferedImage)imageSource,
                                                                 this.useMipMaps);
                    t = TextureIO.newTexture(td);
                    haveMipMapData = td.getMipmapData() != null;
                }
                catch (Exception e)
                {
                    String msg = Logging.getMessage("generic.IOExceptionDuringTextureInitialization");
                    Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                    this.textureInitializationFailed = true;
                    return(null);
                }
            }
            else if (imageSource is URL)
            {
                try
                {
                    InputStream stream = ((URL)imageSource).openStream();
                    if (stream == null)
                    {
                        Logging.logger().log(java.util.logging.Level.SEVERE, "generic.ExceptionAttemptingToReadImageFile",
                                             imageSource);
                        this.textureInitializationFailed = true;
                        return(null);
                    }

                    TextureData td = OGLUtil.newTextureData(gl.getGLProfile(), stream, this.useMipMaps);
                    t = TextureIO.newTexture(td);
                    haveMipMapData = td.getMipmapData() != null;
                }
                catch (Exception e)
                {
                    String msg = Logging.getMessage("layers.TextureLayer.ExceptionAttemptingToReadTextureFile",
                                                    imageSource);
                    Logging.logger().log(java.util.logging.Level.SEVERE, msg, e);
                    this.textureInitializationFailed = true;
                    return(null);
                }
            }
            else
            {
                Logging.logger().log(java.util.logging.Level.SEVERE, "generic.UnrecognizedImageSourceType",
                                     imageSource.GetType().Name);
                this.textureInitializationFailed = true;
                return(null);
            }

            if (t == null) // In case JOGL TextureIO returned null
            {
                Logging.logger().log(java.util.logging.Level.SEVERE, "generic.TextureUnreadable",
                                     imageSource is String ? imageSource : imageSource.GetType().Name);
                this.textureInitializationFailed = true;
                return(null);
            }

            // Textures with the same path are assumed to be identical textures, so key the texture id off the
            // image source.
            dc.getTextureCache().put(imageSource, t);
            t.bind(gl);

            // Enable the appropriate mip-mapping texture filters if the caller has specified that mip-mapping should be
            // enabled, and the texture itself supports mip-mapping.
            bool useMipMapFilter = this.useMipMaps && (haveMipMapData || t.isUsingAutoMipmapGeneration());

            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
                               useMipMapFilter ? GL.GL_LINEAR_MIPMAP_LINEAR : GL.GL_LINEAR);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);

            if (this.isUseAnisotropy() && useMipMapFilter)
            {
                double maxAnisotropy = dc.getGLRuntimeCapabilities().getMaxTextureAnisotropy();
                if (dc.getGLRuntimeCapabilities().isUseAnisotropicTextureFilter() && maxAnisotropy >= 2.0)
                {
                    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAnisotropy);
                }
            }

            this.width     = t.getWidth();
            this.height    = t.getHeight();
            this.texCoords = t.getImageTexCoords();

            return(t);
        }