2013-12-21 47 views
0

我想創建一個紋理從指針到一個像素緩衝區格式BGRA,它不斷地更改/更新它的值。 我想畫這個紋理到每一幀左上角的屏幕。OpenGL到DirectX(緩衝區到紋理到屏幕)

使用我的一些XNA和OpenGL知識,我想出了一些,但我堅持讓紋理看到緩衝區的變化。我也不知道如何正確地繪製紋理,所以我創建了一個精靈(就像在XNA中一樣)。

我已經寫以下..

的DirectX 9:

void CreateSprite(IDirect3DDevice9* Device, ID3DXSprite* &Sprite) 
{ 
    D3DXCreateSprite(Device, Sprite); 
} 

void DrawSprite(IDirect3DTexture9* Texture, ID3DXSprite* Sprite, D3DXVECTOR3 Position) 
{ 
    Sprite->Begin(D3DXSPRITE_ALPHABLEND); 
     sprite->Draw(Texture, nullptr, nullptr, &Position, 0xFFFFFFFF); 
    Sprite->End(); 
    //SafeRelease(Sprite); 
    //SafeRelease(Texture); 
} 

void BufferToTexture(IDirect3DDevice9* Device, BufferInfo* BuffPtr) 
{ 
    if (BuffPtr != nullptr) 
    { 
     if (Texture == nullptr) 
     { 
      Device->CreateTexture(BuffPtr->width, BuffPtr->height, 1, 0, D3DFMT_X8B8G8R8, D3DPOOL_MANAGED, &Texture, 0); 
     } 
     else 
     { 
      D3DLOCKED_RECT rect; 
      Texture->LockRect(&rect, 0, D3DLOCK_DISCARD); 
      std::uint8_t* dest = static_cast<std::uint8_t*>(rect.pBits); 
      memcpy(dest, BuffPtr->Pixels, BuffPtr->width * BuffPtr->height * 4); 
      Texture->UnlockRect(0); 
     } 

     //I modify the Pixel Buffer. Not sure if the Texture will see these changes. 
     //Would be better if I can make the texture's rect.pBits point to BuffPtr->Pixels. 
     //So that I can avoid the above memcpy. 
     std::uint8_t* Ptr = (std::uint8_t*)BuffPtr->Pixels; 
     for (int I = 0; I < BuffPtr->height; ++I) 
     { 
      for (int J = 0; J < BuffPtr->width; ++J) 
      { 
       std::uint8_t B = *(Ptr++); 
       std::uint8_t G = *(Ptr++); 
       std::uint8_t R = *(Ptr++); 
       *(Ptr++) = (B == 0 && G == 0 && R == 0) ? 0 : 0xFF; 
      } 
     } 
    } 
} 

在OpenGL中,代碼直接使用像素緩衝器。對緩衝區的任何更改也發生在紋理上,並且沒有進行復制。該代碼允許我直接在屏幕上繪製一組像素。我試圖在DirectX中完成同樣的事情,但我不確定如何使紋理看到緩衝區中的更改。另外,我不確定如何將我的DirectX紋理繪製到屏幕上。

回答

1

「不確定紋理是否會看到這些更改。」

不,它不會因爲你在做你以後的Unlock RECT更改內存。在D3D中鎖定和解鎖資源類似於在OpenGL中取消映射緩衝區對象。

至於爲什麼它工作在OpenGL中,我只能想象你映射和地方取消映射你的PBO外的函數BufferToTexture (...)。如果你改變你的D3D代碼以這種方式工作,你應該成爲解決這個問題的一部分。

您還應該注意到D3D使用了不同的紋理原點:(0,0)是D3D中的左上角和OpenGL中的左下角。

相關問題