繪製

2014-01-24 15 views
1

時忽略顏色我怎樣才能獲得直接-X繪製各種顏色,除了黑色?目前我使用下面的代碼加載並繪製紋理:繪製

void LoadTexture(IDirect3DDevice9* Device, unsigned char* buffer, int width, int height, IDirect3DTexture9* &Texture, ID3DXSprite* &Sprite) 
{ 
    Device->CreateTexture(width, height, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &Texture, 0); 
    D3DXCreateSprite(Device, &Sprite); 

    D3DLOCKED_RECT rect; 
    Texture->LockRect(0, &rect, nullptr, D3DLOCK_DISCARD); 
    TexturePixels = static_cast<unsigned char*>(rect.pBits); 
    Texture->UnlockRect(0); 

    memcpy(TexturePixels, &buffer[0], width * height * 4); 
} 

void DrawTextureSprite(IDirect3DDevice9* Device, IDirect3DTexture9* Texture, ID3DXSprite* Sprite) 
{ 
    if (Sprite && Texture) 
    {  
     D3DXVECTOR3 Position = D3DXVECTOR3(0, 0, 0); 
     Sprite->Begin(D3DXSPRITE_ALPHABLEND); 
     Sprite->Draw(Texture, nullptr, nullptr, &Position, D3DCOLOR_RGBA(0xFF, 0xFF, 0xFF, 0xFF)); //0xFFFFFFFF - white.. 
     Sprite->End(); 
    } 
} 

void BltBuffer(IDirect3DDevice9* Device) 
{ 
    if (Bitmap != nullptr) 
    { 
     std::uint8_t* Ptr = Bitmap->pixels; 
     for (int I = 0; I < Bitmap->height; ++I) 
     { 
      for (int J = 0; J < Bitmap->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; //if the colour is black, set the alpha channel to 0x0. 
      } 
     } 

     //if (!Texture && !TexturePixels) 
     { 
      LoadTexture(Device, Bitmap->pixels, Bitmap->width, Bitmap->height, Texture, Sprite); 
     } 

     DrawTextureSprite(Device, Texture, Sprite); 
     SafeRelease(Texture); 
     SafeRelease(Sprite); 
    } 
} 

每一幀我打電話BlitBuffer和我通過我的裝置。然而,當它吸引,它吸引我的黑色像素,即使我設置其alpha通道爲0。所有其它的像素有自己的alpha通道設置爲255

任何想法如何,我可以把它不是直接繪製黑色像素?

回答

1

您必須啓用,並從我設置混合模式以及

 d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 
    d3ddev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); 
+1

+1。不僅如此,我還必須將紋理的'格式'更改爲:'D3DFMT_A8R8G8B8'。這當然有幫助。我花了最後一個小時啓用和禁用alpha混合,只要你發佈這個,我看到我的格式是錯誤的.. – Brandon