Qt中嵌入OSG

最近因项目需要在Qt中嵌入OSG界面,遂尝试了一番:

实验环境:vs2010+Qt4.8.3+OSG3.0.1

osg能够和很多的GUI界面进行融合,其源码包中含的工程代码为我们展示了使用的示例:

Qt中嵌入OSG_第1张图片

其中与Qt有关的有三个:osgQtBrowser、osgQtWidgets和osgviewerQt。

前两者是在osg中嵌入Qt,而第三个是在Qt中嵌入osg,符合需求。

新建Qt工程,并将代码拷贝进去:

#include 
#include 
#include 
#include 

#include 
#include 

#include 

#include 

#include 

#include 

class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
{
public:
	ViewerWidget(osgViewer::ViewerBase::ThreadingModel threadingModel=osgViewer::CompositeViewer::SingleThreaded) : QWidget()
	{
		setThreadingModel(threadingModel);

		// disable the default setting of viewer.done() by pressing Escape.
		setKeyEventSetsDone(0);

		QWidget* widget1 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readNodeFile("cow.osgt") );
		QWidget* widget2 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readNodeFile("glider.osgt") );
		QWidget* widget3 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readNodeFile("axes.osgt") );
		QWidget* widget4 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readNodeFile("fountain.osgt") );
		QWidget* popupWidget = addViewWidget( createGraphicsWindow(900,100,320,240,"Popup window",true), osgDB::readNodeFile("dumptruck.osgt") );
		popupWidget->show();

		QGridLayout* grid = new QGridLayout;
		grid->addWidget( widget1, 0, 0 );
		grid->addWidget( widget2, 0, 1 );
		grid->addWidget( widget3, 1, 0 );
		grid->addWidget( widget4, 1, 1 );
		setLayout( grid );

		connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
		_timer.start( 10 );
	}

	QWidget* addViewWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )
	{
		osgViewer::View* view = new osgViewer::View;
		addView( view );

		osg::Camera* camera = view->getCamera();
		camera->setGraphicsContext( gw );

		const osg::GraphicsContext::Traits* traits = gw->getTraits();

		camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
		camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
		camera->setProjectionMatrixAsPerspective(30.0f, static_cast(traits->width)/static_cast(traits->height), 1.0f, 10000.0f );

		view->setSceneData( scene );
		view->addEventHandler( new osgViewer::StatsHandler );
		view->setCameraManipulator( new osgGA::TrackballManipulator );

		return gw->getGLWidget();
	}

	osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
	{
		osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
		osg::ref_ptr traits = new osg::GraphicsContext::Traits;
		traits->windowName = name;
		traits->windowDecoration = windowDecoration;
		traits->x = x;
		traits->y = y;
		traits->width = w;
		traits->height = h;
		traits->doubleBuffer = true;
		traits->alpha = ds->getMinimumNumAlphaBits();
		traits->stencil = ds->getMinimumNumStencilBits();
		traits->sampleBuffers = ds->getMultiSamples();
		traits->samples = ds->getNumMultiSamples();

		return new osgQt::GraphicsWindowQt(traits.get());
	}

	virtual void paintEvent( QPaintEvent* event )
	{ frame(); }

protected:

	QTimer _timer;
};

int main( int argc, char** argv )
{
	osg::ArgumentParser arguments(&argc, argv);

	osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
	while (arguments.read("--SingleThreaded")) threadingModel = osgViewer::ViewerBase::SingleThreaded;
	while (arguments.read("--CullDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
	while (arguments.read("--DrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::DrawThreadPerContext;
	while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext;

	QApplication app(argc, argv);
	ViewerWidget* viewWidget = new ViewerWidget(threadingModel);
	viewWidget->setGeometry( 100, 100, 800, 600 );
	viewWidget->show();
	return app.exec();
}

1.配置好工程VC++目录(包含目录--include,库目录--lib)和链接器->输入中的附加依赖项,注意Qt和OSG的必须同时含有。

编译发现#include 找不到类GraphicsWindowQt,查看osg的include目录,发现不存在osgQt目录;

由于自己的osg3.0.1是在官网上直接下载别人编译好的(自己编比较耗时):

http://openscenegraph.alphapixel.com/osg/downloads/free-openscenegraph-binary-downloads

解压后发现里面不存在osgQt,应该是编译者没有勾选这一选项,怎么办?

尝试用3.2.1的osgQt去填补,失败!

后又发现osg提供后期的svn更新维护,遂checkout该目录下来后补充,OK。

2.接下来编译工程则提示    error LNK2001:无法解析的外部符号

一看就知道是链接器lib库中添加项少了的原因,根据出错的信息反馈,确定是osgQt.lib库缺乏的问题——没编译。

遂去寻找,最后在googlecode上找到了这个库(感谢分享者),当然,release和debug版本都下载:

https://code.google.com/p/reconstructme-qt/source/browse/trunk/3rd_party/?r=141#3rd_party%2Fosg-3.0.1

库加进去后编译OK。

3.编译成功后却提示osg80-osgQt.dll缺乏,遂在上诉链接目录中下载了该dll,加入到osg的bin目录下。

这里说明下链接中的文件为osg3.0.1_32位的lib库和dll文件。

最后编译成功(五个线程独立渲染各自的节点图像)如下:

Qt中嵌入OSG_第2张图片

你可能感兴趣的:(OSG)