实现方式
1、首先设置一个Qt下的一个窗口基类;
2、窗口基类继承自一个重写的QGLWidget类和一个osgViewer::Viewer类
3、重新QGLWidget类,实现OSG在windows窗口的使能。
4、更新窗口继承类,重写构造函数和virtual void initializeGL();
virtual void paintGL();函数。
窗口基类
#ifndef QTOSGVRWIDGET_H
#define QTOSGVRWIDGET_H#include
#include "qtosgviewerwidget.h"
#include "Include.h"class QtOSGVRWidget : public QtOSGViewerWidget, public osgViewer::Viewer
{
public:~QtOSGVRWidget();QtOSGVRWidget(QWidget * parent &#61; 0, const char * name &#61; 0, const QGLWidget * shareWidget &#61; 0, WindowFlags f &#61; 0):QtOSGViewerWidget(parent, name, shareWidget, f){getCamera()->setViewport(new osg::Viewport(0,0,width(),height()));getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);getCamera()->setGraphicsContext(getGraphicsWindow());setThreadingModel(osgViewer::Viewer::SingleThreaded);connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateGL()));m_timer.start(10);}
private:virtual void initializeGL();virtual void paintGL();
private:std::fstream myfile1;std::fstream myfile;QTimer m_timer;unsigned char *buf;unsigned char *buftrtr;unsigned short *buf1;unsigned short *buf2;};#endif #include "qtosgvrwidget.h"QtOSGVRWidget::~QtOSGVRWidget()
{myfile1.close();myfile.close();
}void QtOSGVRWidget::initializeGL()
{glClearColor(1.0, 0.2, 0.3, 1.0);glShadeModel(GL_SMOOTH);glEnable(GL_DEPTH);this->setMaximumSize(512, 512);this->setMinimumSize(512, 512);buf &#61; new unsigned char[width()*height()*3];buftrtr &#61; new unsigned char[width()*height()*3];buf1 &#61; new unsigned short[width()*height()];buf2 &#61; new unsigned short[width()*height()];myfile1.open("F-22-hei.raw", std::ios::binary | std::ios::out | std::ios::app);myfile.open("F-22.raw", std::ios::binary | std::ios::out | std::ios::app);
}void QtOSGVRWidget::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();frame();#if 1int w &#61; width();int h &#61; height();glReadPixels(0, 0, width(), height(), GL_RGB, GL_UNSIGNED_BYTE, buf);for (int i &#61; 0; i < width()*height()*3; i&#43;&#43;){buftrtr[i] &#61; buf[width()*height()*3-1-i];}for (int i &#61; 0; i < width()*height()*3 - 3; ){if (i < width()*height()*3 - 3){unsigned char tmp1 &#61; buftrtr[i];unsigned char tmp2 &#61; buftrtr[i&#43;1];unsigned char tmp3 &#61; buftrtr[i&#43;2];buftrtr[i] &#61; tmp3;buftrtr[i&#43;1] &#61; tmp2;buftrtr[i&#43;2] &#61; tmp1;i&#43;&#61;3;}}myfile.write((char*)buftrtr, width()*height()*3);glReadPixels(0, 0, width(), height(), GL_RED, GL_UNSIGNED_SHORT, buf1);for (int i &#61; 0; i < width()*height(); i&#43;&#43;){buf2[i] &#61; buf1[width()*height()-1-i];}myfile1.write((char*)buf2, width()*height()*2);#endif
}
重写的QGLWidget类
#ifndef QTOSGVIEWERWIDGET_H
#define QTOSGVIEWERWIDGET_H#include
#include "Include.h"using Qt::WindowFlags;class QtOSGViewerWidget : public QGLWidget
{Q_OBJECTpublic:QtOSGViewerWidget(QWidget *parent);QtOSGViewerWidget(QWidget * parent &#61; 0, const char * name &#61; 0, const QGLWidget * shareWidget &#61; 0, WindowFlags f &#61; 0);~QtOSGViewerWidget();osgViewer::GraphicsWindow* getGraphicsWindow();const osgViewer::GraphicsWindow* getGraphicsWindow() const;
private:virtual void initializeGL();virtual void resizeGL( int width, int height );virtual void paintGL();
private:osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> m_gw;
};#endif #include "qtosgviewerwidget.h"QtOSGViewerWidget::QtOSGViewerWidget(QWidget *parent): QGLWidget(parent)
{}QtOSGViewerWidget::QtOSGViewerWidget(QWidget * parent , const char * name , const QGLWidget * shareWidget , WindowFlags f )
{m_gw &#61; new osgViewer::GraphicsWindowEmbedded(0,0,width(),height());setFocusPolicy(Qt::ClickFocus);
}QtOSGViewerWidget::~QtOSGViewerWidget()
{
}osgViewer::GraphicsWindow* QtOSGViewerWidget::getGraphicsWindow()
{return m_gw.get();
}const osgViewer::GraphicsWindow* QtOSGViewerWidget::getGraphicsWindow() const
{return m_gw.get();
}void QtOSGViewerWidget::initializeGL()
{glClearColor(1.0, 0.2, 0.3, 1.0);glShadeModel(GL_SMOOTH);glEnable(GL_DEPTH);
}void QtOSGViewerWidget::resizeGL(int width, int height)
{m_gw->getEventQueue()->windowResize(0, 0, width, height );m_gw->resized(0,0,width,height);
}void QtOSGViewerWidget::paintGL()
{}
嵌入Qt窗口
#ifndef QTMYOSGWIDGET_H
#define QTMYOSGWIDGET_H#include
#include "qtosgvrwidget.h"
#include "SkyModel.h"
#include "MyNodeCallback.h"
#include "ui_qtmyosgwidget.h"class QtMyOSGWidget : public QWidget
{Q_OBJECTpublic:QtMyOSGWidget(QWidget *parent &#61; 0);~QtMyOSGWidget();private:Ui::QtMyOSGWidget ui;CMyNodeCallback *m_pmyNode;
};#endif #include
#include "MyOSGCamera.h"
#include "MyNodeCallback.h"
#include "qtmyosgwidget.h"QtMyOSGWidget::QtMyOSGWidget(QWidget *parent): QWidget(parent)
{ui.setupUi(this);QtOSGVRWidget *myVROSG &#61; new QtOSGVRWidget;osg::ref_ptr<osg::Node> loadedModel &#61; osgDB::readNodeFile("F-22.ive");osg::ref_ptr<osg::Node> myF35Node &#61; osgDB::readNodeFile("F35IR.ive");osg::ref_ptr<osg::PositionAttitudeTransform> chposF35 &#61; new osg::PositionAttitudeTransform;osg::Quat quat(osg::DegreesToRadians(30.0), osg::Y_AXIS); chposF35->setAttitude(quat);chposF35->addChild(myF35Node.get());chposF35->setPosition(osg::Vec3(0, 0, 0));chposF35->setName("F-35");CSkyBox mysky;osg::ref_ptr<osg::Node> myNodesky &#61; mysky.createSkyBox();osg::ref_ptr<osg::PositionAttitudeTransform> chposSkyBox &#61; new osg::PositionAttitudeTransform;chposSkyBox->addChild(myNodesky.get());chposSkyBox->setName("skybox");osg::ref_ptr<osg::Group> myroot &#61; new osg::Group;myroot->addChild(loadedModel);myroot->addChild(chposF35.get());myroot->addChild(myNodesky.get());myVROSG->setCameraManipulator(new MyOSGCamera);m_pmyNode &#61; new CMyNodeCallback();myroot->addEventCallback(m_pmyNode);myVROSG->setSceneData(myroot.get());QGridLayout *playout &#61; new QGridLayout;playout->addWidget(myVROSG);setLayout(playout);
}QtMyOSGWidget::~QtMyOSGWidget()
{}
include.h
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include #ifdef _DEBUG
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgWidgetd.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "osgShadowd.lib")
#pragma comment(lib, "osgAnimationd.lib")
#pragma comment(lib, "osgParticled.lib")
#pragma comment(lib, "osgQtd.lib")
#else
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgViewer.lib")
#pragma comment(lib, "OpenThreads.lib")
#pragma comment(lib, "osgGA.lib")
#pragma comment(lib, "osgUtil.lib")
#pragma comment(lib, "osgText.lib")
#pragma comment(lib, "osgWidget.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "osgShadow.lib")
#pragma comment(lib, "osgAnimation.lib")
#pragma comment(lib, "osgParticle.lib")
#pragma comment(lib, "osgQt.lib")
#endif
运行效果图&#xff1a;