OSG粒子系统与阴影-雨效、雪效模拟(2)

雪效模拟示例

        雪效模拟示例的代码如程序清单11-2所示:

1.	/* 雪效模拟示例 */  
2.	void snow_11_2(const string &strDataFolder)  
3.	{  
4.	    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();  
5.	    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
6.	    traits->x = 40;  
7.	    traits->y = 40;  
8.	    traits->width = 600;  
9.	    traits->height = 480;  
10.	    traits->windowDecoration = true;  
11.	    traits->doubleBuffer = true;  
12.	    traits->sharedContext = 0;  
13.	  
14.	    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());  
15.	  
16.	    osg::ref_ptr<osg::Camera> camera = viewer->getCamera();  
17.	    camera->setGraphicsContext(gc.get());  
18.	    camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));  
19.	    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;  
20.	    camera->setDrawBuffer(buffer);  
21.	    camera->setReadBuffer(buffer);  
22.	  
23.	    osg::ref_ptr<osg::Group> root = new osg::Group();  
24.	  
25.	    // 申请一个粒子系统  
26.	    osg::ref_ptr<osgParticle::PrecipitationEffect> pe = new osgParticle::PrecipitationEffect();  
27.	  
28.	    // 设置雪效的浓度为0.5  
29.	    pe->snow(0.5);  
30.	  
31.	    // 设置背景色  
32.	    camera->setClearColor(pe->getFog()->getColor());  
33.	  
34.	    // 读取模型  
35.	    string strDataPath = strDataFolder + "lz.osg";  
36.	    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strDataPath);  
37.	      
38.	    // 使用雾效  
39.	    //node->getOrCreateStateSet()->setAttributeAndModes(pe->getFog());  
40.	  
41.	    root->addChild(node.get());  
42.	    root->addChild(pe.get());  
43.	  
44.	    // 优化场景数据  
45.	    osgUtil::Optimizer optimize;  
46.	    optimize.optimize(root.get());  
47.	  
48.	    viewer->setSceneData(root.get());  
49.	  
50.	    viewer->realize();  
51.	    viewer->run();  
52.	}  

        运行程序,截图如图11-4 所示。

图11-4雪效模拟示例图

 

​​​​​​​雨效模拟示例

        雨效模拟示例的代码如程序清单11-3所示:

1.	/* 雨效模拟示例 */  
2.	void rain_11_3(const string &strDataFolder)  
3.	{  
4.	    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();  
5.	    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
6.	    traits->x = 40;  
7.	    traits->y = 40;  
8.	    traits->width = 600;  
9.	    traits->height = 480;  
10.	    traits->windowDecoration = true;  
11.	    traits->doubleBuffer = true;  
12.	    traits->sharedContext = 0;  
13.	  
14.	    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());  
15.	  
16.	    osg::ref_ptr<osg::Camera> camera = viewer->getCamera();  
17.	    camera->setGraphicsContext(gc.get());  
18.	    camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));  
19.	    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;  
20.	    camera->setDrawBuffer(buffer);  
21.	    camera->setReadBuffer(buffer);  
22.	  
23.	    osg::ref_ptr<osg::Group> root = new osg::Group();  
24.	  
25.	    // 申请一个粒子系统  
26.	    osg::ref_ptr<osgParticle::PrecipitationEffect> pe = new osgParticle::PrecipitationEffect();  
27.	  
28.	    // 设置雨效的浓度为0.5  
29.	    pe->rain(1.5);  
30.	  
31.	    // 设置背景色  
32.	    camera->setClearColor(pe->getFog()->getColor());  
33.	  
34.	    // 读取模型  
35.	    string strDataPath = strDataFolder + "lz.osg";  
36.	    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strDataPath);  
37.	  
38.	    // 使用雾效  
39.	    //node->getOrCreateStateSet()->setAttributeAndModes(pe->getFog());  
40.	  
41.	    root->addChild(node.get());  
42.	    root->addChild(pe.get());  
43.	  
44.	    // 优化场景数据  
45.	    osgUtil::Optimizer optimize;  
46.	    optimize.optimize(root.get());  
47.	  
48.	    viewer->setSceneData(root.get());  
49.	  
50.	    viewer->realize();  
51.	    viewer->run();  
52.	}  

        运行程序,截图如图11-5所示

图11-5雨效示例截图

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听风者868

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值