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

来源:互联网 发布:服装市场数据 编辑:程序博客网 时间:2024/06/07 22:24
  • 简介

本节课实现了在场景中添加雾效,在OpenGL中添加雾效使用glFog函数来设置,在OSG中也十分的简单,雾效作为一个StateAttribute来添加到节点的。

  • 实现

本节课在第七课的基础上添加少量代码,主要是设置雾效

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. osg::Fog *fog = new osg::Fog;  
  2. fog->setMode(osg::Fog::EXP);  
  3. fog->setColor(osg::Vec4(0.5f,0.5f,0.5f,1.0f));  
  4. fog->setDensity(0.35f);  
  5. fog->setStart(1.0f);  
  6. fog->setEnd(5.0f);  
雾的参数设置和NeHe教程中的完全一致,接下来在响应键盘G键的时候修改雾效计算方式,通过切换LINEAR、EXP、EXP2来修改场景中雾的浓密程度。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (ea.getKey()== osgGA::GUIEventAdapter::KEY_G)  
  2. {  
  3.         static int i = 0;  
  4.         if (i % 3 == 0) {  
  5.             g_fog->setMode(osg::Fog::EXP2);  
  6.         } else if (i % 3 == 1) {  
  7.             g_fog->setMode(osg::Fog::LINEAR);  
  8.         } else {  
  9.             g_fog->setMode(osg::Fog::EXP);  
  10.         }  
  11.         ++i;  
  12. }  

余下代码和第七课相同。编译运行程序:

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

