Exemplo n.º 1
0
        /// <summary>
        /// Creates a cached texture for the specifed texture, using the specified path
        /// as a lookup key.
        /// </summary>
        public int CreateCachedTexture(BLP texture, string texturePath, TextureWrapMode textureWrapMode = TextureWrapMode.Repeat)
        {
            if (texture == null)
            {
                throw new ArgumentNullException(nameof(texture));
            }

            int textureID = GL.GenTexture();

            if (texture.GetCompressionType() == TextureCompressionType.DXTC)
            {
                try
                {
                    LoadDXTTexture(textureID, texture);
                }
                catch (GraphicsErrorException gex)
                {
                    Log.Warn($"GraphicsErrorException in CreateCachedTexture (failed to create DXT texture): {gex.Message}\n" +
                             "The texture will be loaded as a bitmap instead.");
                }
                finally
                {
                    // Load a fallback bitmap instead
                    using (Bitmap mipZero = texture.GetMipMap(0))
                    {
                        LoadBitmapTexture(textureID, mipZero);
                    }
                }
            }
            else
            {
                using (Bitmap mipZero = texture.GetMipMap(0))
                {
                    LoadBitmapTexture(textureID, mipZero);
                }
            }

            // Use linear mipmapped filtering
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)textureWrapMode);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)textureWrapMode);

            int maximalMipLevel = texture.GetMipMapCount() == 0 ? 0 : texture.GetMipMapCount() - 1;

            GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, ref maximalMipLevel);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0);

            this.GLTextureCache.Add(texturePath.ConvertPathSeparatorsToCurrentNativeSeparator().ToUpperInvariant(), textureID);
            return(textureID);
        }