OpenSceneGraph实现的NeHe OpenGL教程 - 第八课

来源:互联网 发布:lol网络异常 编辑:程序博客网 时间:2024/06/05 17:22
  • 简介

本课是在第七课的基础上实现将立方体变透明的效果,其中用到了OpenGL中的混合(Blend)

  • 实现

在OpenGL中如何实现混合以及混合实现的原理和过程在NeHe教程中已经解释的很清楚了,在这里就不在赘述,本课主要探讨在OSG中实现混合的效果,混合同样是作为一种StateSet的方式来进行处理的,OSG中的混合主要涉及到一下几个类BlendFunc、BlendEquation和BlendColor,对应于OpenGL中的glBlendFunc 、glBlendEquation和glBlendColor三个函数,对混合的参数进行一些设置。本课中我们参考NeHe教程中设置混合的计算方式定义如下的BlendFunc

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. osg::BlendFunc *blendFunc = new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE, 0.5, 0.5);  
  2. quadGeometry->getOrCreateStateSet()->setAttribute(blendFunc,  
  3.     osg::StateAttribute::ON);  

需要注意的是我们需要将初始的清除背景的颜色的alpha值设置为0.5

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 0.5) );  

在按下键盘上B键的时候开启和关闭混合模式

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (ea.getKey()== osgGA::GUIEventAdapter::KEY_B)  
  2. {  
  3.     static int i = 0;  
  4.     if (i % 2 == 0) {  
  5.         root->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);  
  6.     } else {  
  7.         root->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::OFF);  
  8.     }  
  9.     ++i;  
  10. }  

其他的代码与第七课相同,编译运行程序后点击B键时可以看到透明的效果。

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

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