OpenSceneGraph实现的NeHe OpenGL教程 - 第四十一课

  • 简介

这节课也是在场景中添加雾效的效果,与之类似的是第十六课,所不同的是这节课使用了雾坐标来计算雾效。为了能运行这一课,你的显卡必须支持GL_EXT_fot_coord扩展。

  • 实现

这节课的实现过程比较简单,在OSG中为几何体的顶点封装好了顶点的各项属性,包括顶点坐标、顶点纹理坐标、顶点颜色,当然也包括本课中的雾坐标。

首先我们还是添加顶点的各项数据:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     osg::Geode *geode = new osg::Geode;  
  2.     osg::Geometry *geometry = new osg::Geometry;  
  3.     osg::Vec3Array *vertexArray = new osg::Vec3Array;  
  4.     osg::FloatArray *fogArray = new osg::FloatArray;  
  5.     osg::Vec2Array *textureArray = new osg::Vec2Array;  
  6.         vertexArray->push_back(osg::Vec3(-2.5f,-2.5f,-15.0f));  
  7.         vertexArray->push_back(osg::Vec3(2.5f,-2.5f,-15.0f));  
  8.         vertexArray->push_back(osg::Vec3(2.5f,2.5f,-15.0f));  
  9.         vertexArray->push_back(osg::Vec3(-2.5f,2.5f,-15.0f));  
  10.         textureArray->push_back(osg::Vec2(0.0f, 0.0f));  
  11.         textureArray->push_back(osg::Vec2(1.0f, 0.0f));  
  12.         textureArray->push_back(osg::Vec2(1.0f, 1.0f));  
  13.         textureArray->push_back(osg::Vec2(0.0f, 1.0f));  
  14.         fogArray->push_back(1.0f);  
  15.         fogArray->push_back(1.0f);  
  16.         fogArray->push_back(1.0f);  
  17.         fogArray->push_back(1.0f);  
  18.         ......  

接着读取纹理:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::Texture2D *texture2D = new osg::Texture2D;  
  2. texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);  
  3. texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  4. texture2D->setImage(osgDB::readImageFile("Data/Wall.bmp"));  
  5. geometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);  

启用雾效特效:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::Fog *fog = new osg::Fog;  
  2. fog->setMode(osg::Fog::LINEAR);  
  3. fog->setColor(osg::Vec4(fogColor[0], fogColor[1],fogColor[2],fogColor[3]));  
  4. fog->setStart(0.0f);  
  5. fog->setEnd(1.0f);  
  6. fog->setFogCoordinateSource(osg::Fog::FOG_COORDINATE);  

编译运行程序:


