基础图元 简单矢量

创建简单矢量图

基础:

OSG坐标系,与笛卡尔三维坐标系方向相同

步骤:

  1. 申请顶点数组,设置垂直xoy面的正方形的四个顶点

    osg::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));
    
  2. 申请颜色数组,设置半透明的颜色组

    osg::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));
    
  3. 申请法向量,令光源在正方形的一侧,压入一个法向量

    osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;
    
    norms->push_back(osg::Vec3(0.0, -1.0, 0.0));
    
  4. 申请一个几何Geometry

    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    
  5. 设置顶点和顶点的关联方式

    geom->setVertexArray(coords.get());
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::QUADS, 0, 4));
    
  6. 设置顶点颜色

    geom->setColorArray(colors.get());
    geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
    
  7. 设置法向量

    geom->setNormalArray(norms.get());
    geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);
    
  8. 申请一个Geode

    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    
  9. 在Geode中绘制几何,打开透明

    geode->addDrawable(geom.get());
    geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
    
  10. 将上一节中main函数中的CreateBox()改为CreateSimple(),运行

  11. 申请一个限制线宽

    osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;
    
  12. 将顶点关联方式改为LINE_LOOP

    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4));
    
  13. 设置线宽

    width->setWidth(5.0);
    
  14. 运行,查看结果

完整代码

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/GUIEventAdapter>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/StateSet>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/LineWidth>
#include <iostream>
using namespace std;

osg::ref_ptr<osg::Geode> CreateSimple() {
	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
	osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
	osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
	osg::ref_ptr<osg::Vec3Array> norms = new osg::Vec3Array;
	osg::ref_ptr<osg::LineWidth> width = new osg::LineWidth;
	
    geode->addDrawable(geom.get());
	geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
	geom->setVertexArray(coords.get());
	geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::Mode::LINE_LOOP, 0, 4));
	geom->setColorArray(colors.get());
	geom->setColorBinding(osg::Geometry::AttributeBinding::BIND_PER_VERTEX);
	geom->setNormalArray(norms.get());
	geom->setNormalBinding(osg::Geometry::AttributeBinding::BIND_OVERALL);
	width->setWidth(5.0);
	geode->getOrCreateStateSet()->setAttributeAndModes(width.get(), osg::StateAttribute::ON);

	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));

	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));

	norms->push_back(osg::Vec3(0.0, -1.0, 0.0));

	return geode;
}

int main() {

	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	osg::ref_ptr<osg::Group> group = new osg::Group;
	
	group->addChild(CreateSimple());
	viewer->setSceneData(group.get());

	return viewer->run();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值