Hello!
I'm trying to create a widget which encapsulate the Direct3D engine(something like OpenGL widget). I have the following code:
.h
class Direct3DWidget
: public QWidget{
Q_OBJECT
public:
Direct3DWidget
(QWidget *parent
= 0);
~Direct3DWidget();
bool InitDirect3D();
public slots:
bool Rendering();
private:
{
Rendering();
}
{
return NULL;
}
LPDIRECT3D9 m_pD3D;
LPDIRECT3DDEVICE9 m_pd3dDevice;
};
class Direct3DWidget : public QWidget
{
Q_OBJECT
public:
Direct3DWidget(QWidget *parent = 0);
~Direct3DWidget();
bool InitDirect3D();
public slots:
bool Rendering();
private:
void paintEvent(QPaintEvent *p)
{
Rendering();
}
QPaintEngine* paintEngine()
{
return NULL;
}
LPDIRECT3D9 m_pD3D;
LPDIRECT3DDEVICE9 m_pd3dDevice;
};
To copy to clipboard, switch view to plain text mode
.cpp
Direct3DWidget
::Direct3DWidget(QWidget *parent
/* = 0 */){
setFixedSize(200, 200);
setAutoFillBackground(false);
setAttribute(Qt::WA_PaintOnScreen, true);
}
Direct3DWidget::~Direct3DWidget()
{
if( m_pd3dDevice != NULL)
m_pd3dDevice->Release();
if( m_pD3D != NULL)
m_pD3D->Release();
}
bool Direct3DWidget::InitDirect3D()
{
m_pD3D = Direct3DCreate9( D3D_SDK_VERSION);
if( !m_pD3D)
return false;
D3DPRESENT_PARAMETERS d3dpp = {0};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
HRESULT hr = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winId(),
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &m_pd3dDevice );
if( FAILED(hr) || !m_pd3dDevice)
return false;
}
bool Direct3DWidget::Rendering()
{
if(m_pd3dDevice == NULL)
return false;
m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0);
if(SUCCEEDED(m_pd3dDevice->BeginScene()))
{
m_pd3dDevice->EndScene();
}
m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
return true;
}
Direct3DWidget::Direct3DWidget(QWidget *parent /* = 0 */)
{
setFixedSize(200, 200);
setAutoFillBackground(false);
setAttribute(Qt::WA_PaintOnScreen, true);
}
Direct3DWidget::~Direct3DWidget()
{
if( m_pd3dDevice != NULL)
m_pd3dDevice->Release();
if( m_pD3D != NULL)
m_pD3D->Release();
}
bool Direct3DWidget::InitDirect3D()
{
m_pD3D = Direct3DCreate9( D3D_SDK_VERSION);
if( !m_pD3D)
return false;
D3DPRESENT_PARAMETERS d3dpp = {0};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
HRESULT hr = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winId(),
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &m_pd3dDevice );
if( FAILED(hr) || !m_pd3dDevice)
return false;
}
bool Direct3DWidget::Rendering()
{
if(m_pd3dDevice == NULL)
return false;
m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0);
if(SUCCEEDED(m_pd3dDevice->BeginScene()))
{
m_pd3dDevice->EndScene();
}
m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
return true;
}
To copy to clipboard, switch view to plain text mode
main.cpp
int main(int argc, char *argv[])
{
Direct3DWidget* widget = new Direct3DWidget(&window);
window.show();
widget->InitDirect3D();
return a.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow window;
Direct3DWidget* widget = new Direct3DWidget(&window);
window.show();
widget->InitDirect3D();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
But this code does nothing
. What should I change to gain the appropriate behavior?
I'll be glad for any advices regarding the Direct3D.
Bookmarks