相交资料:
https://www.cnblogs.com/coolbear/p/8578677.html
https://blog.csdn.net/wang15061955806/article/details/51039177
https://www.freesion.com/article/98571463684/
https://download.csdn.net/download/zhujianqiangqq/86398450 CSDN代码包下载
实例代码:
.pro
1 QT += core gui widgets 2 QT += opengl 3 TARGET = TestOsgQt 4 TEMPLATE = app 5 DEFINES += QT_DEPRECATED_WARNINGS 6 CONFIG += c++11 7 8 SOURCES += \ 9 main.cpp 10 11 HEADERS += 12 13 OsgDir = D:\\Gitee\\osg365R 14 CONFIG(release, debug|release) { 15 LIBS += -L$${OsgDir}/lib/ -losgDB -losgViewer -lOpenThreads -losgAnimation -losg \ 16 -losgEarth -losgEarthAnnotation -losgEarthFeatures -losgEarthSymbology -losgEarthUtil \ 17 -losgQOpenGL -losgUtil -losgText -losgTerrain -losgSim \ 18 -losgShadow -losgParticle -losgManipulator -losgGA -losgFX \ 19 -losgWidget 20 } else { 21 LIBS += -L$${OsgDir}/debug/lib/ -losgDBd -losgViewerd -lOpenThreadsd -losgAnimationd -losgd \ 22 -losgEarthd -losgEarthAnnotationd -losgEarthFeaturesd -losgEarthSymbologyd -losgEarthUtild \ 23 -losgQOpenGLd -losgUtild -losgTextd -losgTerraind -losgSimd \ 24 -losgShadowd -losgParticled -losgManipulatord -losgGAd -losgFXd \ 25 } 26 27 LIBS += -lOpenGL32 28 LIBS += -lGlU32 29 30 INCLUDEPATH += $${OsgDir}/include 31 DEPENDPATH += $${OsgDir}/include
main.cpp
1 #include <QApplication> 2 3 #include <osg/Node> 4 #include <osg/Group> 5 #include <osg/Geode> 6 #include <osg/Geometry> 7 #include <osg/Texture2D> 8 #include <osg/StateSet> 9 #include <osg/PositionAttitudeTransform> 10 #include <osgViewer/Viewer> 11 #include <osgDB/ReadFile> 12 #include <osgParticle/PrecipitationEffect> 13 // 雨雪效果 14 #include <osg/MatrixTransform> 15 // 粒子效果 16 #include <osgParticle/PrecipitationEffect> 17 #include <osgParticle/Particle> 18 #include <osgParticle/LinearInterpolator> 19 #include <osgParticle/ParticleSystem> 20 #include <osgParticle/RandomRateCounter> 21 #include <osgParticle/PointPlacer> 22 #include <osgParticle/RadialShooter> 23 #include <osgParticle/ModularEmitter> 24 #include <osgParticle/ParticleSystemUpdater> 25 #include <osgParticle/ModularProgram> 26 #include <osgUtil/Optimizer> 27 #include <osgUtil/Simplifier> 28 #include <osgParticle/FireEffect> 29 // 雾 30 #include <osg/Fog> 31 #include <osgDB/ReadFile> 32 #include <osgViewer/Viewer> 33 #include <osg/StateSet> 34 #include <osg/StateAttribute> 35 #include <osgViewer/ViewerEventHandlers> 36 #include <osgWidget/ViewerEventHandlers> 37 // 38 #include <QDebug> 39 40 class MyDrawCallback : public osg::Drawable::DrawCallback 41 { 42 public: 43 void drawImplementation(osg::RenderInfo &renderInfo, const osg::Drawable *drawable) const 44 { 45 static double dColor = 0;//颜色 46 glColor3f(dColor, 0, 0); 47 glBegin(GL_TRIANGLES);//在OSG中画一个opengl三角形 48 glVertex3f(0.0, 0.0, -2.0); 49 glVertex3f(0.2, 0.0, -2.0); 50 glVertex3f(0.0, 0.4, -2.0); 51 glEnd(); 52 dColor += 0.01;//颜色渐变 53 if (dColor > 1.0) 54 { 55 dColor = 0.0; 56 } 57 } 58 }; 59 60 int main(int argc, char *argv[]) 61 { 62 osgViewer::Viewer viewer; 63 osg::Geometry* geometry = new osg::Geometry; 64 //此处一定要把显示列表设置为false, 65 //否则DrawCallback的drawImplementation()函数只会调用一次,而不是在画一帧时都动态更新opengl图形 66 geometry->setUseDisplayList(false); 67 geometry->setDrawCallback(new MyDrawCallback);//Drawable设置动态更新opengl图形 68 osg::Geode* geode = new osg::Geode; 69 geode->addDrawable(geometry); 70 geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); 71 osg::Group* group = new osg::Group; 72 group->addChild(geode); 73 viewer.setSceneData(group); 74 viewer.setUpViewInWindow(35, 35, 500, 500); 75 // return viewer.run(); 76 osg::Matrix mt; 77 mt.makeIdentity(); 78 while (!viewer.done()) 79 { 80 viewer.getCamera()->setViewMatrix(mt); 81 viewer.frame(); 82 } 83 }