案例1
#include <osg/Geode>
#include <osg/Geometry>
#include <osgDB/Registry>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <osg/PrimitiveSet>
#include <osgViewer/Viewer>
#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.f, 0.f, 0.f));v->push_back(osg::Vec3(1.f, 0.f, 0.f));v->push_back(osg::Vec3(1.f, 0.f, 1.f));v->push_back(osg::Vec3(0.f, 0.f, 1.f));v->push_back(osg::Vec3(0.f, -1.f, 0.f));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> c = new osg::Vec4Array;c->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));c->push_back(osg::Vec4(0.f, 1.f, 0.f, 1.f));c->push_back(osg::Vec4(0.f, 0.f, 1.f, 1.f));c->push_back(osg::Vec4(1.f, 1.f, 0.f, 1.f));//创建颜色索引数组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(c.get());geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);//为唯一的法线创建一个数组osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;geom->setNormalArray(n.get());geom->setNormalBinding(osg::Geometry::BIND_OVERALL);n->push_back(osg::Vec3(0.f, -1.f, 0.f));//由保存的数据绘制四个顶点的多边形geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));geode->addDrawable(geom.get());return geode.get();}int main()
{osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();osg::ref_ptr<osg::Group> root = new osg::Group();root->addChild(createQuad());osgUtil::Optimizer opt;opt.optimize(root.get());viewer->setSceneData(root.get());viewer->realize();viewer->run();}
案例二,绘制三角带
#include <osg/Geode>
#include <osg/Geometry>
#include <osgDB/Registry>
#include <osgDB/WriteFile>
#include <osg/Notify>
#include <osg/PrimitiveSet>
#include <osgViewer/Viewer>
#include <osgUtil/Optimizer>
#include <osgUtil/TriStripVisitor>#include <osgViewer/ViewerEventHandlers>
#include <osg/Group>
#include <osgUtil/DelaunayTriangulator>
#include <osgGA/StateSetManipulator>//创建一个四边形节点
osg::ref_ptr<osg::Geometry> 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));return geom.get();}int main()
{osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();//方便查在多边形之间的切换,查看三角网viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));osg::ref_ptr<osg::Group> root = new osg::Group();osg::ref_ptr<osg::Geometry> geo = createQuad();osgUtil::TriStripVisitor stripper;stripper.stripify(*(geo.get()));//添加到叶子节点osg::ref_ptr<osg::Geode> geode = new osg::Geode();geode->addDrawable(geo.get());root->addChild(geode.get());//优化场景数据osgUtil::Optimizer opt;opt.optimize(root.get());viewer->setSceneData(root.get());viewer->realize();viewer->run();return 0;}