我是directx的新手,最后我设法加载了一个我想要显示为背景图像/纹理的图像
定义图像
void setBGImage(std::string path)
{
D3DXCreateTextureFromFileA(m_Device, path.c_str(), &m_BGImage);
m_BGImageCenter = D3DXVECTOR3(450.0f, 250.0f, 0.0f); // Image is 900x500
}
绘制图像
void DrawBackground()
{
m_Sprite->Begin(D3DXSPRITE_OBJECTSPACE|D3DXSPRITE_DONOTMODIFY_RENDERSTATE);
// Texture tiling
/*
D3DXMATRIX texScaling;
D3DXMatrixScaling(&texScaling, 1.0f, 1.0f, 0.0f);
m_Device->SetTransform(D3DTS_TEXTURE0, &texScaling);*/
//D3DXMATRIX T, S, R;
//D3DXMatrixTranslation(&T, 0.0f, 0.0f, 0.0f);
//D3DXMatrixScaling(&S, 1.0f, 1.0f, 0.0f);
//D3DXMatrixRotationZ(&R, 0.45f);
//m_Sprite->SetTransform(&(S*T));
m_Sprite->Draw(m_BGImage, 0, &m_BGImageCenter, 0, D3DCOLOR_XRGB(255, 255, 255));
m_Sprite->Flush();
m_Sprite->End();
}
onResetDevice(在启动时被调用)
void onResetDevice()
{
m_Sprite->OnResetDevice();
// Sets up the camera 640 units back looking at the origin.
D3DXMATRIX V;
D3DXVECTOR3 pos(0.0f, 0.0f, -640.0f); // This distance is just a test value to get only the image and no white background/border as soon as I have it centered I will adjust it.
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
m_Device->SetTransform(D3DTS_VIEW, &V);
// The following code defines the volume of space the camera sees.
D3DXMATRIX P;
RECT R;
GetClientRect(m_hWnd, &R);
float width = (float)R.right;
float height = (float)R.bottom;
D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.25f, width/height, 0.0f, 1.0f);
m_Device->SetTransform(D3DTS_PROJECTION, &P);
// This code sets texture filters, which helps to smooth out disortions when you scale a texture.
m_Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
m_Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
m_Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
// This line of code disables Direct3D lighting.
m_Device->SetRenderState(D3DRS_LIGHTING, false);
// The following code specifies an alpha test and reference value.
m_Device->SetRenderState(D3DRS_ALPHAREF, 10);
m_Device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
// The following code is used to setup alpha blending.
m_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
// Indicates that we are using 2D texture coordinates.
m_Device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
}
当我使用这些方法渲染我的背景图像时,它会被显示为倒置,而不是居中(稍微向右),我感觉高度宽度比不正确(图像有点模糊,它感觉就像图像不像它应该的那样高。)
我尝试了什么?
我尝试调整各种坐标,以便从另一侧查看图像,然后旋转它,但无论我尝试什么,它都会变成白色/不存在的背景。
图像居中并不是什么大问题我可以移动相机位置但我很困惑,因为m_BGImageCenter
应该在Draw方法中为我做这个并且似乎不能100%工作正确的。
我的问题(我想在这种情况下可以提出多个问题):
我怎样才能让图像不被颠倒(我知道你会如何在理论上做到这一点,但我不知道怎么做,所以请给我坐标。)
为什么图像不居中?
D3DXMatrixPerspectiveFovLH
是否有可能包裹我的图像,因为它看起来有点模糊。