Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在EndScene中绘制期间降级/混合的颜色_C++_Directx_Directx 9 - Fatal编程技术网

C++ 在EndScene中绘制期间降级/混合的颜色

C++ 在EndScene中绘制期间降级/混合的颜色,c++,directx,directx-9,C++,Directx,Directx 9,我在一个我喜欢的游戏中尝试在屏幕上绘制纹理。问题是纹理有这些奇怪的轮廓 例如,在下图中: 应该是这样的(使用D3D9XSprite绘制): 我正在使用以下工具绘制纹理: //Creates a texture from a buffer containing pixels. void LoadTexture(IDirect3DDevice9* Device, std::uint8_t* buffer, int width, int height, IDirect3DTexture9* &am

我在一个我喜欢的游戏中尝试在屏幕上绘制纹理。问题是纹理有这些奇怪的轮廓

例如,在下图中:

应该是这样的(使用D3D9XSprite绘制):

我正在使用以下工具绘制纹理:

//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();
}
//从包含像素的缓冲区创建纹理。
void LoadTexture(IDirect3DDevice9*设备、标准::uint8_t*缓冲区、整型宽度、整型高度、IDirect3DTexture9*&纹理)
{
设备->创建纹理(宽度、高度、1、0、D3DFMT_A8R8G8B8、D3DPOOL_管理和纹理、0);
D3DLOCKED_RECT;
纹理->锁定矩形(0,&rect,nullptr,D3DLOCK_DISCARD);
std::uint8_t*TexturePixels=静态投射(rect.pBits);
纹理->解除锁定(0);
memcpy(纹理像素和缓冲区[0],宽度*高度*4);
}
//在屏幕上绘制纹理。。
void DrawTexture(IDirect3DDevice9*设备,IDirect3DTexture9*纹理,浮点X1,浮点Y1,浮点X2,浮点Y2)
{
D3DVertex顶点[]=
{
{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}
};
设备->设置FVF(顶点\u FVF\u TEX);
设备->设置纹理(0,纹理);
设备->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,顶点,sizeof(D3DVertex));
}
//在屏幕上加载并绘制纹理。
void BltBuffer(IDirect3DDevice9*设备)
{
加载纹理(设备、缓冲区、w、h、纹理);
DrawTexture(设备、纹理、0、0、w、h);
安全释放(纹理);
}
//钩状尾景。在游戏窗口上绘制我的纹理。
HRESULT Direct3DDevice9Proxy::EndScene()
{
IDirect3DStateBlock9*状态块;
ptr_Direct3DDevice9->CreateStateBlock(D3DSBT_ALL和stateblock);
stateblock->Capture();
ptr_Direct3DDevice9->SetRenderState(D3DRS_照明,错误);
ptr_Direct3DDevice9->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
ptr_Direct3DDevice9->SetRenderState(D3DRS_ZENABLE,false);
ptr_Direct3DDevice9->SetRenderState(D3DRS_字母可更改,错误);
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->Apply();
stateblock->Release();
}
返回ptr_Direct3DDevice9->EndScene();
}

你知道为什么我的颜色被降级了吗?

你忘了用半个像素的大小来偏移你的四边形,以便将纹理直接映射到像素。如果没有此偏移,绘制的纹理将模糊,因为纹理映射到多个像素。有关该问题的详细说明,请参阅


(你的问题相当于下面的帖子)

+1不知道事情是这样的。我在OpenGL中不是这样的,所以我真的很困惑。非常感谢。