附:本课源码(源码中可能存在错误和不足,仅供参考)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "../osgNeHe.h"  
  2.   
  3. #include <QtCore/QTimer>  
  4. #include <QtGui/QApplication>  
  5. #include <QtGui/QVBoxLayout>  
  6.   
  7. #include <osgViewer/Viewer>  
  8. #include <osgDB/ReadFile>  
  9. #include <osgQt/GraphicsWindowQt>  
  10.   
  11. #include <osg/MatrixTransform>  
  12. #include <osg/Texture2D>  
  13. #include <osg/Fog>  
  14.   
  15.   
  16. GLfloat fogColor[4] = {0.6f, 0.3f, 0.0f, 1.0f};  
  17.   
  18. osg::MatrixTransform *g_Trans = NULL;  
  19.   
  20. class ManipulatorSceneHandler : public osgGA::GUIEventHandler  
  21. {  
  22. public:  
  23.     ManipulatorSceneHandler()  
  24.     {  
  25.     }  
  26.   
  27. public:  
  28.     virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)  
  29.     {  
  30.         osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*>(&aa);  
  31.         if (!viewer)  
  32.             return false;  
  33.         if (!viewer->getSceneData())  
  34.             return false;  
  35.         if (ea.getHandled())   
  36.             return false;  
  37.   
  38.         switch (ea.getEventType())  
  39.         {  
  40.         case(osgGA::GUIEventAdapter::KEYDOWN):  
  41.             {  
  42.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)  
  43.                 {  
  44.                     if (!g_Trans)  
  45.                         return false;  
  46.   
  47.                     osg::Vec3 trans = g_Trans->getMatrix().getTrans();  
  48.                     if(trans.z() < 14.0f)  
  49.                         trans += osg::Vec3(0,0,1);  
  50.   
  51.                     g_Trans->setMatrix(osg::Matrix::translate(trans));  
  52.                 }  
  53.   
  54.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Down)  
  55.                 {  
  56.                     if (!g_Trans)  
  57.                         return false;  
  58.   
  59.                     osg::Vec3 trans = g_Trans->getMatrix().getTrans();  
  60.                     if(trans.z() >-19.0f)  
  61.                         trans -= osg::Vec3(0,0,1);  
  62.   
  63.                     g_Trans->setMatrix(osg::Matrix::translate(trans));  
  64.                 }  
  65.             }  
  66.             break;  
  67.   
  68.         defaultbreak;  
  69.         }  
  70.         return false;  
  71.         }  
  72. };  
  73.   
  74.   
  75.   
  76.   
  77.   
  78. class ViewerWidget : public QWidget, public osgViewer::Viewer  
  79. {  
  80. public:  
  81.     ViewerWidget(osg::Node *scene = NULL)  
  82.     {  
  83.         QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);  
  84.   
  85.         QVBoxLayout* layout = new QVBoxLayout;  
  86.         layout->addWidget(renderWidget);  
  87.         layout->setContentsMargins(0, 0, 0, 1);  
  88.         setLayout( layout );  
  89.   
  90.         connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );  
  91.         _timer.start( 10 );  
  92.     }  
  93.   
  94.     QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )  
  95.     {  
  96.         osg::Camera* camera = this->getCamera();  
  97.         camera->setGraphicsContext( gw );  
  98.   
  99.         const osg::GraphicsContext::Traits* traits = gw->getTraits();  
  100.   
  101.         camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 0.5) );  
  102.         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );  
  103.         camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );  
  104.         camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 0), osg::Vec3d(0, 0, -1), osg::Vec3d(0, 1, 0));  
  105.   
  106.         this->setSceneData( scene );  
  107.         this->addEventHandler(new ManipulatorSceneHandler);  
  108.         return gw->getGLWidget();  
  109.     }  
  110.   
  111.     osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name=""bool windowDecoration=false )  
  112.     {  
  113.         osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();  
  114.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  115.         traits->windowName = name;  
  116.         traits->windowDecoration = windowDecoration;  
  117.         traits->x = x;  
  118.         traits->y = y;  
  119.         traits->width = w;  
  120.         traits->height = h;  
  121.         traits->doubleBuffer = true;  
  122.         traits->alpha = ds->getMinimumNumAlphaBits();  
  123.         traits->stencil = ds->getMinimumNumStencilBits();  
  124.         traits->sampleBuffers = ds->getMultiSamples();  
  125.         traits->samples = ds->getNumMultiSamples();  
  126.   
  127.         return new osgQt::GraphicsWindowQt(traits.get());  
  128.     }  
  129.   
  130.     virtual void paintEvent( QPaintEvent* event )  
  131.     {   
  132.         frame();   
  133.     }  
  134.   
  135. protected:  
  136.   
  137.     QTimer _timer;  
  138. };  
  139.   
  140.   
  141.   
  142. osg::Node*  buildScene()  
  143. {  
  144.     osg::Group *root = new osg::Group;  
  145.   
  146.     osg::MatrixTransform *zoomMT = new osg::MatrixTransform;  
  147.     zoomMT->setMatrix(osg::Matrix::translate(0,0,-19.0));  
  148.     g_Trans = zoomMT;  
  149.   
  150.     osg::Geode *geode = new osg::Geode;  
  151.     osg::Geometry *geometry = new osg::Geometry;  
  152.     osg::Vec3Array *vertexArray = new osg::Vec3Array;  
  153.     osg::FloatArray *fogArray = new osg::FloatArray;  
  154.     osg::Vec2Array *textureArray = new osg::Vec2Array;  
  155.   
  156.     vertexArray->push_back(osg::Vec3(-2.5f,-2.5f,-15.0f));  
  157.     vertexArray->push_back(osg::Vec3(2.5f,-2.5f,-15.0f));  
  158.     vertexArray->push_back(osg::Vec3(2.5f,2.5f,-15.0f));  
  159.     vertexArray->push_back(osg::Vec3(-2.5f,2.5f,-15.0f));  
  160.     textureArray->push_back(osg::Vec2(0.0f, 0.0f));  
  161.     textureArray->push_back(osg::Vec2(1.0f, 0.0f));  
  162.     textureArray->push_back(osg::Vec2(1.0f, 1.0f));  
  163.     textureArray->push_back(osg::Vec2(0.0f, 1.0f));  
  164.     fogArray->push_back(1.0f);  
  165.     fogArray->push_back(1.0f);  
  166.     fogArray->push_back(1.0f);  
  167.     fogArray->push_back(1.0f);  
  168.   
  169.     vertexArray->push_back(osg::Vec3(-2.5f,-2.5f,-15.0f));  
  170.     vertexArray->push_back(osg::Vec3(2.5f,-2.5f,-15.0f));  
  171.     vertexArray->push_back(osg::Vec3(2.5f,-2.5f,15.0f));  
  172.     vertexArray->push_back(osg::Vec3(-2.5f,-2.5f,15.0f));  
  173.     textureArray->push_back(osg::Vec2(0.0f, 0.0f));  
  174.     textureArray->push_back(osg::Vec2(1.0f, 0.0f));  
  175.     textureArray->push_back(osg::Vec2(1.0f, 1.0f));  
  176.     textureArray->push_back(osg::Vec2(0.0f, 1.0f));  
  177.     fogArray->push_back(1.0f);  
  178.     fogArray->push_back(1.0f);  
  179.     fogArray->push_back(0.0f);  
  180.     fogArray->push_back(0.0f);  
  181.   
  182.     vertexArray->push_back(osg::Vec3(-2.5f,2.5f,-15.0f));  
  183.     vertexArray->push_back(osg::Vec3(2.5f,2.5f,-15.0f));  
  184.     vertexArray->push_back(osg::Vec3(2.5f,2.5f,15.0f));  
  185.     vertexArray->push_back(osg::Vec3(-2.5f,2.5f,15.0f));  
  186.     textureArray->push_back(osg::Vec2(0.0f, 0.0f));  
  187.     textureArray->push_back(osg::Vec2(1.0f, 0.0f));  
  188.     textureArray->push_back(osg::Vec2(1.0f, 1.0f));  
  189.     textureArray->push_back(osg::Vec2(0.0f, 1.0f));  
  190.     fogArray->push_back(1.0f);  
  191.     fogArray->push_back(1.0f);  
  192.     fogArray->push_back(0.0f);  
  193.     fogArray->push_back(0.0f);  
  194.   
  195.     vertexArray->push_back(osg::Vec3(2.5f,-2.5f,15.0f));  
  196.     vertexArray->push_back(osg::Vec3(2.5f,2.5f,15.0f));  
  197.     vertexArray->push_back(osg::Vec3(2.5f,2.5f,-15.0f));  
  198.     vertexArray->push_back(osg::Vec3(2.5f,-2.5f,-15.0f));  
  199.     textureArray->push_back(osg::Vec2(0.0f, 0.0f));  
  200.     textureArray->push_back(osg::Vec2(0.0f, 1.0f));  
  201.     textureArray->push_back(osg::Vec2(1.0f, 1.0f));  
  202.     textureArray->push_back(osg::Vec2(1.0f, 0.0f));  
  203.     fogArray->push_back(0.0f);  
  204.     fogArray->push_back(0.0f);  
  205.     fogArray->push_back(1.0f);  
  206.     fogArray->push_back(1.0f);  
  207.   
  208.     vertexArray->push_back(osg::Vec3(-2.5f,-2.5f,15.0f));  
  209.     vertexArray->push_back(osg::Vec3(-2.5f,2.5f,15.0f));  
  210.     vertexArray->push_back(osg::Vec3(-2.5f,2.5f,-15.0f));  
  211.     vertexArray->push_back(osg::Vec3(-2.5f,-2.5f,-15.0f));  
  212.     textureArray->push_back(osg::Vec2(0.0f, 0.0f));  
  213.     textureArray->push_back(osg::Vec2(0.0f, 1.0f));  
  214.     textureArray->push_back(osg::Vec2(1.0f, 1.0f));  
  215.     textureArray->push_back(osg::Vec2(1.0f, 0.0f));  
  216.     fogArray->push_back(0.0f);  
  217.     fogArray->push_back(0.0f);  
  218.     fogArray->push_back(1.0f);  
  219.     fogArray->push_back(1.0f);  
  220.   
  221.     geometry->setVertexArray(vertexArray);  
  222.     geometry->setTexCoordArray(0, textureArray);  
  223.     geometry->setFogCoordArray(fogArray, osg::Array::BIND_PER_VERTEX);  
  224.     geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  225.       
  226.     osg::Texture2D *texture2D = new osg::Texture2D;  
  227.     texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);  
  228.     texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  229.     texture2D->setImage(osgDB::readImageFile("Data/Wall.bmp"));  
  230.     geometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);  
  231.   
  232.     osg::Fog *fog = new osg::Fog;  
  233.     fog->setMode(osg::Fog::LINEAR);  
  234.     fog->setColor(osg::Vec4(fogColor[0], fogColor[1],fogColor[2],fogColor[3]));  
  235.     fog->setStart(0.0f);  
  236.     fog->setEnd(1.0f);  
  237.     fog->setFogCoordinateSource(osg::Fog::FOG_COORDINATE);  
  238.       
  239.     geometry->getOrCreateStateSet()->setAttributeAndModes(fog);  
  240.   
  241.     geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, vertexArray->size()));  
  242.     geode->addDrawable(geometry);  
  243.   
  244.     zoomMT->addChild(geode);  
  245.     root->addChild(zoomMT);  
  246.   
  247.     return root;  
  248. }  
  249.   
  250.   
  251.   
  252. int main( int argc, char** argv )  
  253. {  
  254.     QApplication app(argc, argv);  
  255.     ViewerWidget* viewWidget = new ViewerWidget(buildScene());  
  256.     viewWidget->setGeometry( 100, 100, 640, 480 );  
  257.     viewWidget->show();  
  258.     return app.exec();  
  259. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值