Osg-Osg海军教程源代码之金子塔的绘制(Qt5.14.2+osgEarht3.6.5+win10)-No1-DrawGoldTower
相关资料:
https://blog.csdn.net/u012130706/article/details/76044498 【OSG】将显示的图形窗口化
实例代码:
.pro
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 QT += core gui widgets 2 TARGET = TestOsgQt 3 TEMPLATE = app 4 DEFINES += QT_DEPRECATED_WARNINGS 5 CONFIG += c++11 6 7 SOURCES += \ 8 main.cpp 9 10 HEADERS += 11 12 OsgDir = D:\\RuanJian\\osg365R 13 CONFIG(release, debug|release) { 14 LIBS += -L$${OsgDir}/lib/ -losgDB -losgViewer -lOpenThreads -losgAnimation -losg \ 15 -losgEarth -losgEarthAnnotation -losgEarthFeatures -losgEarthSymbology -losgEarthUtil \ 16 -losgQOpenGL -losgUtil -losgText -losgTerrain -losgSim \ 17 -losgShadow -losgParticle -losgManipulator -losgGA -losgFX \ 18 -losgWidget 19 } else { 20 LIBS += -L$${OsgDir}/debug/lib/ -losgDBd -losgViewerd -lOpenThreadsd -losgAnimationd -losgd \ 21 -losgEarthd -losgEarthAnnotationd -losgEarthFeaturesd -losgEarthSymbologyd -losgEarthUtild \ 22 -losgQOpenGLd -losgUtild -losgTextd -losgTerraind -losgSimd \ 23 -losgShadowd -losgParticled -losgManipulatord -losgGAd -losgFXd \ 24 } 25 26 27 INCLUDEPATH += $${OsgDir}/include 28 DEPENDPATH += $${OsgDir}/include
main.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 // 雨雪效果 15 #include <osg/MatrixTransform> 16 // 粒子效果 17 #include <osgParticle/PrecipitationEffect> 18 #include <osgParticle/Particle> 19 #include <osgParticle/LinearInterpolator> 20 #include <osgParticle/ParticleSystem> 21 #include <osgParticle/RandomRateCounter> 22 #include <osgParticle/PointPlacer> 23 #include <osgParticle/RadialShooter> 24 #include <osgParticle/ModularEmitter> 25 #include <osgParticle/ParticleSystemUpdater> 26 #include <osgParticle/ModularProgram> 27 #include <osgUtil/Optimizer> 28 #include <osgUtil/Simplifier> 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 int main(int argc, char *argv[]) 39 { 40 osg::Group* root = new osg::Group(); 41 osg::Geode* pyramidGeode = new osg::Geode(); 42 osg::Geometry* pyramidGeometry = new osg::Geometry(); 43 pyramidGeode->addDrawable(pyramidGeometry); 44 root->addChild(pyramidGeode); 45 osg::Vec3Array* pyramidVertices = new osg::Vec3Array; 46 pyramidVertices->push_back( osg::Vec3( 0, 0, 0) ); // 左前 47 pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); // 右前 48 pyramidVertices->push_back( osg::Vec3(10,10, 0) ); // 右后 49 pyramidVertices->push_back( osg::Vec3( 0,10, 0) ); // 左后 50 pyramidVertices->push_back( osg::Vec3( 5, 5,10) ); // 塔尖 51 pyramidGeometry->setVertexArray( pyramidVertices ); 52 osg::DrawElementsUInt* pyramidBase = 53 new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0); 54 pyramidBase->push_back(3); 55 pyramidBase->push_back(2); 56 pyramidBase->push_back(1); 57 pyramidBase->push_back(0); 58 pyramidGeometry->addPrimitiveSet(pyramidBase); 59 //重复这一过程,添加金字塔的每个面。再次注意,顶点是以逆时针为顺序添加的。 60 osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); 61 pyramidFaceOne->push_back(0); 62 pyramidFaceOne->push_back(1); 63 pyramidFaceOne->push_back(4); 64 pyramidGeometry->addPrimitiveSet(pyramidFaceOne); 65 66 osg::DrawElementsUInt* pyramidFaceTwo = 67 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); 68 pyramidFaceTwo->push_back(1); 69 pyramidFaceTwo->push_back(2); 70 pyramidFaceTwo->push_back(4); 71 pyramidGeometry->addPrimitiveSet(pyramidFaceTwo); 72 73 osg::DrawElementsUInt* pyramidFaceThree = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); 74 pyramidFaceThree->push_back(2); 75 pyramidFaceThree->push_back(3); 76 pyramidFaceThree->push_back(4); 77 pyramidGeometry->addPrimitiveSet(pyramidFaceThree); 78 79 osg::DrawElementsUInt* pyramidFaceFour = 80 new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); 81 pyramidFaceFour->push_back(3); 82 pyramidFaceFour->push_back(0); 83 pyramidFaceFour->push_back(4); 84 pyramidGeometry->addPrimitiveSet(pyramidFaceFour); 85 //定义一个Vec4的数组,用于保存颜色值。 86 osg::Vec4Array* colors = new osg::Vec4Array; 87 colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //索引0 红色 88 colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //索引1 绿色 89 colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //索引2 蓝色 90 colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //索引3 白色 91 osg::TemplateIndexArray 92 <unsigned int, osg::Array::UIntArrayType,4,4> *colorIndexArray; 93 colorIndexArray = 94 new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4>; 95 colorIndexArray->push_back(0); // vertex 0 assigned color array element 0 96 colorIndexArray->push_back(1); // vertex 1 assigned color array element 1 97 colorIndexArray->push_back(2); // vertex 2 assigned color array element 2 98 colorIndexArray->push_back(3); // vertex 3 assigned color array element 3 99 colorIndexArray->push_back(0); // vertex 4 assigned color array element 0 100 pyramidGeometry->setColorArray(colors); 101 // pyramidGeometry->setColorIndices(colorIndexArray); 102 pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 103 104 osg::Vec2Array* texcoords = new osg::Vec2Array(5); 105 (*texcoords)[0].set(0.00f,0.0f); 106 (*texcoords)[1].set(0.25f,0.0f); 107 (*texcoords)[2].set(0.50f,0.0f); 108 (*texcoords)[3].set(0.75f,0.0f); 109 (*texcoords)[4].set(0.50f,1.0f); 110 pyramidGeometry->setTexCoordArray(0,texcoords); 111 //初始化位置变换节点 112 osg::PositionAttitudeTransform* pyramidTwoXForm =new osg::PositionAttitudeTransform(); 113 114 //使用osg::Group的addChild方法,将位置变换节点添加到根节点的子节点上,并将金字塔节点作为变换节点的子节点 115 root->addChild(pyramidTwoXForm); 116 pyramidTwoXForm->addChild(pyramidGeode); 117 118 // 初始化一个Vec3实例,用于改变模型在场景中的位置 119 osg::Vec3 pyramidTwoPosition(15,0,0); 120 pyramidTwoXForm->setPosition( pyramidTwoPosition ); 121 122 //最后,设置视窗类并进入仿真循环。 123 osgViewer::Viewer viewer; 124 //设置图形环境特性 125 osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits(); 126 traits->x = 100; 127 traits->y = 200; 128 traits->width = 600; 129 traits->height = 500; 130 traits->windowDecoration = true; 131 traits->doubleBuffer = true; 132 traits->sharedContext = 0; 133 //创建图形环境特性 134 osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 135 if (gc.valid()) 136 { 137 osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl; 138 139 //清除窗口颜色及清除颜色和深度缓冲 140 gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); 141 gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 142 } 143 else 144 { 145 osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl; 146 } 147 //设置视口 148 viewer.getCamera()->setViewport(new osg::Viewport(0,0, traits->width, traits->height)); 149 //设置图形环境 150 viewer.getCamera()->setGraphicsContext(gc.get()); 151 viewer.setSceneData( root ); 152 return viewer.run(); 153 }
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417075318%E5%8A%A0%E6%88%91QQ.jpg)
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417080925%E5%8A%A0%E6%88%91%E5%BE%AE%E4%BF%A1.jpg)