示例#1
0
        public static GLTextureCubeArray FromDDS(DDS dds)
        {
            int size = (int)dds.Width;

            GLTextureCubeArray texture = new GLTextureCubeArray();

            texture.Width  = size;
            texture.Height = size;
            texture.Bind();

            var format = dds.Platform.OutputFormat;

            var           surfaces        = dds.GetSurfaces();
            List <byte[]> cubemapSurfaces = new List <byte[]>();

            for (int a = 0; a < surfaces.Count; a++)
            {
                cubemapSurfaces.Add(surfaces[a].mipmaps[0]);
            }

            int depth = surfaces.Count;

            Console.WriteLine($"depth {depth}");

            byte[] buffer = ByteUtils.CombineArray(cubemapSurfaces.ToArray());

            for (int j = 0; j < dds.MipCount; j++)
            {
                int mipWidth  = CalculateMipDimension(texture.Width, j);
                int mipHeight = CalculateMipDimension(texture.Height, j);

                if (dds.IsBCNCompressed())
                {
                    var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true);
                    GLTextureDataLoader.LoadCompressedImage(texture.Target, mipWidth, mipHeight, depth, internalFormat, buffer, j);
                }
                else
                {
                    var formatInfo = GLFormatHelper.ConvertPixelFormat(format);
                    if (dds.Platform.OutputFormat == TexFormat.RGBA8_UNORM)
                    {
                        formatInfo.Format = PixelFormat.Rgba;
                    }

                    GLTextureDataLoader.LoadImage(texture.Target, mipWidth, mipHeight, depth, formatInfo, buffer, j);
                }
            }

            GL.TexParameter(texture.Target, TextureParameterName.TextureBaseLevel, 0);
            GL.TexParameter(texture.Target, TextureParameterName.TextureMaxLevel, 13);
            GL.GenerateMipmap(GenerateMipmapTarget.TextureCubeMapArray);

            texture.Unbind();
            return(texture);
        }
        public void LoadImage(int width, int height, TexFormat format, byte[] data)
        {
            if (TextureFormatHelper.IsBCNCompressed(format))
            {
                var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true);
                int imageSize      = GLFormatHelper.CalculateImageSize(width, height, internalFormat);

                GL.CompressedTexImage3D(TextureTarget.Texture2DArray, 0,
                                        internalFormat, width, height, 1, 0, imageSize, data);
            }
            else
            {
                var formatInfo = GLFormatHelper.ConvertPixelFormat(format);

                GL.TexImage3D(Target, 0, formatInfo.InternalFormat, width, height, 1, 0,
                              formatInfo.Format, formatInfo.Type, data);
            }
        }
        public void LoadImage(STGenericTexture texture, ImageParameters parameters)
        {
            if (parameters == null)
            {
                parameters = new ImageParameters();
            }

            Bind();

            var format = texture.Platform.OutputFormat;
            var width  = CalculateMipDimension((int)texture.Width, 0);
            var height = CalculateMipDimension((int)texture.Height, 0);

            int numSurfaces = 1;

            if (Target == TextureTarget.Texture3D || Target == TextureTarget.Texture2DArray)
            {
                numSurfaces = (int)Math.Max(1, texture.Depth);
            }
            if (Target == TextureTarget.TextureCubeMap || Target == TextureTarget.TextureCubeMapArray)
            {
                numSurfaces = (int)Math.Max(1, texture.ArrayCount);
            }

            for (int i = 0; i < numSurfaces; i++)
            {
                var surface = texture.GetDeswizzledSurface(i, 0);
                int depth   = 1;

                bool loadAsBitmap = !IsPower2(width, height) && texture.IsBCNCompressed() && false;
                if (texture.IsASTC() || parameters.FlipY)
                {
                    loadAsBitmap = true;
                }

                int numMips = 1;

                for (int mipLevel = 0; mipLevel < numMips; mipLevel++)
                {
                    if (loadAsBitmap || parameters.UseSoftwareDecoder)
                    {
                        var rgbaData = texture.GetDecodedSurface(i, mipLevel);
                        if (parameters.FlipY)
                        {
                            rgbaData = FlipVertical(width, height, rgbaData);
                        }

                        var formatInfo = GLFormatHelper.ConvertPixelFormat(TexFormat.RGBA8_UNORM);
                        if (texture.IsSRGB)
                        {
                            formatInfo.InternalFormat = PixelInternalFormat.Srgb8Alpha8;
                        }

                        GLTextureDataLoader.LoadImage(Target, width, height, depth, formatInfo, rgbaData, mipLevel);
                    }
                    else if (texture.IsBCNCompressed())
                    {
                        var internalFormat = GLFormatHelper.ConvertCompressedFormat(format, true);
                        GLTextureDataLoader.LoadCompressedImage(Target, width, height, depth, internalFormat, surface, mipLevel);
                    }
                    else
                    {
                        var formatInfo = GLFormatHelper.ConvertPixelFormat(format);
                        GLTextureDataLoader.LoadImage(Target, width, height, depth, formatInfo, surface, mipLevel);
                    }
                }

                if (texture.MipCount > 1 && texture.Platform.OutputFormat != TexFormat.BC5_SNORM)
                {
                    GL.GenerateMipmap((GenerateMipmapTarget)Target);
                }
                else
                {
                    //Set level to base only
                    GL.TexParameter(Target, TextureParameterName.TextureBaseLevel, 0);
                    GL.TexParameter(Target, TextureParameterName.TextureMaxLevel, 0);
                }
            }

            Unbind();
        }