osg学习-5《绘制坐标轴》

40 篇文章 32 订阅
6 篇文章 1 订阅

 设置坐标轴的原点,xyz方向的长度,和字体的大小。方法是以原点位中心,绘制三条相互垂直的线,每条线用不同的颜色,并分别标注xyz文字,并把文字贴到屏幕上。

void DrawShape::makeCoordinate(float ori_x, float ori_y, float ori_z,float a_x, float a_y, float a_z, float font_size)
{
    osg::ref_ptr<osg::Sphere> pSphereShape = new osg::Sphere(osg::Vec3(ori_x, ori_y, ori_z), 1.0f);
    osg::ref_ptr<osg::ShapeDrawable> pShapeDrawable = new osg::ShapeDrawable(pSphereShape.get());
    pShapeDrawable->setColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));

    //创建保存几何信息的对象
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();

    //创建四个顶点
    osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array();
    v->push_back(osg::Vec3(ori_x, ori_y, ori_z));
    v->push_back(osg::Vec3(ori_x + a_x, ori_y, ori_z));

    v->push_back(osg::Vec3(ori_x, ori_y, ori_z));
    v->push_back(osg::Vec3(ori_x, ori_y+a_y, ori_z));
    v->push_back(osg::Vec3(ori_x, ori_y, ori_z));

    v->push_back(osg::Vec3(ori_x, ori_y, ori_z+a_z));
    geom->setVertexArray(v.get());

    osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array();
    c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));
    c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));

    c->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
    c->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
    c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));

    c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
    geom->setColorArray(c.get());
    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

    //xyz
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2));
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 2, 2));
    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 4, 2));

    osg::ref_ptr<osgText::Text> pTextXAuxis1 = new osgText::Text;
    pTextXAuxis1->setText(L"X");
    pTextXAuxis1->setFont("Fonts/simhei.ttf");

    pTextXAuxis1->setAxisAlignment(osgText::Text::SCREEN);
    pTextXAuxis1->setCharacterSize(font_size);
    pTextXAuxis1->setPosition(osg::Vec3(ori_x + a_x, ori_y, ori_z));

    osg::ref_ptr<osgText::Text> pTextYAuxis1 = new osgText::Text;
    pTextYAuxis1->setText(L"Y");
    pTextYAuxis1->setFont("Fonts/simhei.ttf");

    pTextYAuxis1->setAxisAlignment(osgText::Text::SCREEN);
    pTextYAuxis1->setCharacterSize(font_size);
    pTextYAuxis1->setPosition(osg::Vec3(ori_x, ori_y + a_y, ori_z));
    osg::ref_ptr<osgText::Text> pTextZAuxis1 = new osgText::Text;
    pTextZAuxis1->setText(L"Z");
    pTextZAuxis1->setFont("Fonts/simhei.ttf");

    pTextZAuxis1->setAxisAlignment(osgText::Text::SCREEN);
    pTextZAuxis1->setCharacterSize(font_size);
    pTextZAuxis1->setPosition(osg::Vec3(ori_x, ori_y, ori_z + a_z));

    osg::ref_ptr<osg::Geode> geode = new osg::Geode();
    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    geode->getOrCreateStateSet()->setAttribute(new osg::LineWidth(3.0), osg::StateAttribute::ON);

    geode->addDrawable(pShapeDrawable.get());
    geode->addDrawable(geom.get());
    geode->addDrawable(pTextXAuxis1.get());

    geode->addDrawable(pTextYAuxis1.get());
    geode->addDrawable(pTextZAuxis1.get());

    _root->addChild(geode);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oceanstonetree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值