案例1
#include
#include
#include
#include
#include
#include
#include
#include
osg::ref_ptr createQuad() {
osg::ref_ptr geode = new osg::Geode;
//创建一个用于保存几何信息的对象
osg::ref_ptr geom = new osg::Geometry;
//创建四个顶点的数组
osg::ref_ptr 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 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 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 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*
colorIndex = new osg::TemplateIndexArray
();
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 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 viewer = new osgViewer::Viewer();
osg::ref_ptr root = new osg::Group();
root->addChild(createQuad());
osgUtil::Optimizer opt;
opt.optimize(root.get());
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
}
案例二,绘制三角带
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//创建一个四边形节点
osg::ref_ptr createQuad() {
//创建一个叶节点对象
osg::ref_ptr geode = new osg::Geode();
//创建一个几何体对象
osg::ref_ptr geom = new osg::Geometry();
//创建顶点数组,注意顶点的添加顺序是逆时针的
osg::ref_ptr 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 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 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 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 viewer = new osgViewer::Viewer();
//方便查在多边形之间的切换,查看三角网
viewer->addEventHandler(
new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
osg::ref_ptr root = new osg::Group();
osg::ref_ptr geo = createQuad();
osgUtil::TriStripVisitor stripper;
stripper.stripify(*(geo.get()));
//添加到叶子节点
osg::ref_ptr 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;
}