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

来源:互联网 发布:淘宝漏洞1元买东西 编辑:程序博客网 时间:2024/05/17 01:47
  • 简介

NeHe教程在这节课中向我们介绍了贝塞尔曲面,贝塞尔曲面是一种可以只使用很少的参数就可以描述出复杂曲面的一种数学工具。关于贝塞尔曲线的内容可以参考Bézier surface

  • 实现

本课的实现过程非常简单,在理解了贝塞尔曲面之后利用贝塞尔曲面的计算公式计算出控制点和插值点坐标:

控制点线条的代码如下:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. osg::Geode* createBezierControlLineGeode()  
  2. {  
  3.     osg::Geode *geode = new osg::Geode;  
  4.     osg::Geometry *geometry = new osg::Geometry;  
  5.     osg::Vec3Array *vertexArray = new osg::Vec3Array;  
  6.     osg::Vec3Array *colorArray = new osg::Vec3Array;  
  7.     colorArray->push_back(osg::Vec3(1.0, 0.0, 0.0));  
  8.     geometry->setColorArray(colorArray, osg::Array::BIND_OVERALL);  
  9.     geometry->setVertexArray(vertexArray);  
  10.     geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  11.       
  12.     for(int i=0;i<4;i++) {                               // draw the horizontal lines  
  13.         for(int j=0;j<4;j++)  
  14.             vertexArray->push_back(osg::Vec3(mybezier.anchors[i][j].x, mybezier.anchors[i][j].y, mybezier.anchors[i][j].z));  
  15.     }  
  16.     for(int i=0;i<4;i++) {                               // draw the vertical lines  
  17.         for(int j=0;j<4;j++)  
  18.             vertexArray->push_back(osg::Vec3(mybezier.anchors[j][i].x, mybezier.anchors[j][i].y, mybezier.anchors[j][i].z));  
  19.     }  
  20.   
  21.     geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, vertexArray->size() / 2));  
  22.     geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, vertexArray->size() / 2, vertexArray->size() / 2));  
  23.     geode->addDrawable(geometry);  
  24.     return geode;  
  25. }  
利用控制点插值出曲面中的其他坐标点,绘制曲面:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. osg::Geode* createBezierGeode(BEZIER_PATCH patch, int divs)  
  2. {  
  3.     int         u = 0, v;  
  4.     float       py, px, pyold;   
  5.     POINT_3D    temp[4];  
  6.     POINT_3D    *last = (POINT_3D*)malloc(sizeof(POINT_3D)*(divs+1));  
  7.     // array of points to mark the first line of polys  
  8.   
  9.     temp[0] = patch.anchors[0][3];              // the first derived curve (along x axis)  
  10.     temp[1] = patch.anchors[1][3];  
  11.     temp[2] = patch.anchors[2][3];  
  12.     temp[3] = patch.anchors[3][3];  
  13.   
  14.     for (v=0;v<=divs;v++) {                      // create the first line of points  
  15.         px = ((float)v)/((float)divs);          // percent along y axis  
  16.         // use the 4 points from the derives curve to calculate the points along that curve  
  17.         last[v] = Bernstein(px, temp);  
  18.     }  
  19.       
  20.     osg::Geode *geode = new osg::Geode;  
  21.     osg::Geometry *geometry = new osg::Geometry;  
  22.     osg::Vec3Array *vertexArray = new osg::Vec3Array;  
  23.     osg::Vec2Array *textureArray = new osg::Vec2Array;  
  24.     geometry->setVertexArray(vertexArray);  
  25.     geometry->setTexCoordArray(0, textureArray);  
  26.     osg::Vec3Array *colorArray = new osg::Vec3Array;  
  27.     colorArray->push_back(osg::Vec3(1, 1, 1));  
  28.     geometry->setColorArray(colorArray, osg::Array::BIND_OVERALL);  
  29.     geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  30.   
  31.   
  32.     for (u=1;u<=divs;u++) {  
  33.         py    = ((float)u)/((float)divs);           // Percent along Y axis  
  34.         pyold = ((float)u-1.0f)/((float)divs);      // Percent along old Y axis  
  35.   
  36.         temp[0] = Bernstein(py, patch.anchors[0]);  // Calculate new bezier points  
  37.         temp[1] = Bernstein(py, patch.anchors[1]);  
  38.         temp[2] = Bernstein(py, patch.anchors[2]);  
  39.         temp[3] = Bernstein(py, patch.anchors[3]);  
  40.   
  41.         for (v=0;v<=divs;v++) {  
  42.             px = ((float)v)/((float)divs);          // Percent along the X axis  
  43.   
  44.             textureArray->push_back(osg::Vec2(pyold, px));               // Apply the old texture coords  
  45.             vertexArray->push_back(osg::Vec3(last[v].x, last[v].y, last[v].z));  // Old Point  
  46.   
  47.             last[v] = Bernstein(px, temp);          // Generate new point  
  48.             textureArray->push_back(osg::Vec2(py, px));                  // Apply the new texture coords  
  49.             vertexArray->push_back(osg::Vec3(last[v].x, last[v].y, last[v].z));  // New Point  
  50.         }  
  51.     }  
  52.   
  53.     osg::Texture2D *bezierTex = new osg::Texture2D;  
  54.     bezierTex->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);  
  55.     bezierTex->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  56.     bezierTex->setImage(osgDB::readImageFile("Data/NeHe.bmp"));  
  57.   
  58.     geometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, bezierTex);  
  59.     geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, 0, vertexArray->size()));  
  60.     geode->addDrawable(geometry);  
  61.   
  62.     return geode;  
  63. }  
