3

C#ゲームをOpenTK(OpenGL)からSharpDX(DirectX)に変換し、Visual Studio 2010から起動して実行しました。また、Windows 8DeveloperPreviewのMetroのVisualStudio11からも起動して実行しています。ただし、Metroビルドにはまだテクスチャがないため、現在、ポリゴンは単純に色付けされています。問題は、イメージのロードに使用されるメソッドがMetroDLLにないことです。これらのメソッドはすべて、Windows StoreアプリのTexture2Dクラス(Resourceクラスから継承)にはありません。

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

これらが最終的にMetroに実装されるかどうか誰かが知っていますか?または、私が使用できる別のアプローチはありますか?これまでDirectXやSharpDXのことを知らなかったので、まだ耳の後ろでとても濡れていることを覚えておいてください。

SharpDXのヘルプや情報を見つけるのは簡単ではありません。これは、C#を使用したMetroでの3Dの優れたソリューションのように思われるためです。

4

3 に答える 3

6

あなたの質問を明確に理解したかどうかはわかりませんが、SharpDXサンプルで、「LoadBitmap」と「CreateTexture2DFromBitmap」の2つのメソッドを持つクラス「TextureLoader」を見つけました。ほら、この関数を使用して、.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;
        }
于 2012-11-14T20:35:20.173 に答える
4

MetroでテクスチャをロードするためにWICを使用する必要はありません。また、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はWindowsPhone8にテクスチャをロードします

于 2013-09-15T09:02:15.053 に答える
2

朗報です!SharpDXの作者であるAlexandreMutelは、MicrosoftがDirect3D11から「ヘルパーメソッド」を削除したことを知らせてくれました。SharpDXによる脱落ではありませんでした。そして彼はまた私を正しい方向に向けました。WICを使用してテクスチャをロードします。ここでサンプルコードを見つけました:

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

LoadBitmap()とCreateTex2DFromBitmap()の2つのメソッドが必要でした。「R8G8B8A8_UNorm_SRgb」を「R8G8B8A8_UNorm」に変更する必要がありましたが、最終的には機能しました。そして今、私のゲームは、すべてのテクスチャが配置されたMetroで素晴らしいように見えます!:)

于 2012-03-09T22:49:44.557 に答える