雪效模拟示例
雪效模拟示例的代码如程序清单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雨效示例截图