最后将二者添加到Switch节点中,在交互操作中切换控制点线条的显示和隐藏:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. osg::Switch *switchLineAndGeodeSwitch = new osg::Switch;  
  2. g_Swith = switchLineAndGeodeSwitch;  
  3. rotZMT->addChild(switchLineAndGeodeSwitch);  
  4.   
  5. switchLineAndGeodeSwitch->addChild(createBezierControlLineGeode());  
  6. switchLineAndGeodeSwitch->addChild(createBezierGeode(mybezier, divs));  
  7. switchLineAndGeodeSwitch->setAllChildrenOn();  
交互部分代码如下

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Space)  
  2. {  
  3.     if (!g_Swith)  
  4.         return false;  
  5.     if (g_Swith->getValue(0)){   
  6.         g_Swith->setSingleChildOn(1);  
  7.     }else{  
  8.         g_Swith->setAllChildrenOn();  
  9.     }  
  10. }  

最后编译运行程序:


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

[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/Switch>  
  14.   
  15.   
  16. osg::MatrixTransform *g_ZRotMT = NULL;  
  17. osg::Switch                 *g_Swith = NULL;  
  18.   
  19. typedef struct point_3d {  
  20.     double x, y, z;  
  21. } POINT_3D;  
  22.   
  23. typedef struct bpatch {  
  24.     POINT_3D    anchors[4][4];  
  25. } BEZIER_PATCH;  
  26.   
  27.   
  28. BEZIER_PATCH    mybezier;     
  29. int             divs = 7;  
  30.   
  31.   
  32.   
  33. POINT_3D pointAdd(POINT_3D p, POINT_3D q) {  
  34.     p.x += q.x;     p.y += q.y;     p.z += q.z;  
  35.     return p;  
  36. }  
  37.   
  38. POINT_3D pointTimes(double c, POINT_3D p) {  
  39.     p.x *= c;   p.y *= c;   p.z *= c;  
  40.     return p;  
  41. }  
  42.   
  43. POINT_3D makePoint(double a, double b, double c) {  
  44.     POINT_3D p;  
  45.     p.x = a;    p.y = b;    p.z = c;  
  46.     return p;  
  47. }  
  48.   
  49.   
  50. // Calculates 3rd degree polynomial based on array of 4 points  
  51. // and a single variable (u) which is generally between 0 and 1  
  52. POINT_3D Bernstein(float u, POINT_3D *p) {  
  53.     POINT_3D    a, b, c, d, r;  
  54.   
  55.     a = pointTimes(pow(u,3), p[0]);  
  56.     b = pointTimes(3*pow(u,2)*(1-u), p[1]);  
  57.     c = pointTimes(3*u*pow((1-u),2), p[2]);  
  58.     d = pointTimes(pow((1-u),3), p[3]);  
  59.   
  60.     r = pointAdd(pointAdd(a, b), pointAdd(c, d));  
  61.   
  62.     return r;  
  63. }  
  64.   
  65. //////////////////////////////////////////////////////////////////////////  
  66. void initBezier(void) {  
  67.     mybezier.anchors[0][0] = makePoint(-0.75,   -0.75,  -0.5);  
  68.     mybezier.anchors[0][1] = makePoint(-0.25,   -0.75,  0.0);  
  69.     mybezier.anchors[0][2] = makePoint(0.25,    -0.75,  0.0);  
  70.     mybezier.anchors[0][3] = makePoint(0.75,    -0.75,  -0.5);  
  71.     mybezier.anchors[1][0] = makePoint(-0.75,   -0.25,  -0.75);  
  72.     mybezier.anchors[1][1] = makePoint(-0.25,   -0.25,  0.5);  
  73.     mybezier.anchors[1][2] = makePoint(0.25,    -0.25,  0.5);  
  74.     mybezier.anchors[1][3] = makePoint(0.75,    -0.25,  -0.75);  
  75.     mybezier.anchors[2][0] = makePoint(-0.75,   0.25,   0.0);  
  76.     mybezier.anchors[2][1] = makePoint(-0.25,   0.25,   -0.5);  
  77.     mybezier.anchors[2][2] = makePoint(0.25,    0.25,   -0.5);  
  78.     mybezier.anchors[2][3] = makePoint(0.75,    0.25,   0.0);  
  79.     mybezier.anchors[3][0] = makePoint(-0.75,   0.75,   -0.5);  
  80.     mybezier.anchors[3][1] = makePoint(-0.25,   0.75,   -1.0);  
  81.     mybezier.anchors[3][2] = makePoint(0.25,    0.75,   -1.0);  
  82.     mybezier.anchors[3][3] = makePoint(0.75,    0.75,   -0.5);  
  83. }  
  84.   
  85. //////////////////////////////////////////////////////////////////////////  
  86. //创建贝塞尔曲线  
  87.   
  88. osg::Geode* createBezierControlLineGeode()  
  89. {  
  90.     osg::Geode *geode = new osg::Geode;  
  91.     osg::Geometry *geometry = new osg::Geometry;  
  92.     osg::Vec3Array *vertexArray = new osg::Vec3Array;  
  93.     osg::Vec3Array *colorArray = new osg::Vec3Array;  
  94.     colorArray->push_back(osg::Vec3(1.0, 0.0, 0.0));  
  95.     geometry->setColorArray(colorArray, osg::Array::BIND_OVERALL);  
  96.     geometry->setVertexArray(vertexArray);  
  97.     geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  98.       
  99.     for(int i=0;i<4;i++) {                               // draw the horizontal lines  
  100.         for(int j=0;j<4;j++)  
  101.             vertexArray->push_back(osg::Vec3(mybezier.anchors[i][j].x, mybezier.anchors[i][j].y, mybezier.anchors[i][j].z));  
  102.     }  
  103.     for(int i=0;i<4;i++) {                               // draw the vertical lines  
  104.         for(int j=0;j<4;j++)  
  105.             vertexArray->push_back(osg::Vec3(mybezier.anchors[j][i].x, mybezier.anchors[j][i].y, mybezier.anchors[j][i].z));  
  106.     }  
  107.   
  108.     geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, vertexArray->size() / 2));  
  109.     geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, vertexArray->size() / 2, vertexArray->size() / 2));  
  110.     geode->addDrawable(geometry);  
  111.     return geode;  
  112. }  
  113.   
  114. osg::Geode* createBezierGeode(BEZIER_PATCH patch, int divs)  
  115. {  
  116.     int         u = 0, v;  
  117.     float       py, px, pyold;   
  118.     POINT_3D    temp[4];  
  119.     POINT_3D    *last = (POINT_3D*)malloc(sizeof(POINT_3D)*(divs+1));  
  120.     // array of points to mark the first line of polys  
  121.   
  122.     temp[0] = patch.anchors[0][3];              // the first derived curve (along x axis)  
  123.     temp[1] = patch.anchors[1][3];  
  124.     temp[2] = patch.anchors[2][3];  
  125.     temp[3] = patch.anchors[3][3];  
  126.   
  127.     for (v=0;v<=divs;v++) {                      // create the first line of points  
  128.         px = ((float)v)/((float)divs);          // percent along y axis  
  129.         // use the 4 points from the derives curve to calculate the points along that curve  
  130.         last[v] = Bernstein(px, temp);  
  131.     }  
  132.       
  133.     osg::Geode *geode = new osg::Geode;  
  134.     osg::Geometry *geometry = new osg::Geometry;  
  135.     osg::Vec3Array *vertexArray = new osg::Vec3Array;  
  136.     osg::Vec2Array *textureArray = new osg::Vec2Array;  
  137.     geometry->setVertexArray(vertexArray);  
  138.     geometry->setTexCoordArray(0, textureArray);  
  139.     osg::Vec3Array *colorArray = new osg::Vec3Array;  
  140.     colorArray->push_back(osg::Vec3(1, 1, 1));  
  141.     geometry->setColorArray(colorArray, osg::Array::BIND_OVERALL);  
  142.     geometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  143.   
  144.   
  145.     for (u=1;u<=divs;u++) {  
  146.         py    = ((float)u)/((float)divs);           // Percent along Y axis  
  147.         pyold = ((float)u-1.0f)/((float)divs);      // Percent along old Y axis  
  148.   
  149.         temp[0] = Bernstein(py, patch.anchors[0]);  // Calculate new bezier points  
  150.         temp[1] = Bernstein(py, patch.anchors[1]);  
  151.         temp[2] = Bernstein(py, patch.anchors[2]);  
  152.         temp[3] = Bernstein(py, patch.anchors[3]);  
  153.   
  154.         for (v=0;v<=divs;v++) {  
  155.             px = ((float)v)/((float)divs);          // Percent along the X axis  
  156.   
  157.             textureArray->push_back(osg::Vec2(pyold, px));               // Apply the old texture coords  
  158.             vertexArray->push_back(osg::Vec3(last[v].x, last[v].y, last[v].z));  // Old Point  
  159.   
  160.             last[v] = Bernstein(px, temp);          // Generate new point  
  161.             textureArray->push_back(osg::Vec2(py, px));                  // Apply the new texture coords  
  162.             vertexArray->push_back(osg::Vec3(last[v].x, last[v].y, last[v].z));  // New Point  
  163.         }  
  164.     }  
  165.   
  166.     osg::Texture2D *bezierTex = new osg::Texture2D;  
  167.     bezierTex->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);  
  168.     bezierTex->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  169.     bezierTex->setImage(osgDB::readImageFile("Data/NeHe.bmp"));  
  170.   
  171.     geometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, bezierTex);  
  172.     geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, 0, vertexArray->size()));  
  173.     geode->addDrawable(geometry);  
  174.   
  175.     return geode;  
  176. }  
  177.   
  178.   
  179. //////////////////////////////////////////////////////////////////////////  
  180. ////////////////////场景交互代码////////////////////////////////  
  181. //////////////////////////////////////////////////////////////////////////  
  182.   
  183. class RotAxisCallback : public osg::NodeCallback  
  184. {  
  185. public:  
  186.     RotAxisCallback(const osg::Vec3& axis, double rotSpeed = 0.0, double currentAngle = 0.0)  
  187.         : _rotAxis(axis), _rotSpeed(rotSpeed), _currentAngle(currentAngle){ }  
  188.   
  189.     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)  
  190.     {  
  191.         osg::MatrixTransform *rotMT = dynamic_cast<osg::MatrixTransform*>(node);  
  192.         if (!rotMT)  
  193.             return;  
  194.         rotMT->setMatrix(osg::Matrix::rotate(_currentAngle, _rotAxis));  
  195.   
  196.         traverse(node, nv);  
  197.     }  
  198.   
  199.     void setRotateAngle(double speed)  
  200.     {  
  201.         _currentAngle = speed;  
  202.     }  
  203.   
  204.     double getRotateAngle() const  
  205.     {  
  206.         return _currentAngle;  
  207.     }  
  208.   
  209.   
  210. private:  
  211.     osg::Vec3       _rotAxis;  
  212.     double          _currentAngle;  
  213.     double          _rotSpeed;  
  214. };  
  215.   
  216.   
  217.   
  218. //////////////////////////////////////////////////////////////////////////  
  219. //////////////////////////////////////////////////////////////////////////  
  220. class ManipulatorSceneHandler : public osgGA::GUIEventHandler  
  221. {  
  222. public:  
  223.     ManipulatorSceneHandler()  
  224.     {  
  225.     }  
  226.   
  227.     virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)  
  228.     {  
  229.         osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*>(&aa);  
  230.         if (!viewer)  
  231.             return false;  
  232.         if (!viewer->getSceneData())  
  233.             return false;  
  234.         if (ea.getHandled())   
  235.             return false;  
  236.   
  237.         osg::Group *root = viewer->getSceneData()->asGroup();  
  238.   
  239.         switch(ea.getEventType())  
  240.         {  
  241.         case(osgGA::GUIEventAdapter::KEYDOWN):  
  242.             {  
  243.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)  
  244.                 {  
  245.                     if (!g_ZRotMT)  
  246.                         return false;  
  247.   
  248.                     RotAxisCallback *rotCallback = dynamic_cast<RotAxisCallback*>(g_ZRotMT->getUpdateCallback());  
  249.                     if (!rotCallback)  
  250.                         return false;  
  251.   
  252.                     double speed = rotCallback->getRotateAngle();  
  253.                     speed -= 0.02;  
  254.                     rotCallback->setRotateAngle(speed);  
  255.                 }  
  256.   
  257.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)  
  258.                 {  
  259.                     if (!g_ZRotMT)  
  260.                         return false;  
  261.   
  262.                     RotAxisCallback *rotCallback = dynamic_cast<RotAxisCallback*>(g_ZRotMT->getUpdateCallback());  
  263.                     if (!rotCallback)  
  264.                         return false;  
  265.   
  266.                     double speed = rotCallback->getRotateAngle();  
  267.                     speed += 0.02;  
  268.                     rotCallback->setRotateAngle(speed);  
  269.                 }  
  270.   
  271.                 if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Space)  
  272.                 {  
  273.                     if (!g_Swith)  
  274.                         return false;  
  275.                     if (g_Swith->getValue(0)){   
  276.                         g_Swith->setSingleChildOn(1);  
  277.                     }else{  
  278.                         g_Swith->setAllChildrenOn();  
  279.                     }  
  280.                 }  
  281.             }  
  282.         defaultbreak;  
  283.         }  
  284.         return false;  
  285.     }  
  286. };  
  287.   
  288.   
  289.   
  290. class ViewerWidget : public QWidget, public osgViewer::Viewer  
  291. {  
  292. public:  
  293.     ViewerWidget(osg::Node *scene = NULL)  
  294.     {  
  295.         QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);  
  296.   
  297.         QVBoxLayout* layout = new QVBoxLayout;  
  298.         layout->addWidget(renderWidget);  
  299.         layout->setContentsMargins(0, 0, 0, 1);  
  300.         setLayout( layout );  
  301.   
  302.         connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );  
  303.         _timer.start( 10 );  
  304.     }  
  305.   
  306.     QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )  
  307.     {  
  308.         osg::Camera* camera = this->getCamera();  
  309.         camera->setGraphicsContext( gw );  
  310.   
  311.         const osg::GraphicsContext::Traits* traits = gw->getTraits();  
  312.   
  313.         camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) );  
  314.         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );  
  315.         camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );  
  316.         camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0));  
  317.   
  318.         this->setSceneData( scene );  
  319.         addEventHandler(new ManipulatorSceneHandler);  
  320.         return gw->getGLWidget();  
  321.     }  
  322.   
  323.     osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name=""bool windowDecoration=false )  
  324.     {  
  325.         osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();  
  326.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  327.         traits->windowName = name;  
  328.         traits->windowDecoration = windowDecoration;  
  329.         traits->x = x;  
  330.         traits->y = y;  
  331.         traits->width = w;  
  332.         traits->height = h;  
  333.         traits->doubleBuffer = true;  
  334.         traits->alpha = ds->getMinimumNumAlphaBits();  
  335.         traits->stencil = ds->getMinimumNumStencilBits();  
  336.         traits->sampleBuffers = ds->getMultiSamples();  
  337.         traits->samples = ds->getNumMultiSamples();  
  338.   
  339.         return new osgQt::GraphicsWindowQt(traits.get());  
  340.     }  
  341.   
  342.     virtual void paintEvent( QPaintEvent* event )  
  343.     {   
  344.         frame();   
  345.     }  
  346.   
  347. protected:  
  348.   
  349.     QTimer _timer;  
  350. };  
  351.   
  352.   
  353.   
  354. osg::Node*  buildScene()  
  355. {  
  356.     initBezier();  
  357.   
  358.     osg::Group *root = new osg::Group;  
  359.   
  360.     osg::MatrixTransform *zoomMT = new osg::MatrixTransform;  
  361.     zoomMT->setMatrix(osg::Matrix::translate(0.0f,0.0f,-4.0f));  
  362.   
  363.     osg::MatrixTransform *rotXMT = new osg::MatrixTransform;  
  364.     rotXMT->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(-75.0), osg::X_AXIS));  
  365.       
  366.     osg::MatrixTransform *rotZMT = new osg::MatrixTransform;  
  367.     rotZMT->addUpdateCallback(new RotAxisCallback(osg::Z_AXIS));  
  368.     g_ZRotMT = rotZMT;  
  369.   
  370.     osg::Switch *switchLineAndGeodeSwitch = new osg::Switch;  
  371.     g_Swith = switchLineAndGeodeSwitch;  
  372.     rotZMT->addChild(switchLineAndGeodeSwitch);  
  373.   
  374.     switchLineAndGeodeSwitch->addChild(createBezierControlLineGeode());  
  375.     switchLineAndGeodeSwitch->addChild(createBezierGeode(mybezier, divs));  
  376.     switchLineAndGeodeSwitch->setAllChildrenOn();  
  377.   
  378.     root->addChild(zoomMT);  
  379.     zoomMT->addChild(rotXMT);  
  380.     rotXMT->addChild(rotZMT);  
  381.   
  382.     return root;  
  383. }  
  384.   
  385.   
  386. int main( int argc, char** argv )  
  387. {  
  388.     QApplication app(argc, argv);  
  389.     ViewerWidget* viewWidget = new ViewerWidget(buildScene());  
  390.     viewWidget->setGeometry( 100, 100, 640, 480 );  
  391.     viewWidget->show();   
  392.     return app.exec();  
  393. }  
0 0
原创粉丝点击