示例#1
0
 /// <summary>Loads all registered textures.</summary>
 internal static void LoadAllTextures()
 {
     for (int i = 0; i < Program.Renderer.TextureManager.RegisteredTexturesCount; i++)
     {
         Program.Renderer.TextureManager.LoadTexture(ref TextureManager.RegisteredTextures[i], OpenGlTextureWrapMode.ClampClamp, CPreciseTimer.GetClockTicks(), Interface.CurrentOptions.Interpolation, Interface.CurrentOptions.AnisotropicFilteringLevel);
     }
 }
示例#2
0
        // --- load texture ---

        /// <summary>Loads the specified texture into OpenGL if not already loaded.</summary>
        /// <param name="handle">The handle to the registered texture.</param>
        /// <param name="wrap">The texture type indicating the clamp mode.</param>
        /// <param name="currentTicks">The current system clock-ticks</param>
        /// <param name="Interpolation">The interpolation mode to use when loading the texture</param>
        /// <param name="AnisotropicFilteringLevel">The anisotropic filtering level to use when loading the texture</param>
        /// <returns>Whether loading the texture was successful.</returns>
        public bool LoadTexture(ref Texture handle, OpenGlTextureWrapMode wrap, int currentTicks, InterpolationMode Interpolation, int AnisotropicFilteringLevel)
        {
            Texture texture = null;

            //Don't try to load a texture to a null handle, this is a seriously bad idea....
            if (handle == null || handle.OpenGlTextures == null)
            {
                return(false);
            }

            if (handle.MultipleFrames)
            {
                if (!animatedTextures.ContainsKey(handle.Origin))
                {
                    if (!handle.Origin.GetTexture(out texture))
                    {
                        //Loading animated texture barfed
                        return(false);
                    }
                    animatedTextures.Add(handle.Origin, texture);
                }
                else
                {
                    texture = animatedTextures[handle.Origin];
                }
                double elapsedTime   = CPreciseTimer.GetElapsedTime(handle.LastAccess, currentTicks);
                int    elapsedFrames = (int)(elapsedTime / texture.FrameInterval);
                if (elapsedFrames > 0)
                {
                    texture.CurrentFrame += elapsedFrames;
                    texture.CurrentFrame %= texture.TotalFrames;
                    handle.LastAccess     = currentTicks;
                }
            }
            else
            {
                handle.LastAccess = currentTicks;
            }
            //Set last access time

            if (texture != null)
            {
                handle = texture;
            }

            if (handle.OpenGlTextures[(int)wrap].Valid)
            {
                return(true);
            }



            if (handle.Ignore)
            {
                return(false);
            }

            if (texture == null && handle.Origin.GetTexture(out texture) || texture != null)
            {
                if (texture.MultipleFrames)
                {
                    handle.MultipleFrames = true;
                }
                if (texture.BitsPerPixel == 32)
                {
                    int[] names = new int[1];
                    GL.GenTextures(1, names);
                    GL.BindTexture(TextureTarget.Texture2D, names[0]);
                    handle.OpenGlTextures[(int)wrap].Name = names[0];
                    if (texture.MultipleFrames)
                    {
                        texture.OpenGlTextures[(int)wrap].Name = names[0];
                    }

                    handle.Width        = texture.Width;
                    handle.Height       = texture.Height;
                    handle.Transparency = texture.GetTransparencyType();

                    switch (Interpolation)
                    {
                    case InterpolationMode.NearestNeighbor:
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
                        break;

                    case InterpolationMode.Bilinear:
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Linear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
                        break;

                    case InterpolationMode.NearestNeighborMipmapped:
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.NearestMipmapNearest);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Nearest);
                        break;

                    case InterpolationMode.BilinearMipmapped:
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.NearestMipmapLinear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
                        break;

                    case InterpolationMode.TrilinearMipmapped:
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.LinearMipmapLinear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
                        break;

                    default:
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.LinearMipmapLinear);
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
                        break;
                    }

                    if ((wrap & OpenGlTextureWrapMode.RepeatClamp) != 0)
                    {
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.Repeat);
                    }
                    else
                    {
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.ClampToEdge);
                    }

                    if ((wrap & OpenGlTextureWrapMode.ClampRepeat) != 0)
                    {
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.Repeat);
                    }
                    else
                    {
                        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.ClampToEdge);
                    }

                    if (renderer.ForceLegacyOpenGL)
                    {
                        if (Interpolation == InterpolationMode.NearestNeighbor || Interpolation == InterpolationMode.Bilinear)
                        {
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 0);
                        }
                        else
                        {
                            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1);
                        }
                    }

                    if (Interpolation == InterpolationMode.AnisotropicFiltering && AnisotropicFilteringLevel > 0)
                    {
                        GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, AnisotropicFilteringLevel);
                    }

                    if (handle.Transparency == TextureTransparencyType.Opaque)
                    {
                        /*
                         * If the texture is fully opaque, the alpha channel is not used.
                         * If the graphics driver and card support 24-bits per channel,
                         * it is best to convert the bitmap data to that format in order
                         * to save memory on the card. If the card does not support the
                         * format, it will likely be upconverted to 32-bits per channel
                         * again, and this is wasted effort.
                         * */
                        int    width = texture.Width;
                        int    height = texture.Height;
                        int    stride = (3 * (width + 1) >> 2) << 2;
                        byte[] oldBytes = texture.Bytes;
                        byte[] newBytes = new byte[stride * texture.Height];
                        int    i = 0, j = 0;

                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++)
                            {
                                newBytes[j + 0] = oldBytes[i + 0];
                                newBytes[j + 1] = oldBytes[i + 1];
                                newBytes[j + 2] = oldBytes[i + 2];
                                i += 4;
                                j += 3;
                            }

                            j += stride - 3 * width;
                        }

                        GL.TexImage2D(TextureTarget.Texture2D, 0,
                                      PixelInternalFormat.Rgb8,
                                      texture.Width, texture.Height, 0,
                                      OpenTK.Graphics.OpenGL.PixelFormat.Rgb,
                                      PixelType.UnsignedByte, newBytes);
                    }
                    else
                    {
                        /*
                         * The texture uses its alpha channel, so send the bitmap data
                         * in 32-bits per channel as-is.
                         * */
                        GL.TexImage2D(TextureTarget.Texture2D, 0,
                                      PixelInternalFormat.Rgba8,
                                      texture.Width, texture.Height, 0,
                                      OpenTK.Graphics.OpenGL.PixelFormat.Rgba,
                                      PixelType.UnsignedByte, texture.Bytes);
                    }
                    if (renderer.ForceLegacyOpenGL == false)
                    {
                        GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
                    }
                    handle.OpenGlTextures[(int)wrap].Valid = true;
                    if (texture.MultipleFrames)
                    {
                        texture.OpenGlTextures[(int)wrap].Valid = true;
                    }
                    return(true);
                }
            }

            handle.Ignore = true;
            return(false);
        }
示例#3
0
 public override bool LoadTexture(Texture Texture, OpenGlTextureWrapMode wrapMode)
 {
     return(Program.Renderer.TextureManager.LoadTexture(Texture, wrapMode, CPreciseTimer.GetClockTicks(), Interface.CurrentOptions.Interpolation, Interface.CurrentOptions.AnisotropicFilteringLevel));
 }