首页 > 其他分享 >osg学习-2《绘制基本单元》

osg学习-2《绘制基本单元》

时间:2022-08-21 10:36:32浏览次数:66  
标签:1.0 0.0 back geom osg push 绘制 单元

上一篇演示了基本四边形的绘制,这一篇是共享顶点的方法,通过索引绘制顶点和颜色。

为了便于理解特意在ppt中绘制了顶点的坐标位置,5个顶点,绘制了一个四边形和三角形,其中有2个共享顶点定义了4中颜色,有一个颜色共享。

分别测试按顶点渲染和按图元渲染。

直接放效果

 需要注意的是  有个过时的语句,osg3.6.5版本已不再支持,直接删掉geom->setColorIndices(colorIndex);//设置颜色索引数组

同时在颜色数组中增加那个共享的颜色
 

 

// osg_hello.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

#ifdef _WIN32
#include <windows.h>
#endif

#include <osgViewer/Viewer>

#include <osg/Node>
#include <osg/Geode>
#include <osg/Group>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>

#include <osgUtil/Optimizer>

/*4.2.3节
* 索引绑定几何体绘制示例 
* 通过索引的方式,可以减少顶点的存储量
*/

/// <summary>
/// 创建一个四边形节点
/// </summary>
/// <returns></returns>
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));
    v->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));

    //设置顶点数据
    geom->setVertexArray(v.get());

    //创建四边形定点数组,指定绘图基元为四边形,注意添加顺序
    osg::ref_ptr<osg::DrawElementsUInt> quad = 
        new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS,0);
    quad->push_back(0);
    quad->push_back(1);
    quad->push_back(2);
    quad->push_back(3);

    //添加到几何体
    geom->addPrimitiveSet(quad.get());

    //创建三角形顶点索引数组,指定绘图基元为三角形,注意添加顺序
    osg::ref_ptr<osg::DrawElementsUInt> triangle =
        new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
    triangle->push_back(4);
    triangle->push_back(0);
    triangle->push_back(3);

    //添加到几何体
    geom->addPrimitiveSet(triangle.get());

    //创建颜色数组
    osg::ref_ptr<osg::Vec4Array> vc = new osg::Vec4Array();
    vc->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));  //red
    vc->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));  //green
    vc->push_back(osg::Vec4(1.0f, 0.0f, 1.0f, 1.0f));  //pink
    vc->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));  //yellow
    vc->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));  //the shared color

    //创建颜色索引数组
   /* osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType, 4, 4>
        *colorIndex = new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType, 4, 4>();
    colorIndex->push_back(0);
    colorIndex->push_back(1);
    colorIndex->push_back(2);
    colorIndex->push_back(3);
    colorIndex->push_back(2);*/

 
    //设置颜色数组
    geom->setColorArray(vc.get());
    //设置颜色索引数组,是过时的函数,删掉
    //geom->setColorIndices(colorIndex); 
        
    //设置颜色的绑定方式为单个顶点
    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
    //geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);

    // 创建法线数组
    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);

    //添加到叶节点
    geode->addDrawable(geom.get());

    return geode.get();
}


int main()
{
    // 创建Viewer对象,场景浏览器创建一个节点。viewer->setSceneData(root.get())viewer->realize(viewer - un();
    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();

    //创建场最组节点
    osg::ref_ptr<osg::Group> root = new osg::Group();

    //创建几何对象
    osg::ref_ptr<osg::Node> node = createQuad();

    //添加到场景
    root->addChild(node.get());

    //优化场景数据
    osgUtil::Optimizer optimizer;
    optimizer.optimize(root.get());

    //设置场景数据
    viewer->setSceneData(root.get());

    //设置渲染的窗口
    //viewer->setUpViewAcrossAllScreens();  //default on all screens
    viewer->setUpViewOnSingleScreen(0);

    //开始渣染
    viewer->run();

    return 0;
}


 

标签:1.0,0.0,back,geom,osg,push,绘制,单元
From: https://www.cnblogs.com/oliver2022/p/16609420.html

相关文章

  • osg学习-1《绘制基本单元》
     1基于场景的图形绘制    OpenSceneGraph简称OSG是非常著名的三维可视化,在绘制复杂场景方面比VTK更有优势。在OSG中存在两棵树,即场景树和渲染树。场景树是一棵......
  • osg学习-6《显示三维矩阵》
    在三维空间显示三维矩阵,需要显示它的6个外表面。假设xyz三个方向的维数是ni,nj,nk,三个方向的顶点维数是ni+1,nj+1,nk+1。在每个面上分别绘制各自的四边形。每个四边形的颜色根......
  • 单元测试
    原则AutomaticIsolationRepeatable注解@Testexpected——设置期望方法异常timeout——设置方法超时时间@BeforeClass——用于创建资源连接@Parameter——......
  • 【数据库】E-R图相关知识、绘制方法及工具推荐
    一、知识1、介绍E-R图也称实体-联系图(EntityRelationshipDiagram),提供了表示实体、属性和联系的方法,用来描述现实世界的概念模型。2、组成(1)实体(Entity)-矩形标识(2)属......
  • 利用OSGBLab对建筑进行立面出图
    利用OSGBLab对倾斜摄影的建筑进行立面出图OSGBLab实景三维小助手【微信公众号OSGBLab】  OSGBLab除了能输出顶视的真正射DOM,还能选定范围进......
  • spring使用junit单元测试
    <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.6.RELEASE</version></dependency><dependenc......
  • Vs2019 单元测试突然出现 Microsoft.VisualStudio.TestTools.UnitTesting 不识别
    Microsoft.VisualStudio.QualityTools.UnitTestFrameworkusingMicrosoft.VisualStudio.TestTools.UnitTesting;  这句代码编辑器无法识别,  同时 [TestClass] [T......
  • Python抓取汇率并绘制成折线图
    公司的一个小工作,需要抓取美元、欧元汇率并绘制成折线图。很小的功能,背后却涉及很多Python知识,甚至一些冷门的知识点。知识点总结如下:1.python抓取网页数据,利用pandas.rea......
  • 手写卷积单元-python
    一个文本卷积模块defcnn():importnumpyasnpresult=[]n,dim=10,30kernels=[np.random.randint(0,2,(i,dim))foriinrange(3,6)]#......
  • PHPExcel导出设置单元格格式
    找到相关文件PhpSpreadsheet/Style/NumberFormat.php  使用$spreadsheet->getActiveSheet()->getStyle('A')->getNumberFormat()->setFormatCode('@');//将A设置成......