【问题标题】:Loading textures with SharpDX in Windows Store App在 Windows Store App 中使用 SharpDX 加载纹理
【发布时间】:2012-03-07 13:07:53
【问题描述】:

我已将我的 C# 游戏从 OpenTK (OpenGL) 转换为 SharpDX (DirectX),并在 Visual Studio 2010 中启动并运行它。我还在 Metro 的 Visual Studio 11 中启动并运行它,在 Windows 8 开发者预览版中.但是,Metro 构建仍然缺少纹理,所以我的多边形现在只是简单地着色。问题是用于加载图像的方法在 Metro DLL 中不存在。 Windows Store App 中的 Texture2D 类(继承自 Resource 类)中都缺少这些方法:

SharpDX.Direct3D11.Resource.FromStream
SharpDX.Direct3D11.Resource.FromFile
SharpDX.Direct3D11.Resource.FromMemory

有谁知道这些最终是否会在 Metro 中实施?或者有没有我可以使用的替代方法?请记住,在此之前我对 DirectX 或 SharpDX 一无所知,所以我仍然对这一切感到非常兴奋。

为 SharpDX 找到任何类型的帮助或信息都不容易,这很遗憾,因为它似乎是使用 C# 在 Metro 中的 3D 的一个很好的解决方案。

【问题讨论】:

  • 这可能会更好地发布到他们的论坛或邮件列表,他们会比这里的人更了解未来的计划。
  • 他们似乎没有自己的论坛或邮件列表。 =/ code.google.com/p/sharpdx
  • 这很愚蠢。也许你可以提出问题?我在那里看到了一些最近的,但不知道他们是否有回应。

标签: c# windows-8 windows-store-apps sharpdx direct3d11


【解决方案1】:

我不确定我是否清楚地理解了您的问题,但在 SharpDX 示例中,我发现类“TextureLoader”有两种方法:“LoadBitmap”和“CreateTexture2DFromBitmap”。 看,你可以使用这个函数从 .bmp、.png、.jpg 文件中加载 BitmapSource:

    public static BitmapSource LoadBitmap(ImagingFactory2 factory, string filename)
    {
        var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
            factory,
            filename,
            SharpDX.WIC.DecodeOptions.CacheOnDemand
            );

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

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

        return result;
    }

要从 BitmapSource 获取 Texture2D,您可以使用:

    public static Texture2D CreateTexture2DFromBitmap(Device device, 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));
        }
    }

为了方便起见,我创建了这个函数:

        public static Texture2D LoadFromFile(Device device, ImagingFactory2 factory, string fileName)
        {
            var bs = LoadBitmap(factory, fileName);
            var texture = CreateTexture2DFromBitmap(device, bs);
            return texture;
        }

【讨论】:

    【解决方案2】:

    您不需要使用 WIC 在 Metro 中加载纹理,您也可以从 BitmapImage 创建它们(它在 Metro 中工作并且不太复杂):

    public static Texture2D FromImage(BitmapImage image, GraphicsDevice device)
    {
        WriteableBitmap bitmap = new WriteableBitmap(image);
    
        return FromImageData(bitmap.Pixels, bitmap.PixelWidth, 
                                bitmap.PixelHeight, device);
    }
    
    public static Texture2D FromImageData(int[] data, int width, int height, GraphicsDevice device)
    {
        Texture2D texture = Texture2D.New(device, width, height,
                                        PixelFormat.B8G8R8A8.UNorm);
    
        texture.SetData<int>(data);
    
        return texture;
    }
    

    在这里查看我的答案:SharpDX load a texture in Windows Phone 8

    【讨论】:

      【解决方案3】:

      好消息! SharpDX 的作者 Alexandre Mutel 告诉我,微软从 Direct3D11 中删除了“辅助方法”。这不是 SharpDX 的遗漏。他还为我指明了正确的方向;使用 WIC 加载我的纹理。我在这里找到了示例代码:

      http://crazylights.googlecode.com/svn/CLReach/win8/SDX_CLGC/clgc.cs

      我只需要两个方法:LoadBitmap() 和 CreateTex2DFromBitmap()。我不得不将“R8G8B8A8_UNorm_SRgb”更改为“R8G8B8A8_UNorm”,但最终我让它工作了。现在我的游戏在 Metro 中看起来棒极了,它的所有纹理都到位了! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-14
        相关资源
        最近更新 更多