绘制时忽略颜色

Ignore Colour when drawing

本文关键字:颜色 绘制      更新时间:2023-10-16

除了黑色以外,如何获得直接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,也会绘制我的黑色像素。

有什么想法我如何使它不绘制黑色像素?

您必须启用并设置混合模式

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