1

我尝试按照我的 3D directx 书中的教程进行一些修改。我遇到的问题是我想绘制图像但无法正常工作。该示例使用相机,因为它基于一个小游戏,但我只想加载图像而不需要任何花哨的相机转换。(按原样绘制图像,无需调整大小+ alpha 混合)。

这是我的代码,应该包含相关部分。

。H

class Screen
{
private:
    IDirect3DTexture9* m_BGImage;
    ID3DXSprite* m_Sprite;

    IDirect3DDevice9* m_Device;

public:
    Screen();
    ~Screen();

    void setDevice(IDirect3DDevice9* device);  
    void setBGImage(std::string path);

    void Draw();
    void onLostDevice();
    void onResetDevice();

    void Clean();
};

.cpp

Screen::Screen() {}

Screen::~Screen()
{
    Clean();
}

void Screen::setDevice(IDirect3DDevice9* device) 
{
    m_Device = device;
    D3DXCreateSprite(m_Device, &m_Sprite);
}

void Screen::setBGImage(std::string path)
{
    D3DXCreateTextureFromFileA(m_Device, path.c_str(), &m_BGImage);
}

void Screen::Draw()
{
    m_Sprite->Begin(D3DXSPRITE_DONOTMODIFY_RENDERSTATE); // This is (so I believe) what causes the problem. If I use D3DXSPRITE_OBJECTSPACE|D3DXSPRITE_DONOTMODIFY_RENDERSTATE like described in the example from my book I only get a black screen.

    m_Sprite->Draw(m_BGImage, 0, &D3DXVECTOR3(256.0f, 256.0f, 0.0f), 0, D3DCOLOR_XRGB(255, 255, 255));
    m_Sprite->Flush();

    m_Sprite->End();
}

void Screen::Clean()
{
    ReleaseCOM(m_Sprite);
    ReleaseCOM(m_BGImage);
}

void Screen::onLostDevice()
{
    m_Sprite->OnLostDevice();
}

void Screen::onResetDevice()
{
    m_Sprite->OnResetDevice();

    m_Device->SetRenderState(D3DRS_ALPHAREF, 10);
    m_Device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);

    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);

    m_Device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
}

编辑:差点忘了:

#define ReleaseCOM(x) { if(x){ x->Release(); x = 0; } }
4

1 回答 1

0

调用函数“Begin”时应使用 D3DXSPRITE_ALPHABLEND 而不是 D3DXSPRITE_DONOTMODIFY_RENDERSTATE,指定 D3DXSPRITE_ALPHABLEND 并不意味着必须使用 alpha blend,如果不调用 Screen::onResetDevice(),alpha blend 将不起作用,所以这里是 D3DXSPRITE_ALPHABLEND只是使“开始”起作用的参数。如果您不想在调用函数“Begin”时指定任何标志,则可以将 0 传递给它。

 m_Sprite->Begin(0); 
 m_Sprite->Draw(m_BGImage, 0, &D3DXVECTOR3(256.0f, 256.0f, 0.0f), 0, D3DCOLOR_XRGB(255, 255, 255));
 m_Sprite->Flush();
 m_Sprite->End();

参考: http: //msdn.microsoft.com/en-us/library/windows/desktop/bb205466 (v=vs.85).aspx

于 2012-10-11T03:25:47.407 回答