先发个找到的原文
http://www.cppblog.com/eros/archive/2009/05/13/82758.html
因为QT使用起来非常方便,也来用一下!
不过因为Dx的头文件在编译的时候N多警告!
不过没事!可以继续!
貌似没有idle事件,难道只能用定时器作更新?
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class
D3DWidget :
public
QWidget
{
Q_OBJECT
public :
explicit D3DWidget(QWidget * parent = 0 );
~ D3DWidget();
void show();
protected :
void init();
bool supportedCheck( IDirect3D9 * );
protected slots:
void creatResource();
void frameMove();
void render();
void destory();
void loop();
protected :
QPaintEngine * paintEngine() const ;
void paintEvent(QPaintEvent * );
void timerEvent ( QTimerEvent * event );
private :
{
Q_OBJECT
public :
explicit D3DWidget(QWidget * parent = 0 );
~ D3DWidget();
void show();
protected :
void init();
bool supportedCheck( IDirect3D9 * );
protected slots:
void creatResource();
void frameMove();
void render();
void destory();
void loop();
protected :
QPaintEngine * paintEngine() const ;
void paintEvent(QPaintEvent * );
void timerEvent ( QTimerEvent * event );
private :
//摄像机控制
void wheelEvent(QWheelEvent * );
void mousePressEvent(QMouseEvent * );
void mouseMoveEvent(QMouseEvent * );
void keyPressEvent(QKeyEvent * );
void keyReleaseEvent(QKeyEvent * );
private :
Ui::D3DWidget * ui;
};
void wheelEvent(QWheelEvent * );
void mousePressEvent(QMouseEvent * );
void mouseMoveEvent(QMouseEvent * );
void keyPressEvent(QKeyEvent * );
void keyReleaseEvent(QKeyEvent * );
private :
Ui::D3DWidget * ui;
};
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
D3DWidget::D3DWidget(QWidget
*
parent) :
QWidget(parent),
ui( new Ui::D3DWidget),
enableHdr( false )
{
setWindowTitle( " Direct3D Window " );
ui -> setupUi( this );
QWidget::setAttribute(Qt::WA_PaintOnScreen, true );
setMouseTracking( true );
}
QPaintEngine * D3DWidget::paintEngine() const
{
return NULL;
}
void D3DWidget::paintEvent(QPaintEvent * )
{
}
void D3DWidget::show()
{
// 初始化错误输出
errorManager::getInstance().initChannel();
// 初始化
init();
// 创建资源
creatResource();
QWidget::show();
getState().lastTickCount. ref () = GetTickCount();
startTimer( 1000 / 40 );
}
void D3DWidget::timerEvent ( QTimerEvent * event )
{
float curTickCount = GetTickCount();
getState().elapsedTime. ref () = static_cast < float > ( curTickCount - getState().lastTickCount. get () ) / 1000.0f ;
getState().time. ref () += getState().elapsedTime. get ();
loop();
// FPS更新频率
static float update = 0.5f ;
update -= getState().elapsedTime. get ();
if ( update <= 0.0f )
{
update = 0.5f ;
setWindowTitle( tr( " 3D FPS:%1 " ).arg( 1.0f / getState().elapsedTime. get ()) );
}
getState().lastTickCount. ref () = curTickCount;
event -> accept();
}
void D3DWidget::loop()
{
frameMove();
if ( isActiveWindow() && ! isHidden() )
render();
}
void D3DWidget::render()
{
LPDIRECT3DDEVICE9 device = getState().d3dDevice. get ();
device -> SetTransform( D3DTS_VIEW , getState().camera. get () -> getViewMatrix() );
device -> SetTransform( D3DTS_PROJECTION , getState().camera. get () -> getProjectMatrix() );
device -> Clear( 0 , NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0 , 1.0f , 0 );
device -> BeginScene();
device -> EndScene();
device -> Present( 0 , 0 , 0 , 0 );
}
QWidget(parent),
ui( new Ui::D3DWidget),
enableHdr( false )
{
setWindowTitle( " Direct3D Window " );
ui -> setupUi( this );
QWidget::setAttribute(Qt::WA_PaintOnScreen, true );
setMouseTracking( true );
}
QPaintEngine * D3DWidget::paintEngine() const
{
return NULL;
}
void D3DWidget::paintEvent(QPaintEvent * )
{
}
void D3DWidget::show()
{
// 初始化错误输出
errorManager::getInstance().initChannel();
// 初始化
init();
// 创建资源
creatResource();
QWidget::show();
getState().lastTickCount. ref () = GetTickCount();
startTimer( 1000 / 40 );
}
void D3DWidget::timerEvent ( QTimerEvent * event )
{
float curTickCount = GetTickCount();
getState().elapsedTime. ref () = static_cast < float > ( curTickCount - getState().lastTickCount. get () ) / 1000.0f ;
getState().time. ref () += getState().elapsedTime. get ();
loop();
// FPS更新频率
static float update = 0.5f ;
update -= getState().elapsedTime. get ();
if ( update <= 0.0f )
{
update = 0.5f ;
setWindowTitle( tr( " 3D FPS:%1 " ).arg( 1.0f / getState().elapsedTime. get ()) );
}
getState().lastTickCount. ref () = curTickCount;
event -> accept();
}
void D3DWidget::loop()
{
frameMove();
if ( isActiveWindow() && ! isHidden() )
render();
}
void D3DWidget::render()
{
LPDIRECT3DDEVICE9 device = getState().d3dDevice. get ();
device -> SetTransform( D3DTS_VIEW , getState().camera. get () -> getViewMatrix() );
device -> SetTransform( D3DTS_PROJECTION , getState().camera. get () -> getProjectMatrix() );
device -> Clear( 0 , NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0 , 1.0f , 0 );
device -> BeginScene();
device -> EndScene();
device -> Present( 0 , 0 , 0 , 0 );
}