ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-08-21 10:37:35  阅读:212  来源: 互联网

标签: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,绘制,单元
来源: https://www.cnblogs.com/oliver2022/p/16609420.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有