在EndScene中绘制期间降低/混合的颜色

时间:2014-02-01 03:29:49

标签: c++ directx directx-9

我正试图在我迷上的游戏中将纹理绘制到屏幕上。问题是纹理有这些奇怪的轮廓..

例如,在下图中:

enter image description here

应该看起来像这样(使用D3D9XSprite绘制):

enter image description here

我正在绘制纹理:

//Creates a texture from a buffer containing pixels.
void LoadTexture(IDirect3DDevice9* Device, std::uint8_t* buffer, int width, int height, IDirect3DTexture9* &Texture)
{
    Device->CreateTexture(width, height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &Texture, 0);

    D3DLOCKED_RECT rect;
    Texture->LockRect(0, &rect, nullptr, D3DLOCK_DISCARD);
    std::uint8_t* TexturePixels = static_cast<std::uint8_t*>(rect.pBits);
    Texture->UnlockRect(0);
    memcpy(TexturePixels, &buffer[0], width * height * 4);
}

//Draws a texture to the screen..
void DrawTexture(IDirect3DDevice9* Device, IDirect3DTexture9* Texture, float X1, float Y1, float X2, float Y2)
{
    D3DVertex vertices[] =
    {
        {X1, Y1, 1.0f, 1.0f, D3DCOLOR_RGBA(0xFF, 0xFF, 0xFF, 0xFF), 0.0f, 0.0f},
        {X2, Y1, 1.0f, 1.0f, D3DCOLOR_RGBA(0xFF, 0xFF, 0xFF, 0xFF), 1.0f, 0.0f},
        {X1, Y2, 1.0f, 1.0f, D3DCOLOR_RGBA(0xFF, 0xFF, 0xFF, 0xFF), 0.0f, 1.0f},
        {X2, Y2, 1.0f, 1.0f, D3DCOLOR_RGBA(0xFF, 0xFF, 0xFF, 0xFF), 1.0f, 1.0f}
    };

    Device->SetFVF(VERTEX_FVF_TEX);
    Device->SetTexture(0, Texture);
    Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertices, sizeof(D3DVertex));
}

//Loads and draws a texture to the screen.
void BltBuffer(IDirect3DDevice9* Device)
{
        LoadTexture(Device, buffer, w, h, Texture);
        DrawTexture(Device, Texture, 0, 0, w, h);
        SafeRelease(Texture);
}


//Hooked EndScene. Draws my texture on the game's window.
HRESULT Direct3DDevice9Proxy::EndScene()
{
    IDirect3DStateBlock9* stateblock;
    ptr_Direct3DDevice9->CreateStateBlock(D3DSBT_ALL, &stateblock);
    stateblock->Capture();

    ptr_Direct3DDevice9->SetRenderState(D3DRS_LIGHTING, false);
    ptr_Direct3DDevice9->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    ptr_Direct3DDevice9->SetRenderState(D3DRS_ZENABLE, false);
    ptr_Direct3DDevice9->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
    ptr_Direct3DDevice9->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
    ptr_Direct3DDevice9->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    ptr_Direct3DDevice9->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

    BltBuffer(ptr_Direct3DDevice9);

    if (stateblock)
    {
        stateblock->Apply();
        stateblock->Release();
    }
    return ptr_Direct3DDevice9->EndScene();
}

为什么我的颜色会降级?

1 个答案:

答案 0 :(得分:2)

您忘记使用像素的一半大小来偏移四边形,以将纹素直接映射到像素。如果没有此偏移,绘制的纹理将会模糊,因为纹理像素会映射到多个像素。问题在Directly Mapping Texels to Pixels (MSDN)中有更详细的解释。

(您的问题等于以下帖子Precise Texture Overlay