• Osg-海军osg教程源代码之金子塔的绘制


    相关资料:

    实例代码:

    .pro

     1 QT       += core gui widgets
     2 TARGET = TestOsgQt
     3 TEMPLATE = app
     4 DEFINES += QT_DEPRECATED_WARNINGS
     5 CONFIG += c++11
     6 
     7 SOURCES += 
     8         main.cpp 
     9         widget.cpp
    10 
    11 HEADERS += 
    12         widget.h
    13 
    14 OsgDir = D:\RuanJian\osg365R
    15 CONFIG(release, debug|release) {
    16     LIBS += -L$${OsgDir}/lib/ -losgQOpenGL -losgDB -losgViewer -losg -losgUtil -losgGA
    17 } else {
    18     LIBS += -L$${OsgDir}/lib/ -losgQOpenGLd -losgDBd -losgViewerd -losgd -losgUtild -losgGAd
    19 }
    20 
    21 INCLUDEPATH += $${OsgDir}/include
    22 DEPENDPATH += $${OsgDir}/include
    View Code

    main.cpp

      1 #include "widget.h"
      2 #include <QApplication>
      3 
      4 #include <osg/Node>
      5 #include <osg/Group>
      6 #include <osg/Geode>
      7 #include <osg/Geometry>
      8 #include <osg/Texture2D>
      9 #include <osg/StateSet>
     10 #include <osg/PositionAttitudeTransform>
     11 #include <osgViewer/Viewer>
     12 #include <osgDB/ReadFile>
     13 //#include <osg/Ref_ptr>
     14 
     15 int main(int argc, char *argv[])
     16 {
     17     osg::Group* root = new osg::Group();
     18    osg::Geode* pyramidGeode = new osg::Geode();
     19    osg::Geometry* pyramidGeometry = new osg::Geometry();
     20    pyramidGeode->addDrawable(pyramidGeometry);
     21    root->addChild(pyramidGeode);
     22     osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
     23    pyramidVertices->push_back( osg::Vec3( 0, 0, 0) ); // 左前
     24    pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); // 右前
     25    pyramidVertices->push_back( osg::Vec3(10,10, 0) ); // 右后
     26    pyramidVertices->push_back( osg::Vec3( 0,10, 0) ); // 左后
     27    pyramidVertices->push_back( osg::Vec3( 5, 5,10) ); // 塔尖
     28    pyramidGeometry->setVertexArray( pyramidVertices );
     29    osg::DrawElementsUInt* pyramidBase =
     30       new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
     31    pyramidBase->push_back(3);
     32    pyramidBase->push_back(2);
     33    pyramidBase->push_back(1);
     34    pyramidBase->push_back(0);
     35    pyramidGeometry->addPrimitiveSet(pyramidBase);
     36 //重复这一过程,添加金字塔的每个面。再次注意,顶点是以逆时针为顺序添加的。
     37    osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
     38    pyramidFaceOne->push_back(0);
     39    pyramidFaceOne->push_back(1);
     40    pyramidFaceOne->push_back(4);
     41    pyramidGeometry->addPrimitiveSet(pyramidFaceOne);
     42 
     43    osg::DrawElementsUInt* pyramidFaceTwo =
     44       new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
     45    pyramidFaceTwo->push_back(1);
     46    pyramidFaceTwo->push_back(2);
     47    pyramidFaceTwo->push_back(4);
     48    pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);
     49 
     50    osg::DrawElementsUInt* pyramidFaceThree = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
     51    pyramidFaceThree->push_back(2);
     52    pyramidFaceThree->push_back(3);
     53    pyramidFaceThree->push_back(4);
     54    pyramidGeometry->addPrimitiveSet(pyramidFaceThree);
     55 
     56    osg::DrawElementsUInt* pyramidFaceFour =
     57       new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
     58    pyramidFaceFour->push_back(3);
     59    pyramidFaceFour->push_back(0);
     60    pyramidFaceFour->push_back(4);
     61    pyramidGeometry->addPrimitiveSet(pyramidFaceFour);
     62 //定义一个Vec4的数组,用于保存颜色值。
     63    osg::Vec4Array* colors = new osg::Vec4Array;
     64    colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //索引0 红色
     65    colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //索引1 绿色
     66    colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //索引2 蓝色
     67    colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //索引3 白色
     68    osg::TemplateIndexArray
     69       <unsigned int, osg::Array::UIntArrayType,4,4> *colorIndexArray;
     70    colorIndexArray =
     71       new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4>;
     72    colorIndexArray->push_back(0); // vertex 0 assigned color array element 0
     73    colorIndexArray->push_back(1); // vertex 1 assigned color array element 1
     74    colorIndexArray->push_back(2); // vertex 2 assigned color array element 2
     75    colorIndexArray->push_back(3); // vertex 3 assigned color array element 3
     76    colorIndexArray->push_back(0); // vertex 4 assigned color array element 0
     77    pyramidGeometry->setColorArray(colors);
     78 //   pyramidGeometry->setColorIndices(colorIndexArray);
     79    pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
     80 
     81    osg::Vec2Array* texcoords = new osg::Vec2Array(5);
     82    (*texcoords)[0].set(0.00f,0.0f);
     83    (*texcoords)[1].set(0.25f,0.0f);
     84    (*texcoords)[2].set(0.50f,0.0f);
     85    (*texcoords)[3].set(0.75f,0.0f);
     86    (*texcoords)[4].set(0.50f,1.0f);
     87     pyramidGeometry->setTexCoordArray(0,texcoords);
     88 //初始化位置变换节点
     89    osg::PositionAttitudeTransform* pyramidTwoXForm =new osg::PositionAttitudeTransform();
     90 
     91 //使用osg::Group的addChild方法,将位置变换节点添加到根节点的子节点上,并将金字塔节点作为变换节点的子节点
     92    root->addChild(pyramidTwoXForm);
     93    pyramidTwoXForm->addChild(pyramidGeode);
     94 
     95 // 初始化一个Vec3实例,用于改变模型在场景中的位置
     96    osg::Vec3 pyramidTwoPosition(15,0,0);
     97    pyramidTwoXForm->setPosition( pyramidTwoPosition );
     98 //最后,设置视窗类并进入仿真循环。
     99    osgViewer::Viewer viewer;
    100 
    101    viewer.setSceneData( root );
    102 
    103   return viewer.run();
    104 }
    View Code

     

    作者:疯狂Delphi
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    欢迎关注我,一起进步!扫描下方二维码即可加我

  • 相关阅读:
    基于连通性状态压缩的动态规划问题
    2005年IT行业趋势Top10
    企业应用之性能实时度量系统演变
    云计算参考架构几例
    在CentOS上构建.net自动化编译环境
    Asp.net SignalR 实现服务端消息推送到Web端
    餐饮行业解决方案之客户分析流程
    餐饮行业解决方案之采购战略制定与实施流程
    餐饮行业解决方案之业务设计流程
    零售连锁专卖信息化解决方案简介之三
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/15504205.html
Copyright © 2020-2023  润新知