绘图时忽略颜色

时间:2014-01-24 08:05:25

标签: c++ directx directx-9

如何让Direct-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 个答案:

答案 0 :(得分:1)

您还必须启用和设置混合模式

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