1
我試圖用DirectX 9構建使用2D精靈的簡單2D遊戲,並且出現了乾淨地顯示圖像的問題。我想加載bmp圖像並將它們按原樣顯示在屏幕上(無插值,無放大,無濾波或抗鋸齒等)。使用Direct3D 9進行2D編程9 - 測試圖像失真
我確定我錯過了一些東西,但是當我嘗試在屏幕上呈現一個100x100的bmp時,它看起來不穩定和扭曲,就像像素藝術圖像在稍微縮小時通常會看起來一樣。我希望bmp看起來與在MS Paint中加載時完全一樣。
有沒有人有任何想法,爲什麼這可能是這種情況?我的代碼如下所示:
初始化代碼:
g_DxCom = Direct3DCreate9(D3D_SDK_VERSION);
if (g_DxCom == NULL)
{
return false;
}
D3DDISPLAYMODE d3dDisplayMode;
if (FAILED(g_DxCom->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dDisplayMode)))
{
return false;
}
D3DPRESENT_PARAMETERS d3dPresentParameters;
::ZeroMemory(&d3dPresentParameters, sizeof(D3DPRESENT_PARAMETERS));
d3dPresentParameters.Windowed = FALSE;
d3dPresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dPresentParameters.BackBufferFormat = d3dDisplayMode.Format; // D3DFMT_X8R8G8B8
d3dPresentParameters.BackBufferWidth = d3dDisplayMode.Width;
d3dPresentParameters.BackBufferHeight = d3dDisplayMode.Height;
d3dPresentParameters.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
if (FAILED(g_DxCom->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
this->hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dPresentParameters,
&pd3dDevice)))
{
if (FAILED(g_DxCom->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
this->hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dPresentParameters,
&pd3dDevice)))
{
return false;
}
}
texture = NULL;
bg_texture = NULL;
渲染代碼:
LPDIRECT3DDEVICE9 g_dxDevice;
float float1 = 99.5f; // I'd like to render my 100x100 sprite from screen coordinates 100, 100 to 200, 200
float float2 = 198.5f;
CUSTOMVERTEX OurVertices[] =
{
{ float1, float2, 1.0f, 1.0f, 0.0f, 1.0f },
{ float1, float1, 1.0f, 1.0f, 0.0f, 0.0f },
{ float2, float1, 1.0f, 1.0f, 1.0f, 0.0f },
{ float1, float2, 1.0f, 1.0f, 0.0f, 1.0f },
{ float2, float1, 1.0f, 1.0f, 1.0f, 0.0f },
{ float2, float2, 1.0f, 1.0f, 1.0f, 1.0f }
};
LPDIRECT3DVERTEXBUFFER9 v_buffer;
g_dxDevice->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
VOID* pVoid;
// Lock the vertex buffer into memory
v_buffer->Lock(0, 0, &pVoid, 0);
// Copy our vertex buffer to memory
::memcpy(pVoid, OurVertices, sizeof(OurVertices));
// Unlock buffer
v_buffer->Unlock();
LPDIRECT3DTEXTURE9 g_texture;
HRESULT hError;
DWORD dwTextureFilter = D3DTEXF_NONE;
g_dxDevice->SetSamplerState(0, D3DSAMP_MINFILTER, dwTextureFilter);
g_dxDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, dwTextureFilter);
g_dxDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, dwTextureFilter);
g_dxDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);
g_dxDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
g_dxDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);
hError = D3DXCreateTextureFromFile(g_dxDevice, L"Test.bmp", &g_texture); // 100x100 sprite
g_dxDevice->SetTexture(0, g_texture);
g_dxDevice->Clear(0,
NULL,
D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0, 40, 100),
1.0f,
0);
g_dxDevice->BeginScene();
// Do rendering on the back buffer here
g_dxDevice->SetFVF(CUSTOMFVF);
g_dxDevice->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
g_dxDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 6);
g_dxDevice->EndScene();
g_dxDevice->Present(NULL, NULL, NULL, NULL);
g_texture->Release();
v_buffer->Release();