1. 程式人生 > >OSG中使用Qt做介面

OSG中使用Qt做介面

最近使用OSG做三維引擎開發,在外層使用Qt做主框架,三維渲染使用OSG,但是在OSG中想快速寫出UI設計師設計的介面有時就顯得吃力,OSG提供了osgQt來支援Qt嵌入OSG,那麼如果能在OSG中直接使用Qt做介面必定事半功倍,於是在網路一頓胡搜,結果令人沮喪,大多標題寫著osg嵌入Qt卻幹著Qt嵌入OSG的事。於是想到OSG的例子,進去也是一頓亂搜,不知道是我沒找到還是真沒有,本人反正沒有收到。於是在經過各種測試找到了一種方法,原來osgQt本身就支援,這個類叫:QWidgetImage.

本人使用Qt5.8測試。

下面是程式碼:

1.引入所需標頭檔案

#include "osg/Texture2D"

#include"osg/Geometry"
#include"osg/CullStack"
#include"osg/AutoTransform"
#include"osg/MatrixTransform"
#include"osg/PositionAttitudeTransform"
#include"osgDB/ReadFile"
#include"osgGA/TrackballManipulator"
#include"osgViewer/ViewerEventHandlers"
#include"osgViewer/Viewer"
#include"osgQt/QWidgetImage"
#include
<QLabel>
#include<QPushButton>
#include<QVBoxLayout>
#include<QApplication>

2.建立一個Qt窗體

QWidget* createWidget(constQString& movieFile,constQString& title)

{
QLabel*label=newQLabel;
QPushButton*playBtn=newQPushButton("Play"+title);
QPushButton*stopBtn=newQPushButton("Stop
"+title);
QWidget*demo=newQWidget;
demo->setLayout(newQVBoxLayout);
demo->layout()->addWidget(label);
demo->layout()->addWidget(playBtn);
demo->layout()->addWidget(stopBtn);
demo->setStyleSheet("background-color:lightblue;");
QMovie*movie=newQMovie(movieFile);
label->setMovie(movie);
label->setFixedHeight(400);
QObject::connect(playBtn,SIGNAL(clicked()),movie,SLOT(start()));
QObject::connect(stopBtn,SIGNAL(clicked()),movie,SLOT(stop()));
returndemo;
}
3. 建立一個osg節點

osg::Node*createNode(constosg::Vec3&position,constQString& title, const QString &backgroundImage)

{
osg::ref_ptr<osgQt::QWidgetImage>widgetImage=
newosgQt::QWidgetImage(createDemoWidget(backgroundImage,title));
osg::ref_ptr<osgViewer::InteractiveImageHandler>handler=
newosgViewer::InteractiveImageHandler(widgetImage.get());
osg::ref_ptr<osg::Texture2D>texture=newosg::Texture2D;
texture->setImage(widgetImage.get());
osg::ref_ptr<osg::Geometry>quad=
osg::createTexturedQuadGeometry(osg::Vec3(),
osg::Vec3(widgetImage->getQWidget()->width(),0,0),
osg::Vec3(0,widgetImage->getQWidget()->height(),0));
quad->setEventCallback(handler.get());
quad->setCullCallback(handler.get());
osg::ref_ptr<osg::Geode>geode=newosg::Geode;
geode->addDrawable(quad.get());
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture.get());
geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
osg::ref_ptr<osg::AutoTransform>autoNode=newosg::AutoTransform;
autoNode->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
autoNode->setAutoScaleToScreen(true);
autoNode->addChild(geode.get());
osg::ref_ptr<osg::PositionAttitudeTransform>pat=newosg::PositionAttitudeTransform;
pat->setPosition(position);
pat->addChild(geode.get());
returnpat.release();
}
4.將節點加入場景
intmain(intargc,char**argv)
{
QApplicationapp(argc,argv);
osg::ref_ptr<osg::Group>root=newosg::Group;
//root->addChild(createDemoNode(osg::Vec3(-1.0,0.0,0.0),"Widget1", F:\\UrbanOMp\\resource\\image\\start_up_bk1.gif));
root->addChild(createDemoNode(osg::Vec3(1.0,0.0,0.0),"Widget2", F:\\UrbanOMp\\resource\\image\\start_up_bk1.gif));
osgViewer::Viewerviewer;
viewer.setSceneData(root.get());
viewer.setCameraManipulator(newosgGA::TrackballManipulator);
viewer.addEventHandler(newosgViewer::StatsHandler);
while(!viewer.done())
{
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
viewer.frame();
}
return0;
}
執行成功的在OSG中使用Qt,是不是太簡單了。。。。。。。。。。。。。。。。。。。。。。。。。。