2012-07-14 124 views
0

我試圖使用DirectX創建並繪製成位圖,然後使用該位圖繪製到屏幕。我有以下測試代碼,我已經從多個來源拼湊在一起。我似乎能夠無誤地創建位圖,並嘗試用紅色填充它。但是,當我使用位圖繪製屏幕時,我只是得到一個黑色的矩形。誰能告訴我我可能會做錯什麼?請注意,這是作爲Windows 8 Metro應用程序運行的,如果這有所幫助的話。在Windows下使用DirectX繪製位圖

請注意格式化程序似乎是刪除我的尖括號,我不知道如何阻止它做到這一點。 'device'變量是ID3D11Device類,'context'變量是ID3D11DeviceContext,d2dContext變量是ID2D1DeviceContext,dgixDevice變量是IDXGIDevice,'d2dDevice'是ID2D1Device。

void PaintTestBitmap(GRectF& rect) 
{ 
    HRESULT hr; 
    UINT  creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; 

    D3D_FEATURE_LEVEL featureLevels[] = 
    { 
     D3D_FEATURE_LEVEL_11_1, 
     D3D_FEATURE_LEVEL_11_0, 
     D3D_FEATURE_LEVEL_10_1, 
     D3D_FEATURE_LEVEL_10_0, 
     D3D_FEATURE_LEVEL_9_3, 
     D3D_FEATURE_LEVEL_9_2, 
     D3D_FEATURE_LEVEL_9_1 
    }; 

    ComPtr<ID3D11Device>  device; 
    ComPtr<ID3D11DeviceContext> context; 
    ComPtr<ID2D1DeviceContext> d2dContext; 

    hr = D3D11CreateDevice( NULL, 
           D3D_DRIVER_TYPE_HARDWARE, 
           NULL, 
           creationFlags, 
           featureLevels, 
           ARRAYSIZE(featureLevels), 
           D3D11_SDK_VERSION, 
           &device, 
           NULL, 
           &context); 

    if (SUCCEEDED(hr)) 
    { 
     ComPtr<IDXGIDevice> dxgiDevice; 
     device.As(&dxgiDevice); 

     // create D2D device context 
     ComPtr<ID2D1Device> d2dDevice; 
     hr = D2D1CreateDevice(dxgiDevice.Get(), NULL, &d2dDevice); 

     if (SUCCEEDED(hr)) 
     { 
      hr = d2dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2dContext); 

      if (SUCCEEDED(hr)) 
      { 
       ID2D1Bitmap1 *pBitmap; 
       D2D1_SIZE_U size = { rect.m_width, rect.m_height }; 
       D2D1_BITMAP_PROPERTIES1 properties = {{ DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED }, 96, 96, D2D1_BITMAP_OPTIONS_TARGET, 0 }; 

       hr = d2dContext->CreateBitmap(size, (const void *) 0, 0, &properties, &pBitmap); 
       if (SUCCEEDED(hr)) 
       { 
        d2dContext->SetTarget(pBitmap); 
        d2dContext->BeginDraw();  
        d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::Red));  // Clear the bitmap to Red 
        d2dContext->EndDraw(); 

        // If I now use pBitmap to paint to the screen, I get a black rectange 
        // rather than the red one I was expecting 
       } 
      } 
     } 
    } 
} 

請注意,我對DirectX和Metro風格的應用程序都很新。

謝謝

+0

快速瀏覽看起來不錯。我會考慮位圖格式,因爲我相信D2D更喜歡BGRA,並且您如何呈現給碎石可能會影響您的結果。 – Denis 2012-07-14 20:13:47

+0

感謝您的反饋意見。我嘗試將像素格式更改爲DXGI_FORMAT_B8G8R8A8_UNORM(包含和不包含alpha),但仍得到相同的結果。你知道如何使用DirectX寫入位圖的例子嗎?謝謝 – 2012-07-14 21:18:03

+0

你可以查看這個示例:http://code.msdn.microsoft.com/windowsapps/Direct2D-basic-image-6b4b1a26或者你可以看看其他的D2D示例http://code.msdn.microsoft.com/ windowsapps /網站/搜索?F%5B0%5D.Type =技術&F%5B0%5D.Value =的DirectX&F%5B0%5D.Text =的DirectX – Denis 2012-07-15 05:16:19

回答

0

你可能會忘記的是你不要在你的上下文中繪製你的位圖。

你可以嘗試是

hr = d2dContext->CreateBitmap(size, (const void *) 0, 0, &properties, &pBitmap); 
      if (SUCCEEDED(hr)) 
      { 
       ID2D1Image *pImage = nullptr; 
       d2dContext->GetTarget(&image); 

       d2dContext->SetTarget(pBitmap); 
       d2dContext->BeginDraw();  
       d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::Red));  // Clear the bitmap to Red 
       d2dContext->SetTarget(pImage); 
       d2dContext->DrawBitmap(pBitmap); 
       d2dContext->EndDraw(); 


      } 

你做的是保存您的渲染目標,然後繪製位圖,並在年底您設置上下文正確的渲染目標,讓它吸取它

位圖