SharpDX 3加载.DDS应用于3D模型(C#)

时间:2017-04-27 17:36:23

标签: c# directx-11 bmp sharpdx

我正在尝试为游戏创建模型查看器以尝试学习SharpDX,但游戏使用.DDS文件,查看器只能读取.BMP。我在网上看得很远,我能找到的唯一东西是加载它们但是似乎不适用于SharpDX(我不知道我是菜鸟:D)

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));
            }
        }

    }
}

我觉得需要完全重做才能使用DDS格式。是简单地读取一个然后将其转换为位图更容易吗?

0 个答案:

没有答案