2013-12-21 50 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中的左下角。

相关问题