Osg-Osg实例实现环境雨效果(Qt5.14.2+osgEarht3.6.5+win10)-No3-EnvironmentRain


相关资料:

https://blog.csdn.net/chlk118/article/details/47009027?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control

代码实例:

.pro

 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

 1 #include 
 2 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 #include 
 8 #include 
 9 #include 
10 #include 
11 #include 
12 #include 
13 // 雨雪效果
14 #include 
15 // 粒子效果
16 #include 
17 #include 
18 #include 
19 #include 
20 #include 
21 #include 
22 #include 
23 #include 
24 #include 
25 #include 
26 #include 
27 #include 
28 //
29 #include 
30 #include 
31 #include 
32 #include 
33 #include 
34 #include 
35 #include 
36 
37 int main(int argc, char *argv[])
38 {
39     osg::Group* root = new osg::Group();
40     // ------------------雨----------------------
41     osg::Matrixd matrixEffect;
42     matrixEffect.makeTranslate(osg::Vec3d(0.0, 1.0, 0.0));
43     // 设置粒子位置
44     osg::ref_ptr trans = new osg::MatrixTransform;
45     // 对粒子范围进行了放大
46     trans->setMatrix(matrixEffect * osg::Matrixd::scale(100, 100, 100));
47     // 创建雨粒子
48     osg::ref_ptr pe =
49     new osgParticle::PrecipitationEffect;
50     pe->rain(1.0);
51     pe->setUseFarLineSegments(true);
52     // iLevel参数是一个int值,表示雨的级别,一般1-10就够用了
53     int iLevel = 1;
54     pe->setParticleSize(iLevel / 10.0);
55     // 设置颜色
56     pe->setParticleColor(osg::Vec4(1, 1, 1, 1));
57 
58     trans->addChild(pe);
59     root->addChild(trans);
60     // ---------------设置窗体口显示-----------
61     //最后,设置视窗类并进入仿真循环。
62     osgViewer::Viewer viewer;
63     //设置图形环境特性
64     osg::ref_ptr traits = new osg::GraphicsContext::Traits();
65     traits->x = 100;
66     traits->y = 200;
67     traits->width = 600;
68     traits->height = 500;
69     traits->windowDecoration = true;
70     traits->doubleBuffer = true;
71     traits->sharedContext = 0;
72     //创建图形环境特性
73     osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits.get());
74     if (gc.valid())
75     {
76         osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
77 
78         //清除窗口颜色及清除颜色和深度缓冲
79         gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
80         gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
81     }
82     else
83     {
84         osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
85     }
86     //设置视口
87     viewer.getCamera()->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
88     //设置图形环境
89     viewer.getCamera()->setGraphicsContext(gc.get());
90     viewer.setSceneData( root );
91     return viewer.run();
92 }

相关