1

我是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包裹我的图像,因为它看起来有点模糊。

4

0 回答 0