osg的解释:
SceneView已弃用,还保留它仅为了向后兼容。需要嵌入式渲染支持时,建议组合使用 osgViewer::Viewer 和 osgViewer::GraphicsWindowEmbedded,osgViewer::GraphicsWindowEmbedded提供了更大范围的功能和一致性的API。
嵌入QT窗口的示例:
//qt
#include <QtWidgets/QApplication>
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QMouseEvent>
//osg
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
//osglib
#pragma comment(lib,"OpenThreads_d.lib")
#pragma comment(lib,"osg_d.lib")
#pragma comment(lib,"osgDB_d.lib")
#pragma comment(lib,"osgFX_d.lib")
#pragma comment(lib,"osgGA_d.lib")
#pragma comment(lib,"osgUtil_d.lib")
#pragma comment(lib,"osgViewer_d.lib")
class OSGWidget : public QOpenGLWidget
{
public:
OSGWidget(QWidget* parent = nullptr)
: QOpenGLWidget(parent)
, _mGraphicsWindow(new osgViewer::GraphicsWindowEmbedded(this->x(), this->y(),
this->width(), this->height()))
, _viewer(new osgViewer::Viewer())
{
//threadmodel
_viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
//master camera
osg::Camera* camera = new osg::Camera;
camera->setViewport(0, 0, this->width(), this->height());
camera->setClearColor(osg::Vec4(0.9f, 0.9f, 1.f, 1.f));
float aspectRatio = static_cast<float>(this->width()) / static_cast<float>(this->height());
camera->setProjectionMatrixAsPerspective(47.0f, aspectRatio, 1.f, 100.f);
camera->setGraphicsContext(_mGraphicsWindow);
_viewer->setCamera(camera);
// camera manipulator
_viewer->setCameraManipulator(new osgGA::TrackballManipulator);
// Load a model to display
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFile("D:\\osg365vs2015x64\\Data\\cow.osg");
if (scene) { _viewer->setSceneData(scene); }
_viewer->realize();
}
protected:
virtual void resizeGL(int width, int height)
{
_mGraphicsWindow->getEventQueue()->windowResize(this->x(), this->y() , width, height);
_mGraphicsWindow->resized(this->x(), this->y(), width, height);
_viewer->getCamera()->setViewport(0, 0, this->width(), this->height());
}
virtual bool event(QEvent* event)
{
bool handled = QOpenGLWidget::event(event);
this->update();
return handled;
}
virtual void wheelEvent(QWheelEvent* event)
{//注意:此处仅处理了 鼠标滚轮 事件,所以启动后仅对 鼠标滚轮 有回应!
int delta = event->delta();
osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ?
osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN;
_mGraphicsWindow->getEventQueue()->mouseScroll(motion);
}
private:
virtual void paintGL() {_viewer->frame();}
private:
osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _mGraphicsWindow;
osg::ref_ptr<osgViewer::Viewer> _viewer;
};
int main(int argc, char** argv)
{
QApplication qapp(argc, argv);
QMainWindow window;
OSGWidget* widget = new OSGWidget(&window);
window.setCentralWidget(widget);
window.show();
return qapp.exec();
}
运行结果:
环境配置:
vs2015+qt5.12.12,具体设置如下图,osg版本为365(注:可设置环境变量也可以直接设置其 include和lib路径)