zoukankan      html  css  js  c++  java
  • OSG:幼儿园篇 第二章 几何体绘制类

    一.简介

    在osg中,通常有三种生成几何体的方法:

    1.用封装的OpenGL绘图基元osg::Geometry

    2.是用OSG中预定义的基本几何体

    3.是从文件中导入场景模型

     

    二.绘制几何体流程

    1.指定向量数据

    参考:OSG:先导篇 数据类型

    2.设置绑定方式

    数据绑定主要有两项:法线绑定和颜色绑定

    3.数据解析

    指定绘制三角形的方式,从而绘制不同的图形

    #include <osgViewer/Viewer>
    
    #include <osg/Node>
    #include <osg/Geode>
    #include <osg/Group>
    
    #include <osgDB/ReadFile>
    #include <osgDB/WriteFile>
    
    #include <osgUtil/Optimizer>
    
    //创建一个四边形节点
    osg::ref_ptr<osg::Node> createQuad()
    {
     
      //创建一个叶节点对象
      osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    
      //创建一个几何体对象
      osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
    
      //创建顶点数组,注意顶点的添加顺序是逆时针的
      osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array();
      //添加数据
      v->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
      v->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
      v->push_back(osg::Vec3(1.0f, 0.0f, 1.0f));
      v->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
    
      //设置顶点数据
      geom->setVertexArray(v.get());
    
      //创建纹理坐标
      osg::ref_ptr<osg::Vec2Array> vt = new osg::Vec2Array();
    
      //添加数据
      vt->push_back(osg::Vec2(0.0f, 0.0f));
      vt->push_back(osg::Vec2(1.0f, 0.0f));
      vt->push_back(osg::Vec2(1.0f, 1.0f));
      vt->push_back(osg::Vec2(0.0f, 1.0f));
    
      //添加纹理坐标
      geom->setTexCoordArray(0, vt.get());
    
      //创建颜色数组
      osg::ref_ptr<osg::Vec4Array> vc = new osg::Vec4Array();
    
      //添加数据
      vc->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
      vc->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
      vc->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
      vc->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
    
      //设置颜色数组
      geom->setColorArray(vc.get());
    
      //设置颜色的绑定方式为单个顶点
      geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
    
      //创建法线数组
      osg::ref_ptr<osg::Vec3Array> nc = new osg::Vec3Array();
    
      //添加法线
      nc->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));  
    
      //设置法线数组
      geom->setNormalArray(nc.get());
      //设置法线的绑定方式为全部顶点
      geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
    
      //添加图元,绘图基元为四边形
      geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
    
      //添加到叶节点
      geode->addDrawable(geom.get());
    
      return geode.get();
    }
    
    int main()
    {
      //创建Viewer对象,场景浏览器
      osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
    
      osg::ref_ptr<osg::Group> root = new osg::Group();
    
      //添加到场景
      root->addChild(createQuad());
    
      //优化场景数据
      osgUtil::Optimizer optimizer;
      optimizer.optimize(root.get());
    
      viewer->setSceneData(root.get());
      viewer->realize();
    
    }
  • 相关阅读:
    排序去重
    $(...)[0].attr is not a function问题
    daterangepicker的汉化和简单使用
    wx.openSetting的调整
    css3中-moz、-ms、-webkit与盒子模型
    动态翻滚的导航条
    CSS3 transition 浏览器兼容性
    jq实现两个input输入同时不为空时,改变确认框背景颜色
    js 监听input 实现数据绑定
    关于html 修改滚动条的问题
  • 原文地址:https://www.cnblogs.com/k5bg/p/11454754.html
Copyright © 2011-2022 走看看