我正在尝试将一些 Direct2D 绘图合并到 Ogre 项目中。目前 Ogre 仅真正支持 DirectX9,这会导致使用 Direct2D 时出现一些问题。我的解决方案是这样的。使用 Direct2D 将我的叠加层绘制到 png,然后使用 DirectX 将图像绘制到精灵。我尝试使用这种方法,一旦绘制方法完成,精灵就会损坏。首先Direct2D绘图代码:
void SceneManager::_initalize( void ){
// Initalize the bitmiap
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
reinterpret_cast<void **>(&pWICFactory)
);
// Initalize Direct2D
if(SUCCEEDED(hr)){
hr = D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory );
}
if(SUCCEEDED(hr)){
hr = pWICFactory->CreateBitmap(
rc.right - rc.left,
rc.bottom - rc.top,
GUID_WICPixelFormat8bppAlpha,
WICBitmapCacheOnLoad,
&pBitMap
);
}
if(SUCCEEDED(hr)){
hr = pD2DFactory->CreateWicBitmapRenderTarget(
pBitMap,
D2D1::RenderTargetProperties(),
&pRenderTarget
);
}
if(SUCCEEDED(hr)){
hr = pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(1,1,1,1), &pWhiteBrush);
}
// Draw to the bitmap here :)
if(SUCCEEDED(hr)){
pRenderTarget->BeginDraw();
pRenderTarget->Clear(D2D1::ColorF(0,0,0,0));
pRenderTarget->FillRectangle( D2D1::RectF( 0, 0, 500, 500), pWhiteBrush);
pRenderTarget->EndDraw();
}
if(SUCCEEDED(hr)){
hr = pWICFactory->CreateStream(&pStream);
}
WICPixelFormatGUID format = GUID_WICPixelFormat8bppAlpha;
if(SUCCEEDED(hr)){
hr = pStream->InitializeFromFilename(L"c:\\tmp\\dsplyr.xmg", GENERIC_WRITE);
}
if(SUCCEEDED(hr)){
hr = pWICFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &pEncoder);
}
if(SUCCEEDED(hr)){
hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
}
if(SUCCEEDED(hr)){
hr = pEncoder->CreateNewFrame(&pFrameEncode, NULL);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->Initialize(NULL);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->SetSize(rc.right - rc.left, rc.bottom - rc.top);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->SetPixelFormat(&format);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->WriteSource(pBitMap, NULL);
}
if(SUCCEEDED(hr)){
hr = pFrameEncode->Commit();
}
if(SUCCEEDED(hr)){
hr = pEncoder->Commit();
}
if(!SUCCEEDED(hr)){
MessageBox(NULL, L"ERROR!", L"ERROR!", MB_OKCANCEL);
}
}
这是绘图的代码:
void SceneManager::Render( void ){
LPDIRECT3DDEVICE9 pd3dDevice = NULL; // Our rendering device
LPD3DXSPRITE d3dspt;
LPDIRECT3DTEXTURE9 sprite;
mWindow->getCustomAttribute("D3DDEVICE", &pd3dDevice);
D3DXCreateSprite(pd3dDevice, &d3dspt);
//D3DXCreateTextureFromFileInMemory(pd3dDevice, pBitMap, sizeof(pBitMap) , &sprite);
D3DXCreateTextureFromFile(pd3dDevice, L"c:\\tmp\\testxmg" , &sprite);
d3dspt->Begin( D3DXSPRITE_ALPHABLEND );
D3DXVECTOR3 center(0,0,0);
D3DXVECTOR3 position(0,0,0);
d3dspt->Draw(sprite, &rc, ¢er, &position, D3DCOLOR_XRGB(255,255,255));
d3dspt->End();
sprite->Release();
d3dspt->Release();
}
错误发生在这一行 d3dspt->End();
这是错误:
First-chance exception at 0x000007FA34B51F9C (D3DX9_43.dll) in GUI.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Unhandled exception at 0x000007FA34B51F9C (D3DX9_43.dll) in GUI.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
显然 d3dspt 正在损坏,因为它的地址现在是 0xFFFFFFFFFFFFFFFF。我不知道为什么会这样。将继续调试并尝试找到解决我的问题的方法,但是来自 SO 的任何输入都会很棒!:)
环境信息:Microsfot Visual Studio 2012 C++ Ogre 1.9 DirectX9 Direct2D