2012-03-07 240 views
2

我已經將我的C#遊戲從OpenTK(OpenGL)轉換爲SharpDX(DirectX),並從Visual Studio 2010開始運行。我也從Visual Studio 11開始運行Metro,在Windows 8開發人員預覽版中。然而,Metro的構建仍然缺乏紋理,所以我的多邊形現在只是簡單的着色。問題是用於加載圖像的方法在Metro DLL中不存在。這些方法都是從Windows Store應用的Texture2D類(從資源類繼承)丟失:在Windows Store應用程序中使用SharpDX加載紋理

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

有誰知道,如果這些都將最終在地鐵實施?還是有一種替代方法可以使用?請記住,在此之前我從來不知道關於DirectX或SharpDX的一切,所以我仍然在這些耳朵後面非常潮溼。

尋找SharpDX的任何幫助或信息並不容易,這是一個恥辱,因爲它似乎是一個很好的解決方案,使用C#在地鐵中的3D。

+0

這可以更好地貼到自己的論壇或郵件列表,他們會更加了解比這裏人們未來的計劃。 – ssube 2012-03-07 13:34:52

+0

他們似乎沒有自己的論壇或郵件列表。 =/http://code.google.com/p/sharpdx/ – msedore 2012-03-07 14:04:21

+0

這很愚蠢。也許你可以提出問題?我在那裏看到一些相當新近的東西,但不知道他們是否迴應。 – ssube 2012-03-07 14:27:57

回答

2

好消息! 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中看起來很棒,它的紋理已經到位! :)

6

我不確定我已經清楚地理解了你的問題,但在SharpDX示例中,我找到了類'TextureLoader',它有兩個方法:'LoadBitmap'和'CreateTexture2DFromBitmap'。 瞧,你可以使用此功能從.BMP加載的BitmapSource,png格式,.jpg文件:

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

你並不需要使用WIC加載紋理地鐵,您還可以從BitmapImage的(它在地鐵創建它們,是比較少licated):

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

相關問題