我对D3D有0的经验。目前,我正在阅读弗兰克·露娜(Frank露娜)关于D3D11的书,并试图通过w/o效果框架在C#中使用SharpDX制作示例。我现在一直在做贴图。我不知道如何从文件中加载纹理,以及如何将其发送到着色器。
官方维基已经死了。或者至少它不给我装。我在这里搜索了一下,但是只找到了一些使用SharpDX.WIC的方法,也就是D2D,所以在D3D应用程序中使用一些D2D组件看起来有点奇怪。
发布于 2016-03-18 11:07:26
在这里查找纹理加载程序: How to load a texture from a file?
用法:
var device = this.DeviceManager.Direct3DDevice; var texture = TextureLoader.CreateTexture2DFromBitmap(device, TextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "Texture2.png")); ShaderResourceView textureView = new ShaderResourceView(device, texture);
发布于 2017-03-05 06:27:51
很抱歉在旧线程上发布,但是使用.NET框架中提供的内容来实现这一点不是更好吗?
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;
发布于 2016-05-18 04:32:50
WIC不是direct2d的一部分,wic是在directX中加载图像的最佳方式,即使在c++中,您必须使用wic,所以只需按照第一个答案中的链接就可以了。
或
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, 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,