osg绘制坐标轴(XYZ)代码

#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>

#include <osg/ShapeDrawable>
#include <osgText/Text>
#include <osg/LineWidth>

using namespace osg;

// 创建单个坐标分量
void addAxis(Geode *coord, Vec3 pt, std::string text, Vec4 color){  
    Vec3Arrary *v = new Vec3Arrary();
    v.push_back(Vec3(0., 0., 0.));
    v.push_back(pt);

    Vec4Arrary *c = new Vec4Arrary();
    c.push_back(color);

    Geometry *axis = new Geometry;
    axis->setVertexArray(v);
    axis->setColorArray(c);
    axis->setColorBinding(Geometry::BIND_OVERALL);
    axis->addPrimitiveSet(new DrawArrays(PrimitiveSet::LINES, 0, 2));

    Text *tx = new Text;
    tx->setText(text);
    tx->setFont("Fonts/simhei.ttf");
    tx->setAxisAlignment(Text::SCREEN);
    tx->setCharacterSize(2);
    tx->setPosition(pt);

    coord->addDrawable(axis);
    coord->addDrawable(tx);
}

// 主函数
int main(){
    float len = 30.;

    Geode *coord = new Geode;
    coord->getOrCreateStateSet()->setMode(GL_LIGHTING, StateAttribute::OFF);
    coord->getOrCreateStateSet()->setAttribute(new LineWidth(2), StateAttribute::ON);

    addAxis(coord, Vec3(len, 0., 0.), "X", Vec4(1., 0., 0., 1.)); // x-axis
    addAxis(coord, Vec3(0., len, 0.), "Y", Vec4(0., 1., 0., 1.)); // y-axis
    addAxis(coord, Vec3(0., 0., len), "Z", Vec4(0., 0., 1., 1.)); // z-axis
    
    osgViewer::Viewer viewer;
    viewer.setSceneData(coord);
    return viewer.run();    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值