创建简单矢量图
基础:
OSG坐标系,与笛卡尔三维坐标系方向相同
步骤:
申请顶点数组,设置垂直xoy面的正方形的四个顶点
1
2
3
4
5
6osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
coords->push_back(osg::Vec3(-10.0, 5.0, -10.0));
coords->push_back(osg::Vec3(10.0, 5.0, -10.0));
coords->push_back(osg::Vec3(10.0, 5.0, 10.0));
coords->push_back(osg::Vec3(-10.0, 5.0, 10.0));申请颜色数组,设置半透明的颜色组
1
2
3
4
5
6osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4f(1.0, 0.0, 0.0, 0.5));
colors->push_back(osg::Vec4f(0.0, 1.0, 0.0, 0.5));
colors->push_back(osg::Vec4f(0.0, 0.0, 1.0, 0.5));
colors->push_back(osg::Vec4f(1.0, 1.0, 0.0, 0.5));申请法向量,令光源在正方形的一侧,压入一个法向量
1
2
3osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;
norms->push_back(osg::Vec3(0.0, -1.0, 0.0));申请一个几何
Geometry
1
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
设置顶点和顶点的关联方式
1
2geom->setVertexArray(coords.get());
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::QUADS, 0, 4));设置顶点颜色
1
2geom->setColorArray(colors.get());
geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);设置法向量
1
2geom->setNormalArray(norms.get());
geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);申请一个
Geode
1
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
在Geode中绘制几何,打开透明
1
2geode->addDrawable(geom.get());
geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);将上一节中
main
函数中的CreateBox()
改为CreateSimple()
,运行申请一个限制线宽
1
osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;
将顶点关联方式改为
LINE_LOOP
1
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4));
设置线宽
1
width->setWidth(5.0);
运行,查看结果
完整代码
1 |
|
原文链接: http://enofeng.github.io/2021/08/19/基础图元 简单矢量/
版权声明: 转载请注明出处.