[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/NodeVisitor>  
  13. #include <osg/Texture2D>  
  14.   
  15. #include <osgGA/GUIEventAdapter>  
  16.   
  17. #include <osg/Light>  
  18. #include <osg/LightModel>  
  19. #include <osg/LightSource>  
  20.   
  21. #include <osg/Fog>  
  22.   
  23. osg::Fog *g_fog;  
  24.   
  25. float textureVertices[][2] = {  
  26.     //Front Face  
  27.     {0.0f, 0.0f},  
  28.     {1.0f, 0.0f},  
  29.     {1.0f, 1.0f},  
  30.     {0.0f, 1.0f},  
  31.     // Back Face  
  32.     {1.0f, 0.0f},  
  33.     {1.0f, 1.0f},  
  34.     {0.0f, 1.0f},  
  35.     {0.0f, 0.0f},  
  36.     // Top Face  
  37.     {0.0f, 1.0f},  
  38.     {0.0f, 0.0f},  
  39.     {1.0f, 0.0f},  
  40.     {1.0f, 1.0f},  
  41.     // Bottom Face  
  42.     {1.0f, 1.0f},  
  43.     {0.0f, 1.0f},  
  44.     {0.0f, 0.0f},  
  45.     {1.0f, 0.0f},  
  46.     // Right face  
  47.     {1.0f, 0.0f},  
  48.     {1.0f, 1.0f},  
  49.     {0.0f, 1.0f},  
  50.     {0.0f, 0.0f},  
  51.     // Left Face  
  52.     {0.0f, 0.0f},  
  53.     {1.0f, 0.0f},  
  54.     {1.0f, 1.0f},  
  55.     {0.0f, 1.0f}  
  56. };  
  57.   
  58. float QuadVertices[][3] = {  
  59.   
  60.     {-1.0f, -1.0f,  1.0f},  
  61.     { 1.0f, -1.0f,  1.0f},  
  62.     { 1.0f,  1.0f,  1.0f},  
  63.     {-1.0f,  1.0f,  1.0f},  
  64.   
  65.     {-1.0f, -1.0f, -1.0f},  
  66.     {-1.0f,  1.0f, -1.0f},  
  67.     { 1.0f,  1.0f, -1.0f},  
  68.     { 1.0f, -1.0f, -1.0f},  
  69.   
  70.     {-1.0f,  1.0f, -1.0f},  
  71.     {-1.0f,  1.0f,  1.0f},  
  72.     { 1.0f,  1.0f,  1.0f},  
  73.     { 1.0f,  1.0f, -1.0f},  
  74.   
  75.     {-1.0f, -1.0f, -1.0f},  
  76.     { 1.0f, -1.0f, -1.0f},  
  77.     { 1.0f, -1.0f,  1.0f},  
  78.     {-1.0f, -1.0f,  1.0f},  
  79.   
  80.     { 1.0f, -1.0f, -1.0f},  
  81.     { 1.0f,  1.0f, -1.0f},  
  82.     { 1.0f,  1.0f,  1.0f},  
  83.     { 1.0f, -1.0f,  1.0f},  
  84.   
  85.     {-1.0f, -1.0f, -1.0f},  
  86.     {-1.0f, -1.0f,  1.0f},  
  87.     {-1.0f,  1.0f,  1.0f},  
  88.     {-1.0f,  1.0f, -1.0f}  
  89.   
  90. };  
  91.   
  92. float normalVertics[][3] = {  
  93.     // Front Face  
  94.     { 0.0f, 0.0f, 1.0f},  
  95.     // Back Face  
  96.     { 0.0f, 0.0f,-1.0f},  
  97.     // Top Face  
  98.     { 0.0f, 1.0f, 0.0f},  
  99.     // Bottom Face  
  100.     { 0.0f,-1.0f, 0.0f},  
  101.     // Right face  
  102.     { 1.0f, 0.0f, 0.0f},  
  103.     // Left Face  
  104.     {-1.0f, 0.0f, 0.0f}  
  105. };  
  106.   
  107. //////////////////////////////////////////////////////////////////////////  
  108. //FindFirstNamedNodeVisitor用来遍历寻找与指定名称相同的节点  
  109.   
  110. class FindFirstNamedNodeVisitor : public osg::NodeVisitor  
  111. {  
  112. public:  
  113.     FindFirstNamedNodeVisitor(const std::string& name):  
  114.       osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),  
  115.           _name(name), _foundNode(NULL) {}  
  116.   
  117.       virtual void apply(osg::Node& node)  
  118.       {  
  119.           if (node.getName()==_name)  
  120.           {  
  121.               _foundNode = &node;  
  122.               return;  
  123.           }  
  124.           traverse(node);  
  125.       }  
  126.   
  127.       std::string _name;  
  128.       osg::Node *_foundNode;  
  129. };  
  130.   
  131. //////////////////////////////////////////////////////////////////////////  
  132. //RotateCallback  
  133.   
  134. class RotateCallback : public osg::NodeCallback  
  135. {  
  136.   
  137. public:  
  138.     RotateCallback(osg::Vec3d rotateAxis, double rotateSpeed) :   
  139.       osg::NodeCallback(),  
  140.           _rotateAxis(rotateAxis),   
  141.           _rotateSpeed(rotateSpeed),  
  142.           _rotateAngle(0.0)  
  143.       {  
  144.           //Nop  
  145.       }  
  146.   
  147.       void setRotateSpeed(double speed)  
  148.       {  
  149.           _rotateSpeed = speed;  
  150.       }  
  151.   
  152.       double getRotateSpeed() const  
  153.       {  
  154.           return _rotateSpeed;  
  155.       }  
  156.   
  157.       virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)  
  158.       {  
  159.           osg::MatrixTransform *currentMT = dynamic_cast<osg::MatrixTransform*>(node);  
  160.           if (currentMT)  
  161.           {  
  162.               //获取当前的平移位置  
  163.               osg::Vec3d currentTranslate = currentMT->getMatrix().getTrans();  
  164.               osg::Matrix newMatrix = osg::Matrix::rotate(_rotateAngle, _rotateAxis) * osg::Matrix::translate(currentTranslate);  
  165.               currentMT->setMatrix(newMatrix);  
  166.               _rotateAngle += _rotateSpeed;  
  167.           }  
  168.   
  169.           traverse(node, nv);  
  170.       }  
  171.   
  172.   
  173. private:  
  174.     osg::Vec3d _rotateAxis;         //旋转轴  
  175.     double        _rotateSpeed;     //旋转速度  
  176.     double        _rotateAngle;     //当前旋转的角度  
  177. };  
  178.   
  179.   
  180.   
  181.   
  182. //////////////////////////////////////////////////////////////////////////  
  183.   
  184. class ManipulatorSceneHandler : public osgGA::GUIEventHandler  
  185. {  
  186. public:  
  187.     ManipulatorSceneHandler()  
  188.     {  
  189.     }  
  190.   
  191.     virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)  
  192.     {  
  193.         osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*>(&aa);  
  194.         if (!viewer)  
  195.             return false;  
  196.         if (!viewer->getSceneData())  
  197.             return false;  
  198.         if (ea.getHandled())   
  199.             return false;  
  200.   
  201.         osg::Group *root = viewer->getSceneData()->asGroup();  
  202.   
  203.         switch(ea.getEventType())  
  204.         {  
  205.         case(osgGA::GUIEventAdapter::KEYDOWN):  
  206.             {  
  207.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)  
  208.                 {  
  209.                     FindFirstNamedNodeVisitor fnv("yRotMT");  
  210.                     root->accept(fnv);  
  211.   
  212.                     osg::Node *mtNode = fnv._foundNode;  
  213.                     osg::MatrixTransform *yRotMT = dynamic_cast<osg::MatrixTransform*>(mtNode);  
  214.                     if (!yRotMT)  
  215.                         return false;  
  216.   
  217.                     RotateCallback *rotCallback = dynamic_cast<RotateCallback*>(yRotMT->getUpdateCallback());  
  218.   
  219.                     if (!rotCallback)  
  220.                         return false;  
  221.   
  222.                     double speed = rotCallback->getRotateSpeed();  
  223.                     speed += 0.02;  
  224.                     rotCallback->setRotateSpeed(speed);  
  225.   
  226.                 }  
  227.   
  228.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)  
  229.                 {  
  230.                     FindFirstNamedNodeVisitor fnv("yRotMT");  
  231.                     root->accept(fnv);  
  232.   
  233.                     osg::Node *mtNode = fnv._foundNode;  
  234.                     osg::MatrixTransform *yRotMT = dynamic_cast<osg::MatrixTransform*>(mtNode);  
  235.                     if (!yRotMT)  
  236.                         return false;  
  237.   
  238.                     RotateCallback *rotCallback = dynamic_cast<RotateCallback*>(yRotMT->getUpdateCallback());  
  239.   
  240.                     if (!rotCallback)  
  241.                         return false;  
  242.   
  243.                     double speed = rotCallback->getRotateSpeed();  
  244.                     speed -= 0.02;  
  245.                     rotCallback->setRotateSpeed(speed);  
  246.                 }  
  247.   
  248.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)  
  249.                 {  
  250.                     FindFirstNamedNodeVisitor fnv("xRotMT");  
  251.                     root->accept(fnv);  
  252.   
  253.                     osg::Node *mtNode = fnv._foundNode;  
  254.                     osg::MatrixTransform *xRotMT = dynamic_cast<osg::MatrixTransform*>(mtNode);  
  255.                     if (!xRotMT)  
  256.                         return false;  
  257.   
  258.                     RotateCallback *rotCallback = dynamic_cast<RotateCallback*>(xRotMT->getUpdateCallback());  
  259.   
  260.                     if (!rotCallback)  
  261.                         return false;  
  262.   
  263.                     double speed = rotCallback->getRotateSpeed();  
  264.                     speed += 0.02;  
  265.                     rotCallback->setRotateSpeed(speed);  
  266.                 }  
  267.   
  268.                 if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Down)  
  269.                 {  
  270.                     FindFirstNamedNodeVisitor fnv("xRotMT");  
  271.                     root->accept(fnv);  
  272.   
  273.                     osg::Node *mtNode = fnv._foundNode;  
  274.                     osg::MatrixTransform *xRotMT = dynamic_cast<osg::MatrixTransform*>(mtNode);  
  275.                     if (!xRotMT)  
  276.                         return false;  
  277.   
  278.                     RotateCallback *rotCallback = dynamic_cast<RotateCallback*>(xRotMT->getUpdateCallback());  
  279.   
  280.                     if (!rotCallback)  
  281.                         return false;  
  282.   
  283.                     double speed = rotCallback->getRotateSpeed();  
  284.                     speed -= 0.02;  
  285.                     rotCallback->setRotateSpeed(speed);  
  286.                 }  
  287.   
  288.                 if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Page_Up)  
  289.                 {     
  290.                     FindFirstNamedNodeVisitor fnv("zoomMT");  
  291.                     root->accept(fnv);  
  292.   
  293.                     osg::Node *mtNode = fnv._foundNode;  
  294.                     osg::MatrixTransform *zoomMT = dynamic_cast<osg::MatrixTransform*>(mtNode);  
  295.                     if (!zoomMT)  
  296.                         return false;  
  297.   
  298.                     osg::Vec3 trans = zoomMT->getMatrix().getTrans();  
  299.                     trans.set(trans.x(), trans.y(), trans.z() + 0.2);  
  300.                     zoomMT->setMatrix(osg::Matrix::translate(trans));  
  301.                 }  
  302.   
  303.                 if (ea.getKey()== osgGA::GUIEventAdapter::KEY_Page_Down)  
  304.                 {  
  305.                     FindFirstNamedNodeVisitor fnv("zoomMT");  
  306.                     root->accept(fnv);  
  307.   
  308.                     osg::Node *mtNode = fnv._foundNode;  
  309.                     osg::MatrixTransform *zoomMT = dynamic_cast<osg::MatrixTransform*>(mtNode);  
  310.                     if (!zoomMT)  
  311.                         return false;  
  312.   
  313.                     osg::Vec3 trans = zoomMT->getMatrix().getTrans();  
  314.                     trans.set(trans.x(), trans.y(), trans.z() - 0.2);  
  315.                     zoomMT->setMatrix(osg::Matrix::translate(trans));  
  316.                 }  
  317.   
  318.                 if (ea.getKey()== osgGA::GUIEventAdapter::KEY_L)  
  319.                 {  
  320.                     static int i = 0;  
  321.                     if (i % 2 == 0) {  
  322.                         root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  323.                     } else {  
  324.                         root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);  
  325.                     }  
  326.                     ++i;  
  327.                 }  
  328.   
  329.                 if (ea.getKey()== osgGA::GUIEventAdapter::KEY_F)  
  330.                 {  
  331.                     FindFirstNamedNodeVisitor fnv("quadGeode");  
  332.                     root->accept(fnv);  
  333.   
  334.                     osg::Node *mtNode = fnv._foundNode;  
  335.                     osg::Geode *quadGeode = dynamic_cast<osg::Geode*>(mtNode);  
  336.                     if (!quadGeode)  
  337.                         return false;  
  338.   
  339.                     osg::Texture2D *texture2D = dynamic_cast<osg::Texture2D*>(quadGeode->getOrCreateStateSet()->getTextureAttribute(0, osg::StateAttribute::TEXTURE));  
  340.   
  341.                     static int i = 0;  
  342.                     if (i % 3 == 0) {  
  343.                         texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  344.                         texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);  
  345.                     } else if (i % 3 == 1) {  
  346.                         texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  347.                         texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR_MIPMAP_NEAREST);  
  348.                     } else {  
  349.                         texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);  
  350.                         texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);  
  351.                     }  
  352.                     ++i;  
  353.                 }  
  354.   
  355.                 if (ea.getKey()== osgGA::GUIEventAdapter::KEY_G)  
  356.                 {  
  357.                         static int i = 0;  
  358.                         if (i % 3 == 0) {  
  359.                             g_fog->setMode(osg::Fog::EXP2);  
  360.                         } else if (i % 3 == 1) {  
  361.                             g_fog->setMode(osg::Fog::LINEAR);  
  362.                         } else {  
  363.                             g_fog->setMode(osg::Fog::EXP);  
  364.                         }  
  365.                         ++i;  
  366.                 }  
  367.             }  
  368.         defaultbreak;  
  369.         }  
  370.         return false;  
  371.     }  
  372. };  
  373.   
  374. //////////////////////////////////////////////////////////////////////////  
  375.   
  376.   
  377.   
  378. class ViewerWidget : public QWidget, public osgViewer::Viewer  
  379. {  
  380. public:  
  381.     ViewerWidget(osg::Node *scene = NULL)  
  382.     {  
  383.         QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);  
  384.   
  385.         QVBoxLayout* layout = new QVBoxLayout;  
  386.         layout->addWidget(renderWidget);  
  387.         layout->setContentsMargins(0, 0, 0, 1);  
  388.         setLayout( layout );  
  389.   
  390.         connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );  
  391.         _timer.start( 10 );  
  392.     }  
  393.   
  394.     QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )  
  395.     {  
  396.         osg::Camera* camera = this->getCamera();  
  397.         camera->setGraphicsContext( gw );  
  398.   
  399.         const osg::GraphicsContext::Traits* traits = gw->getTraits();  
  400.   
  401.         camera->setClearColor( osg::Vec4(0.5, 0.5, 0.5, 1.0) );  
  402.         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );  
  403.         camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );  
  404.         camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0));  
  405.   
  406.         osg::StateSet* globalStateset = camera->getStateSet();  
  407.         if (globalStateset)  
  408.         {  
  409.             osg::LightModel* lightModel = new osg::LightModel;  
  410.             lightModel->setAmbientIntensity(osg::Vec4(0,0,0,0));  
  411.             globalStateset->setAttributeAndModes(lightModel, osg::StateAttribute::ON);  
  412.         }  
  413.   
  414.         this->setSceneData( scene );  
  415.         this->addEventHandler(new ManipulatorSceneHandler());  
  416.   
  417.         return gw->getGLWidget();  
  418.     }  
  419.   
  420.     osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name=""bool windowDecoration=false )  
  421.     {  
  422.         osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();  
  423.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  424.         traits->windowName = name;  
  425.         traits->windowDecoration = windowDecoration;  
  426.         traits->x = x;  
  427.         traits->y = y;  
  428.         traits->width = w;  
  429.         traits->height = h;  
  430.         traits->doubleBuffer = true;  
  431.         traits->alpha = ds->getMinimumNumAlphaBits();  
  432.         traits->stencil = ds->getMinimumNumStencilBits();  
  433.         traits->sampleBuffers = ds->getMultiSamples();  
  434.         traits->samples = ds->getNumMultiSamples();  
  435.   
  436.         return new osgQt::GraphicsWindowQt(traits.get());  
  437.     }  
  438.   
  439.     virtual void paintEvent( QPaintEvent* event )  
  440.     {   
  441.         frame();   
  442.     }  
  443.   
  444. protected:  
  445.   
  446.     QTimer _timer;  
  447. };  
  448.   
  449.   
  450.   
  451. osg::Node*  buildScene()  
  452. {  
  453.     osg::Group *root = new osg::Group;  
  454.   
  455.     osg::Light* light = new osg::Light();  
  456.     light->setLightNum(0);  
  457.     light->setAmbient(osg::Vec4(0.5f, 0.5f, 0.5f, 1.0f));  
  458.     light->setDiffuse(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));  
  459.     light->setPosition(osg::Vec4(0.0f, 0.0f, 2.0f, 1.0f));  
  460.     osg::LightSource* lightsource = new osg::LightSource();  
  461.     lightsource->setLight (light);  
  462.     root->addChild (lightsource);   
  463.   
  464.     osg::MatrixTransform *quadMT = new osg::MatrixTransform;  
  465.     quadMT->setName("zoomMT");  
  466.     quadMT->setMatrix(osg::Matrix::translate(0.0, 0.0, -5.0));  
  467.   
  468.     osg::MatrixTransform *quadXRotMT = new osg::MatrixTransform;  
  469.     quadXRotMT->setName("xRotMT");  
  470.     RotateCallback *xRotCallback = new RotateCallback(osg::X_AXIS, 0.0);  
  471.     quadXRotMT->setUpdateCallback(xRotCallback);  
  472.   
  473.   
  474.     osg::MatrixTransform *quadYRotMT = new osg::MatrixTransform;  
  475.     quadYRotMT->setName("yRotMT");  
  476.     RotateCallback *yRotCallback = new RotateCallback(osg::Y_AXIS, 0.0);  
  477.     quadYRotMT->setUpdateCallback(yRotCallback);  
  478.   
  479.     osg::Geometry *quadGeometry = new osg::Geometry;  
  480.     osg::Vec3Array *quadVertexArray = new osg::Vec3Array;  
  481.     for (unsigned i = 0; i < sizeof(QuadVertices); ++i)  
  482.     {  
  483.         quadVertexArray->push_back(osg::Vec3(QuadVertices[i][0], QuadVertices[i][1], QuadVertices[i][2]));  
  484.     }  
  485.     quadGeometry->setVertexArray(quadVertexArray);  
  486.   
  487.     osg::Vec2Array* texcoords = new osg::Vec2Array;  
  488.     for (unsigned i = 0; i < sizeof(textureVertices); ++i)  
  489.     {  
  490.         texcoords->push_back(osg::Vec2(textureVertices[i][0], textureVertices[i][1]));  
  491.     }  
  492.     quadGeometry->setTexCoordArray(0,texcoords);  
  493.   
  494.     osg::Vec3Array *quadNormalArray = new osg::Vec3Array;  
  495.     for (unsigned i = 0; i < sizeof(normalVertics); ++i)  
  496.     {  
  497.         quadNormalArray->push_back(osg::Vec3(normalVertics[i][0], normalVertics[i][1], normalVertics[i][2]));  
  498.     }  
  499.     quadGeometry->setNormalArray(quadNormalArray, osg::Array::BIND_PER_PRIMITIVE_SET);  
  500.   
  501.     int first = 0;  
  502.     for (unsigned i = 0; i < 6; ++i)  
  503.     {  
  504.         osg::DrawArrays *vertexIndices = new osg::DrawArrays(osg::PrimitiveSet::QUADS, first, 4);  
  505.         first += 4;  
  506.         quadGeometry->addPrimitiveSet(vertexIndices);  
  507.     }  
  508.   
  509.     osg::Image *textureImage = osgDB::readImageFile("Data/Crate.bmp");  
  510.     osg::Texture2D *texture2D = new osg::Texture2D;  
  511.     texture2D->setImage(textureImage);  
  512.     texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);  
  513.     texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);  
  514.   
  515.     osg::Fog *fog = new osg::Fog;  
  516.     fog->setMode(osg::Fog::EXP);  
  517.     fog->setColor(osg::Vec4(0.5f,0.5f,0.5f,1.0f));  
  518.     fog->setDensity(0.35f);  
  519.     fog->setStart(1.0f);  
  520.     fog->setEnd(5.0f);  
  521.     g_fog = fog;  
  522.   
  523.     osg::Geode *quadGeode = new osg::Geode;  
  524.     quadGeode->setName("quadGeode");  
  525.     quadGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);  
  526.     quadGeode->getOrCreateStateSet()->setAttributeAndModes(fog);  
  527.     quadGeode->addDrawable(quadGeometry);  
  528.     quadYRotMT->addChild(quadGeode);  
  529.     quadXRotMT->addChild(quadYRotMT);  
  530.     quadMT->addChild(quadXRotMT);  
  531.   
  532.     root->addChild(quadMT);  
  533.   
  534.     return root;  
  535. }  
  536.   
  537.   
  538.   
  539. int main( int argc, char** argv )  
  540. {  
  541.     QApplication app(argc, argv);  
  542.     ViewerWidget* viewWidget = new ViewerWidget(buildScene());  
  543.     viewWidget->setGeometry( 100, 100, 640, 480 );  
  544.     viewWidget->show();  
  545.     return app.exec();  
  546. }  

0 0