首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SharpDX 3.0.2 D3D11 -如何从文件中加载纹理并使其在着色器中工作?

SharpDX 3.0.2 D3D11 -如何从文件中加载纹理并使其在着色器中工作?
EN

Stack Overflow用户
提问于 2016-03-17 18:16:41
回答 4查看 7K关注 0票数 2

我对D3D有0的经验。目前,我正在阅读弗兰克·露娜(Frank露娜)关于D3D11的书,并试图通过w/o效果框架在C#中使用SharpDX制作示例。我现在一直在做贴图。我不知道如何从文件中加载纹理,以及如何将其发送到着色器。

官方维基已经死了。或者至少它不给我装。我在这里搜索了一下,但是只找到了一些使用SharpDX.WIC的方法,也就是D2D,所以在D3D应用程序中使用一些D2D组件看起来有点奇怪。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-03-18 11:07:26

在这里查找纹理加载程序:How to load a texture from a file?

用法:

代码语言:javascript
运行
复制
var device = this.DeviceManager.Direct3DDevice;
var texture = TextureLoader.CreateTexture2DFromBitmap(device, TextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "Texture2.png"));
ShaderResourceView textureView = new ShaderResourceView(device, texture);
票数 0
EN

Stack Overflow用户

发布于 2017-03-05 06:27:51

很抱歉在旧线程上发布,但是使用.NET框架中提供的内容来实现这一点不是更好吗?

代码语言:javascript
运行
复制
    if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
    {
        bitmap = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format32bppArgb);
    }
    var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    var ret = new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
    {
        Width = bitmap.Width,
        Height = bitmap.Height,
        ArraySize = 1,
        BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
        Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
        CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
        Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
        MipLevels = 1,
        OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
        SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
    }, new SharpDX.DataRectangle(data.Scan0, data.Stride));
    bitmap.UnlockBits(data);
    return ret;
票数 3
EN

Stack Overflow用户

发布于 2016-05-18 04:32:50

WIC不是direct2d的一部分,wic是在directX中加载图像的最佳方式,即使在c++中,您必须使用wic,所以只需按照第一个答案中的链接就可以了。

代码语言:javascript
运行
复制
 public class TextureLoader
{
    /// <summary>
    /// Loads a bitmap using WIC.
    /// </summary>
    /// <param name="deviceManager"></param>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static SharpDX.WIC.BitmapSource LoadBitmap(SharpDX.WIC.ImagingFactory2 factory, string filename)
    {
        var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
            factory,
            filename,
            SharpDX.WIC.DecodeOptions.CacheOnDemand
            );

        var formatConverter = new SharpDX.WIC.FormatConverter(factory);

        formatConverter.Initialize(
            bitmapDecoder.GetFrame(0),
            SharpDX.WIC.PixelFormat.Format32bppPRGBA,
            SharpDX.WIC.BitmapDitherType.None, 
            null,
            0.0, 
            SharpDX.WIC.BitmapPaletteType.Custom);

        return formatConverter;
    }

    /// <summary>
    /// Creates a <see cref="SharpDX.Direct3D11.Texture2D"/> from a WIC <see cref="SharpDX.WIC.BitmapSource"/>
    /// </summary>
    /// <param name="device">The Direct3D11 device</param>
    /// <param name="bitmapSource">The WIC bitmap source</param>
    /// <returns>A Texture2D</returns>
    public static SharpDX.Direct3D11.Texture2D CreateTexture2DFromBitmap(SharpDX.Direct3D11.Device device, SharpDX.WIC.BitmapSource bitmapSource)
    {
        // Allocate DataStream to receive the WIC image pixels
        int stride = bitmapSource.Size.Width * 4;
        using (var buffer = new SharpDX.DataStream(bitmapSource.Size.Height * stride, true, true))
        {
            // Copy the content of the WIC to the buffer
            bitmapSource.CopyPixels(stride, buffer);
            return new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Width = bitmapSource.Size.Width,
                Height = bitmapSource.Size.Height,
                ArraySize = 1,
                BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
                Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                MipLevels = 1,
                OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
            }, new SharpDX.DataRectangle(buffer.DataPointer, stride));
        }
    }
}

我刚刚从链接中取出它,所以它可能需要一些修改才能使用SharpDX 3,我使用几乎相同的代码路径,但是在加载阶段我有更多的选项。法线不加乘,转换为Srgb之类的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36068631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档