SharpDX 3 loading.DDS应用于3d模型(C#)

SharpDX 3 loading.DDS应用于3d模型(C#),c#,directx-11,bmp,sharpdx,C#,Directx 11,Bmp,Sharpdx,我正在尝试为游戏创建一个模型查看器,以尝试学习SharpDX,但游戏使用.DDS文件,查看器只能读取.BMPs。我在网上到处找了找,唯一能找到的就是加载它们,但似乎不适合SharpDX(我不知道我是一个noob:D) 我有一种感觉,这将需要完全重做利用DDS格式。简单地读取一个,然后将其转换为位图是否更容易 using SharpDX.Direct3D11; using SharpDX.WIC; namespace ModelViewer.Programming.GraphicClasses

我正在尝试为游戏创建一个模型查看器,以尝试学习SharpDX,但游戏使用.DDS文件,查看器只能读取.BMPs。我在网上到处找了找,唯一能找到的就是加载它们,但似乎不适合SharpDX(我不知道我是一个noob:D)

我有一种感觉,这将需要完全重做利用DDS格式。简单地读取一个,然后将其转换为位图是否更容易

using SharpDX.Direct3D11;
using SharpDX.WIC;

namespace ModelViewer.Programming.GraphicClasses
{
    public class TextureClass
    {
        public ShaderResourceView TextureResource { get; private set; }
        public bool Init(Device device, string fileName)
        {
            try
            {
                using (var texture = LoadFromFile(device, new ImagingFactory(), fileName))
                {
                    ShaderResourceViewDescription srvDesc = new ShaderResourceViewDescription()
                    {
                        Format = texture.Description.Format,
                        Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                    };
                    srvDesc.Texture2D.MostDetailedMip = 0;
                    srvDesc.Texture2D.MipLevels = -1;

                    TextureResource = new ShaderResourceView(device, texture, srvDesc);
                    device.ImmediateContext.GenerateMips(TextureResource);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }        
        public void Shutdown()
        {
            TextureResource?.Dispose();
            TextureResource = null;
        }
        public Texture2D LoadFromFile(Device device, ImagingFactory factory, string fileName)
        {
            using (var bs = LoadBitmap(factory, fileName))
                return CreateTextureFromBitmap(device, bs);
        }
        public BitmapSource LoadBitmap(ImagingFactory factory, string filename)
        {
            var bitmapDecoder = new BitmapDecoder(factory, filename, DecodeOptions.CacheOnDemand);
            var result = new FormatConverter(factory);
            result.Initialize(bitmapDecoder.GetFrame(0), SharpDX.WIC.PixelFormat.Format32bppPRGBA, BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom);
            return result;
        }
        public Texture2D CreateTextureFromBitmap(Device device, BitmapSource bitmapSource)
        {
            int stride = bitmapSource.Size.Width * 4;
            using (var buffer = new SharpDX.DataStream(bitmapSource.Size.Height * stride, true, true))
            {
                bitmapSource.CopyPixels(stride, buffer);
                return new Texture2D(device, new Texture2DDescription()
                {
                    Width = bitmapSource.Size.Width,
                    Height = bitmapSource.Size.Height,
                    ArraySize = 1,
                    BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget,
                    Usage = ResourceUsage.Default,
                    CpuAccessFlags = CpuAccessFlags.None,
                    Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    MipLevels = 1,
                    OptionFlags = ResourceOptionFlags.GenerateMipMaps,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                },
                new SharpDX.DataRectangle(buffer.DataPointer, stride));
            }
        }

    }
}