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

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

作者:互联网

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

为了便于理解特意在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